Bug 509448 - The Feature Selection Dialog in fragment editor should be 

..opened on the right Class

With this change, the feature selection editor selects the right class
even when using 'xpath:/'

Change-Id: I6de6c57f61d79c89d496d2dbe1205f9c796ce46f
Signed-off-by: Patrik Suzzi <psuzzi@gmail.com>
Signed-off-by: Olivier Prouvoust <olivier.prouvost@opcoach.com>
Signed-off-by: Olivier Prouvost <olivier.prouvost@opcoach.com>
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF
index 373de63..f85a96c 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF
@@ -36,7 +36,8 @@
  org.eclipse.jdt.core;resolution:=optional,
  org.eclipse.pde.core;resolution:=optional,
  org.eclipse.e4.core.commands;bundle-version="0.10.0",
- org.eclipse.e4.ui.dialogs;bundle-version="1.0.0"
+ org.eclipse.e4.ui.dialogs;bundle-version="1.0.0",
+ org.eclipse.e4.emf.xpath
 Bundle-ActivationPolicy: lazy
 Import-Package: javax.annotation,
  javax.inject;version="1.0.0",
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java
index c874572..797b46c 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java
@@ -605,6 +605,7 @@
 	public String FindParentReferenceElementDialog_Message;
 	public String FindParentReferenceElementDialog_ContainerType;
 	public String FindParentReferenceElementDialog_Search;
+	public String FindParentReferenceElementDialog_HelpTooltip;
 	public String FindParentReferenceElementDialog_NoId;
 	public String FindParentReferenceElementDialog_ClearCache;
 	public String FindParentReferenceElementDialog_NoReferenceId;
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties
index e728f83..417d616 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties
@@ -609,7 +609,8 @@
 
 FindParentReferenceElementDialog_ShellTitle=Select Extended Element
 FindParentReferenceElementDialog_Title=Select Extended Element
-FindParentReferenceElementDialog_Message=Search for the element to be extended by the model fragment.
+FindParentReferenceElementDialog_Message=Search for the element to be contributed by the model fragment
+FindParentReferenceElementDialog_HelpTooltip=Elements of the selected class will be searched in all e4xmi files found in:\n* model fragment extensions\n* applicationXMI parameters of products extension.\n\nIf you need to find a 'live' ID, please use the Model Spy (Alt Shift F9) to get it from your running application.
 FindParentReferenceElementDialog_ContainerType=Container-Type
 FindParentReferenceElementDialog_Search=Search
 FindParentReferenceElementDialog_ClearCache=Clear Cache
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ClassContributionCollector.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ClassContributionCollector.java
index 801ffa5..dc4d1f2 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ClassContributionCollector.java
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ClassContributionCollector.java
@@ -11,10 +11,11 @@
 package org.eclipse.e4.tools.emf.ui.internal.common;
 
 import java.util.concurrent.CopyOnWriteArrayList;
+
 import org.eclipse.e4.tools.emf.ui.common.IClassContributionProvider;
+import org.eclipse.e4.tools.emf.ui.common.IModelElementProvider;
 import org.eclipse.e4.tools.emf.ui.common.IClassContributionProvider.ContributionResultHandler;
 import org.eclipse.e4.tools.emf.ui.common.IClassContributionProvider.Filter;
-import org.eclipse.e4.tools.emf.ui.common.IModelElementProvider;
 import org.eclipse.e4.tools.emf.ui.common.IModelElementProvider.ModelResultHandler;
 
 public class ClassContributionCollector {
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 104f807..5d6dc29 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
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2015 BestSolution.at and others.
+ * Copyright (c) 2010, 2016 BestSolution.at and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,33 +9,47 @@
  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
  * Steven Spungin <steve@spungin.tv> - Ongoing Maintenance, Bug 439532, Bug 443945
  * Patrik Suzzi <psuzzi@gmail.com> - Bug 467262
+ * Olivier Prouvost <olivier.prouvost@opcoach.com> - Bug 509488
  ******************************************************************************/
 package org.eclipse.e4.tools.emf.ui.internal.common.component;
 
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Iterator;
 import java.util.List;
 
 import javax.annotation.PostConstruct;
 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;
+import org.eclipse.e4.ui.model.application.impl.ApplicationElementImpl;
 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.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.databinding.EMFDataBindingContext;
 import org.eclipse.emf.databinding.EMFProperties;
@@ -54,10 +68,11 @@
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.jface.viewers.ViewerComparator;
-import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.CTabFolder;
 import org.eclipse.swt.custom.CTabItem;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.graphics.Image;
@@ -67,24 +82,33 @@
 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;
 
 	private final IListProperty MODEL_FRAGMENT__ELEMENTS = EMFProperties
 			.list(FragmentPackageImpl.Literals.MODEL_FRAGMENT__ELEMENTS);
 
 	private final List<Action> actions = new ArrayList<>();
 
-	private EClass selectedContainer;
-
 	@Inject
 	IEclipseContext eclipseContext;
 
 	@Inject
 	public StringModelFragment() {
 		super();
+		collector = getCollector();
 	}
 
 	@PostConstruct
@@ -152,6 +176,56 @@
 		return composite;
 	}
 
