KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > JavaUI


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.IPath;
20
21 import org.eclipse.core.resources.IProject;
22
23 import org.eclipse.swt.dnd.Transfer;
24 import org.eclipse.swt.widgets.Shell;
25
26 import org.eclipse.jface.operation.IRunnableContext;
27
28 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.PartInitException;
31 import org.eclipse.ui.dialogs.SelectionDialog;
32 import org.eclipse.ui.texteditor.IDocumentProvider;
33
34 import org.eclipse.jdt.core.IClasspathEntry;
35 import org.eclipse.jdt.core.IJavaElement;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.IPackageFragmentRoot;
38 import org.eclipse.jdt.core.ISourceReference;
39 import org.eclipse.jdt.core.JavaCore;
40 import org.eclipse.jdt.core.JavaModelException;
41 import org.eclipse.jdt.core.search.IJavaSearchConstants;
42 import org.eclipse.jdt.core.search.IJavaSearchScope;
43 import org.eclipse.jdt.core.search.SearchEngine;
44
45 import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations;
46 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaElementTransfer;
47
48 import org.eclipse.jdt.ui.dialogs.TypeSelectionExtension;
49 import org.eclipse.jdt.ui.text.IColorManager;
50
51 import org.eclipse.jdt.internal.ui.JavaPlugin;
52 import org.eclipse.jdt.internal.ui.JavaUIMessages;
53 import org.eclipse.jdt.internal.ui.SharedImages;
54 import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog;
55 import org.eclipse.jdt.internal.ui.dialogs.MainTypeSelectionDialog;
56 import org.eclipse.jdt.internal.ui.dialogs.MultiMainTypeSelectionDialog;
57 import org.eclipse.jdt.internal.ui.dialogs.PackageSelectionDialog;
58 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
59 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
60
61 /**
62  * Central access point for the Java UI plug-in (id <code>"org.eclipse.jdt.ui"</code>).
63  * This class provides static methods for:
64  * <ul>
65  * <li> creating various kinds of selection dialogs to present a collection
66  * of Java elements to the user and let them make a selection.</li>
67  * <li> opening a Java editor on a compilation unit.</li>
68  * </ul>
69  * <p>
70  * This class provides static methods and fields only; it is not intended to be
71  * instantiated or subclassed by clients.
72  * </p>
73  */

