KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > provider > PropertyDescriptor


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

18 package org.eclipse.emf.edit.ui.provider;
19
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.eclipse.jface.viewers.CellEditor;
28 import org.eclipse.jface.viewers.ICellEditorValidator;
29 import org.eclipse.jface.viewers.ILabelProvider;
30 import org.eclipse.jface.viewers.LabelProvider;
31 import org.eclipse.jface.viewers.TextCellEditor;
32 import org.eclipse.swt.graphics.Image;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.ui.views.properties.IPropertyDescriptor;
36
37 import org.eclipse.emf.common.ui.celleditor.ExtendedComboBoxCellEditor;
38 import org.eclipse.emf.common.ui.celleditor.ExtendedDialogCellEditor;
39 import org.eclipse.emf.ecore.EClassifier;
40 import org.eclipse.emf.ecore.EDataType;
41 import org.eclipse.emf.ecore.EReference;
42 import org.eclipse.emf.ecore.EStructuralFeature;
43 import org.eclipse.emf.ecore.EcorePackage;
44 import org.eclipse.emf.edit.provider.IItemLabelProvider;
45 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
46 import org.eclipse.emf.edit.provider.IItemPropertySource;
47 import org.eclipse.emf.edit.ui.celleditor.FeatureEditorDialog;
48
49
50 /**
51  * This is used to encapsulate an {@link IItemPropertyDescriptor} along with the object for which it is an item property source
52  * and make it behave like an {@link org.eclipse.ui.views.properties.IPropertyDescriptor}.
53  */

