KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > ShowInPackageViewAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui.actions;
12
13 import org.eclipse.jdt.core.IJavaElement;
14 import org.eclipse.jdt.core.IOpenable;
15 import org.eclipse.jdt.core.JavaModelException;
16 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
17 import org.eclipse.jdt.internal.ui.JavaPlugin;
18 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
19 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
20 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
21 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.ui.IWorkbenchSite;
26 import org.eclipse.ui.PlatformUI;
27 /**
28  * This action reveals the currently selected Java element in the
29  * package explorer.
30  * <p>
31  * The action is applicable to selections containing elements of type
32  * <code>IJavaElement</code>.
33  *
34  * <p>
35  * This class may be instantiated; it is not intended to be subclassed.
36  * </p>
37  * @since 2.0
38  */

39 public class ShowInPackageViewAction extends SelectionDispatchAction {
40     
41     private JavaEditor fEditor;
42     
43     /**
44      * Creates a new <code>ShowInPackageViewAction</code>. The action requires
45      * that the selection provided by the site's selection provider is of type
46      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
47      *
48      * @param site the site providing context information for this action
49      */

50     public ShowInPackageViewAction(IWorkbenchSite site) {
51         super(site);
52         setText(ActionMessages.ShowInPackageViewAction_label);
53         setDescription(ActionMessages.ShowInPackageViewAction_description);
54         setToolTipText(ActionMessages.ShowInPackageViewAction_tooltip);
55         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SHOW_IN_PACKAGEVIEW_ACTION);
56     }
57     
58     /**
59      * Note: This constructor is for internal use only. Clients should not call this constructor.
60      * @param editor the Java editor
61      */

62     public ShowInPackageViewAction(JavaEditor editor) {
63         this(editor.getEditorSite());
64         fEditor= editor;
65         setEnabled(SelectionConverter.canOperateOn(fEditor));
66     }
67     
68     /* (non-Javadoc)
69      * Method declared on SelectionDispatchAction.
70      */

71     public void selectionChanged(ITextSelection selection) {
72     }
73
74     /* (non-Javadoc)
75      * Method declared on SelectionDispatchAction.
76      */

77     public void selectionChanged(IStructuredSelection selection) {
78         setEnabled(checkEnabled(selection));
79     }
80     
81     private boolean checkEnabled(IStructuredSelection selection) {
82         if (selection.size() != 1)
83             return false;
84         return selection.getFirstElement() instanceof IJavaElement;
85     }
86     
87     /* (non-Javadoc)
88      * Method declared on SelectionDispatchAction.
89      */

90     public void run(ITextSelection selection) {
91         try {
92             IJavaElement element= SelectionConverter.getElementAtOffset(fEditor);
93             if (element != null)
94                 run(element);
95         } catch (JavaModelException e) {
96             JavaPlugin.log(e);
97             String JavaDoc message= ActionMessages.ShowInPackageViewAction_error_message;
98             ErrorDialog.openError(getShell(), getDialogTitle(), message, e.getStatus());
99         }
100     }
101     
102     /* (non-Javadoc)
103      * Method declared on SelectionDispatchAction.
104      */

105     public void run(IStructuredSelection selection) {
106         if (!checkEnabled(selection))
107             return;
108         run((IJavaElement)selection.getFirstElement());
109     }
110     
111     /*
112      * No Javadoc. The method should be internal but can't be changed since
113      * we shipped it with a public visibility
114      */

115     public void run(IJavaElement element) {
116         if (element == null)
117             return;
118             
119         // reveal the top most element only
120
IOpenable openable= element.getOpenable();
121         if (openable instanceof IJavaElement)
122             element= (IJavaElement)openable;
123
124         PackageExplorerPart view= PackageExplorerPart.openInActivePerspective();
125         view.tryToReveal(element);
126     }
127
128     private static String JavaDoc getDialogTitle() {
129         return ActionMessages.ShowInPackageViewAction_dialog_title;
130     }
131 }
132
Popular Tags