emf_tutorial4 [GMF Samples And Tutorials]
 

Overview

This tutorial is an EMF tutorial and is compatible with GMF. To go fast, it takes the output of the GMF tutorial 7 as an input (in order not to have to build a new ecore model).

It shows how to change the element label in the list dialog opened from the properties view.

This is helpful when your model has several elements with the same label that you cannot distinguish in the default generated EMF list.

This tutorial is based on the model built in the seventh GMF tutorial wich is availabale through :

This tutorial has been built with :

  • Eclipse 3.6
  • Graphical Modeling Framework SDK 2.3.0
  • Eclipse Modeling Framework 2.6.0

Default EMF behaviour

Let's imagine we've created a School model in which we have several classrooms like this (notice that the 3 classrooms have a student named Beck) :

If you select a Student and click on the Friends property, a dialog appears in which you see three Beck but it is not possible to know which Beck belongs to which Classroom :

Tutorial steps

In that case, it would be interesting to put the classroom's name before each student.

In order to do that, we have to modify the ItemProvider associated to the Student entity in the edit plugin.

  • Open the /School.edit/src/jfb/examples/gmf/school/provider/StudentItemProvider.java file
  • Go to the addFriendsPropertyDescriptor method
  • Add NOT after the @generate string.
  • Replace the string createItemPropertyDescriptor by new ItemPropertyDescriptor
  • Open a brace in order to override a method (see below)
  • Override the getLabelProvider method like this :
/**
 * This adds a property descriptor for the Friends feature.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
protected void addFriendsPropertyDescriptor(Object object) {
	itemPropertyDescriptors.add
		(new ItemPropertyDescriptor
			(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
			 getResourceLocator(),
			 getString("_UI_Student_friends_feature"),
			 getString("_UI_PropertyDescriptor_description", "_UI_Student_friends_feature", "_UI_Student_type"),
			 SchoolPackage.Literals.STUDENT__FRIENDS,
			 true,
			 false,
			 true,
			 null,
			 null,
			 null) {
			@Override
			public IItemLabelProvider getLabelProvider(Object object) {
				final IItemLabelProvider defaultLabelProvider = super.getLabelProvider(object);
				return new IItemLabelProvider() {
					@Override
					public String getText(Object object) {
						// If the object is a student
						if (object instanceof Student) {
							Student student = (Student) object;
							Classroom classroom = (Classroom) student.eContainer();
							return student.getName() + " [" + classroom.getName() + "]";
						}
						// Other cases (student list)
						else {
							return defaultLabelProvider.getText(object);
						}
					}
					@Override
					public Object getImage(Object object) {
						return defaultLabelProvider.getImage(object);
					}
				};
			}
		});
}

Run and test

  • Run the eclipse configuration that had been created in the first tutorial.
  • Open your School model
  • Select a student in a classroom and open the Friends editor dialog
  • You should now get a students label list containing the classroom names. In our example, it is now possible to see which Beck comes from which Classroom :

Thank you

I hope that this material will be helpful for you. If you want to support it, your help is welcome :

 
emf_tutorial4.txt · Last modified: 2011/08/08 20:05 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