54 public class PropertyDescriptor implements IPropertyDescriptor
55 {
56   /**
57    * This is the object for which this class is a property source.
58    */

59   protected Object JavaDoc object;
60
61   /**
62    * This is the descriptor to which we will delegate all the {@link org.eclipse.ui.views.properties.IPropertyDescriptor} methods.
63    */

64   protected IItemPropertyDescriptor itemPropertyDescriptor;
65
66   /**
67    * An instance is constructed from an object and its item property source.
68    */

69   public PropertyDescriptor(Object JavaDoc object, IItemPropertyDescriptor itemPropertyDescriptor)
70   {
71     this.object = object;
72     this.itemPropertyDescriptor = itemPropertyDescriptor;
73   }
74
75   public String JavaDoc getCategory()
76   {
77     return itemPropertyDescriptor.getCategory(object);
78   }
79
80   public String JavaDoc getDescription()
81   {
82     return itemPropertyDescriptor.getDescription(object);
83   }
84
85   public String JavaDoc getDisplayName()
86   {
87     return itemPropertyDescriptor.getDisplayName(object);
88   }
89
90   public String JavaDoc[] getFilterFlags()
91   {
92     return itemPropertyDescriptor.getFilterFlags(object);
93   }
94
95   public Object JavaDoc getHelpContextIds()
96   {
97     return itemPropertyDescriptor.getHelpContextIds(object);
98   }
99
100   public Object JavaDoc getId()
101   {
102     return itemPropertyDescriptor.getId(object);
103   }
104
105   public ILabelProvider getLabelProvider()
106   {
107     final IItemLabelProvider itemLabelProvider = itemPropertyDescriptor.getLabelProvider(object);
108     return
109       new LabelProvider()
110       {
111         public String JavaDoc getText(Object JavaDoc object)
112         {
113           return itemLabelProvider.getText(object);
114         }
115         public Image getImage(Object JavaDoc object)
116         {
117           return ExtendedImageRegistry.getInstance().getImage(itemLabelProvider.getImage(object));
118         }
119       };
120   }
121
122   protected ILabelProvider getEditLabelProvider()
123   {
124     return getLabelProvider();
125   }
126
127   public boolean isCompatibleWith(IPropertyDescriptor anotherProperty)
128   {
129     return false;
130   }
131
132   /**
133    * This cell editor ensures that only type-compatible values are supported
134    */

135   public static class EDataTypeCellEditor extends TextCellEditor
136   {
137     protected EDataType eDataType;
138
139     public EDataTypeCellEditor(EDataType eDataType, Composite composite)
140     {
141       super(composite);
142       this.eDataType = eDataType;
143       setValidator
144         (new ICellEditorValidator()
145          {
146            public String JavaDoc isValid(Object JavaDoc object)
147            {
148              if (EDataTypeCellEditor.this.eDataType.isInstance(object) &&
149                    !(EDataTypeCellEditor.this.eDataType.getInstanceClass() == Object JavaDoc.class && object instanceof String JavaDoc))
150              {
151                return null;
152              }
153              else
154              {
155                String JavaDoc string = (String JavaDoc)object;
156                try
157                {
158                  EDataTypeCellEditor.this.eDataType.getEPackage().getEFactoryInstance().createFromString
159                    (EDataTypeCellEditor.this.eDataType, string);
160                  return null;
161                }
162                catch (Exception JavaDoc exception)
163                {
164                  return exception.getMessage();
165                }
166              }
167            }
168          });
169     }
170
171     public Object JavaDoc doGetValue()
172     {
173       return eDataType.getEPackage().getEFactoryInstance().createFromString(eDataType, (String JavaDoc)super.doGetValue());
174     }
175
176     public void doSetValue(Object JavaDoc value)
177     {
178       String JavaDoc stringValue = eDataType.getEPackage().getEFactoryInstance().convertToString(eDataType, value);
179       super.doSetValue(stringValue = (stringValue == null ? "" : stringValue));
180       valueChanged(true, isCorrect(stringValue));
181     }
182   }
183
184   /**
185    * This cell editor ensures that only Integer values are supported
186    * @deprecated
187    */

188   public static class IntegerCellEditor extends TextCellEditor
189   {
190     public IntegerCellEditor(Composite composite)
191     {
192       super(composite);
193       setValidator
194         (new ICellEditorValidator()
195          {
196            public String JavaDoc isValid(Object JavaDoc object)
197            {
198              if (object instanceof Integer JavaDoc)
199              {
200                return null;
201              }
202              else
203              {
204                String JavaDoc string = (String JavaDoc)object;
205                try
206                {
207                  Integer.parseInt(string);
208                  return null;
209                }
210                catch (NumberFormatException JavaDoc exception)
211                {
212                  return exception.getMessage();
213                }
214              }
215            }
216          });
217     }
218
219     public Object JavaDoc doGetValue()
220     {
221       return new Integer JavaDoc(Integer.parseInt((String JavaDoc)super.doGetValue()));
222     }
223
224     public void doSetValue(Object JavaDoc value)
225     {
226       super.doSetValue(value.toString());
227     }
228   }
229
230   /**
231    * This cell editor ensures that only Float values are supported
232    * @deprecated
233    */

234   public static class FloatCellEditor extends TextCellEditor
235   {
236     public FloatCellEditor(Composite composite)
237     {
238       super(composite);
239       setValidator
240         (new ICellEditorValidator()
241          {
242            public String JavaDoc isValid(Object JavaDoc object)
243            {
244              if (object instanceof Float JavaDoc)
245              {
246                return null;
247              }
248              else
249              {
250                String JavaDoc string = (String JavaDoc)object;
251                try
252                {
253                  Float.parseFloat(string);
254                  return null;
255                }
256                catch (NumberFormatException JavaDoc exception)
257                {
258                  return exception.getMessage();
259                }
260              }
261            }
262          });
263     }
264
265     public Object JavaDoc doGetValue()
266     {
267       return new Float JavaDoc(Float.parseFloat((String JavaDoc)super.doGetValue()));
268     }
269
270     public void doSetValue(Object JavaDoc value)
271     {
272       super.doSetValue(value.toString());
273     }
274   }
275
276   protected static final EcorePackage ecorePackage = EcorePackage.eINSTANCE;
277
278   /**
279    * This returns the cell editor that will be used to edit the value of this property.
280    * This default implementation determines the type of cell editor from the nature of the structural feature.
281    */

282   public CellEditor createPropertyEditor(Composite composite)
283   {
284     if (!itemPropertyDescriptor.canSetProperty(object))
285     {
286       return null;
287     }
288
289     CellEditor result = null;
290
291     Object JavaDoc genericFeature = itemPropertyDescriptor.getFeature(object);
292     if (genericFeature instanceof EReference[])
293     {
294       result = new ExtendedComboBoxCellEditor(
295         composite,
296         new ArrayList JavaDoc(itemPropertyDescriptor.getChoiceOfValues(object)),
297         getEditLabelProvider(),
298         true);
299     }
300     else if (genericFeature instanceof EStructuralFeature)
301     {
302       final EStructuralFeature feature = (EStructuralFeature)genericFeature;
303       final EClassifier eType = feature.getEType();
304       final Collection JavaDoc choiceOfValues = itemPropertyDescriptor.getChoiceOfValues(object);
305       if (choiceOfValues != null)
306       {
307         if (itemPropertyDescriptor.isMany(object))
308         {
309           boolean valid = true;
310           for (Iterator JavaDoc i = choiceOfValues.iterator(); i.hasNext();)
311           {
312             Object JavaDoc choice = i.next();
313             if (!eType.isInstance(choice))
314             {
315               valid = false;
316               break;
317             }
318           }
319
320           if (valid)
321           {
322             result = new ExtendedDialogCellEditor(composite, getEditLabelProvider())
323               {
324                 protected Object JavaDoc openDialogBox(Control cellEditorWindow)
325                 {
326                   FeatureEditorDialog dialog = new FeatureEditorDialog(
327                     cellEditorWindow.getShell(),
328                     getEditLabelProvider(),
329                     object,
330                     feature.getEType(),
331                     (List JavaDoc)((IItemPropertySource)itemPropertyDescriptor.getPropertyValue(object)).getEditableValue(object),
332                     getDisplayName(),
333                     new ArrayList JavaDoc(choiceOfValues));
334                   dialog.open();
335                   return dialog.getResult();
336                 }
337               };
338           }
339         }
340
341         if (result == null)
342         {
343           result = new ExtendedComboBoxCellEditor(composite, new ArrayList JavaDoc(choiceOfValues), getEditLabelProvider(), true);
344         }
345       }
346       else if (eType instanceof EDataType)
347       {
348         EDataType eDataType = (EDataType)eType;
349         if (eDataType.isSerializable())
350         {
351           if (itemPropertyDescriptor.isMany(object))
352           {
353             result = new ExtendedDialogCellEditor(composite, getEditLabelProvider())
354               {
355                 protected Object JavaDoc openDialogBox(Control cellEditorWindow)
356                 {
357                   FeatureEditorDialog dialog = new FeatureEditorDialog(
358                     cellEditorWindow.getShell(),
359                     getEditLabelProvider(),
360                     object,
361                     feature.getEType(),
362                     (List JavaDoc)((IItemPropertySource)itemPropertyDescriptor.getPropertyValue(object)).getEditableValue(object),
363                     getDisplayName(),
364                     null);
365                   dialog.open();
366                   return dialog.getResult();
367                 }
368               };
369           }
370           else if (eDataType.getInstanceClass() == Boolean JavaDoc.class || eDataType.getInstanceClass() == Boolean.TYPE)
371           {
372             result = new ExtendedComboBoxCellEditor(
373               composite,
374               Arrays.asList(new Object JavaDoc []{ Boolean.FALSE, Boolean.TRUE }),
375               getEditLabelProvider(),
376               true);
377           }
378           else
379           {
380             result = new EDataTypeCellEditor(eDataType, composite);
381           }
382         }
383       }
384     }
385
386     return result;
387   }
388 }
389
Popular Tags