74 public final class JavaUI {
75     
76     private static ISharedImages fgSharedImages= null;
77     
78     private JavaUI() {
79         // prevent instantiation of JavaUI.
80
}
81     
82     /**
83      * The id of the Java plug-in (value <code>"org.eclipse.jdt.ui"</code>).
84      */

85     public static final String JavaDoc ID_PLUGIN= "org.eclipse.jdt.ui"; //$NON-NLS-1$
86

87     /**
88      * The id of the Java perspective
89      * (value <code>"org.eclipse.jdt.ui.JavaPerspective"</code>).
90      */

91     public static final String JavaDoc ID_PERSPECTIVE= "org.eclipse.jdt.ui.JavaPerspective"; //$NON-NLS-1$
92

93     /**
94      * The id of the Java hierarchy perspective
95      * (value <code>"org.eclipse.jdt.ui.JavaHierarchyPerspective"</code>).
96      */

97     public static final String JavaDoc ID_HIERARCHYPERSPECTIVE= "org.eclipse.jdt.ui.JavaHierarchyPerspective"; //$NON-NLS-1$
98

99     /**
100      * The id of the Java action set
101      * (value <code>"org.eclipse.jdt.ui.JavaActionSet"</code>).
102      */

103     public static final String JavaDoc ID_ACTION_SET= "org.eclipse.jdt.ui.JavaActionSet"; //$NON-NLS-1$
104

105     /**
106      * The id of the Java Element Creation action set
107      * (value <code>"org.eclipse.jdt.ui.JavaElementCreationActionSet"</code>).
108      *
109      * @since 2.0
110      */

111     public static final String JavaDoc ID_ELEMENT_CREATION_ACTION_SET= "org.eclipse.jdt.ui.JavaElementCreationActionSet"; //$NON-NLS-1$
112

113     /**
114      * The id of the Java Coding action set
115      * (value <code>"org.eclipse.jdt.ui.CodingActionSet"</code>).
116      *
117      * @since 2.0
118      */

119     public static final String JavaDoc ID_CODING_ACTION_SET= "org.eclipse.jdt.ui.CodingActionSet"; //$NON-NLS-1$
120

121     /**
122      * The id of the Java action set for open actions
123      * (value <code>"org.eclipse.jdt.ui.A_OpenActionSet"</code>).
124      *
125      * @since 2.0
126      */

127     public static final String JavaDoc ID_OPEN_ACTION_SET= "org.eclipse.jdt.ui.A_OpenActionSet"; //$NON-NLS-1$
128

129     /**
130      * The id of the Java Search action set
131      * (value <code>org.eclipse.jdt.ui.SearchActionSet"</code>).
132      *
133      * @since 2.0
134      */

135     public static final String JavaDoc ID_SEARCH_ACTION_SET= "org.eclipse.jdt.ui.SearchActionSet"; //$NON-NLS-1$
136

137     /**
138      * The editor part id of the editor that presents Java compilation units
139      * (value <code>"org.eclipse.jdt.ui.CompilationUnitEditor"</code>).
140      */

141     public static final String JavaDoc ID_CU_EDITOR= "org.eclipse.jdt.ui.CompilationUnitEditor"; //$NON-NLS-1$
142

143     /**
144      * The editor part id of the editor that presents Java binary class files
145      * (value <code>"org.eclipse.jdt.ui.ClassFileEditor"</code>).
146      */

147     public static final String JavaDoc ID_CF_EDITOR= "org.eclipse.jdt.ui.ClassFileEditor"; //$NON-NLS-1$
148

149     /**
150      * The editor part id of the code snippet editor
151      * (value <code>"org.eclipse.jdt.ui.SnippetEditor"</code>).
152      */

153     public static final String JavaDoc ID_SNIPPET_EDITOR= "org.eclipse.jdt.ui.SnippetEditor"; //$NON-NLS-1$
154

155     /**
156      * The view part id of the Packages view
157      * (value <code>"org.eclipse.jdt.ui.PackageExplorer"</code>).
158      * <p>
159      * When this id is used to access
160      * a view part with <code>IWorkbenchPage.findView</code> or
161      * <code>showView</code>, the returned <code>IViewPart</code>
162      * can be safely cast to an <code>IPackagesViewPart</code>.
163      * </p>
164      *
165      * @see IPackagesViewPart
166      * @see org.eclipse.ui.IWorkbenchPage#findView(java.lang.String)
167      * @see org.eclipse.ui.IWorkbenchPage#showView(java.lang.String)
168      */

169     public static final String JavaDoc ID_PACKAGES= "org.eclipse.jdt.ui.PackageExplorer"; //$NON-NLS-1$
170

171     /**
172      * The view part id of the type hierarchy part
173      * (value <code>"org.eclipse.jdt.ui.TypeHierarchy"</code>).
174      * <p>
175      * When this id is used to access
176      * a view part with <code>IWorkbenchPage.findView</code> or
177      * <code>showView</code>, the returned <code>IViewPart</code>
178      * can be safely cast to an <code>ITypeHierarchyViewPart</code>.
179      * </p>
180      *
181      * @see ITypeHierarchyViewPart
182      * @see org.eclipse.ui.IWorkbenchPage#findView(java.lang.String)
183      * @see org.eclipse.ui.IWorkbenchPage#showView(java.lang.String)
184      */

185     public static final String JavaDoc ID_TYPE_HIERARCHY= "org.eclipse.jdt.ui.TypeHierarchy"; //$NON-NLS-1$
186

187     /**
188      * The view part id of the source (declaration) view
189      * (value <code>"org.eclipse.jdt.ui.SourceView"</code>).
190      *
191      * @see org.eclipse.ui.IWorkbenchPage#findView(java.lang.String)
192      * @see org.eclipse.ui.IWorkbenchPage#showView(java.lang.String)
193      * @since 3.0
194      */

195     public static final String JavaDoc ID_SOURCE_VIEW= "org.eclipse.jdt.ui.SourceView"; //$NON-NLS-1$
196

197     /**
198      * The view part id of the Javadoc view
199      * (value <code>"org.eclipse.jdt.ui.JavadocView"</code>).
200      *
201      * @see org.eclipse.ui.IWorkbenchPage#findView(java.lang.String)
202      * @see org.eclipse.ui.IWorkbenchPage#showView(java.lang.String)
203      * @since 3.0
204      */

205     public static final String JavaDoc ID_JAVADOC_VIEW= "org.eclipse.jdt.ui.JavadocView"; //$NON-NLS-1$
206

207     /**
208      * The id of the Java Browsing Perspective
209      * (value <code>"org.eclipse.jdt.ui.JavaBrowsingPerspective"</code>).
210      *
211      * @since 2.0
212      */

213     public static String JavaDoc ID_BROWSING_PERSPECTIVE= "org.eclipse.jdt.ui.JavaBrowsingPerspective"; //$NON-NLS-1$
214

215     /**
216      * The view part id of the Java Browsing Projects view
217      * (value <code>"org.eclipse.jdt.ui.ProjectsView"</code>).
218      *
219      * @since 2.0
220      */

221     public static String JavaDoc ID_PROJECTS_VIEW= "org.eclipse.jdt.ui.ProjectsView"; //$NON-NLS-1$
222

223     /**
224      * The view part id of the Java Browsing Packages view
225      * (value <code>"org.eclipse.jdt.ui.PackagesView"</code>).
226      *
227      * @since 2.0
228      */

229     public static String JavaDoc ID_PACKAGES_VIEW= "org.eclipse.jdt.ui.PackagesView"; //$NON-NLS-1$
230

231     /**
232      * The view part id of the Java Browsing Types view
233      * (value <code>"org.eclipse.jdt.ui.TypesView"</code>).
234      *
235      * @since 2.0
236      */

237     public static String JavaDoc ID_TYPES_VIEW= "org.eclipse.jdt.ui.TypesView"; //$NON-NLS-1$
238

239     /**
240      * The view part id of the Java Browsing Members view
241      * (value <code>"org.eclipse.jdt.ui.MembersView"</code>).
242      *
243      * @since 2.0
244      */

245     public static String JavaDoc ID_MEMBERS_VIEW= "org.eclipse.jdt.ui.MembersView"; //$NON-NLS-1$
246

247     /**
248      * The class org.eclipse.debug.core.model.IProcess allows attaching
249      * String properties to processes. The Java UI contributes a property
250      * page for IProcess that will show the contents of the property
251      * with this key.
252      * The intent of this property is to show the command line a process
253      * was launched with.
254      * @deprecated
255      */

256     public final static String JavaDoc ATTR_CMDLINE= "org.eclipse.jdt.ui.launcher.cmdLine"; //$NON-NLS-1$
257

258     
259     /**
260      * @deprecated Constant introduced to avoid deprecated warning
261      */

262     private final static int DEPRECATED_CONSIDER_TYPES= IJavaElementSearchConstants.CONSIDER_TYPES;
263
264     /**
265      * Returns the shared images for the Java UI.
266      *
267      * @return the shared images manager
268      */

269     public static ISharedImages getSharedImages() {
270         if (fgSharedImages == null)
271             fgSharedImages= new SharedImages();
272             
273         return fgSharedImages;
274     }
275      
276     /**
277      * Creates a selection dialog that lists all packages of the given Java project.
278      * The caller is responsible for opening the dialog with <code>Window.open</code>,
279      * and subsequently extracting the selected package (of type
280      * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
281      *
282      * @param parent the parent shell of the dialog to be created
283      * @param project the Java project
284      * @param style flags defining the style of the dialog; the valid flags are:
285      * <code>IJavaElementSearchConstants.CONSIDER_BINARIES</code>, indicating that
286      * packages from binary package fragment roots should be included in addition
287      * to those from source package fragment roots;
288      * <code>IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS</code>, indicating that
289      * packages from required projects should be included as well.
290      * @param filter the initial pattern to filter the set of packages. For example "com" shows
291      * all packages starting with "com". The meta character '?' representing any character and
292      * '*' representing any string are supported. Clients can pass an empty string if no filtering
293      * is required.
294      * @return a new selection dialog
295      * @exception JavaModelException if the selection dialog could not be opened
296      *
297      * @since 2.0
298      */

299     public static SelectionDialog createPackageDialog(Shell parent, IJavaProject project, int style, String JavaDoc filter) throws JavaModelException {
300         Assert.isTrue((style | IJavaElementSearchConstants.CONSIDER_BINARIES | IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) ==
301             (IJavaElementSearchConstants.CONSIDER_BINARIES | IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS));
302
303         IPackageFragmentRoot[] roots= null;
304         if ((style & IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) != 0) {
305             roots= project.getAllPackageFragmentRoots();
306         } else {
307             roots= project.getPackageFragmentRoots();
308         }
309         
310         List JavaDoc consideredRoots= null;
311         if ((style & IJavaElementSearchConstants.CONSIDER_BINARIES) != 0) {
312             consideredRoots= Arrays.asList(roots);
313         } else {
314             consideredRoots= new ArrayList JavaDoc(roots.length);
315             for (int i= 0; i < roots.length; i++) {
316                 IPackageFragmentRoot root= roots[i];
317                 if (root.getKind() != IPackageFragmentRoot.K_BINARY)
318                     consideredRoots.add(root);
319                     
320             }
321         }
322         
323         IJavaSearchScope searchScope= SearchEngine.createJavaSearchScope((IJavaElement[])consideredRoots.toArray(new IJavaElement[consideredRoots.size()]));
324         BusyIndicatorRunnableContext context= new BusyIndicatorRunnableContext();
325         if (style == 0 || style == IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) {
326             return createPackageDialog(parent, context, searchScope, false, true, filter);
327         } else {
328             return createPackageDialog(parent, context, searchScope, false, false, filter);
329         }
330     }
331     
332     /**
333      * Creates a selection dialog that lists all packages of the given Java search scope.
334      * The caller is responsible for opening the dialog with <code>Window.open</code>,
335      * and subsequently extracting the selected package (of type
336      * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
337      *
338      * @param parent the parent shell of the dialog to be created
339      * @param context the runnable context to run the search in
340      * @param scope the scope defining the available packages.
341      * @param multipleSelection true if multiple selection is allowed
342      * @param removeDuplicates true if only one package is shown per package name
343      * @param filter the initial pattern to filter the set of packages. For example "com" shows
344      * all packages starting with "com". The meta character '?' representing any character and
345      * '*' representing any string are supported. Clients can pass an empty string if no filtering
346      * is required.
347      * @return a new selection dialog
348      *
349      * @since 3.2
350      */

351     public static SelectionDialog createPackageDialog(Shell parent, IRunnableContext context, IJavaSearchScope scope,
352             boolean multipleSelection, boolean removeDuplicates, String JavaDoc filter) {
353         
354         int flag= removeDuplicates ? PackageSelectionDialog.F_REMOVE_DUPLICATES : 0;
355         PackageSelectionDialog dialog= new PackageSelectionDialog(parent, context, flag, scope);
356         dialog.setFilter(filter);
357         dialog.setIgnoreCase(false);
358         dialog.setMultipleSelection(multipleSelection);
359         return dialog;
360     }
361     
362     /**
363      * Creates a selection dialog that lists all packages of the given Java project.
364      * The caller is responsible for opening the dialog with <code>Window.open</code>,
365      * and subsequently extracting the selected package (of type
366      * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
367      *
368      * @param parent the parent shell of the dialog to be created
369      * @param project the Java project
370      * @param style flags defining the style of the dialog; the valid flags are:
371      * <code>IJavaElementSearchConstants.CONSIDER_BINARIES</code>, indicating that
372      * packages from binary package fragment roots should be included in addition
373      * to those from source package fragment roots;
374      * <code>IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS</code>, indicating that
375      * packages from required projects should be included as well.
376      * @return a new selection dialog
377      * @exception JavaModelException if the selection dialog could not be opened
378      */

379     public static SelectionDialog createPackageDialog(Shell parent, IJavaProject project, int style) throws JavaModelException {
380         return createPackageDialog(parent, project, style, ""); //$NON-NLS-1$
381
}
382     
383     /**
384      * Creates a selection dialog that lists all packages under the given package
385      * fragment root.
386      * The caller is responsible for opening the dialog with <code>Window.open</code>,
387      * and subsequently extracting the selected package (of type
388      * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
389      *
390      * @param parent the parent shell of the dialog to be created
391      * @param root the package fragment root
392      * @param filter the initial pattern to filter the set of packages. For example "com" shows
393      * all packages starting with "com". The meta character '?' representing any character and
394      * '*' representing any string are supported. Clients can pass an empty string if no filtering
395      * is required.
396      * @return a new selection dialog
397      * @exception JavaModelException if the selection dialog could not be opened
398      *
399      * @since 2.0
400      */

401     public static SelectionDialog createPackageDialog(Shell parent, IPackageFragmentRoot root, String JavaDoc filter) throws JavaModelException {
402         IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] {root});
403         BusyIndicatorRunnableContext context= new BusyIndicatorRunnableContext();
404         return createPackageDialog(parent, context, scope, false, true, filter);
405     }
406
407     /**
408      * Creates a selection dialog that lists all packages under the given package
409      * fragment root.
410      * The caller is responsible for opening the dialog with <code>Window.open</code>,
411      * and subsequently extracting the selected package (of type
412      * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
413      *
414      * @param parent the parent shell of the dialog to be created
415      * @param root the package fragment root
416      * @return a new selection dialog
417      * @exception JavaModelException if the selection dialog could not be opened
418      */

419     public static SelectionDialog createPackageDialog(Shell parent, IPackageFragmentRoot root) throws JavaModelException {
420         return createPackageDialog(parent, root, ""); //$NON-NLS-1$
421
}
422
423     /**
424      * Creates a selection dialog that lists all types in the given project.
425      * The caller is responsible for opening the dialog with <code>Window.open</code>,
426      * and subsequently extracting the selected type(s) (of type
427      * <code>IType</code>) via <code>SelectionDialog.getResult</code>.
428      *
429      * @param parent the parent shell of the dialog to be created
430      * @param context the runnable context used to show progress when the dialog
431      * is being populated
432      * @param project the Java project
433      * @param style flags defining the style of the dialog; the only valid values are
434      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES</code>,
435      * <code>IJavaElementSearchConstants.CONSIDER_INTERFACES</code>,
436      * <code>IJavaElementSearchConstants.CONSIDER_ANNOTATION_TYPES</code>,
437      * <code>IJavaElementSearchConstants.CONSIDER_ENUMS</code>,
438      * <code>IJavaElementSearchConstants.CONSIDER_ALL_TYPES</code>,
439      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES</code>
440      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES_AND_ENUMS</code>. Please note that
441      * the bitwise OR combination of the elementary constants is not supported.
442      * @param multipleSelection <code>true</code> if multiple selection is allowed
443      *
444      * @return a new selection dialog
445      *
446      * @exception JavaModelException if the selection dialog could not be opened
447      */

