KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > provider > AdapterFactoryItemDelegator


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: AdapterFactoryItemDelegator.java,v 1.2 2005/06/08 06:17:05 nickb Exp $
16  */

17 package org.eclipse.emf.edit.provider;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.eclipse.emf.common.command.Command;
26 import org.eclipse.emf.common.command.UnexecutableCommand;
27 import org.eclipse.emf.common.notify.AdapterFactory;
28 import org.eclipse.emf.common.util.EList;
29 import org.eclipse.emf.edit.command.CommandParameter;
30 import org.eclipse.emf.edit.domain.EditingDomain;
31
32
33 /**
34  */

35 public class AdapterFactoryItemDelegator
36   implements
37     IEditingDomainItemProvider,
38     IItemLabelProvider,
39     IItemPropertySource,
40     IStructuredItemContentProvider,
41     ITableItemLabelProvider,
42     ITreeItemContentProvider
43 {
44   /**
45    * This keeps track of the factory used to generate the adapters.
46    */

47   protected AdapterFactory adapterFactory;
48
49   /**
50    */

51   public AdapterFactoryItemDelegator(AdapterFactory adapterFactory)
52   {
53     this.adapterFactory = adapterFactory;
54   }
55
56   public AdapterFactory getAdapterFactory()
57   {
58     return adapterFactory;
59   }
60
61   /**
62    * This does the same thing as ILabelProvider.getText,
63    * it fetches the label text specific to this object instance.
64    */

65   public String JavaDoc getText(Object JavaDoc object)
66   {
67     if (object instanceof EList)
68     {
69       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
70       for (Iterator JavaDoc i = ((List JavaDoc)object).iterator(); i.hasNext(); )
71       {
72         Object JavaDoc child = i.next();
73         if (result.length() != 0)
74         {
75           result.append(", ");
76         }
77         result.append(getText(child));
78       }
79       return result.toString();
80     }
81     else
82     {
83       //
84
IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProvider.class);
85   
86       return
87         itemLabelProvider != null ?
88           itemLabelProvider.getText(object) :
89           object == null ?
90             "" :
91             object.toString();
92     }
93   }
94
95   /**
96    * This does the same thing as ILabelProvider.getImage,
97    * it fetches the label image specific to this object instance.
98    */

99   public Object JavaDoc getImage(Object JavaDoc object)
100   {
101     if (object instanceof EList)
102     {
103       for (Iterator JavaDoc i = ((List JavaDoc)object).iterator(); i.hasNext(); )
104       {
105         Object JavaDoc child = i.next();
106         return getImage(child);
107       }
108
109       return null;
110     }
111     else
112     {
113       //
114
IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProvider.class);
115   
116       return
117         itemLabelProvider != null ?
118           itemLabelProvider.getImage(object) :
119           null;
120     }
121   }
122
123   /**
124    * This does the same thing as ITableLabelProvider.getColumnText.
125    */

126   public String JavaDoc getColumnText(Object JavaDoc object, int columnIndex)
127   {
128     // Get the adapter from the factory.
129
//
130
ITableItemLabelProvider tableItemLabelProvider = (ITableItemLabelProvider)adapterFactory.adapt(object, ITableItemLabelProvider.class);
131
132     // Now we could check that the adapter implements interface ITableItemLabelProvider.
133
//
134
if (tableItemLabelProvider != null)
135     {
136       // And delegate the call.
137
//
138
return tableItemLabelProvider.getColumnText(object, columnIndex);
139     }
140     // Otherwise, we could check that the adapter implements interface IItemLabelProvider.
141
//
142
else
143     {
144       IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProvider.class);
145       if (itemLabelProvider != null)
146       {
147         // And delegate the call.
148
//
149
return itemLabelProvider.getText(object);
150       }
151       // If there is a column object, just convert it to a string.
152
//
153
else if (object != null)
154       {
155         return object.toString();
156       }
157       else
158       {
159       return "";
160       }
161     }
162   }
163
164   /**
165    * This does the same thing as ITableLabelProvider.getColumnImage.
166    */

167   public Object JavaDoc getColumnImage(Object JavaDoc object, int columnIndex)
168   {
169     // Get the adapter from the factory.
170
//
171
ITableItemLabelProvider tableItemLabelProvider = (ITableItemLabelProvider)adapterFactory.adapt(object, ITableItemLabelProvider.class);
172
173     // No image is a good default.
174
//
175
Object JavaDoc result = null;
176
177     // Now we could check that the adapter implements interface ITableItemLabelProvider.
178
//
179
if (tableItemLabelProvider != null)
180     {
181       // And delegate the call.
182
//
183
result = tableItemLabelProvider.getColumnImage(object, columnIndex);
184     }
185     // Otherwise, we could check that the adapter implements interface IItemLabelProvider.
186
//
187
else
188     {
189       IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(object, IItemLabelProvider.class);
190       if (itemLabelProvider != null)
191       {
192         // And delegate the call.
193
//
194
result = itemLabelProvider.getImage(object);
195       }
196     }
197
198     return result;
199   }
200
201
202   public List JavaDoc getPropertyDescriptors(Object JavaDoc object)
203   {
204     //
205
IItemPropertySource itemPropertySource = (IItemPropertySource)adapterFactory.adapt(object, IItemPropertySource.class);
206
207     return
208       itemPropertySource != null ?
209         itemPropertySource.getPropertyDescriptors(object) :
210         null;
211   }
212
213   public IItemPropertyDescriptor getPropertyDescriptor(Object JavaDoc object, Object JavaDoc propertyId)
214   {
215     //
216
IItemPropertySource itemPropertySource = (IItemPropertySource)adapterFactory.adapt(object, IItemPropertySource.class);
217
218     return
219       itemPropertySource != null ?
220         itemPropertySource.getPropertyDescriptor(object, propertyId) :
221         null;
222   }
223
224   public Object JavaDoc getEditableValue(Object JavaDoc object)
225   {
226     //
227
IItemPropertySource itemPropertySource = (IItemPropertySource)adapterFactory.adapt(object, IItemPropertySource.class);
228
229     return
230       itemPropertySource != null ?
231         itemPropertySource.getEditableValue(object) :
232         object;
233   }
234
235   /**
236    * This does the same thing as IStructuredContentProvider.getElements.
237    */

