Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4847fb846b1fa362903ac4e79dff29af5296d4de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.dialogfield;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jst.jsf.common.ui.JSFUICommonPlugin;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.SelectionDialog;
import org.eclipse.ui.ide.IDE;

/**
 * @author mengbo
 */
public class JavaUIHelper {
	public static void doOpenClass(IProject project, String className) {
		String path = className.replace('.', '/') + ".java"; //$NON-NLS-1$
		try {
			if (project.hasNature(JavaCore.NATURE_ID)) {
				IJavaProject javaProject = JavaCore.create(project);
				IJavaElement result = javaProject.findElement(new Path(path));
				JavaUI.openInEditor(result);
			} else {
				IResource resource = project.findMember(new Path(path));
				if (resource != null && resource instanceof IFile) {
					IWorkbenchPage page = PlatformUI.getWorkbench()
							.getActiveWorkbenchWindow().getActivePage();
					IDE.openEditor(page, (IFile) resource, true);
				}
			}
		} catch (PartInitException e) {
			e.printStackTrace();// PDEPlugin.logException(e);
		} catch (JavaModelException e) {
			e.printStackTrace();// Display.getCurrent().beep();
		} catch (CoreException e) {
			e.printStackTrace();// PDEPlugin.logException(e);
		}
	}

	public static boolean doesClassExist(IProject project, String className) {
		String path = className.replace('.', '/') + ".java"; //$NON-NLS-1$
		try {
			if (project.hasNature(JavaCore.NATURE_ID)) {
				IJavaProject javaProject = JavaCore.create(project);

				IJavaElement result = javaProject.findElement(new Path(path));
				return result != null;
			}
            IResource resource = project.findMember(new Path(path));
            return resource != null;
		} catch (JavaModelException e) {
			return false;
		} catch (CoreException e) {
			return false;
		}
	}

	public static SelectionDialog openSelectionDialog(Shell shell,
			IJavaSearchScope searchScope, int typeFlag) {
		try {
			return JavaUI.createTypeDialog(shell, new ProgressMonitorDialog(
					shell), searchScope, typeFlag, false);
		} catch (JavaModelException e) {
            JSFUICommonPlugin.getLogger(JavaUIHelper.class).error(e);
			return null;
		}
	}

	public static SelectionDialog openSelectionDialog(Shell shell,
			IProject project, String superType, int typeFlag) {
		IJavaSearchScope searchScope = findSearchScope(project, superType);
		return openSelectionDialog(shell, searchScope, typeFlag);
	}

	public static SelectionDialog openSelectionDialog(Shell shell,
			IProject project, String superType) {
		IJavaSearchScope searchScope = findSearchScope(project, superType);
		return openSelectionDialog(shell, searchScope,
				IJavaElementSearchConstants.CONSIDER_ALL_TYPES);
	}

	public static IJavaSearchScope findSearchScope(IProject project,
			String superType) {
		if (project != null) {
			if (superType == null || "".equals(superType)) {
				superType = "java.lang.Object";//$NON-NLS-1$
			}
			return new JavaSearchScope(project, superType);
		}
		return SearchEngine.createWorkspaceScope();
	}
}

Back to the top