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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
13 import java.util.ArrayList;
14 import java.util.List;
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;
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;
27 public class OpenActionUtil {
29 private OpenActionUtil() {
34 * Opens the editor on the given element and subsequently selects it.
36 public static void open(Object element) throws JavaModelException,
42 * Opens the editor on the given element and subsequently selects it.
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);
52 * Filters out source references from the given code resolve results. A
53 * utility method that can be called by subclassers.
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]);
66 * Shows a dialog for resolving an ambigous java element. Utility method
67 * that can be called by subclassers.
69 public static IJavaElement selectJavaElement(IJavaElement[] elements,
70 Shell shell, String title, String message) {
72 int nResults = elements.length;
80 int flags = JavaElementLabelProvider.SHOW_DEFAULT
81 | JavaElementLabelProvider.SHOW_QUALIFIED
82 | JavaElementLabelProvider.SHOW_ROOT;
84 ElementListSelectionDialog dialog = new ElementListSelectionDialog(
85 shell, new JavaElementLabelProvider(flags));
86 dialog.setTitle(title);
87 dialog.setMessage(message);
88 dialog.setElements(elements);
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;