emf_tutorial3 [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 restrict the available elements that can be chosen in a list property (by default EMF allows to select any element that matches the selected property type).

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 :

Let's imagine that we don't want a student to become a friend of a student that is not in his classroom.

Actually, if you select a Student and click on the Friends property, a dialog appears in which all the students are present :

Tutorial steps

In order to restrict the students list, 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 getChoiceOfValues method like this (the implementation only takes the students of the same classroom, removes from the list the student himself and sorts the result according to the students names) :
/**
 * 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 Collection<?> getChoiceOfValues(Object object) {
				Student student = (Student) object;
				Classroom classroom = (Classroom) student.eContainer(); // Retrieve the classroom students
				List<Student> students = new ArrayList<Student>(); // Copy the students to a temporary list
				students.addAll(classroom.getStudents());
				students.remove(object); // Removes the student himself from the available friends
				// Sorts the result
				Collections.sort(students, new Comparator<Student>() {
					@Override
					public int compare(Student s1, Student s2) {
						String s1Name = s1.getName();
						String s2Name = s2.getName();
						if (s1Name == null && s2Name == null) {
							return 0;
						}
						else if (s1Name == null) {
							return 1;
						}
						else if (s2Name == null) {
							return -1;
						}
						else {
							return s1Name.compareTo(s2Name);
						}
					}
				});
				return students;
			}
		});
}

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 only get the students of the same classroom and sorted by name. In our example, if we select Matt Sharp, we get the following result :

Thank you

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

Discussion

rajaa, 2012/05/15 22:49

salut,merci pour ces magnifiques tuto ;) en fait je veux savoir comment afficher une liste deroulante qui prend deux valeurs(par exemple pour l'attribut coleur je dois afficher rouge et noire.

merci d'avance!!

 
emf_tutorial3.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