448     public static SelectionDialog createTypeDialog(Shell parent, IRunnableContext context, IProject project, int style, boolean multipleSelection) throws JavaModelException {
449         IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaProject[] { JavaCore.create(project) });
450         return createTypeDialog(parent, context, scope, style, multipleSelection);
451     }
452     
453     /**
454      * Creates a selection dialog that lists all types in the given scope.
455      * The caller is responsible for opening the dialog with <code>Window.open</code>,
456      * and subsequently extracting the selected type(s) (of type
457      * <code>IType</code>) via <code>SelectionDialog.getResult</code>.
458      *
459      * @param parent the parent shell of the dialog to be created
460      * @param context the runnable context used to show progress when the dialog
461      * is being populated
462      * @param scope the scope that limits which types are included
463      * @param style flags defining the style of the dialog; the only valid values are
464      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES</code>,
465      * <code>IJavaElementSearchConstants.CONSIDER_INTERFACES</code>,
466      * <code>IJavaElementSearchConstants.CONSIDER_ANNOTATION_TYPES</code>,
467      * <code>IJavaElementSearchConstants.CONSIDER_ENUMS</code>,
468      * <code>IJavaElementSearchConstants.CONSIDER_ALL_TYPES</code>,
469      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES</code>
470      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES_AND_ENUMS</code>. Please note that
471      * the bitwise OR combination of the elementary constants is not supported.
472      * @param multipleSelection <code>true</code> if multiple selection is allowed
473      *
474      * @return a new selection dialog
475      *
476      * @exception JavaModelException if the selection dialog could not be opened
477      */

478     public static SelectionDialog createTypeDialog(Shell parent, IRunnableContext context, IJavaSearchScope scope, int style, boolean multipleSelection) throws JavaModelException {
479         return createTypeDialog(parent, context, scope, style, multipleSelection, "");//$NON-NLS-1$
480
}
481         
482     /**
483      * Creates a selection dialog that lists all types in the given scope.
484      * The caller is responsible for opening the dialog with <code>Window.open</code>,
485      * and subsequently extracting the selected type(s) (of type
486      * <code>IType</code>) via <code>SelectionDialog.getResult</code>.
487      *
488      * @param parent the parent shell of the dialog to be created
489      * @param context the runnable context used to show progress when the dialog
490      * is being populated
491      * @param scope the scope that limits which types are included
492      * @param style flags defining the style of the dialog; the only valid values are
493      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES</code>,
494      * <code>IJavaElementSearchConstants.CONSIDER_INTERFACES</code>,
495      * <code>IJavaElementSearchConstants.CONSIDER_ANNOTATION_TYPES</code>,
496      * <code>IJavaElementSearchConstants.CONSIDER_ENUMS</code>,
497      * <code>IJavaElementSearchConstants.CONSIDER_ALL_TYPES</code>,
498      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES</code>
499      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES_AND_ENUMS</code>. Please note that
500      * the bitwise OR combination of the elementary constants is not supported.
501      * @param multipleSelection <code>true</code> if multiple selection is allowed
502      * @param filter the initial pattern to filter the set of types. For example "Abstract" shows
503      * all types starting with "abstract". The meta character '?' representing any character and
504      * '*' representing any string are supported. Clients can pass an empty string if no filtering
505      * is required.
506      *
507      * @return a new selection dialog
508      *
509      * @exception JavaModelException if the selection dialog could not be opened
510      *
511      * @since 2.0
512      */

