gmf_how_to_programmatically_define_readonly_labels [GMF Samples And Tutorials]
 

Overview

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.

Diagram Editor plugin modification

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 plugin modification

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

Eshika, 2011/09/08 13:20

I want to know the xmi id of my component when i right click on that.How can i get it?Please help.

Jean-François Brazeau, 2011/12/08 21:17

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.

 
gmf_how_to_programmatically_define_readonly_labels.txt · Last modified: 2010/01/29 17:49 by jfbraz
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki