Adding two small options for code formatter (zhil's patch). These features are being...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / actions / OpenActionUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.ISourceReference;
18 import net.sourceforge.phpdt.core.JavaModelException;
19 import net.sourceforge.phpdt.ui.JavaElementLabelProvider;
20 import net.sourceforge.phpeclipse.phpeditor.EditorUtility;
21
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
26
27 public class OpenActionUtil {
28
29         private OpenActionUtil() {
30                 // no instance.
31         }
32
33         /**
34          * Opens the editor on the given element and subsequently selects it.
35          */
36         public static void open(Object element) throws JavaModelException,
37                         PartInitException {
38                 open(element, true);
39         }
40
41         /**
42          * Opens the editor on the given element and subsequently selects it.
43          */
44         public static void open(Object element, boolean activate)
45                         throws JavaModelException, PartInitException {
46                 IEditorPart part = EditorUtility.openInEditor(element, activate);
47                 if (element instanceof IJavaElement)
48                         EditorUtility.revealInEditor(part, (IJavaElement) element);
49         }
50
51         /**
52          * Filters out source references from the given code resolve results. A
53          * utility method that can be called by subclassers.
54          */
55         public static List filterResolveResults(IJavaElement[] codeResolveResults) {
56                 int nResults = codeResolveResults.length;
57                 List refs = new ArrayList(nResults);
58                 for (int i = 0; i < nResults; i++) {
59                         if (codeResolveResults[i] instanceof ISourceReference)
60                                 refs.add(codeResolveResults[i]);
61                 }
62                 return refs;
63         }
64
65         /**
66          * Shows a dialog for resolving an ambigous java element. Utility method
67          * that can be called by subclassers.
68          */
69         public static IJavaElement selectJavaElement(IJavaElement[] elements,
70                         Shell shell, String title, String message) {
71
72                 int nResults = elements.length;
73
74                 if (nResults == 0)
75                         return null;
76
77                 if (nResults == 1)
78                         return elements[0];
79
80                 int flags = JavaElementLabelProvider.SHOW_DEFAULT
81                                 | JavaElementLabelProvider.SHOW_QUALIFIED
82                                 | JavaElementLabelProvider.SHOW_ROOT;
83
84                 ElementListSelectionDialog dialog = new ElementListSelectionDialog(
85                                 shell, new JavaElementLabelProvider(flags));
86                 dialog.setTitle(title);
87                 dialog.setMessage(message);
88                 dialog.setElements(elements);
89
90                 if (dialog.open() == ElementListSelectionDialog.OK) {
91                         Object[] selection = dialog.getResult();
92                         if (selection != null && selection.length > 0) {
93                                 nResults = selection.length;
94                                 for (int i = 0; i < nResults; i++) {
95                                         Object current = selection[i];
96                                         if (current instanceof IJavaElement)
97                                                 return (IJavaElement) current;
98                                 }
99                         }
100                 }
101                 return null;
102         }
103 }