Index: trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/dialogs/FindReferencesDialog.java
===================================================================
--- trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/dialogs/FindReferencesDialog.java	(revision 1638)
+++ trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/dialogs/FindReferencesDialog.java	(revision 1638)
@@ -0,0 +1,163 @@
+package org.openmodelica.modelicaml.relations.dialogs;
+
+import java.util.HashSet;
+import java.util.List;
+
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.MouseAdapter;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Tree;
+import org.eclipse.swt.widgets.TreeItem;
+import org.eclipse.uml2.uml.Element;
+import org.eclipse.uml2.uml.Generalization;
+import org.eclipse.uml2.uml.NamedElement;
+import org.openmodelica.modelicaml.common.services.EditorServices;
+import org.openmodelica.modelicaml.common.services.ModelicaMLServices;
+import org.openmodelica.modelicaml.common.utls.ResourceManager;
+
+public class FindReferencesDialog extends Dialog {
+	private Tree tree;
+	private HashSet<Element> foundElements;
+	private NamedElement elementToSearchFor;
+	private HashSet<TreeItem> allTreeItems = new HashSet<TreeItem>();
+
+
+	public FindReferencesDialog(Shell parentShell, HashSet<Element> foundElements) {
+		
+		super(parentShell);
+		setShellStyle(SWT.SHELL_TRIM);
+		this.foundElements = foundElements;
+
+	}
+
+	
+	@Override
+	protected void configureShell(Shell newShell) {
+		super.configureShell(newShell);
+		newShell.setImage(ResourceManager.getPluginImage("org.openmodelica.modelicaml.modelexplorer","icons/search_references.gif"));
+		newShell.setText("References for "+ModelicaMLServices.getName(getElementToSearchFor())+" (" + foundElements.size() + " found)");			
+	}
+	
+	@Override
+	protected Control createDialogArea(Composite parent) {
+		
+		Composite area = (Composite) super.createDialogArea(parent);
+		Composite container = new Composite(area, SWT.NONE);
+		container.setLayout(new GridLayout(1, false));
+		container.setLayoutData(new GridData(GridData.FILL_BOTH));
+		
+		Group grpFoundItems = new Group(container, SWT.NONE);
+		grpFoundItems.setText("Found items");
+		grpFoundItems.setLayout(new GridLayout(1, false));
+		grpFoundItems.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+		
+		tree = new Tree(grpFoundItems, SWT.BORDER | SWT.V_SCROLL);
+		tree.addMouseListener(new MouseAdapter() {
+			@Override
+			public void mouseDoubleClick(MouseEvent e) {
+				TreeItem[] selectedItems = tree.getSelection();
+				for (TreeItem selectedItem : selectedItems) {
+					EditorServices.locateInModelExplorer(selectedItem.getData(), false);
+				}
+			}
+		});
+		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+		createTree(tree);
+		
+		
+		Composite composite = new Composite(grpFoundItems, SWT.NONE);
+		composite.setLayout(new GridLayout(2, false));
+		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+		
+		Button btnLocate = new Button(composite, SWT.NONE);
+		btnLocate.addSelectionListener(new SelectionAdapter() {
+			@Override
+			public void widgetSelected(SelectionEvent e) {
+				TreeItem[] selectedItems = tree.getSelection();
+				for (TreeItem selectedItem : selectedItems) {
+					EditorServices.locateInModelExplorer(selectedItem.getData(), false);
+				}
+			}
+		});
+		btnLocate.setText("Locate");
+		btnLocate.setImage(ResourceManager.getPluginImage("org.openmodelica.modelicaml.relations","icons/editor/ModelExplorer.png"));
+		
+		Composite composite_1 = new Composite(composite, SWT.NONE);
+		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
+		gd_composite_1.heightHint = 9;
+		composite_1.setLayoutData(gd_composite_1);
+
+		return area;
+	}
+
+	public void createTree(Tree tree){
+		
+		allTreeItems.clear();
+		
+//		List<Element> elementsSorted = ModelicaMLServices.getSortedByEClassName(foundElements); // is far too slow
+		List<Element> elementsSorted = ModelicaMLServices.getSortedByName(foundElements);
+		if (elementsSorted != null) {
+			for (Element element : elementsSorted) {
+				
+				TreeItem treeItem = new TreeItem(tree, SWT.NONE);
+				treeItem.setData(element);
+				allTreeItems.add(treeItem);
+				
+				if (element instanceof Generalization) {
+					treeItem.setText(ModelicaMLServices.getQualifiedName(element.getOwner()));
+				}
+				else {
+					treeItem.setText(ModelicaMLServices.getName(element) + "          (in "+ModelicaMLServices.getQualifiedName(element.getOwner())+")");	
+				}
+
+				// set image
+				treeItem.setImage(ModelicaMLServices.getImage(element));
+				
+			}
+		}
+	}
+	
+	
+	/**
+	 * Create contents of the button bar.
+	 * 
+	 * @param parent
+	 */
+	@Override
+	protected void createButtonsForButtonBar(Composite parent) {
+//		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
+//				true);
+//		createButton(parent, IDialogConstants.CANCEL_ID,
+//				IDialogConstants.CANCEL_LABEL, false);
+	}
+
+	/**
+	 * Return the initial size of the dialog.
+	 */
+	@Override
+	protected Point getInitialSize() {
+		return new Point(494, 413);
+	}
+
+
+	public NamedElement getElementToSearchFor() {
+		return elementToSearchFor;
+	}
+
+
+	public void setElementToSearchFor(NamedElement elementToSearchFor) {
+		this.elementToSearchFor = elementToSearchFor;
+	}
+}
+
Index: trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/dialogs/SelectInvalidRelationsToDeleteDialog.java
===================================================================
--- trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/dialogs/SelectInvalidRelationsToDeleteDialog.java	(revision 1634)
+++ trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/dialogs/SelectInvalidRelationsToDeleteDialog.java	(revision 1638)
@@ -212,6 +212,6 @@
 		allTreeItems.clear();
 		
