Skip to main content
summaryrefslogtreecommitdiffstats
blob: b6a89c42f3f09fa45114ce38016cfb4173976bc8 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (c) 2002, 2005 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.jst.j2ee.internal.plugin;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
import org.eclipse.jem.internal.adapters.jdom.JavaClassJDOMAdaptor;
import org.eclipse.jem.internal.java.adapters.ReadAdaptor;
import org.eclipse.jem.java.JavaClass;
import org.eclipse.jem.java.JavaPackage;
import org.eclipse.jem.util.emf.workbench.ProjectUtilities;
import org.eclipse.jem.workbench.utility.JemProjectUtilities;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.wst.common.internal.emfworkbench.WorkbenchResourceHelper;

/**
 * A number of routines for working with JavaElements in editors
 * 
 * Use 'isOpenInEditor' to test if an element is already open in a editor Use 'openInEditor' to
 * force opening an element in a editor With 'getWorkingCopy' you get the working copy (element in
 * the editor) of an element
 */
public class J2EEEditorUtility {
	// //$NON-NLS-1$
	public static final String EJB_JAVA_EDITOR_ID = "org.eclipse.jst.j2ee.internal.internal.ejb.ui.misc.EJBJavaEditor"; //$NON-NLS-1$

	public static ICompilationUnit getCompilationUnit(JavaClass javaClass) {
		IProject project = ProjectUtilities.getProject(javaClass);
		if (project == null)
			return null;
		return getCompilationUnit(javaClass, project);
	}

	public static ICompilationUnit getCompilationUnit(JavaClass javaClass, IProject project) {
		if (javaClass == null)
			return null;
		IJavaProject javaProj = getJavaProject(project);
		if (javaProj == null)
			return null;
		return getCompilationUnit(javaClass, javaProj);
	}

	public static ICompilationUnit getCompilationUnit(JavaClass javaClass, IJavaProject javaProject) {
		if (javaClass == null)
			return null;
		IType type = getType(javaClass);
		if (type != null)
			return type.getCompilationUnit();
		return null;
	}

	public static IType getType(JavaClass javaClass) {
		if (javaClass != null) {
			JavaClassJDOMAdaptor adaptor = (JavaClassJDOMAdaptor) EcoreUtil.getRegisteredAdapter(javaClass, ReadAdaptor.TYPE_KEY);
			if (adaptor != null)
				return adaptor.getSourceType();
		}
		return null;
	}

	public static IEditorInput getEditorInput(Object input) {
		if (input instanceof EObject)
			return new FileEditorInput(WorkbenchResourceHelper.getFile((EObject) input));
		if (input instanceof IFile)
			return new FileEditorInput((IFile) input);
		return null;
	}

	public static IFile getFile(JavaClass javaClass) {
		ICompilationUnit comp = getCompilationUnit(javaClass);
		if (comp == null)
			return null;
		IEditorInput input = null;
		try {
			input = EditorUtility.getEditorInput(comp);
		} catch (JavaModelException ex) {
			return null;
		}
		if (input != null) {
			return (IFile) input.getAdapter(IFile.class);
		}
		return null;
	}

	public static IJavaProject getJavaProject(IProject aProject) {
		if (aProject == null)
			return null;
		IJavaModel javaModel = JemProjectUtilities.getJavaModel();
		return javaModel.getJavaProject(aProject.getName());
	}

	/**
	 * Return the IPackageFragment for the JavaPackage for
	 * 
	 * @javaClass.
	 */
	public static IPackageFragment getPackageFragment(JavaClass javaClass, IJavaProject javaProject) {
		if (javaProject != null) {
			try {
				JavaPackage javaPackage = (JavaPackage) javaClass.eContainer();
				String packageName = javaPackage.getName();
				IPackageFragment[] pkgFrags = javaProject.getPackageFragments();
				for (int i = 0; i < pkgFrags.length; i++) {
					if ((pkgFrags[i].getElementName().equals(packageName)))
						return pkgFrags[i];
				}
			} catch (JavaModelException e) {
				//Ignore
			}
		}
		return null;
	}

	/**
	 * Opens a Java editor for the given element if the element is a Java compilation unit or a Java
	 * class file.
	 * 
	 * @return the IEditorPart or null if wrong element type or opening failed
	 */
	public static IEditorPart openInEditor(JavaClass javaClass, IProject aProject) throws JavaModelException, PartInitException {
		return openInEditor(javaClass, getJavaProject(aProject));
	}

	/**
	 * Opens a Java editor for the given element if the element is a Java compilation unit or a Java
	 * class file.
	 * 
	 * @return the IEditorPart or null if wrong element type or opening failed
	 */
	public static IEditorPart openInEditor(JavaClass javaClass, IJavaProject javaProject) throws JavaModelException, PartInitException {
		if (javaClass == null || javaProject == null)
			return null;
		IType type = getType(javaClass);
		if (type == null)
			return openInEditor(getCompilationUnit(javaClass, javaProject));
		return EditorUtility.openInEditor(type);
	}


	/**
	 * Opens a Java editor for the given element if the element is a Java compilation unit or a Java
	 * class file.
	 * 
	 * @return the IEditorPart or null if wrong element type or opening failed
	 */
	public static IEditorPart openInEditor(JavaClass javaClass, IJavaProject javaProject, String editorId) throws JavaModelException, PartInitException {
		return openInEditor(getCompilationUnit(javaClass, javaProject), editorId);
	}

	/**
	 * Opens a Java editor for the given element if the element is a Java compilation unit or a Java
	 * class file.
	 * 
	 * @return the IEditorPart or null if wrong element type or opening failed
	 */
	public static IEditorPart openInEditor(ICompilationUnit aCompilationUnit) throws JavaModelException, PartInitException {
		return EditorUtility.openInEditor(aCompilationUnit);
	}

	/**
	 * Opens a Java editor for the given element if the element is a Java compilation unit or a Java
	 * class file.
	 * 
	 * @return the IEditorPart or null if wrong element type or opening failed
	 */
	public static IEditorPart openInEditor(ICompilationUnit aCompilationUnit, String editorId) throws JavaModelException, PartInitException {
		return openInEditor(EditorUtility.getEditorInput(aCompilationUnit), editorId);
	}

	private static IEditorPart openInEditor(IEditorInput input, String editorID) throws PartInitException {
		if (input != null) {
			IWorkbenchPage p = J2EEUIPlugin.getActiveWorkbenchWindow().getActivePage();
			if (p != null)
				return p.openEditor(input, editorID, true);
		}
		return null;
	}
}

Back to the top