001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.actions;
011:
012: import org.eclipse.jdt.core.IJavaElement;
013: import org.eclipse.jdt.core.IOpenable;
014: import org.eclipse.jdt.core.JavaModelException;
015: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
016: import org.eclipse.jdt.internal.ui.JavaPlugin;
017: import org.eclipse.jdt.internal.ui.actions.ActionMessages;
018: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
019: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
020: import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
021: import org.eclipse.jface.dialogs.ErrorDialog;
022: import org.eclipse.jface.text.ITextSelection;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.ui.IWorkbenchSite;
025: import org.eclipse.ui.PlatformUI;
026:
027: /**
028: * This action reveals the currently selected Java element in the
029: * package explorer.
030: * <p>
031: * The action is applicable to selections containing elements of type
032: * <code>IJavaElement</code>.
033: *
034: * <p>
035: * This class may be instantiated; it is not intended to be subclassed.
036: * </p>
037: * @since 2.0
038: */
039: public class ShowInPackageViewAction extends SelectionDispatchAction {
040:
041: private JavaEditor fEditor;
042:
043: /**
044: * Creates a new <code>ShowInPackageViewAction</code>. The action requires
045: * that the selection provided by the site's selection provider is of type
046: * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
047: *
048: * @param site the site providing context information for this action
049: */
050: public ShowInPackageViewAction(IWorkbenchSite site) {
051: super (site);
052: setText(ActionMessages.ShowInPackageViewAction_label);
053: setDescription(ActionMessages.ShowInPackageViewAction_description);
054: setToolTipText(ActionMessages.ShowInPackageViewAction_tooltip);
055: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
056: IJavaHelpContextIds.SHOW_IN_PACKAGEVIEW_ACTION);
057: }
058:
059: /**
060: * Note: This constructor is for internal use only. Clients should not call this constructor.
061: * @param editor the Java editor
062: */
063: public ShowInPackageViewAction(JavaEditor editor) {
064: this (editor.getEditorSite());
065: fEditor = editor;
066: setEnabled(SelectionConverter.canOperateOn(fEditor));
067: }
068:
069: /* (non-Javadoc)
070: * Method declared on SelectionDispatchAction.
071: */
072: public void selectionChanged(ITextSelection selection) {
073: }
074:
075: /* (non-Javadoc)
076: * Method declared on SelectionDispatchAction.
077: */
078: public void selectionChanged(IStructuredSelection selection) {
079: setEnabled(checkEnabled(selection));
080: }
081:
082: private boolean checkEnabled(IStructuredSelection selection) {
083: if (selection.size() != 1)
084: return false;
085: return selection.getFirstElement() instanceof IJavaElement;
086: }
087:
088: /* (non-Javadoc)
089: * Method declared on SelectionDispatchAction.
090: */
091: public void run(ITextSelection selection) {
092: try {
093: IJavaElement element = SelectionConverter
094: .getElementAtOffset(fEditor);
095: if (element != null)
096: run(element);
097: } catch (JavaModelException e) {
098: JavaPlugin.log(e);
099: String message = ActionMessages.ShowInPackageViewAction_error_message;
100: ErrorDialog.openError(getShell(), getDialogTitle(),
101: message, e.getStatus());
102: }
103: }
104:
105: /* (non-Javadoc)
106: * Method declared on SelectionDispatchAction.
107: */
108: public void run(IStructuredSelection selection) {
109: if (!checkEnabled(selection))
110: return;
111: run((IJavaElement) selection.getFirstElement());
112: }
113:
114: /*
115: * No Javadoc. The method should be internal but can't be changed since
116: * we shipped it with a public visibility
117: */
118: public void run(IJavaElement element) {
119: if (element == null)
120: return;
121:
122: // reveal the top most element only
123: IOpenable openable = element.getOpenable();
124: if (openable instanceof IJavaElement)
125: element = (IJavaElement) openable;
126:
127: PackageExplorerPart view = PackageExplorerPart
128: .openInActivePerspective();
129: view.tryToReveal(element);
130: }
131:
132: private static String getDialogTitle() {
133: return ActionMessages.ShowInPackageViewAction_dialog_title;
134: }
135: }
|