513     public static SelectionDialog createTypeDialog(Shell parent, IRunnableContext context, IJavaSearchScope scope, int style, boolean multipleSelection, String JavaDoc filter) throws JavaModelException {
514         return createTypeDialog(parent, context, scope, style, multipleSelection, filter, null);
515     }
516     
517     /**
518      * Creates a selection dialog that lists all types in the given scope.
519      * The caller is responsible for opening the dialog with <code>Window.open</code>,
520      * and subsequently extracting the selected type(s) (of type
521      * <code>IType</code>) via <code>SelectionDialog.getResult</code>.
522      *
523      * @param parent the parent shell of the dialog to be created
524      * @param context the runnable context used to show progress when the dialog
525      * is being populated
526      * @param scope the scope that limits which types are included
527      * @param style flags defining the style of the dialog; the only valid values are
528      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES</code>,
529      * <code>IJavaElementSearchConstants.CONSIDER_INTERFACES</code>,
530      * <code>IJavaElementSearchConstants.CONSIDER_ANNOTATION_TYPES</code>,
531      * <code>IJavaElementSearchConstants.CONSIDER_ENUMS</code>,
532      * <code>IJavaElementSearchConstants.CONSIDER_ALL_TYPES</code>,
533      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES</code>
534      * <code>IJavaElementSearchConstants.CONSIDER_CLASSES_AND_ENUMS</code>. Please note that
535      * the bitwise OR combination of the elementary constants is not supported.
536      * @param multipleSelection <code>true</code> if multiple selection is allowed
537      * @param filter the initial pattern to filter the set of types. For example "Abstract" shows
538      * all types starting with "abstract". The meta character '?' representing any character and
539      * '*' representing any string are supported. Clients can pass an empty string if no filtering
540      * is required.
541      * @param extension a user interface extension to the type selection dialog or <code>null</code>
542      * if no extension is desired
543      *
544      * @return a new selection dialog
545      *
546      * @exception JavaModelException if the selection dialog could not be opened
547      *
548      * @since 3.2
549      */

550     public static SelectionDialog createTypeDialog(Shell parent, IRunnableContext context, IJavaSearchScope scope, int style,
551             boolean multipleSelection, String JavaDoc filter, TypeSelectionExtension extension) throws JavaModelException {
552         int elementKinds= 0;
553         if (style == IJavaElementSearchConstants.CONSIDER_ALL_TYPES) {
554             elementKinds= IJavaSearchConstants.TYPE;
555         } else if (style == IJavaElementSearchConstants.CONSIDER_INTERFACES) {
556             elementKinds= IJavaSearchConstants.INTERFACE;
557         } else if (style == IJavaElementSearchConstants.CONSIDER_CLASSES) {
558             elementKinds= IJavaSearchConstants.CLASS;
559         } else if (style == IJavaElementSearchConstants.CONSIDER_ANNOTATION_TYPES) {
560             elementKinds= IJavaSearchConstants.ANNOTATION_TYPE;
561         } else if (style == IJavaElementSearchConstants.CONSIDER_ENUMS) {
562             elementKinds= IJavaSearchConstants.ENUM;
563         } else if (style == IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES) {
564             elementKinds= IJavaSearchConstants.CLASS_AND_INTERFACE;
565         } else if (style == IJavaElementSearchConstants.CONSIDER_CLASSES_AND_ENUMS) {
566             elementKinds= IJavaSearchConstants.CLASS_AND_ENUM;
567         } else if (style == DEPRECATED_CONSIDER_TYPES) {
568             elementKinds= IJavaSearchConstants.CLASS_AND_INTERFACE;
569         } else {
570             throw new IllegalArgumentException JavaDoc("Invalid style constant."); //$NON-NLS-1$
571
}
572         FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(parent, multipleSelection,
573             context, scope, elementKinds, extension);
574         dialog.setMessage(JavaUIMessages.JavaUI_defaultDialogMessage);
575         dialog.setInitialPattern(filter);
576         return dialog;
577     }
578
579     /**
580      * Creates a selection dialog that lists all types in the given scope containing
581      * a standard <code>main</code> method.
582      * The caller is responsible for opening the dialog with <code>Window.open</code>,
583      * and subsequently extracting the selected type(s) (of type
584      * <code>IType</code>) via <code>SelectionDialog.getResult</code>.
585      *
586      * @param parent the parent shell of the dialog to be created
587      * @param context the runnable context used to show progress when the dialog
588      * is being populated
589      * @param scope the scope that limits which types are included
590      * @param style flags defining the style of the dialog; the only valid values are
591      * <code>IJavaElementSearchConstants.CONSIDER_BINARIES</code>,
592      * <code>CONSIDER_EXTERNAL_JARS</code>, or their bitwise OR, or <code>0</code>
593      * @param multipleSelection <code>true</code> if multiple selection is allowed
594      * @param filter the initial pattern to filter the set of types containing a main method. For
595      * example "App" shows all types starting with "app". The meta character '?' representing
596      * any character and '*' representing any string are supported. Clients can pass an empty
597      * string if no filtering is required.
598      * @return a new selection dialog
599      *
600      * @since 2.0
601      */

