wiki:CustomizeProperties

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 chooser 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);
    	}
    	
    	
    }
    
  • the last thing is that you add the new property descriptor in you AItemProvider Class:
    /**
     * This returns the property descriptors for the adapted class.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated NOT
     */
    @Override
    public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) {
    	if (itemPropertyDescriptors == null) {
    		super.getPropertyDescriptors(object);
    
    		addTitlePropertyDescriptor(object);
    		addDescriptionPropertyDescriptor(object);
                    // --------------- this is new -----------
    		addBackgroundImageDescriptor(object);
                    // ---------------------------------------
    	}
    	return itemPropertyDescriptors;
    }
    


and this is the new method:

protected void addBackgroundImageDescriptor(Object object)
{
   IItemPropertyDescriptor imagePropertyDescriptor =
	new ImagePropertyDescriptor(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
	   getResourceLocator(),
	   getString("_UI_Scene_backgroundImage_feature"),
	   getString("_UI_PropertyDescriptor_description", "_UI_Scene_backgroundImage_feature", "_UI_Scene_type"),
	   ExhibitionPackage.Literals.SCENE__BACKGROUND_IMAGE,
	   true,
	   false,
	   false,
	   ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
	   null,
  	   null);
   itemPropertyDescriptors.add(imagePropertyDescriptor);
}
Last modified 17 years ago Last modified on Mar 27, 2008, 4:55:17 PM