Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "FAQ How can I use IWorkbenchAdapter to display my model elements?"

m (See Also:)
m
 
Line 1: Line 1:
The <tt>IAdaptable</tt> mechanism in Eclipse can be used to add visual presentation
+
The <tt>IAdaptable</tt> mechanism in Eclipse can be used to add visual presentation to your model objects without introducing UI code in your model layer.  This follows the layering principle followed in the Eclipse Platform, where core code has no dependency on UI code.  To do this, your model objects must implement the <tt>IAdaptable</tt> interface.  This is typically achieved by simply subclassing <tt>PlatformObject</tt>, but if that&#146;s not possible, you can implement the interface directly.  See the javadoc of <tt>PlatformObject</tt> for more details.
to your model objects without introducing UI code in your model layer.  This follows
+
the layering principle followed in the Eclipse Platform, where core code
+
has no dependency on UI code.  To do this, your model objects must implement
+
the <tt>IAdaptable</tt> interface.  This is typically achieved by simply subclassing
+
<tt>PlatformObject</tt>, but if that&#146;s not possible, you can implement the interface  
+
directly.  See the javadoc of <tt>PlatformObject</tt> for more details.
+
  
 +
In your UI layer, you need to register an adapter factory that can return an implementation of <tt>IWorbenchAdapter</tt> for your model objects.  If your adapter doesn't need to maintain any state, it's a good idea to make it a singleton to avoid creating extra objects for each model element. See the class <tt>WorkbenchAdapterFactory</tt> in the <tt>org.eclipse.ui.ide</tt> plug-in for an example of an adapter factory that creates <tt>IWorkbenchAdapter</tt> instances for <tt>IResource</tt> objects.
  
In your UI layer, you need to register an adapter factory that can return an implementation
+
Once you have defined and registered such a factory, you can simply use <tt>WorkbenchContentProvider</tt> and <tt>WorkbenchLabelProvider</tt> in any tree or table viewer that contains your model objects. These special providers delegate their implementations to the underlying model objects by asking their <tt>IWorkbenchAdapter</tt> to compute the label or children of the elements.
of <tt>IWorbenchAdapter</tt> for your model objects.  If your adapter
+
doesn&#146;t need to maintain any state, it&#146;s a good idea to make it a singleton
+
to avoid creating extra objects for each model element. See the class <tt>WorkbenchAdapterFactory</tt>
+
in the <tt>org.eclipse.ui.ide</tt>
+
plug-in for an example of an adapter factory that creates <tt>IWorkbenchAdapter</tt>
+
instances for <tt>IResource</tt> objects.
+
 
+
 
+
Once you have defined and registered such a factory, you can simply use  
+
<tt>WorkbenchContentProvider</tt> and <tt>WorkbenchLabelProvider</tt>  
+
in any tree or table viewer that contains your model objects. These special  
+
providers delegate their implementations to the underlying model objects  
+
by asking their <tt>IWorkbenchAdapter</tt> to compute the label or children  
+
of the elements.
+
 
+
  
 
== See Also: ==
 
== See Also: ==
 +
*[[FAQ How do I use IAdaptable and IAdapterFactory?]]
  
[[FAQ_How_do_I_use_IAdaptable_and_IAdapterFactory?]]
+
{{Template:FAQ_Tagline}}
 
+
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+

Latest revision as of 21:20, 29 May 2006

The IAdaptable mechanism in Eclipse can be used to add visual presentation to your model objects without introducing UI code in your model layer. This follows the layering principle followed in the Eclipse Platform, where core code has no dependency on UI code. To do this, your model objects must implement the IAdaptable interface. This is typically achieved by simply subclassing PlatformObject, but if that&#146;s not possible, you can implement the interface directly. See the javadoc of PlatformObject for more details.

In your UI layer, you need to register an adapter factory that can return an implementation of IWorbenchAdapter for your model objects. If your adapter doesn't need to maintain any state, it's a good idea to make it a singleton to avoid creating extra objects for each model element. See the class WorkbenchAdapterFactory in the org.eclipse.ui.ide plug-in for an example of an adapter factory that creates IWorkbenchAdapter instances for IResource objects.

Once you have defined and registered such a factory, you can simply use WorkbenchContentProvider and WorkbenchLabelProvider in any tree or table viewer that contains your model objects. These special providers delegate their implementations to the underlying model objects by asking their IWorkbenchAdapter to compute the label or children of the elements.

See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top