Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "E4/EAS/Adapting Objects"
(→Implementation) |
(→Adapting objects) |
||
Line 55: | Line 55: | ||
====IAdaptable definition==== | ====IAdaptable definition==== | ||
+ | <source lang="java"> | ||
public interface IAdaptable { | public interface IAdaptable { | ||
Line 60: | Line 61: | ||
} | } | ||
+ | </source> | ||
====Implementation==== | ====Implementation==== | ||
Line 88: | Line 90: | ||
====Client code==== | ====Client code==== | ||
<source lang="java"> | <source lang="java"> | ||
+ | // casting no longer required in client code | ||
IModelProvider modelProvider = editor.getAdapter(IModelProvider.class); | IModelProvider modelProvider = editor.getAdapter(IModelProvider.class); | ||
if (modelProvider != null) { | if (modelProvider != null) { |
Latest revision as of 12:34, 30 October 2009
The adapter pattern is widely used in the Eclipse platform as a way for different classes to retrieve information about a given object. This can usually be seen by how different views react completely different to a given object. Consider how the 'Javadoc' view renders the javadoc specification of a given class but the 'Outline' view provides an overview of the entire class instead of just the item under the current text caret.
Contents
Eclipse 3.x API
Getting an IAdapterManager
The existing IAdapterManager can currently either be retrieved directly via a static method (Platform.getAdapterManger()) or queried through via OSGi as a service.
Adapting objects
In Eclipse 3.x, there is the org.eclipse.core.runtime.IAdaptable interface that types can implement/extend if they wish clients to adapt their object to another object.
Implementation
public interface IModelProvider { public Model getModel(); } public class ModelEditor implements IAdaptable, IModelProvider { private Model model; public Model getModel() { return model; } public Object getAdapter(Class adapter) { if (adapter == IModelProvider.class) { return this; } return null; } }
Client code
IModelProvider modelProvider = (IModelProvider) editor.getAdapter(IModelProvider.class); if (modelProvider != null) { // do something with it }
e4 (Java)
Getting an IAdapterManager
The adapter manager can be easily queried for by asking the Eclipse context.
private IAdapterManager getAdapterManager(IEclipseContext context) { return (IAdapterManager) context.get(IAdapterManager.class.getName()); }
Adapting objects
If generics could be used, the client code can be simplified as casting would no longer be required.
IAdaptable definition
public interface IAdaptable { public <T> T getAdapter(Class<T> cls); }
Implementation
public interface IModelProvider { public Model getModel(); } public class ModelEditor implements IAdaptable, IModelProvider { private Model model; public Model getModel() { return model; } public <T> T getAdapter(Class<T> adapter) { if (adapter == IModelProvider.class) { return (T) this; } return null; } }
Client code
// casting no longer required in client code IModelProvider modelProvider = editor.getAdapter(IModelProvider.class); if (modelProvider != null) { // do something with it }