+	/**
+	 * Returns the selectedContainer, which is the EClass behind the Extended
+	 * Element ID. It can be known thanks to the dialog or must be computed from
+	 * the ID value
+	 *
+	 * @return
+	 */
+	private EClass getSelectedContainer() {
+
+		if (selectedContainer != null) {
+			return selectedContainer;
+		}
+
+		// we get the StringModelFragment
+		StringModelFragmentImpl modelFragment = getStringModelFragment();
+
+		Filter filter = new Filter(ApplicationPackageImpl.eINSTANCE.getApplication(), ""); //$NON-NLS-1$
+		WritableList list = new WritableList<>();
+		ModelResultHandler resultHandler = new ModelResultHandlerImpl(list, filter, this,
+				modelFragment.eResource());
+
+		collector.findModelElements(filter, resultHandler);
+		List<EClass> globalResult = new ArrayList<>();
+
+		for (Object o : list) {
+			if (o instanceof MApplication) {
+				MApplication mApp = (MApplication) o;
+				List<EClass> targetClass = getTargetClass(mApp);
+				globalResult.addAll(targetClass);
+			}
+		}
+		selectedContainer = globalResult.isEmpty() ? null : globalResult.get(0);
+
+		return selectedContainer;
+	}
+
+	private StringModelFragmentImpl getStringModelFragment() {
+		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);
 
@@ -192,20 +266,25 @@
 					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.
+			t.addModifyListener(new ModifyListener() {
+				@Override
+				public void modifyText(ModifyEvent e) {
+					selectedContainer = null;
+				}
+			});
+
 			final Button b = new Button(comp, SWT.PUSH | SWT.FLAT);
 			b.setText(Messages.ModelTooling_Common_FindEllipsis);
 			b.addSelectionListener(new SelectionAdapter() {
 				@Override
 				public void widgetSelected(SelectionEvent e) {
 					final FindParentReferenceElementDialog dialog = new FindParentReferenceElementDialog(b.getShell(),
-							StringModelFragment.this, (MStringModelFragment) getMaster().getValue(), Messages);
+							StringModelFragment.this, (MStringModelFragment) getMaster().getValue(), Messages,
+							collector);
 					dialog.open();
-					// store user selected container
-					if (dialog.getReturnCode() == Window.OK) {
-						selectedContainer = dialog.getSelectedContainer();
-					} else {
-						selectedContainer = null;
-					}
+					selectedContainer = dialog.getSelectedContainer();
 				}
 			});
 		}
@@ -242,7 +321,7 @@
 				public void widgetSelected(SelectionEvent e) {
 					final FeatureSelectionDialog dialog = new FeatureSelectionDialog(button.getShell(),
 							getEditingDomain(), (MStringModelFragment) getMaster().getValue(), Messages,
-							selectedContainer);
+							getSelectedContainer());
 					dialog.open();
 				}
 			});
@@ -356,4 +435,81 @@
 		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
+	 * value (without prefix)
+	 *
+	 * @param application
+	 *            : the application to be parsed
+	 * @param xpath
+	 *            : 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<>();
+
+		XPathContextFactory<EObject> f = EcoreXPathContextFactory.newInstance();
+		XPathContext xpathContext = f.newContext((EObject) application);
+		Iterator<Object> i = xpathContext.iterate(xpath);
+
+		try {
+			while (i.hasNext()) {
+				Object obj = i.next();
+				if (obj instanceof MApplicationElement) {
+					ApplicationElementImpl ae = (ApplicationElementImpl) obj;
+					ret.add(ae.eClass());
+				}
+			}
+		} catch (Exception ex) {
+			// custom xpath functions will throw exceptions
+			ex.printStackTrace();
+		}
+
+		return ret;
+	}
+
+
+
 }
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 34bd76c..4f1d65a 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
@@ -9,6 +9,7 @@
  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
  * Steven Spungin <steven@spungin.tv> - Bug 437469
  * Patrik Suzzi <psuzzi@gmail.com> - Bug 467262
