wiki:CustomizeProperties

Version 3 (modified by jdamerow, 16 years ago) (diff)

--

Customize properties

<back

This section is about customizing properties on the basis of an example.

What I want: My element A has a property of type B. B is something like a file so it has a property "path" of type String. I want a file chooser to choose the file. The path of the file should be stored in the path property of the property B of element A.

What to do:

  • Write a class like the following. This is the file choose itself.
    public class FileChooserCellEditor extends DialogCellEditor {
    
       private Composite parent;
    	
       public FileChooserCellEditor(Composite parent) {
    	super(parent);
    	this.parent = parent;
       }
    
       protected Object openDialogBox(Control cellEditorWindow) {
    	FileDialog dialog = new FileDialog(parent.getShell(), SWT.OPEN);
    	return dialog.open();
       }
    }
    
    
  • Write a property descriptor for all your properties:
    public class ExhibitionPropertyDescriptor extends
    		EMFCompositeSourcePropertyDescriptor {
    
       IItemPropertyDescriptor itemPropertyDescriptor;
    	
       public ExhibitionPropertyDescriptor(Object object,
    		IItemPropertyDescriptor itemPropertyDescriptor, String category) {
    	super(object, itemPropertyDescriptor, category);
    	this.itemPropertyDescriptor = itemPropertyDescriptor;
       }
    
       /* here you can instanciate whatever you want to be 
          opened when the user clicks on the button in the property field
       */
       protected CellEditor doCreateEditor(Composite composite) {
    	if (itemPropertyDescriptor instanceof ImagePropertyDescriptor) {
    	   return new FileChooserCellEditor(composite);
    	}
    	return super.doCreateEditor(composite);
       }
    }
    
  • Change your XXXPropertySection in the package "sheet" as follows:
    /**
     * @generated NOT
     */
    public IPropertySource getPropertySource(Object object) {
       if (object instanceof IPropertySource) {
    	return (IPropertySource) object;
       }
       AdapterFactory af = getAdapterFactory(object);
       if (af != null) {
    	IItemPropertySource ips = (IItemPropertySource) af.adapt(object,
    				IItemPropertySource.class);
    	if (ips != null) {
    	   return new EMFCompositePropertySource(object, ips,
    					null) {
    		protected IPropertyDescriptor newPropertyDescriptor(
    			IItemPropertyDescriptor itemPropertyDescriptor) {
    			return new ExhibitionPropertyDescriptor(object,
    				itemPropertyDescriptor, getCategory());
    		}
    	   };
    	}
       }
       if (object instanceof IAdaptable) {
    	return (IPropertySource) ((IAdaptable) object)
    		.getAdapter(IPropertySource.class);
    	}
       return null;
    }
    
  • Write a new ItemPropertyDescriptor? for B:
    public class ImagePropertyDescriptor extends ItemPropertyDescriptor {
    	
    	EStructuralFeature feature;
    	
    	public ImagePropertyDescriptor(AdapterFactory adapterFactory,
    			ResourceLocator resourceLocator, String displayName,
    			String description, EStructuralFeature feature, boolean isSettable,
    			boolean multiLine, boolean sortChoices, Object staticImage,
    			String category, String[] filterFlags) {
    		super(adapterFactory, resourceLocator, displayName, description, feature,
    				isSettable, multiLine, sortChoices, staticImage, category, filterFlags);
    		this.feature = feature;
    	}
    
            // here you decide what data of B is displayed in property view for A
    	@Override
    	protected Object getValue(EObject object, EStructuralFeature feature) {
    		Object propertyObject = object.eGet(feature);
    		if (propertyObject == null) return "";
    		if (propertyObject instanceof Image)
    		{
    			String path = ((Image) propertyObject).getImagePath();
    			return path;
    		}
    		return super.getValue(object, feature);
    	}
    
            // this is how to handle the input from the file chooser
    	@Override
    	public void setPropertyValue(Object object, Object value) {
    		if (object instanceof EObject)
    		{
    			EObject eobject = (EObject) object;
    			Image backgroundImage = ExhibitionFactory.eINSTANCE.createImage();
    			backgroundImage.setImagePath(value.toString());
    			eobject.eSet(feature, backgroundImage);
    			return;
    		}
    		super.setPropertyValue(object, value);
    	}
    	
    	
    }