KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > OpenActionUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.internal.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.widgets.Shell;
17
18 import org.eclipse.jface.window.Window;
19
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.PartInitException;
22 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
23
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.ISourceReference;
26 import org.eclipse.jdt.core.JavaModelException;
27
28 import org.eclipse.jdt.ui.JavaElementLabelProvider;
29
30 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
31
32 public class OpenActionUtil {
33     
34     private OpenActionUtil() {
35         // no instance.
36
}
37         
38     /**
39      * Opens the editor on the given element and subsequently selects it.
40      */

41     public static void open(Object JavaDoc element) throws JavaModelException, PartInitException {
42         open(element, true);
43     }
44     
45     /**
46      * Opens the editor on the given element and subsequently selects it.
47      */

48     public static void open(Object JavaDoc element, boolean activate) throws JavaModelException, PartInitException {
49         IEditorPart part= EditorUtility.openInEditor(element, activate);
50         if (element instanceof IJavaElement)
51             EditorUtility.revealInEditor(part, (IJavaElement)element);
52     }
53     
54     /**
55      * Filters out source references from the given code resolve results.
56      * A utility method that can be called by subclasses.
57      */

58     public static List JavaDoc filterResolveResults(IJavaElement[] codeResolveResults) {
59         int nResults= codeResolveResults.length;
60         List JavaDoc refs= new ArrayList JavaDoc(nResults);
61         for (int i= 0; i < nResults; i++) {
62             if (codeResolveResults[i] instanceof ISourceReference)
63                 refs.add(codeResolveResults[i]);
64         }
65         return refs;
66     }
67                         
68     /**
69      * Shows a dialog for resolving an ambiguous java element.
70      * Utility method that can be called by subclasses.
71      */

72     public static IJavaElement selectJavaElement(IJavaElement[] elements, Shell shell, String JavaDoc title, String JavaDoc message) {
73         
74         int nResults= elements.length;
75         
76         if (nResults == 0)
77             return null;
78         
79         if (nResults == 1)
80             return elements[0];
81         
82         int flags= JavaElementLabelProvider.SHOW_DEFAULT
83                         | JavaElementLabelProvider.SHOW_QUALIFIED
84                         | JavaElementLabelProvider.SHOW_ROOT;
85                         
86         ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new JavaElementLabelProvider(flags));
87         dialog.setTitle(title);
88         dialog.setMessage(message);
89         dialog.setElements(elements);
90         
91         if (dialog.open() == Window.OK) {
92             Object JavaDoc[] selection= dialog.getResult();
93             if (selection != null && selection.length > 0) {
94                 nResults= selection.length;
95                 for (int i= 0; i < nResults; i++) {
96                     Object JavaDoc current= selection[i];
97                     if (current instanceof IJavaElement)
98                         return (IJavaElement) current;
99                 }
100             }
101         }
102         return null;
103     }
104 }
105
Popular Tags