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, PartInitException {
41 * Opens the editor on the given element and subsequently selects it.
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);
50 * Filters out source references from the given code resolve results.
51 * A utility method that can be called by subclassers.
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]);
64 * Shows a dialog for resolving an ambigous java element.
65 * Utility method that can be called by subclassers.
67 public static IJavaElement selectJavaElement(IJavaElement[] elements, Shell shell, String title, String message) {
69 int nResults= elements.length;
77 int flags= JavaElementLabelProvider.SHOW_DEFAULT
78 | JavaElementLabelProvider.SHOW_QUALIFIED
79 | JavaElementLabelProvider.SHOW_ROOT;
81 ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new JavaElementLabelProvider(flags));
82 dialog.setTitle(title);
83 dialog.setMessage(message);
84 dialog.setElements(elements);
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;