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 :
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 :
In order to restrict the students list, we have to modify the ItemProvider
associated to the Student
entity in the edit
plugin.
/School.edit/src/jfb/examples/gmf/school/provider/StudentItemProvider.java
fileaddFriendsPropertyDescriptor
methodNOT
after the @generate
string.createItemPropertyDescriptor
by new ItemPropertyDescriptor
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; } }); }
School
modelFriends
editor dialogMatt Sharp
, we get the following result :
I hope that this material will be helpful for you. If you want to support it, your help is welcome :
Discussion
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!!