Bug 422323 - [event spy] Improve the way of loading the selected classes
in the IDE

Signed-off-by: Daniel Rolka <daniel.rolka@pl.ibm.com>
diff --git a/bundles/org.eclipse.e4.tools.event.spy/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.tools.event.spy/META-INF/MANIFEST.MF
index 8879b35..ba02892 100644
--- a/bundles/org.eclipse.e4.tools.event.spy/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.e4.tools.event.spy/META-INF/MANIFEST.MF
@@ -16,9 +16,6 @@
  org.eclipse.core.resources;bundle-version="3.8.100",
  org.eclipse.jdt.core;bundle-version="3.9.0",
  org.eclipse.jdt.ui;bundle-version="3.9.0",
- org.eclipse.pde.core;bundle-version="3.9.0",
- org.eclipse.pde.ui;bundle-version="3.8.0",
- org.eclipse.pde.runtime;bundle-version="3.4.400",
  org.eclipse.osgi.services;bundle-version="3.3.100",
  org.eclipse.e4.ui.services;bundle-version="1.0.0",
  org.eclipse.jface.databinding,
diff --git a/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/ui/CapturedEventTree.java b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/ui/CapturedEventTree.java
index 85608f9..ca1a515 100644
--- a/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/ui/CapturedEventTree.java
+++ b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/ui/CapturedEventTree.java
@@ -22,7 +22,7 @@
 import org.eclipse.e4.tools.event.spy.model.CapturedEventTreeSelection;
 import org.eclipse.e4.tools.event.spy.model.IEventItem;
 import org.eclipse.e4.tools.event.spy.model.ItemToFilter;
-import org.eclipse.e4.tools.event.spy.util.PDEUtils;
+import org.eclipse.e4.tools.event.spy.util.JDTUtils;
 import org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider;
 import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
 import org.eclipse.jface.databinding.viewers.TreeStructureAdvisor;
