KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > snippeteditor > 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  * Sebastian Davids <sdavids@gmx.de> - initial API and implementation
10  * IBM Corporation - bug fixes
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.debug.ui.snippeteditor;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
16 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IElementComparer;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.ui.PlatformUI;
23
24 /**
25  * This action reveals the snippet editor in the package explorer.
26  *
27  * <p>
28  * This class may be instantiated; it is not intended to be subclassed.
29  * </p>
30  * @since 3.0
31  */

32 public class ShowInPackageViewAction extends Action {
33     
34     private JavaSnippetEditor fEditor;
35     
36     /**
37      * Creates a new <code>ShowInPackageViewAction</code>.
38      *
39      * @param site the site providing context information for this action
40      */

41     public ShowInPackageViewAction() {
42         super(SnippetMessages.getString("ShowInPackageViewAction.label")); //$NON-NLS-1$
43
setDescription(SnippetMessages.getString("ShowInPackageViewAction.description")); //$NON-NLS-1$
44
setToolTipText(SnippetMessages.getString("ShowInPackageViewAction.tooltip")); //$NON-NLS-1$
45
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SHOW_IN_PACKAGEVIEW_ACTION);
46     }
47     
48     /**
49      * Note: This constructor is for internal use only. Clients should not call this constructor.
50      */

51     public ShowInPackageViewAction(JavaSnippetEditor editor) {
52         this();
53         fEditor= editor;
54     }
55     
56     /* (non-Javadoc)
57      * @see org.eclipse.jface.action.Action#run()
58      */

59     public void run() {
60         IFile file= fEditor.getFile();
61         if (file == null) {
62             return;
63         }
64         PackageExplorerPart view= PackageExplorerPart.openInActivePerspective();
65         if (!reveal(view, file)) {
66             MessageDialog.openInformation(fEditor.getShell(), getDialogTitle(), SnippetMessages.getString("ShowInPackageViewAction.not_found")); //$NON-NLS-1$
67
}
68     }
69
70     private boolean reveal(PackageExplorerPart view, Object JavaDoc element) {
71         if (view == null) {
72             return false;
73         }
74         view.selectReveal(new StructuredSelection(element));
75         IElementComparer comparer= view.getTreeViewer().getComparer();
76         Object JavaDoc selected= getSelectedElement(view);
77         if (comparer != null ? comparer.equals(element, selected) : element.equals(selected)) {
78             return true;
79         }
80         return false;
81     }
82
83     private Object JavaDoc getSelectedElement(PackageExplorerPart view) {
84         return ((IStructuredSelection) view.getSite().getSelectionProvider().getSelection()).getFirstElement();
85     }
86     
87     private static String JavaDoc getDialogTitle() {
88         return SnippetMessages.getString("ShowInPackageViewAction.dialog.title"); //$NON-NLS-1$
89
}
90 }
91
Popular Tags