+ * Olivier Prouvost <olivier.prouvost@opcoach.com> - Bug 509488
  ******************************************************************************/
 package org.eclipse.e4.tools.emf.ui.internal.common.component.dialogs;
 
@@ -16,10 +17,8 @@
 import java.util.Arrays;
 import java.util.List;
 
-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.Util;
 import org.eclipse.e4.tools.emf.ui.common.Util.InternalPackage;
 import org.eclipse.e4.tools.emf.ui.common.component.AbstractComponentEditor;
@@ -31,7 +30,6 @@
 import org.eclipse.emf.common.command.Command;
 import org.eclipse.emf.ecore.EClass;
 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.dialogs.TitleAreaDialog;
@@ -69,10 +67,6 @@
 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;
@@ -84,13 +78,15 @@
 	private ComboViewer eClassViewer;
 	private Text searchText;
 	private EClass selectedContainer;
+	private ClassContributionCollector collector;
 
 	public FindParentReferenceElementDialog(Shell parentShell, AbstractComponentEditor editor,
-			MStringModelFragment fragment, Messages Messages) {
+			MStringModelFragment fragment, Messages Messages, ClassContributionCollector collector) {
 		super(parentShell);
 		this.fragment = fragment;
 		this.editor = editor;
 		this.Messages = Messages;
+		this.collector = collector;
 	}
 
 	@Override
@@ -124,8 +120,13 @@
 		Label l = new Label(container, SWT.NONE);
 		l.setText(Messages.FindParentReferenceElementDialog_ContainerType);
 
-		final Combo combo = new Combo(container, SWT.NONE);
+		Composite parentForCombo = new Composite(container, SWT.NONE);
+		parentForCombo.setLayout(new GridLayout(2, false));
+		parentForCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
+
+		final Combo combo = new Combo(parentForCombo, SWT.NONE);
 		eClassViewer = new ComboViewer(combo);
+		combo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
 		eClassViewer.setLabelProvider(new LabelProvider() {
 			@Override
 			public String getText(Object element) {
@@ -172,6 +173,12 @@
 		};
 		new AutoCompleteField(combo, textContentAdapter, values);
 
+		Label help = new Label(parentForCombo, SWT.NONE);
+		final Image helpImage = new Image(parent.getDisplay(),
+				getClass().getClassLoader().getResourceAsStream("/icons/full/obj16/missing_image_placeholder.png")); //$NON-NLS-1$
+		help.setImage(helpImage);
+		help.setToolTipText(Messages.FindParentReferenceElementDialog_HelpTooltip);
+
 		l = new Label(container, SWT.NONE);
 		l.setText(Messages.FindParentReferenceElementDialog_Search);
 
@@ -220,8 +227,6 @@
 		list = new WritableList();
 		viewer.setInput(list);
 
-		final ClassContributionCollector collector = getCollector();
-
 		searchText.addModifyListener(new ModifyListener() {
 
 			@Override
@@ -244,7 +249,7 @@
 
 	protected void updateSearch() {
 		if (currentResultHandler != null) {
-			currentResultHandler.cancled = true;
+			currentResultHandler.cancel();
 		}
 		list.clear();
 
@@ -252,7 +257,7 @@
 				(EClass) ((IStructuredSelection) eClassViewer.getSelection()).getFirstElement(), searchText.getText());
 
 		currentResultHandler = new ModelResultHandlerImpl(list, filter, editor, ((EObject) fragment).eResource());
-		getCollector().findModelElements(filter, currentResultHandler);
+		collector.findModelElements(filter, currentResultHandler);
 
 	}
 
@@ -285,60 +290,6 @@
 		return selectedContainer;
 	}
 
-	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 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);
-								return;
-							}
-						}
-					}
-				}
-			}
-		}
-	}
 
 }
\ 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/ModelResultHandlerImpl.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/ModelResultHandlerImpl.java
new file mode 100644
index 0000000..d01facc
--- /dev/null
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/component/dialogs/ModelResultHandlerImpl.java
@@ -0,0 +1,59 @@
+package org.eclipse.e4.tools.emf.ui.internal.common.component.dialogs;
+
+import org.eclipse.core.databinding.observable.list.IObservableList;
+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.ui.model.application.MApplicationElement;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+
+public class ModelResultHandlerImpl implements ModelResultHandler {
+	private boolean canceled = 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;
+	}
+
+	public void cancel() {
+		canceled = true;
+	}
+
+	@Override
+	public void result(EObject data) {
+		if (!canceled) {
+			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);
+							return;
+						}
+					}
+				}
+			}
+		}
+	}
+}
\ No newline at end of file