Bug 509521 - Combo box to get the selected element ID is not well
updated and contents is not refreshed

Change-Id: I9cfb2bd2aa5f5526ffcbe2117b451533fe25c954
Signed-off-by: Olivier Prouvost <olivier.prouvost@opcoach.com>
diff --git a/bundles/org.eclipse.e4.tools.emf.editor3x/src/org/eclipse/e4/tools/emf/editor3x/TargetElementProviders.java b/bundles/org.eclipse.e4.tools.emf.editor3x/src/org/eclipse/e4/tools/emf/editor3x/TargetElementProviders.java
index ff99aa5..19d14aa 100644
--- a/bundles/org.eclipse.e4.tools.emf.editor3x/src/org/eclipse/e4/tools/emf/editor3x/TargetElementProviders.java
+++ b/bundles/org.eclipse.e4.tools.emf.editor3x/src/org/eclipse/e4/tools/emf/editor3x/TargetElementProviders.java
@@ -1,116 +1,36 @@
 package org.eclipse.e4.tools.emf.editor3x;
 
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IWorkspaceRoot;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.IExtension;
 import org.eclipse.e4.tools.emf.ui.common.IModelElementProvider;
+import org.eclipse.e4.tools.emf.ui.common.Util;
 import org.eclipse.e4.ui.model.fragment.impl.FragmentPackageImpl;
 import org.eclipse.emf.common.util.TreeIterator;
-import org.eclipse.emf.common.util.URI;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
 import org.eclipse.emf.ecore.util.EcoreUtil;
