Optimized Scanner
[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, PartInitException {
37                 open(element, true);
38         }
39         
40         /**
41          * Opens the editor on the given element and subsequently selects it.
42          */
43         public static void open(Object element, boolean activate) throws JavaModelException, PartInitException {
44                 IEditorPart part= EditorUtility.openInEditor(element, activate);
45                 if (element instanceof IJavaElement)
46                         EditorUtility.revealInEditor(part, (IJavaElement)element);
47         }
48         
49         /**
50          * Filters out source references from the given code resolve results.
51          * A utility method that can be called by subclassers. 
52          */
53         public static List filterResolveResults(IJavaElement[] codeResolveResults) {
54                 int nResults= codeResolveResults.length;
55                 List refs= new ArrayList(nResults);
56                 for (int i= 0; i < nResults; i++) {
57                         if (codeResolveResults[i] instanceof ISourceReference)
58                                 refs.add(codeResolveResults[i]);
59                 }
60                 return refs;
61         }
62                                                 
63         /**
64          * Shows a dialog for resolving an ambigous java element.
65          * Utility method that can be called by subclassers.
66          */
67         public static IJavaElement selectJavaElement(IJavaElement[] elements, Shell shell, String title, String message) {
68                 
69                 int nResults= elements.length;
70                 
71                 if (nResults == 0)
72                         return null;
73                 
74                 if (nResults == 1)
75                         return elements[0];
76                 
77                 int flags= JavaElementLabelProvider.SHOW_DEFAULT
78                                                 | JavaElementLabelProvider.SHOW_QUALIFIED
79                                                 | JavaElementLabelProvider.SHOW_ROOT;
80                                                 
81                 ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new JavaElementLabelProvider(flags));
82                 dialog.setTitle(title);
83                 dialog.setMessage(message);
84                 dialog.setElements(elements);
85                 
86                 if (dialog.open() == ElementListSelectionDialog.OK) {
87                         Object[] selection= dialog.getResult();
88                         if (selection != null && selection.length > 0) {
89                                 nResults= selection.length;
90                                 for (int i= 0; i < nResults; i++) {
91                                         Object current= selection[i];
92                                         if (current instanceof IJavaElement)
93                                                 return (IJavaElement) current;
94                                 }
95                         }
96                 }               
97                 return null;
98         }       
99 }