602     public static SelectionDialog createMainTypeDialog(Shell parent, IRunnableContext context, IJavaSearchScope scope, int style, boolean multipleSelection, String JavaDoc filter) {
603         if (multipleSelection) {
604             MultiMainTypeSelectionDialog dialog= new MultiMainTypeSelectionDialog(parent, context, scope, style);
605             dialog.setFilter(filter);
606             return dialog;
607         } else {
608             MainTypeSelectionDialog dialog= new MainTypeSelectionDialog(parent, context, scope, style);
609             dialog.setFilter(filter);
610             return dialog;
611         }
612     }
613
614     /**
615      * Creates a selection dialog that lists all types in the given scope containing
616      * a standard <code>main</code> method.
617      * The caller is responsible for opening the dialog with <code>Window.open</code>,
618      * and subsequently extracting the selected type(s) (of type
619      * <code>IType</code>) via <code>SelectionDialog.getResult</code>.
620      *
621      * @param parent the parent shell of the dialog to be created
622      * @param context the runnable context used to show progress when the dialog
623      * is being populated
624      * @param scope the scope that limits which types are included
625      * @param style flags defining the style of the dialog; the only valid values are
626      * <code>IJavaElementSearchConstants.CONSIDER_BINARIES</code>,
627      * <code>CONSIDER_EXTERNAL_JARS</code>, or their bitwise OR, or <code>0</code>
628      * @param multipleSelection <code>true</code> if multiple selection is allowed
629      * @return a new selection dialog
630      */

631     public static SelectionDialog createMainTypeDialog(Shell parent, IRunnableContext context, IJavaSearchScope scope, int style, boolean multipleSelection) {
632         return createMainTypeDialog(parent, context, scope, style, multipleSelection, "");//$NON-NLS-1$
633
}
634     
635     /**
636      * Opens an editor on the given Java element in the active page. Valid elements are all Java elements that are {@link ISourceReference}.
637      * For elements inside a compilation unit or class file, the parent is opened in the editor is opened and the element revealed.
638      * If there already is an open Java editor for the given element, it is returned.
639      *
640      * @param element the input element; either a compilation unit
641      * (<code>ICompilationUnit</code>) or a class file (<code>IClassFile</code>) or source references inside.
642      * @return returns the editor part of the opened editor or <code>null</code> if the element is not a {@link ISourceReference} or the
643      * file was opened in an external editor.
644      * @exception PartInitException if the editor could not be initialized or no workbench page is active
645      * @exception JavaModelException if this element does not exist or if an exception occurs while accessing its underlying resource
646      */

647     public static IEditorPart openInEditor(IJavaElement element) throws JavaModelException, PartInitException {
648         return openInEditor(element, true, true);
649     }
650     
651     /**
652      * Opens an editor on the given Java element in the active page. Valid elements are all Java elements that are {@link ISourceReference}.
653      * For elements inside a compilation unit or class file, the parent is opened in the editor is opened.
654      * If there already is an open Java editor for the given element, it is returned.
655      *
656      * @param element the input element; either a compilation unit
657      * (<code>ICompilationUnit</code>) or a class file (<code>IClassFile</code>) or source references inside.
658      * @param activate if set, the editor will be activated.
659      * @param reveal if set, the element will be revealed.
660      * @return returns the editor part of the opened editor or <code>null</code> if the element is not a {@link ISourceReference} or the
661      * file was opened in an external editor.
662      * @exception PartInitException if the editor could not be initialized or no workbench page is active
663      * @exception JavaModelException if this element does not exist or if an exception occurs while accessing its underlying resource
664      * @since 3.3
665      */

666     public static IEditorPart openInEditor(IJavaElement element, boolean activate, boolean reveal) throws JavaModelException, PartInitException {
667         if (!(element instanceof ISourceReference)) {
668             return null;
669         }
670         IEditorPart part= EditorUtility.openInEditor(element, activate);
671         if (reveal && part != null) {
672             EditorUtility.revealInEditor(part, element);
673         }
674         return part;
675     }
676
677     /**
678      * Reveals the source range of the given source reference element in the
679      * given editor. No checking is done if the editor displays a compilation unit or
680      * class file that contains the given source reference. The editor simply reveals
681      * the source range denoted by the given source reference.
682      *
683      * @param part the editor displaying the compilation unit or class file
684      * @param element the source reference element defining the source range to be revealed
685      *
686      * @deprecated use <code>revealInEditor(IEditorPart, IJavaElement)</code> instead
687      */

688     public static void revealInEditor(IEditorPart part, ISourceReference element) {
689         if (element instanceof IJavaElement)
690             revealInEditor(part, (IJavaElement) element);
691     }
692     
693     /**
694      * Reveals the given java element in the given editor. If the element is not an instance
695      * of <code>ISourceReference</code> this method result in a NOP. If it is a source
696      * reference no checking is done if the editor displays a compilation unit or class file that
697      * contains the source reference element. The editor simply reveals the source range
698      * denoted by the given element.
699      *
700      * @param part the editor displaying a compilation unit or class file
701      * @param element the element to be revealed
702      *
703      * @since 2.0
704      */

705     public static void revealInEditor(IEditorPart part, IJavaElement element) {
706         EditorUtility.revealInEditor(part, element);
707     }
708      
709     /**
710      * Returns the working copy manager for the Java UI plug-in.
711      *
712      * @return the working copy manager for the Java UI plug-in
713      */