-import org.eclipse.pde.internal.core.PDEExtensionRegistry;
 
 @SuppressWarnings("restriction")
 public class TargetElementProviders implements IModelElementProvider {
-	private static final String APP_E4XMI_DEFAULT = "Application.e4xmi"; //$NON-NLS-1$
 	private ResourceSet resourceSet;
 
 	@Override
 	public void getModelElements(Filter filter, ModelResultHandler handler) {
 		if (resourceSet == null) {
-			resourceSet = new ResourceSetImpl();
-			final PDEExtensionRegistry reg = new PDEExtensionRegistry();
-			IExtension[] extensions = reg.findExtensions("org.eclipse.e4.workbench.model", true); //$NON-NLS-1$
-			final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
-
-			for (final IExtension ext : extensions) {
-				for (final IConfigurationElement el : ext.getConfigurationElements()) {
-					if (el.getName().equals("fragment")) { //$NON-NLS-1$
-						URI uri;
-						// System.err.println("Model-Ext: Checking: " + ext.getContributor().getName());
-						final IProject p = root.getProject(ext.getContributor().getName());
-						if (p.exists() && p.isOpen()) {
-							uri = URI.createPlatformResourceURI(
-								ext.getContributor().getName() + "/" + el.getAttribute("uri"), true); //$NON-NLS-1$ //$NON-NLS-2$
-						} else {
-							uri = URI.createURI("platform:/plugin/" + ext.getContributor().getName() + "/" //$NON-NLS-1$ //$NON-NLS-2$
-								+ el.getAttribute("uri")); //$NON-NLS-1$
-						}
-						// System.err.println(uri);
-						try {
-							resourceSet.getResource(uri, true);
-						} catch (final Exception e) {
-							e.printStackTrace();
-							// System.err.println("=============> Failing");
-						}
-
-					}
-				}
-			}
-
-			extensions = reg.findExtensions("org.eclipse.core.runtime.products", true); //$NON-NLS-1$
-			for (final IExtension ext : extensions) {
-				for (final IConfigurationElement el : ext.getConfigurationElements()) {
-					if (el.getName().equals("product")) { //$NON-NLS-1$
-						boolean xmiPropertyPresent = false;
-						for (final IConfigurationElement prop : el.getChildren("property")) { //$NON-NLS-1$
-							if (prop.getAttribute("name").equals("applicationXMI")) { //$NON-NLS-1$//$NON-NLS-2$
-								final String v = prop.getAttribute("value"); //$NON-NLS-1$
-								setUpResourceSet(root, v);
-								xmiPropertyPresent = true;
-								break;
-							}
-						}
-						if (!xmiPropertyPresent) {
-							setUpResourceSet(root, ext.getNamespaceIdentifier() + "/" + APP_E4XMI_DEFAULT); //$NON-NLS-1$
-							break;
-						}
-					}
-				}
-			}
+			resourceSet = Util.getModelElementResources();
 		}
 
 		applyFilter(filter, handler);
 	}
 
-	private void setUpResourceSet(IWorkspaceRoot root, String v) {
-		final String[] s = v.split("/"); //$NON-NLS-1$
-		URI uri;
-		// System.err.println("Product-Ext: Checking: " + v + " => P:" + s[0] + "");
-		final IProject p = root.getProject(s[0]);
-		if (p.exists() && p.isOpen()) {
-			uri = URI.createPlatformResourceURI(v, true);
-		} else {
-			uri = URI.createURI("platform:/plugin/" + v); //$NON-NLS-1$
-		}
-
-		// System.err.println(uri);
-		try {
-			// prevent some unnecessary calls by checking the uri
-			if (resourceSet.getURIConverter().exists(uri, null)) {
-				resourceSet.getResource(uri, true);
-			}
-		} catch (final Exception e) {
-			e.printStackTrace();
-			// System.err.println("=============> Failing");
-		}
-	}
 
 	private void applyFilter(Filter filter, ModelResultHandler handler) {
 		for (final Resource res : resourceSet.getResources()) {
 			final TreeIterator<EObject> it = EcoreUtil.getAllContents(res,
-				true);
+					true);
 			while (it.hasNext()) {
 				final EObject o = it.next();
 				if (o.eContainingFeature() != FragmentPackageImpl.Literals.MODEL_FRAGMENTS__IMPORTS) {
-					if (o.eClass().equals(filter.eClass)) {
-						// System.err.println("Found: " + o);
+					if ((filter.eClass == null) || o.eClass().equals(filter.eClass)) {
 						handler.result(o);
 					}
 				}
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/Util.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/Util.java
index cba1af0..de7b1b9 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/Util.java
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/common/Util.java
@@ -20,6 +20,10 @@
 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExtension;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.e4.tools.emf.ui.common.IEditorFeature.FeatureClass;
 import org.eclipse.e4.ui.model.application.MApplicationElement;
@@ -30,6 +34,7 @@
 import org.eclipse.e4.ui.model.fragment.impl.FragmentPackageImpl;
 import org.eclipse.emf.common.command.Command;
 import org.eclipse.emf.common.util.TreeIterator;
+import org.eclipse.emf.common.util.URI;
 import org.eclipse.emf.ecore.EClass;
 import org.eclipse.emf.ecore.EClassifier;
 import org.eclipse.emf.ecore.EObject;
@@ -37,15 +42,21 @@
 import org.eclipse.emf.ecore.EReference;
 import org.eclipse.emf.ecore.EStructuralFeature;
 import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
 import org.eclipse.emf.edit.command.MoveCommand;
 import org.eclipse.emf.edit.domain.EditingDomain;
 import org.eclipse.jface.fieldassist.ControlDecoration;
 import org.eclipse.jface.fieldassist.FieldDecoration;
 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
+import org.eclipse.pde.internal.core.PDEExtensionRegistry;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Control;
 
 public class Util {
+
+	private static final String APP_E4XMI_DEFAULT = "Application.e4xmi"; //$NON-NLS-1$
+
 	public static final boolean isNullOrEmpty(String element) {
 		return element == null || element.trim().length() == 0;
 	}
@@ -59,8 +70,8 @@
 			if (c instanceof EClass) {
 				final EClass eclass = (EClass) c;
 				if (eclass != ApplicationPackageImpl.Literals.APPLICATION && !eclass.isAbstract()
-					&& !eclass.isInterface()
-					&& eclass.getEAllSuperTypes().contains(ApplicationPackageImpl.Literals.APPLICATION_ELEMENT)) {
+						&& !eclass.isInterface()
+						&& eclass.getEAllSuperTypes().contains(ApplicationPackageImpl.Literals.APPLICATION_ELEMENT)) {
 					list.add(new FeatureClass(eclass.getName(), eclass));
 				}
 			}
@@ -150,7 +161,7 @@
 	}
 
 	public static boolean moveElementByIndex(EditingDomain editingDomain, MUIElement element, boolean liveModel,
-		int index, EStructuralFeature feature) {
+			int index, EStructuralFeature feature) {
 		if (liveModel) {
 			final EObject container = ((EObject) element).eContainer();
 			@SuppressWarnings("unchecked")
@@ -176,7 +187,7 @@
 	}
 
 	public static boolean moveElementByIndex(EditingDomain editingDomain, MUIElement element, boolean liveModel,
-		int index) {
+			int index) {
 		if (liveModel) {
 			final MElementContainer<MUIElement> container = element.getParent();
 			container.getChildren().remove(element);
@@ -192,7 +203,7 @@
 		}
 		final MElementContainer<MUIElement> container = element.getParent();
 		final Command cmd = MoveCommand.create(editingDomain, container,
-			UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, element, index);
+				UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, element, index);
 
 		if (cmd.canExecute()) {
 			editingDomain.getCommandStack().execute(cmd);
@@ -201,6 +212,90 @@
 		return false;
 	}
 
+	/**
+	 * This method searches for fragments or application model elements
+	 * resources
+	 */
+	public static ResourceSet getModelElementResources() {
+		ResourceSet resourceSet = new ResourceSetImpl();
+		final PDEExtensionRegistry reg = new PDEExtensionRegistry();
+		IExtension[] extensions = reg.findExtensions("org.eclipse.e4.workbench.model", true); //$NON-NLS-1$
+		final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+
+		for (final IExtension ext : extensions) {
+			for (final IConfigurationElement el : ext.getConfigurationElements()) {
+				if (el.getName().equals("fragment")) { //$NON-NLS-1$
+					URI uri;
+					// System.err.println("Model-Ext: Checking: " +
+					// ext.getContributor().getName());
+					final IProject p = root.getProject(ext.getContributor().getName());
+					if (p.exists() && p.isOpen()) {
+						uri = URI.createPlatformResourceURI(
+								ext.getContributor().getName() + "/" + el.getAttribute("uri"), true); //$NON-NLS-1$ //$NON-NLS-2$
+					} else {
+						uri = URI.createURI("platform:/plugin/" + ext.getContributor().getName() + "/" //$NON-NLS-1$ //$NON-NLS-2$
+								+ el.getAttribute("uri")); //$NON-NLS-1$
+					}
+					// System.err.println(uri);
+					try {
+						resourceSet.getResource(uri, true);
+					} catch (final Exception e) {
+						e.printStackTrace();
+						// System.err.println("=============> Failing");
+					}
+
+				}
+			}
+		}
+
+		extensions = reg.findExtensions("org.eclipse.core.runtime.products", true); //$NON-NLS-1$
+		for (final IExtension ext : extensions) {
+			for (final IConfigurationElement el : ext.getConfigurationElements()) {
+				if (el.getName().equals("product")) { //$NON-NLS-1$
+					boolean xmiPropertyPresent = false;
+					for (final IConfigurationElement prop : el.getChildren("property")) { //$NON-NLS-1$
+						if (prop.getAttribute("name").equals("applicationXMI")) { //$NON-NLS-1$//$NON-NLS-2$
+							final String v = prop.getAttribute("value"); //$NON-NLS-1$
+							setUpResourceSet(resourceSet, root, v);
+							xmiPropertyPresent = true;
+							break;
+						}
+					}
+					if (!xmiPropertyPresent) {
+						setUpResourceSet(resourceSet, root, ext.getNamespaceIdentifier() + "/" + APP_E4XMI_DEFAULT); //$NON-NLS-1$
+						break;
+					}
+				}
+			}
+		}
+		return resourceSet;
+
+	}
+
+	private static void setUpResourceSet(ResourceSet resourceSet, IWorkspaceRoot root, String v) {
+		final String[] s = v.split("/"); //$NON-NLS-1$
+		URI uri;
+		// System.err.println("Product-Ext: Checking: " + v + " => P:" + s[0] +
+		// "");
+		final IProject p = root.getProject(s[0]);
+		if (p.exists() && p.isOpen()) {
+			uri = URI.createPlatformResourceURI(v, true);
+		} else {
+			uri = URI.createURI("platform:/plugin/" + v); //$NON-NLS-1$
+		}
+
+		// System.err.println(uri);
+		try {
+			// prevent some unnecessary calls by checking the uri
+			if (resourceSet.getURIConverter().exists(uri, null)) {
+				resourceSet.getResource(uri, true);
+			}
+		} catch (final Exception e) {
+			e.printStackTrace();
+			// System.err.println("=============> Failing");
+		}
+	}
+
 	public static final void addDecoration(Control control, Binding binding) {
 		final ControlDecoration dec = new ControlDecoration(control, SWT.BOTTOM);
 		binding.getValidationStatus().addValueChangeListener(new IValueChangeListener() {
@@ -227,8 +322,8 @@
 						fieldDecorationID = FieldDecorationRegistry.DEC_ERROR;
 						break;
 					}
-					final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
-						fieldDecorationID);
+					final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault()
+							.getFieldDecoration(fieldDecorationID);
 					dec.setImage(fieldDecoration == null ? null : fieldDecoration.getImage());
 				}
 			}
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/StringModelFragment.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/StringModelFragment.java
index 5d6dc29..08f1bb3 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/StringModelFragment.java
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/StringModelFragment.java
@@ -23,24 +23,19 @@
 import javax.inject.Inject;
 
 import org.eclipse.core.databinding.observable.list.IObservableList;
-import org.eclipse.core.databinding.observable.list.WritableList;
 import org.eclipse.core.databinding.property.list.IListProperty;
 import org.eclipse.e4.core.contexts.IEclipseContext;
 import org.eclipse.e4.emf.xpath.EcoreXPathContextFactory;
 import org.eclipse.e4.emf.xpath.XPathContext;
 import org.eclipse.e4.emf.xpath.XPathContextFactory;
 import org.eclipse.e4.tools.emf.ui.common.IEditorFeature.FeatureClass;
-import org.eclipse.e4.tools.emf.ui.common.IModelElementProvider.Filter;
-import org.eclipse.e4.tools.emf.ui.common.IModelElementProvider.ModelResultHandler;
 import org.eclipse.e4.tools.emf.ui.common.Util;
 import org.eclipse.e4.tools.emf.ui.common.component.AbstractComponentEditor;
 import org.eclipse.e4.tools.emf.ui.internal.ResourceProvider;
-import org.eclipse.e4.tools.emf.ui.internal.common.ClassContributionCollector;
 import org.eclipse.e4.tools.emf.ui.internal.common.E4PickList;
 import org.eclipse.e4.tools.emf.ui.internal.common.component.ControlFactory.TextPasteHandler;
 import org.eclipse.e4.tools.emf.ui.internal.common.component.dialogs.FeatureSelectionDialog;
 import org.eclipse.e4.tools.emf.ui.internal.common.component.dialogs.FindParentReferenceElementDialog;
-import org.eclipse.e4.tools.emf.ui.internal.common.component.dialogs.ModelResultHandlerImpl;
 import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty.E;
 import org.eclipse.e4.ui.model.application.MApplication;
 import org.eclipse.e4.ui.model.application.MApplicationElement;
@@ -49,8 +44,8 @@
 import org.eclipse.e4.ui.model.fragment.MStringModelFragment;
 import org.eclipse.e4.ui.model.fragment.impl.FragmentPackageImpl;
 import org.eclipse.e4.ui.model.fragment.impl.StringModelFragmentImpl;
-import org.eclipse.e4.ui.model.internal.ModelUtils;
 import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.common.util.TreeIterator;
 import org.eclipse.emf.databinding.EMFDataBindingContext;
 import org.eclipse.emf.databinding.EMFProperties;
 import org.eclipse.emf.databinding.FeaturePath;
@@ -58,6 +53,8 @@
 import org.eclipse.emf.databinding.edit.EMFEditProperties;
 import org.eclipse.emf.ecore.EClass;
 import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
 import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.emf.edit.command.AddCommand;
 import org.eclipse.jface.action.Action;
@@ -82,17 +79,10 @@
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.ServiceReference;
 
 public class StringModelFragment extends AbstractComponentEditor {
 	private Composite composite;
 	private EMFDataBindingContext context;
-	// The local collector used by both dialogs (findParentReference and
-	// FeaturesSelection)
-	private ClassContributionCollector collector;
 	// The selected Container is the class that match the ID.
 	// It can be get from the FindParentReferenceDialog or computed from the ID.
 	private EClass selectedContainer;
@@ -102,13 +92,15 @@
 
 	private final List<Action> actions = new ArrayList<>();
 
+	// The resource set where to search for element with a specific ID.
+	private ResourceSet resourceSet = null;
+
 	@Inject
 	IEclipseContext eclipseContext;
 
 	@Inject
 	public StringModelFragment() {
 		super();
-		collector = getCollector();
 	}
 
 	@PostConstruct
@@ -189,25 +181,53 @@
 			return selectedContainer;
 		}
 
-		// we get the StringModelFragment
+		// we get the StringModelFragment. If no ID, no search...
 		StringModelFragmentImpl modelFragment = getStringModelFragment();
+		String parentElementId = modelFragment.getParentElementId();
+		if ((parentElementId == null) || (parentElementId.isEmpty())) {
+			return null;
+		}
 
-		Filter filter = new Filter(ApplicationPackageImpl.eINSTANCE.getApplication(), ""); //$NON-NLS-1$
-		WritableList list = new WritableList<>();
-		ModelResultHandler resultHandler = new ModelResultHandlerImpl(list, filter, this,
-				modelFragment.eResource());
+		if ("xpath:/".equals(parentElementId) || "org.eclipse.e4.legacy.ide.application".equals(parentElementId)) {
+			return ApplicationPackageImpl.eINSTANCE.getApplication();
+		}
 
-		collector.findModelElements(filter, resultHandler);
-		List<EClass> globalResult = new ArrayList<>();
+		// We have to proceed to a simple search on all elements in all resource
+		// set...
+		// Must load the models and check for ids.
+		if (resourceSet == null) {
+			resourceSet = Util.getModelElementResources();
+		}
 
-		for (Object o : list) {
-			if (o instanceof MApplication) {
-				MApplication mApp = (MApplication) o;
-				List<EClass> targetClass = getTargetClass(mApp);
-				globalResult.addAll(targetClass);
+		String xpath = parentElementId.startsWith("xpath:") ? parentElementId.substring(6) : null;
+
+		for (final Resource res : resourceSet.getResources()) {
+			final TreeIterator<EObject> it = EcoreUtil.getAllContents(res, true);
+			while (it.hasNext()) {
+				final EObject o = it.next();
+				// We found this element, if this is an application element not
+				// contained in model fragement imports
+				// and having the same ID. We return the first found.
+
+				// Deal with non default MApplication IDs.
+				if (xpath != null) {
+					if (o instanceof MApplication) {
+						EClass found = getTargetClassFromXPath((MApplication) o, xpath);
+						if (found != null) {
+							return found;
+						}
+					}
+				} else {
+					// This is a standard search with ID.
+					if ((o instanceof MApplicationElement)
+							&& (o.eContainingFeature() != FragmentPackageImpl.Literals.MODEL_FRAGMENTS__IMPORTS)
+							&& parentElementId.equals(((MApplicationElement) o).getElementId())) {
+						selectedContainer = o.eClass();
+						return selectedContainer;
+					}
+				}
 			}
 		}
-		selectedContainer = globalResult.isEmpty() ? null : globalResult.get(0);
 
 		return selectedContainer;
 	}
@@ -216,16 +236,6 @@
 		return ((StringModelFragmentImpl) getMaster().getValue());
 	}
 
-	private ClassContributionCollector getCollector() {
-		final Bundle bundle = FrameworkUtil.getBundle(FindParentReferenceElementDialog.class);
-		final BundleContext context = bundle.getBundleContext();
-		final ServiceReference<?> ref = context.getServiceReference(ClassContributionCollector.class.getName());
-		if (ref != null) {
-			return (ClassContributionCollector) context.getService(ref);
-		}
-		return null;
-	}
-
 	private Composite createForm(Composite parent) {
 		final CTabFolder folder = new CTabFolder(parent, SWT.BOTTOM);
 
@@ -261,10 +271,11 @@
 			// t.setEditable(false);
 			gd = new GridData(GridData.FILL_HORIZONTAL);
 			t.setLayoutData(gd);
-			context.bindValue(
-					textProp.observeDelayed(200, t),
-					EMFEditProperties.value(getEditingDomain(),
-							FragmentPackageImpl.Literals.STRING_MODEL_FRAGMENT__PARENT_ELEMENT_ID).observeDetail(getMaster()));
+			context.bindValue(textProp.observeDelayed(200, t),
+					EMFEditProperties
+					.value(getEditingDomain(),
+							FragmentPackageImpl.Literals.STRING_MODEL_FRAGMENT__PARENT_ELEMENT_ID)
+					.observeDetail(getMaster()));
 
 			// Add a modify listener to control the change of the ID -> Must
 			// force the computation of selectedContainer.
@@ -282,7 +293,7 @@
 				public void widgetSelected(SelectionEvent e) {
 					final FindParentReferenceElementDialog dialog = new FindParentReferenceElementDialog(b.getShell(),
 							StringModelFragment.this, (MStringModelFragment) getMaster().getValue(), Messages,
-							collector);
+							getSelectedContainer());
 					dialog.open();
 					selectedContainer = dialog.getSelectedContainer();
 				}
@@ -309,10 +320,10 @@
 			TextPasteHandler.createFor(t);
 			gd = new GridData(GridData.FILL_HORIZONTAL);
 			t.setLayoutData(gd);
-			context.bindValue(
-					textProp.observeDelayed(200, t),
-					EMFEditProperties.value(getEditingDomain(),
-							FragmentPackageImpl.Literals.STRING_MODEL_FRAGMENT__FEATURENAME).observeDetail(getMaster()));
+			context.bindValue(textProp.observeDelayed(200, t),
+					EMFEditProperties
+					.value(getEditingDomain(), FragmentPackageImpl.Literals.STRING_MODEL_FRAGMENT__FEATURENAME)
+					.observeDetail(getMaster()));
 
 			final Button button = new Button(comp, SWT.PUSH | SWT.FLAT);
 			button.setText(Messages.ModelTooling_Common_FindEllipsis);
@@ -435,47 +446,6 @@
 		return l;
 	}
 
-	/**
-	 * This method returns the target class of the element pointed by the
-	 * 'extended element ID' value In case of several matches it returns a list
-	 * of possible classes (but this case means that the ID is used for
-	 * different objects in different models present in the workspace). If the
-	 * parent element ID value is xpath:/ or
-	 * 'org.eclipse.e4.legacy.ide.application', it returns MApplication EClass
-	 *
-	 * @param application
-	 *            : the application to be parsed
-	 * @return the list of EClass
-	 */
-	public List<EClass> getTargetClass(MApplication application) {
-		List<EClass> ret = Collections.emptyList();
-		StringModelFragmentImpl modelFragment = getStringModelFragment();
-
-
-		String idsOrXPath = modelFragment.getParentElementId();
-		if ("xpath:/".equals(idsOrXPath) || "org.eclipse.e4.legacy.ide.application".equals(idsOrXPath)) {
-			ret = new ArrayList<>();
-			ret.add(ApplicationPackageImpl.eINSTANCE.getApplication());
-		} else {
-			// Deal with non default MApplication IDs.
-			if (idsOrXPath.startsWith("xpath:")) {
-				String xPath = idsOrXPath.substring(6);
-				ret = getTargetClassFromXPath(application, xPath);
-			} else {
-
-				MApplicationElement o = ModelUtils.findElementById(application, idsOrXPath);
-				if (o != null) {
-					ret = new ArrayList<>();
-					ret.add(((EObject) o).eClass());
-				}
-			}
-		}
-
-		return ret;
-
-	}
-
-
 
 	/**
 	 * Returns the EClass of the Application element(s) referenced by the xpath
@@ -487,8 +457,7 @@
 	 *            : the xpath value without the 'xpath:' prefix
 	 * @return the list of EClass(es) matching this xpath
 	 */
-	private List<EClass> getTargetClassFromXPath(MApplication application, String xpath) {
-		List<EClass> ret = new ArrayList<>();
+	private EClass getTargetClassFromXPath(MApplication application, String xpath) {
 
 		XPathContextFactory<EObject> f = EcoreXPathContextFactory.newInstance();
 		XPathContext xpathContext = f.newContext((EObject) application);
@@ -499,7 +468,7 @@
 				Object obj = i.next();
 				if (obj instanceof MApplicationElement) {
 					ApplicationElementImpl ae = (ApplicationElementImpl) obj;
-					ret.add(ae.eClass());
+					return ae.eClass();
 				}
 			}
 		} catch (Exception ex) {
@@ -507,9 +476,7 @@
 			ex.printStackTrace();
 		}
 
-		return ret;
+		return null;
 	}
 
-
-
 }
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/FindImportElementDialog.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/FindImportElementDialog.java
index bf62e9f..415c26a 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/FindImportElementDialog.java
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/FindImportElementDialog.java
@@ -11,10 +11,8 @@
  ******************************************************************************/
 package org.eclipse.e4.tools.emf.ui.internal.common.component.dialogs;
 
-import org.eclipse.core.databinding.observable.list.IObservableList;
 import org.eclipse.core.databinding.observable.list.WritableList;
 import org.eclipse.e4.tools.emf.ui.common.IModelElementProvider.Filter;
-import org.eclipse.e4.tools.emf.ui.common.IModelElementProvider.ModelResultHandler;
 import org.eclipse.e4.tools.emf.ui.common.component.AbstractComponentEditor;
 import org.eclipse.e4.tools.emf.ui.internal.Messages;
 import org.eclipse.e4.tools.emf.ui.internal.common.ClassContributionCollector;
@@ -22,7 +20,6 @@
 import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
 import org.eclipse.emf.common.command.Command;
 import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.edit.command.SetCommand;
 import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
 import org.eclipse.jface.viewers.DoubleClickEvent;
@@ -142,7 +139,7 @@
 			@Override
 			public void modifyText(ModifyEvent e) {
 				if (currentResultHandler != null) {
-					currentResultHandler.cancled = true;
+					currentResultHandler.cancel();
 				}
 				list.clear();
 				final Filter filter = new Filter(element.eClass(), searchText.getText());
@@ -192,48 +189,4 @@
 		return null;
 	}
 
-	private static class ModelResultHandlerImpl implements ModelResultHandler {
-		private boolean cancled = false;
-		private final IObservableList list;
-		private final Filter filter;
-		private final AbstractComponentEditor editor;
-		private final Resource resource;
-
-		public ModelResultHandlerImpl(IObservableList list, Filter filter, AbstractComponentEditor editor,
-				Resource resource) {
-			this.list = list;
-			this.filter = filter;
-			this.editor = editor;
-			this.resource = resource;
-		}
-
-		@Override
-		public void result(EObject data) {
-			if (!cancled) {
-				if (!resource.getURI().equals(data.eResource().getURI())) {
-					if (data instanceof MApplicationElement) {
-						final String elementId = ((MApplicationElement) data).getElementId();
-						if (elementId == null) {
-							list.add(data);
-							return;
-						}
-
-						if (elementId.trim().length() > 0) {
-							if (filter.elementIdPattern.matcher(elementId).matches()) {
-								list.add(data);
-								return;
-							}
-						}
-
-						final String label = editor.getDetailLabel(data);
-						if (label != null && label.trim().length() > 0) {
-							if (filter.elementIdPattern.matcher(label).matches()) {
-								list.add(data);
-							}
-						}
-					}
-				}
-			}
-		}
-	}
 }
