Bug 23032 - [Editor Mgmt] Make IEditorReference adaptable
Summary: [Editor Mgmt] Make IEditorReference adaptable
Status: RESOLVED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.0   Edit
Hardware: Other All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Eduardo Pereira CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-08-30 10:20 EDT by Jared Burns CLA
Modified: 2002-10-24 14:17 EDT (History)
1 user (show)

See Also:


Attachments
editoradapter.patch - A patch containing the change (927 bytes, patch)
2002-09-04 16:10 EDT, Jared Burns CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jared Burns CLA 2002-08-30 10:20:21 EDT
I'd like EditorManager$Editor to implement IAdaptable so that the team menu 
can appear on IEditorReferences in the editor management view (Bug 10941). 
Ideally, IEditorReference would extend IAdaptable, but since the API can't 
be changed at this point, adding it to the implementation is the next best 
thing.

I'd like an adapter registered on EditorManager$Editor that can return an 
IResource. It seems like a reasonable adapter to provide as part of the 
default getAdapter() implementation on EditorManager$Editor, but I could 
just register it myself.

There are two code changes required for this:
1. Add "IAdaptable" as an implemented interface on EditorManager$Editor
2. Define the following method on EditorManager$Editor:
  public Object getAdapter(Class adapter) {
    return Platform.getAdapterManager().getAdapter(this, adapter);
  }
Comment 1 Jared Burns CLA 2002-09-04 14:22:36 EDT
I'd be happy to contribute the code for this change. Having this bug up in the 
air is preventing me from sharing the latest editor view code.
Comment 2 Jared Burns CLA 2002-09-04 16:10:18 EDT
Created attachment 1927 [details]
editoradapter.patch - A patch containing the change
Comment 3 Jared Burns CLA 2002-09-09 11:15:35 EDT
A slight change to my request:
Since the API on IEditorReference says that it is not intended to be 
implemented by clients, making it extend IAdaptable actually doesn't break any 
API rules.

Instead of adding IAdaptable on the private editor reference implementation, 
it would be better to add it to the IEditorReference declaration.
Comment 4 Eduardo Pereira CLA 2002-09-09 11:39:29 EDT
I undestand it is a simple change and it would take me 30 seconds to apply and 
release it but I am not convinded yet it is a good change. I now it enables you 
to add a "cool" feature to the editors view but I am concerned about the 
feature as well and I would like to test the view since it may become part of 
the platform and then address this PR again.

I don't undestand IAdaptable 100% but the idea of implementing IAdaptable in 
one class so some other component may register adaptable for that object does 
not look right.

Questions:
 1) What happens if I have an editor that edits a set (> 1) of resources.
 2) What happens if it does not edit a resource.
Comment 5 Jared Burns CLA 2002-10-22 10:43:40 EDT
Is my submission not being integrated because of a problem? If so, please let 
me know so I can address it. If you prefer to provide the adapter for 
IResource directly in the editor implementation, let me know and I'll change 
the submission.
Comment 6 Eduardo Pereira CLA 2002-10-22 13:35:14 EDT
We are analising the editor managment again and we hope to get to a conclusion 
soon. So I am not doing any work on any PR related to the editors view.
About this PR: There is not a map "1 to 1" between editors and resources so 
making it adaptable may not be the right answer. I mean "what happens if may 
editor edits more that one file at the same time". So adding the Team menu to 
the editors view may not have a easy solution but if you realy want it you can 
wrap the IEditorReference with some adaptable object before adding them to your 
view.
Comment 7 Nick Edgar CLA 2002-10-24 12:00:26 EDT
IEditorReference cannot provide an adapter to IResource for three reasons:
1. the editor may present multiple files (as Eduardo points out above)
2. the editor may not be materialized yet, so the reference doesn't even know 
the editor input
3. the basic workbench support should not have dependencies on Core Resources

However, note that IEditorInput already supports IAdaptable. Both getAdapter
(IFile.class) and getAdapter(IResource.class) work for FileEditorInputs.
Currently you could wrap the editor reference with your own object that 
delegates getAdapter to the editor's input.  However, for this to work the 
editor has to already be materialized.
Comment 8 Jared Burns CLA 2002-10-24 12:03:26 EDT
Noted, thanks.
Comment 9 Jared Burns CLA 2002-10-24 12:04:12 EDT
Marking as worksforme. I'll try out the suggestions here.
Comment 10 Nick Edgar CLA 2002-10-24 14:07:32 EDT
Actually, I'm curious as to how you got it to work at all with the code snippet 
in your original request.  Here you're looking up adapters registered against 
EditorManager$Editor, which implements IEditorReference.  So this will find 
adapters that register against IEditorReference.  But nobody does this.
Or did you do something else?
Comment 11 Jared Burns CLA 2002-10-24 14:09:29 EDT
I registered the adapter from my code.
Comment 12 Jared Burns CLA 2002-10-24 14:17:12 EDT
In case you're curious, here's the registration code:

// TODO: Remove this code which checks for the IAdaptable
//             interface on IEditorReference once Bug 23032 is resolved.
Class classes[]= IEditorReference.class.getInterfaces();
for (int i= 0, numClasses= classes.length; i < numClasses; i++) {
  if (classes[i].equals(IAdaptable.class)) {
    // Only needs to be run once - Belongs in UI Plugin startup()?
    Platform.getAdapterManager().registerAdapters(new EditorAdapterFactory(), IEditorReference.class);
  }
}

And here's the adapter code:
/**
 * The adapter factory which the editor list view uses to get the IResource 
 * associated with an IEditorReference.
 */
public class EditorAdapterFactory implements IAdapterFactory {
  public Object getAdapter(Object adaptableObject, Class adapterType) {
    if (adaptableObject instanceof IEditorReference) {
      if (IResource.class.equals(adapterType)) {
        IEditorPart part = ((IEditorReference)adaptableObject).getEditor(true);
        if (part != null) {
          IEditorInput input = part.getEditorInput();
          if (input instanceof IFileEditorInput) {
            return ((IFileEditorInput) input).getFile();
          }
        }
      }
    }
    return null;
  }
  public Class[] getAdapterList() {
    return new Class[] { IResource.class };
  }
}