714     public static IWorkingCopyManager getWorkingCopyManager() {
715         return JavaPlugin.getDefault().getWorkingCopyManager();
716     }
717
718     /**
719      * Returns the Java element wrapped by the given editor input.
720      *
721      * @param editorInput the editor input
722      * @return the Java element wrapped by <code>editorInput</code> or <code>null</code> if none
723      * @since 3.2
724      */

725     public static IJavaElement getEditorInputJavaElement(IEditorInput editorInput) {
726         // Performance: check working copy manager first: this is faster
727
IJavaElement je= JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
728         if (je != null)
729             return je;
730         
731         return (IJavaElement)editorInput.getAdapter(IJavaElement.class);
732     }
733
734     /**
735      * Answers the shared working copies currently registered for the Java plug-in.
736      * Note that the returned array can include working copies that are
737      * not on the class path of a Java project.
738      *
739      * @return the list of shared working copies
740      *
741      * @see org.eclipse.jdt.core.JavaCore#getSharedWorkingCopies(org.eclipse.jdt.core.IBufferFactory)
742      * @since 2.0
743      * @deprecated Use {@link JavaCore#getWorkingCopies(org.eclipse.jdt.core.WorkingCopyOwner)} instead with <code>null</code> as
744      * argument for owner.
745      */

746     public static org.eclipse.jdt.core.IWorkingCopy[] getSharedWorkingCopies() {
747         return JavaCore.getSharedWorkingCopies(getBufferFactory());
748     }
749     
750     /**
751      * Answers the shared working copies that are on the class path of a Java
752      * project currently registered for the Java plug-in.
753      *
754      *
755      * @return the list of shared working copies
756      *
757      * @see #getSharedWorkingCopies()
758      * @since 2.1
759      * @deprecated Use {@link JavaCore#getWorkingCopies(org.eclipse.jdt.core.WorkingCopyOwner)} instead and filter the list
760      * with {@link IJavaProject#isOnClasspath(IJavaElement)}.
761      */

762     public static org.eclipse.jdt.core.IWorkingCopy[] getSharedWorkingCopiesOnClasspath() {
763         org.eclipse.jdt.core.IWorkingCopy[] wcs= getSharedWorkingCopies();
764         List JavaDoc result= new ArrayList JavaDoc(wcs.length);
765         for (int i = 0; i < wcs.length; i++) {
766             org.eclipse.jdt.core.IWorkingCopy wc= wcs[i];
767             if (wc instanceof IJavaElement) {
768                 IJavaElement je= (IJavaElement)wc;
769                 if (je.getJavaProject().isOnClasspath(je)) {
770                     result.add(wc);
771                 }
772             }
773         }
774         return (org.eclipse.jdt.core.IWorkingCopy[])result.toArray(new org.eclipse.jdt.core.IWorkingCopy[result.size()]);
775     }
776     
777     /**
778      * Returns the buffer factory for the Java UI plug-in.
779      *
780      * @return the buffer factory for the Java UI plug-in
781      *
782      * @see org.eclipse.jdt.core.IBufferFactory
783      * @since 2.0
784      * @deprecated {@link org.eclipse.jdt.core.IBufferFactory} has been replaced by {@link org.eclipse.jdt.core.WorkingCopyOwner}.
785      * The Java UI plug-in uses the <i>primary working copy owner</i> that can be accessed with <code>null</code> in
786      * API's that require an owner
787      */

788     public static org.eclipse.jdt.core.IBufferFactory getBufferFactory() {
789         return JavaPlugin.getDefault().getBufferFactory();
790     }
791
792     /**
793      * Returns the DocumentProvider used for Java compilation units.
794      *
795      * @return the DocumentProvider for Java compilation units.
796      *
797      * @see IDocumentProvider
798      * @since 2.0
799      */

800     public static IDocumentProvider getDocumentProvider() {
801         return JavaPlugin.getDefault().getCompilationUnitDocumentProvider();
802     }
803         
804     /**
805      * Sets the Javadoc location for an archive with the given path.
806      *
807      * @param archivePath the path of the library; this can be an workspace path
808      * or an external path in case of an external library.
809      * @param url the Javadoc location to set. This location should contain index.html and
810      * a file 'package-list'. <code>null</code> clears the current documentation
811      * location.
812      * @deprecated Javadoc is now attached to the classpath entry.
813      * Evaluate the libraries classpath entry and reconfigure the Javadoc location there.
814      *
815      * @since 2.0
816      */

817     public static void setLibraryJavadocLocation(IPath archivePath, URL JavaDoc url) {
818         // deprecated
819
}
820     
821     /**
822      * Sets the Javadoc locations for archives with the given paths.
823      *
824      * @param archivePaths the paths of the libraries. can be workspace paths
825      * or external paths in case of an external library.
826      * @param urls the Javadoc locations to set. Each location corresponds to the archive path of the same index. A location should contain index.html and
827      * a file 'package-list'. <code>null</code> is a valid location entry and clears the current documentation
828      * location. The length of the location array must be equals to the number of archive paths passed.
829      *
830      * @deprecated Javadoc is now attached to the classpath entry.
831      * Evaluate the libraries classpath entry and reconfigure the Javadoc location there.
832      *
833      * @since 3.0
834      */