\ No newline at end of file
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/FindParentReferenceElementDialog.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/FindParentReferenceElementDialog.java
index 4f1d65a..6feecdf 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/FindParentReferenceElementDialog.java
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/FindParentReferenceElementDialog.java
@@ -25,6 +25,7 @@
 import org.eclipse.e4.tools.emf.ui.internal.Messages;
 import org.eclipse.e4.tools.emf.ui.internal.common.ClassContributionCollector;
 import org.eclipse.e4.ui.model.application.MApplicationElement;
+import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
 import org.eclipse.e4.ui.model.fragment.MStringModelFragment;
 import org.eclipse.e4.ui.model.fragment.impl.FragmentPackageImpl;
 import org.eclipse.emf.common.command.Command;
@@ -67,6 +68,10 @@
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
 
 public class FindParentReferenceElementDialog extends TitleAreaDialog {
 	private final MStringModelFragment fragment;
@@ -81,12 +86,24 @@
 	private ClassContributionCollector collector;
 
 	public FindParentReferenceElementDialog(Shell parentShell, AbstractComponentEditor editor,
-			MStringModelFragment fragment, Messages Messages, ClassContributionCollector collector) {
+			MStringModelFragment fragment, Messages Messages,
+			EClass previousSelection) {
 		super(parentShell);
 		this.fragment = fragment;
 		this.editor = editor;
 		this.Messages = Messages;
-		this.collector = collector;
+		this.collector = getCollector();
+		this.selectedContainer = previousSelection;
+	}
+
+	private ClassContributionCollector getCollector() {
+		final Bundle bundle = FrameworkUtil.getBundle(FindParentReferenceElementDialog.class);
+		final BundleContext context = bundle.getBundleContext();
+		final ServiceReference<?> ref = context.getServiceReference(ClassContributionCollector.class.getName());
+		if (ref != null) {
+			return (ClassContributionCollector) context.getService(ref);
+		}
+		return null;
 	}
 
 	@Override
@@ -147,7 +164,6 @@
 			}
 		});
 		eClassViewer.setInput(eClassList);
-		eClassViewer.getCombo().select(0);
 		eClassViewer.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 		eClassViewer.addSelectionChangedListener(new ISelectionChangedListener() {
 
@@ -244,6 +260,16 @@
 				collector.clearModelCache();
 			}
 		});
+
+		// Manage the current selection on comboviewer (previous or first one).
+		if (selectedContainer != null) {
+			eClassViewer.setSelection(new StructuredSelection(selectedContainer));
+		} else {
+			// Select addon by default (not necessary in the first position in
+			// the eclasslist...)
+			eClassViewer.setSelection(new StructuredSelection(ApplicationPackageImpl.Literals.ADDON));
+		}
+
 		return comp;
 	}