@@ -182,7 +182,7 @@
 				if (selectedItemIndex > 0 /*we check the 2nd and 3rd column only*/ &&
 						item.getParentItem() == null /*we don't check parameters at this moment*/) {
 					String text = item.getText(selectedItemIndex);
-					if (PDEUtils.containsClassName(text)) {
+					if (JDTUtils.containsClassName(text)) {
 						selectedClassItem.setClassName(text);
 						selectedClassItem.setColumnIndex(selectedItemIndex);
 						selectedClassItem.setTreeItem(item);
diff --git a/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/ui/SpyDialog.java b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/ui/SpyDialog.java
index 4e60c03..594ecca 100644
--- a/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/ui/SpyDialog.java
+++ b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/ui/SpyDialog.java
@@ -22,7 +22,7 @@
 import org.eclipse.e4.tools.event.spy.model.CapturedEventTreeSelection;
 import org.eclipse.e4.tools.event.spy.model.SpyDialogMemento;
 import org.eclipse.e4.tools.event.spy.util.LoggerWrapper;
-import org.eclipse.e4.tools.event.spy.util.PDEUtils;
+import org.eclipse.e4.tools.event.spy.util.JDTUtils;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.swt.SWT;
@@ -222,7 +222,7 @@
 	@SuppressWarnings("restriction")
 	private void openResource(CapturedEventTreeSelection selection) {
 		try {
-			PDEUtils.openClass(selection.getSelection());
+			JDTUtils.openClass(selection.getSelection());
 		} catch(ClassNotFoundException exc) {
 			logger.warn(exc.getMessage());
 		}
diff --git a/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/util/JDTUtils.java b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/util/JDTUtils.java
new file mode 100644
index 0000000..9505553
--- /dev/null
+++ b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/util/JDTUtils.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.e4.tools.event.spy.util;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.ui.JavaUI;
+import org.osgi.framework.FrameworkUtil;
+
+public class JDTUtils {
+	private final static Pattern CLASS_NAME_PATTERN = Pattern.compile("(([a-zA-Z_]+[0-9]*\\.)+[a-zA-Z_]+[a-z0-9]*)");
+
+	public static boolean containsClassName(String name) {
+		return CLASS_NAME_PATTERN.matcher(name).find();
+	}
+
+	public static void openClass(String clsName) throws ClassNotFoundException {
+		Matcher matcher = CLASS_NAME_PATTERN.matcher(clsName);
+		if (!matcher.find()) {
+			return;
+		}
+		try {
+			Class<?> cls =FrameworkUtil.getBundle(JDTUtils.class).loadClass(matcher.group(1).trim());
+			IProject project = findProjectFor(cls);
+			
+			if (project != null) {
+				openInEditor(JavaCore.create(project), cls.getName());
+			}						
+		} catch(ClassNotFoundException exc) {
+			throw new ClassNotFoundException("Class not found in the bundle classpath: " + clsName);
+		}
+	}
+	
+	private static IProject findProjectFor(Class<?> cls) {
+		for (IProject project: ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
+			if (project.getFile(cls.getName()) != null) {
+				return project;
+			}
+		}
+		return null;
+	}
+	
+	private static void openInEditor(IJavaProject project, String clazz) throws ClassNotFoundException {
+		if (project == null) {
+			return;
+		}
+		try {
+			IType type = project.findType(clazz);
+			JavaUI.openInEditor(type, false, true);
+		} catch (Exception e) {
+			throw new ClassNotFoundException(e.getMessage());
+		}
+	}
+}
diff --git a/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/util/PDEUtils.java b/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/util/PDEUtils.java
deleted file mode 100644
index 30ea9a4..0000000
--- a/bundles/org.eclipse.e4.tools.event.spy/src/org/eclipse/e4/tools/event/spy/util/PDEUtils.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2013 IBM Corporation 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
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.e4.tools.event.spy.util;
-
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.jdt.core.IJavaProject;
-import org.eclipse.jdt.core.IType;
-import org.eclipse.jdt.core.JavaCore;
-import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.ui.JavaUI;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.osgi.util.NLS;
-import org.eclipse.pde.core.plugin.IPluginModelBase;
-import org.eclipse.pde.core.plugin.PluginRegistry;
-import org.eclipse.pde.internal.core.PDECore;
-import org.eclipse.pde.internal.core.SearchablePluginsManager;
-import org.eclipse.pde.internal.runtime.PDERuntimeMessages;
-import org.eclipse.pde.internal.runtime.PDERuntimePlugin;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.PartInitException;
-
-public class PDEUtils {
-	private final static Pattern CLASS_NAME_PATTERN = Pattern.compile("(([a-zA-Z_]+[0-9]*\\.)+[a-zA-Z_]+[a-z0-9]*)");
-
-	public static boolean containsClassName(String name) {
-		return CLASS_NAME_PATTERN.matcher(name).find();
-	}
-
-	public static void openClass(String clsName) throws ClassNotFoundException {
-		Matcher matcher = CLASS_NAME_PATTERN.matcher(clsName);
-		if (matcher.find()) {
-			try {
-				clsName = matcher.group(1).trim();
-				openClass(Class.forName(clsName));
-			} catch(ClassNotFoundException exc) {
-				throw new ClassNotFoundException("Class not found in the bundle classpath: " + clsName);
-			}
-		}
-	}
-
-	public static void openClass(Class<?> cls) {
-		IPluginModelBase model = PluginRegistry.findModel(PluginUtils.getBundleId(cls));
-		IResource resource = model != null ? model.getUnderlyingResource() : null;
-		IJavaProject project = null;
-
-		// if we don't find a model
-		if (model == null) {
-			MessageDialog.openError(Display.getCurrent().getActiveShell(), PDERuntimeMessages.SpyIDEUtil_noSourceFound_title,
-					NLS.bind(PDERuntimeMessages.SpyIDEUtil_noSourceFound_message, new Object[] {cls.getName()}));
-			return;
-		}
-
-		if (resource != null) { // project is open in workspace
-			project = JavaCore.create(resource.getProject());
-		} else {
-			SearchablePluginsManager manager = PDECore.getDefault().getSearchablePluginsManager();
-			try {
-				manager.createProxyProject(new NullProgressMonitor());
-				manager.addToJavaSearch(new IPluginModelBase[] {model});
-				project = manager.getProxyProject();
-			} catch (CoreException e) {
-			}
-		}
-		if (project != null)
-			openInEditor(project, cls.getName());
-	}
-
-	private static void openInEditor(IJavaProject project, String clazz) {
-		try {
-			IType type = project.findType(clazz);
-			JavaUI.openInEditor(type, false, true);
-		} catch (JavaModelException e) {
-			PDERuntimePlugin.log(e);
-		} catch (PartInitException e) {
-			PDERuntimePlugin.log(e);
-		}
-	}
-}