835     public static void setLibraryJavadocLocations(IPath[] archivePaths, URL JavaDoc[] urls) {
836         // deprecated
837
}
838     
839     /**
840      * Returns the Javadoc location for an archive or <code>null</code> if no
841      * location is available.
842      *
843      * @param archivePath the path of the library. This can be an workspace path
844      * or an external path in case of an external library.
845      * @return the Javadoc location for an archive or <code>null</code>.
846      *
847      * @deprecated Javadoc is now attached to the classpath entry. Use {@link #getJavadocBaseLocation(IJavaElement)}
848      * with the archive's {@link IPackageFragmentRoot} or use {@link #getLibraryJavadocLocation(IClasspathEntry)}
849      * with the archive's {@link IClasspathEntry}.
850      *
851      * @since 2.0
852      */

853     public static URL JavaDoc getLibraryJavadocLocation(IPath archivePath) {
854         return null;
855     }
856     
857     /**
858      * Returns the Javadoc location for library's classpath entry or <code>null</code> if no
859      * location is available. Note that only classpath entries of kind {@link IClasspathEntry#CPE_LIBRARY} and
860      * {@link IClasspathEntry#CPE_VARIABLE} support Javadoc locations.
861      *
862      * @param entry the classpath entry to get the Javadoc location for
863      * @return the Javadoc location or<code>null</code> if no Javadoc location is available
864      * @throws IllegalArgumentException Thrown when the entry is <code>null</code> or not of kind
865      * {@link IClasspathEntry#CPE_LIBRARY} or {@link IClasspathEntry#CPE_VARIABLE}.
866      *
867      * @since 3.1
868      */

869     public static URL JavaDoc getLibraryJavadocLocation(IClasspathEntry entry) {
870         return JavaDocLocations.getLibraryJavadocLocation(entry);
871     }
872     
873     /**
874      * Sets the Javadoc location for a Java project. This location is used for
875      * all types located in the project's source folders.
876      *
877      * @param project the project
878      * @param url the Javadoc location to set. This location should contain index.html and
879      * a file 'package-list'. <code>null</code> clears the current documentation
880      * location.
881      *
882      * @since 2.1
883      */

884     public static void setProjectJavadocLocation(IJavaProject project, URL JavaDoc url) {
885         JavaDocLocations.setProjectJavadocLocation(project, url);
886     }
887
888     /**
889      * Returns the Javadoc location for a Java project or <code>null</code> if no
890      * location is available. This location is used for all types located in the project's
891      * source folders.
892      *
893      * @param project the project
894      * @return the Javadoc location for a Java project or <code>null</code>
895      *
896      * @since 2.1
897      */

898     public static URL JavaDoc getProjectJavadocLocation(IJavaProject project) {
899         return JavaDocLocations.getProjectJavadocLocation(project);
900     }
901
902     /**
903      * Returns the Javadoc base URL for an element. The base location contains the
904      * index file. This location doesn't have to exist. Returns <code>null</code>
905      * if no javadoc location has been attached to the element's library or project.
906      * Example of a returned URL is <i>http://www.junit.org/junit/javadoc</i>.
907      *
908      * @param element the element for which the documentation URL is requested.
909      * @return the base location
910      * @throws JavaModelException thrown when the element can not be accessed
911      *
912      * @since 2.0
913      */

914     public static URL JavaDoc getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
915         return JavaDocLocations.getJavadocBaseLocation(element);
916     }
917     
918     /**
919      * Returns the Javadoc URL for an element. Example of a returned URL is
920      * <i>http://www.junit.org/junit/javadoc/junit/extensions/TestSetup.html</i>.
921      * This returned location doesn't have to exist. Returns <code>null</code>
922      * if no javadoc location has been attached to the element's library or
923      * project.
924      *
925      * @param element the element for which the documentation URL is requested.
926      * @param includeAnchor If set, the URL contains an anchor for member references:
927      * <i>http://www.junit.org/junit/javadoc/junit/extensions/TestSetup.html#run(junit.framework.TestResult)</i>. Note
928      * that this involves type resolving and is a more expensive call than without anchor.
929      * @return the Javadoc URL for the element
930      * @throws JavaModelException thrown when the element can not be accessed
931      *
932      * @since 2.0
933      */

934     public static URL JavaDoc getJavadocLocation(IJavaElement element, boolean includeAnchor) throws JavaModelException {
935         return JavaDocLocations.getJavadocLocation(element, includeAnchor);
936     }
937     
938     /**
939      * Returns the transfer instance used to copy/paste Java elements to
940      * and from the clipboard. Objects managed by this transfer instance
941      * are of type <code>IJavaElement[]</code>. So to access data from the
942      * clipboard clients should use the following code snippet:
943      * <pre>
944      * IJavaElement[] elements=
945      * (IJavaElement[])clipboard.getContents(JavaUI.getJavaElementClipboardTransfer());
946      * </pre>
947      *
948      * To put elements into the clipboard use the following snippet:
949      *
950      * <pre>
951      * IJavaElement[] javaElements= ...;
952      * clipboard.setContents(
953      * new Object[] { javaElements },
954      * new Transfer[] { JavaUI.getJavaElementClipboardTransfer() } );
955      * </pre>
956      *
957      * @return returns the transfer object used to copy/paste Java elements
958      * to and from the clipboard
959      *
960      * @since 3.0
961      */

962     public static Transfer getJavaElementClipboardTransfer() {
963         return JavaElementTransfer.getInstance();
964     }
965     
966     /**
967      * Returns the color manager the Java UI plug-in which is used to manage
968      * any Java-specific colors needed for such things like syntax highlighting.
969      *
970      * @return the color manager to be used for Java text viewers
971      * @since 3.2
972      */

973     public static IColorManager getColorManager() {
974         return JavaPlugin.getDefault().getJavaTextTools().getColorManager();
975     }
976 }
977
Popular Tags