-//		List<Element> relations = ModelicaMLServices.getSortedByName(invalidRelations); 
-		List<Element> relations = ModelicaMLServices.getSortedByEClassName(invalidRelations);
+		List<Element> relations = ModelicaMLServices.getSortedByName(invalidRelations); 
+//		List<Element> relations = ModelicaMLServices.getSortedByEClassName(invalidRelations); // is far too slow! 
 		if (relations != null) {
 			for (Element relation : relations) {
Index: trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/handlers/FindInvalidRelationsToolbarHandler.java
===================================================================
--- trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/handlers/FindInvalidRelationsToolbarHandler.java	(revision 1634)
+++ trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/handlers/FindInvalidRelationsToolbarHandler.java	(revision 1638)
@@ -31,4 +31,7 @@
 	public Object execute(ExecutionEvent event) throws ExecutionException {
 
+		// reset 
+		collector = null;
+		
 		// get UML model data
 		UmlModel umlModel = UmlUtils.getUmlModel();
Index: trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/handlers/FindReferencesPopupHandler.java
===================================================================
--- trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/handlers/FindReferencesPopupHandler.java	(revision 1638)
+++ trunk/modelicaml/org.openmodelica.modelicaml.relations/src/org/openmodelica/modelicaml/relations/handlers/FindReferencesPopupHandler.java	(revision 1638)
@@ -0,0 +1,161 @@
+package org.openmodelica.modelicaml.relations.handlers;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.core.resource.NotFoundException;
+import org.eclipse.papyrus.infra.core.resource.uml.UmlModel;
+import org.eclipse.papyrus.infra.core.resource.uml.UmlUtils;
+import org.eclipse.papyrus.infra.core.utils.BusinessModelResolver;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.uml2.uml.Element;
+import org.eclipse.uml2.uml.NamedElement;
+import org.openmodelica.modelicaml.common.datacollection.ReferenceElementsCollector;
+import org.openmodelica.modelicaml.common.services.ModelicaMLServices;
+import org.openmodelica.modelicaml.relations.dialogs.FindReferencesDialog;
+
+
+public class FindReferencesPopupHandler extends AbstractHandler{
+	
+	private ReferenceElementsCollector collector;	
+	private NamedElement selectedElement;
+	 
+	@Override
+	public Object execute(ExecutionEvent event) throws ExecutionException {
+		
+		// reset 
+		collector = null;
+		selectedElement = null;
+		
+		EObject eObject = (EObject) adaptSelectedElement(getCurrentSelections().get(0));
+		
+		if (eObject != null) {
+			if (eObject instanceof NamedElement) {
+				selectedElement = (NamedElement) eObject;
+			}
+		}
+		
+		if (selectedElement == null) {
+			MessageDialog.openError(ModelicaMLServices.getShell(), "ModelicaML Reference Search", "It is not possible to search for this element.");
+			return null;
+		}
+		
+		// get UML model data
+		UmlModel umlModel = UmlUtils.getUmlModel();
+
+		if (umlModel != null && selectedElement != null) {
+			try {
+				final EObject root = umlModel.lookupRoot();
+				if (root instanceof Element) {
+					
+					ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(ModelicaMLServices.getShell());
+					
+					try {
+						progressMonitor.run(false, true, new IRunnableWithProgress() {
+							
+							@Override
+							public void run(IProgressMonitor monitor) throws InvocationTargetException,
+									InterruptedException {
+
+								monitor.beginTask("Searching for "+ModelicaMLServices.getName(selectedElement)+" ...", 100);
+								
+								// collect data
+								collector = new ReferenceElementsCollector(monitor);
+								collector.setElemenToSearchFor(selectedElement);
+								
+								collector.collectElementsFromModel(root);
+								
+								monitor.done();
+							}
+						});
+					} catch (InvocationTargetException e) {
+						e.printStackTrace();
+						MessageDialog.openError(ModelicaMLServices.getShell(), "ModelicaML Reference Search Error", "Could not invoce the data collection for the references search. ");
+					} catch (InterruptedException e) {
+						e.printStackTrace();
+						MessageDialog.openInformation(ModelicaMLServices.getShell(), "ModelicaML Collection", "Data collection was interrupted.");		}
+
+				
+					if (collector != null) {
+						
+						if (collector.getElements() != null && collector.getElements().size() > 0) {
+							// open dialog
+							FindReferencesDialog dialog = new FindReferencesDialog(ModelicaMLServices.getShell(), collector.getElements());
+							dialog.setElementToSearchFor((NamedElement) collector.getElemenToSearchFor());
+							dialog.open();
+						}
+						else {
+							MessageDialog.openInformation(ModelicaMLServices.getShell(), "References Search", "No references were found for "+ ModelicaMLServices.getQualifiedName(selectedElement));
+						}
+					}
+				}
+			} catch (NotFoundException e) {
+				MessageDialog.openError(ModelicaMLServices.getShell(), "ModelicaML Model Access", "Could not access the ModelicaML model. Please open it in editor.");
+			}
+		}
+		else {
+			MessageDialog.openError(ModelicaMLServices.getShell(), "ModelicaML Model Access", "Could not access the ModelicaML model. Please open it in editor.");
+		}
+		
+		return null;
+	}
+	
+	
+	
+	@SuppressWarnings("unchecked")
+	protected List<Object> getSelectedElements(ExecutionEvent event) throws ExecutionException {
+		
+		ISelection selection = getSelection(event);
+		if (!( selection instanceof IStructuredSelection))
+			return Collections.emptyList();
+		return (List<Object>)((IStructuredSelection) selection).toList();
+	}
+	
+	protected ISelection getSelection(ExecutionEvent event) throws ExecutionException {
+		return HandlerUtil.getCurrentSelectionChecked(event);
+	}
+
+	@SuppressWarnings("unchecked")
+	private List<Object> getCurrentSelections() {
+		ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
+		if(selection instanceof IStructuredSelection) {
+			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
+			return structuredSelection.toList();
+		}
+	
+		return null;
+	}
+
+	protected EObject adaptSelectedElement( Object selection) {
+
+		EObject eObject = null;
+	
+		if(selection != null) {
+	
+			if(selection instanceof IAdaptable) {
+				selection = ((IAdaptable)selection).getAdapter(EObject.class);
+			}
+	
+			Object businessObject = BusinessModelResolver.getInstance().getBusinessModel(selection);
+			if(businessObject instanceof EObject) {
+				eObject = (EObject)businessObject;
+			}
+		}
+		return eObject;
+	}
+
+
+}