238   public Collection JavaDoc getElements(Object JavaDoc object)
239   {
240     // Get the adapter from the factory.
241
//
242
IStructuredItemContentProvider structuredItemContentProvider =
243       (IStructuredItemContentProvider)adapterFactory.adapt(object, IStructuredItemContentProvider.class);
244
245     // Either delegate the call or return nothing.
246
//
247
return
248       structuredItemContentProvider != null ?
249         structuredItemContentProvider.getElements(object) :
250         Collections.EMPTY_LIST;
251   }
252
253
254   /**
255    * This does the same thing as ITreeContentProvider.getChildren.
256    */

257   public Collection JavaDoc getChildren(Object JavaDoc object)
258   {
259     // Get the adapter from the factory.
260
//
261
ITreeItemContentProvider treeItemContentProvider =
262       (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProvider.class);
263
264     // Either delegate the call or return nothing.
265
//
266
return
267       treeItemContentProvider != null ?
268         treeItemContentProvider.getChildren(object) :
269         Collections.EMPTY_LIST;
270   }
271
272   /**
273    * This does the same thing as ITreeContentProvider.hasChildren.
274    */

275   public boolean hasChildren(Object JavaDoc object)
276   {
277     // Get the adapter from the factory.
278
//
279
ITreeItemContentProvider treeItemContentProvider =
280       (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProvider.class);
281
282     // Either delegate the call or return nothing.
283
//
284
return
285       treeItemContentProvider != null &&
286         treeItemContentProvider.hasChildren(object);
287   }
288
289   /**
290    * This does the same thing as ITreeContentProvider.getParent.
291    */

292   public Object JavaDoc getParent(Object JavaDoc object)
293   {
294     // Get the adapter from the factory.
295
//
296
ITreeItemContentProvider treeItemContentProvider =
297       (ITreeItemContentProvider)adapterFactory.adapt(object, ITreeItemContentProvider.class);
298
299     // Either delegate the call or return nothing.
300
//
301
return
302       treeItemContentProvider != null ?
303         treeItemContentProvider.getParent(object) :
304         null;
305   }
306
307   /**
308    * This sets the given object's parent to be parent.
309    */

310   //public void setParent(Object object, Object parent)
311

312
313   /**
314    * This returns the text that will be displayed when editing begins.
315    */

316   //public String getUpdateableText(Object object)
317

318   /**
319    * This sets the given object's label text to the given text.
320    */

321   //public void setText(Object object, String text)
322

323   /**
324    * This returns a list of objects describing the different children that
325    * can be added under the specified object in the editing domain,
326    * following the specified sibling as closely as possible (if non-null).
327    */

328   public Collection JavaDoc getNewChildDescriptors(Object JavaDoc object, EditingDomain editingDomain, Object JavaDoc sibling)
329   {
330     // Get the adapter from the factory.
331
//
332
IEditingDomainItemProvider editingDomainItemProvider =
333       (IEditingDomainItemProvider)adapterFactory.adapt(object, IEditingDomainItemProvider.class);
334
335     // Either delegate the call or return nothing.
336
//
337
return
338       editingDomainItemProvider != null ?
339         editingDomainItemProvider.getNewChildDescriptors(object, editingDomain, sibling) :
340         Collections.EMPTY_LIST;
341   }
342
343   /**
344    * This does the same thing as {@link org.eclipse.emf.edit.domain.EditingDomain#createCommand EditingDomain.createCommand},
345    * i.e., it creates commands for a domain's model objects.
346    */

347   public Command createCommand(Object JavaDoc object, EditingDomain editingDomain, Class JavaDoc commandClass, CommandParameter commandParameter)
348   {
349     // Get the adapter from the factory.
350
//
351
IEditingDomainItemProvider editingDomainItemProvider =
352       (IEditingDomainItemProvider)adapterFactory.adapt(object, IEditingDomainItemProvider.class);
353
354     // Either delegate the call or return nothing.
355
//
356
return
357       editingDomainItemProvider != null ?
358         editingDomainItemProvider.createCommand(object, editingDomain, commandClass, commandParameter) :
359         UnexecutableCommand.INSTANCE;
360   }
361 }
362
Popular Tags