With the gmfmap model, GMF allows easily to make a label readonly.
But sometimes, it is necessary to make a label get readonly or not programmatically, for example by coding conditions based on the diagram model.
There are two ways to modify a label's value : through the diagram editor and through the properties view. In order to cover both ways, we have to work modify the XXX.diagram
plugin and the XXX.edit
plugin.
Edit the editpart's source associated to your label.
Look at the isEditable
method which by default looks like this :
/** * @generated */ protected boolean isEditable() { return getParser() != null; }
Modify it in order to meet your own needs…
/** * @generated NOT */ protected boolean isEditable() { MyType element = (MyType) getNotationView().getElement(); boolean isEditable = false; if (element..... // Custom conditions here... return isEditable ; }
Edit the item provider of the class that contains the attribute of which we want to control the readonly behavior.
Look at the method named add[attribute name]PropertyDescriptor
.
In the implementation body of this method, replace the createItemPropertyDescriptor
method invocation by an instanciation of a IItemPropertyDescriptor
class (keeping the createItemPropertyDescriptor
method arguments as constructor arguments) and override the canSetProperty
like this :
/** * This adds a property descriptor for the Probability feature. * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated NOT */ protected void add[attribute name]PropertyDescriptor(Object object) { IItemPropertyDescriptor descriptor = new ItemPropertyDescriptor( ((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_[type name]_[attribute name]_feature"), getString("_UI_PropertyDescriptor_description", "_UI_[type name]_[attribute name]_feature", "_UI_[type name]_type"), [Model name]Package.Literals.[TYPE NAME]__[ATTRIBUTE NAME], true, // isSettable false, // multiline false, // sortChoices ItemPropertyDescriptor.REAL_VALUE_IMAGE, null, null) { @Override public boolean canSetProperty(Object object) { MyType element = (MyType) object; boolean canSetProperty = false; // logic here.... return canSetProperty ; } }; itemPropertyDescriptors.add(descriptor); }
Discussion
I want to know the xmi id of my component when i right click on that.How can i get it?Please help.
What do you mean by “the xmi id” of my component ? I guess that you have added an EAttribute to your EClass which is an identifier. Right ? Maybe you should take a look at this tutorial to see how to implement a custom action.