wiki:CustomizeProperties

Version 2 (modified by jdamerow, 17 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;
    }