1 18 package org.eclipse.emf.edit.ui.provider; 19 20 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 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 54 public class PropertyDescriptor implements IPropertyDescriptor 55 { 56 59 protected Object object; 60 61 64 protected IItemPropertyDescriptor itemPropertyDescriptor; 65 66 69 public PropertyDescriptor(Object object, IItemPropertyDescriptor itemPropertyDescriptor) 70 { 71 this.object = object; 72 this.itemPropertyDescriptor = itemPropertyDescriptor; 73 } 74 75 public String getCategory() 76 { 77 return itemPropertyDescriptor.getCategory(object); 78 } 79 80 public String getDescription() 81 { 82 return itemPropertyDescriptor.getDescription(object); 83 } 84 85 public String getDisplayName() 86 { 87 return itemPropertyDescriptor.getDisplayName(object); 88 } 89 90 public String [] getFilterFlags() 91 { 92 return itemPropertyDescriptor.getFilterFlags(object); 93 } 94 95 public Object getHelpContextIds() 96 { 97 return itemPropertyDescriptor.getHelpContextIds(object); 98 } 99 100 public Object 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 getText(Object object) 112 { 113 return itemLabelProvider.getText(object); 114 } 115 public Image getImage(Object 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 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 isValid(Object object) 147 { 148 if (EDataTypeCellEditor.this.eDataType.isInstance(object) && 149 !(EDataTypeCellEditor.this.eDataType.getInstanceClass() == Object .class && object instanceof String )) 150 { 151 return null; 152 } 153 else 154 { 155 String string = (String )object; 156 try 157 { 158 EDataTypeCellEditor.this.eDataType.getEPackage().getEFactoryInstance().createFromString 159 (EDataTypeCellEditor.this.eDataType, string); 160 return null; 161 } 162 catch (Exception exception) 163 { 164 return exception.getMessage(); 165 } 166 } 167 } 168 }); 169 } 170 171 public Object doGetValue() 172 { 173 return eDataType.getEPackage().getEFactoryInstance().createFromString(eDataType, (String )super.doGetValue()); 174 } 175 176 public void doSetValue(Object value) 177 { 178 String stringValue = eDataType.getEPackage().getEFactoryInstance().convertToString(eDataType, value); 179 super.doSetValue(stringValue = (stringValue == null ? "" : stringValue)); 180 valueChanged(true, isCorrect(stringValue)); 181 } 182 } 183 184 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 isValid(Object object) 197 { 198 if (object instanceof Integer ) 199 { 200 return null; 201 } 202 else 203 { 204 String string = (String )object; 205 try 206 { 207 Integer.parseInt(string); 208 return null; 209 } 210 catch (NumberFormatException exception) 211 { 212 return exception.getMessage(); 213 } 214 } 215 } 216 }); 217 } 218 219 public Object doGetValue() 220 { 221 return new Integer (Integer.parseInt((String )super.doGetValue())); 222 } 223 224 public void doSetValue(Object value) 225 { 226 super.doSetValue(value.toString()); 227 } 228 } 229 230 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 isValid(Object object) 243 { 244 if (object instanceof Float ) 245 { 246 return null; 247 } 248 else 249 { 250 String string = (String )object; 251 try 252 { 253 Float.parseFloat(string); 254 return null; 255 } 256 catch (NumberFormatException exception) 257 { 258 return exception.getMessage(); 259 } 260 } 261 } 262 }); 263 } 264 265 public Object doGetValue() 266 { 267 return new Float (Float.parseFloat((String )super.doGetValue())); 268 } 269 270 public void doSetValue(Object value) 271 { 272 super.doSetValue(value.toString()); 273 } 274 } 275 276 protected static final EcorePackage ecorePackage = EcorePackage.eINSTANCE; 277 278 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 genericFeature = itemPropertyDescriptor.getFeature(object); 292 if (genericFeature instanceof EReference[]) 293 { 294 result = new ExtendedComboBoxCellEditor( 295 composite, 296 new ArrayList (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 choiceOfValues = itemPropertyDescriptor.getChoiceOfValues(object); 305 if (choiceOfValues != null) 306 { 307 if (itemPropertyDescriptor.isMany(object)) 308 { 309 boolean valid = true; 310 for (Iterator i = choiceOfValues.iterator(); i.hasNext();) 311 { 312 Object 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 openDialogBox(Control cellEditorWindow) 325 { 326 FeatureEditorDialog dialog = new FeatureEditorDialog( 327 cellEditorWindow.getShell(), 328 getEditLabelProvider(), 329 object, 330 feature.getEType(), 331 (List )((IItemPropertySource)itemPropertyDescriptor.getPropertyValue(object)).getEditableValue(object), 332 getDisplayName(), 333 new ArrayList (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 (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 openDialogBox(Control cellEditorWindow) 356 { 357 FeatureEditorDialog dialog = new FeatureEditorDialog( 358 cellEditorWindow.getShell(), 359 getEditLabelProvider(), 360 object, 361 feature.getEType(), 362 (List )((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 .class || eDataType.getInstanceClass() == Boolean.TYPE) 371 { 372 result = new ExtendedComboBoxCellEditor( 373 composite, 374 Arrays.asList(new Object []{ 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 |