KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ShowInAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.ui.internal;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.ui.IEditorPart;
18 import org.eclipse.ui.IViewPart;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.internal.util.Util;
24 import org.eclipse.ui.part.IShowInSource;
25 import org.eclipse.ui.part.IShowInTarget;
26 import org.eclipse.ui.part.ShowInContext;
27 import org.eclipse.ui.views.IViewDescriptor;
28
29 /**
30  * Action for a particular target in the Show In menu.
31  */

32 public class ShowInAction extends Action {
33     private IWorkbenchWindow window;
34
35     private IViewDescriptor desc;
36
37     /**
38      * Creates a new <code>ShowInAction</code>.
39      */

40     protected ShowInAction(IWorkbenchWindow window, IViewDescriptor desc) {
41         super(desc.getLabel());
42         setImageDescriptor(desc.getImageDescriptor());
43         window.getWorkbench().getHelpSystem().setHelp(this,
44                 IWorkbenchHelpContextIds.SHOW_IN_ACTION);
45         this.window = window;
46         this.desc = desc;
47     }
48
49     /**
50      * Shows the current context in this action's view.
51      */

52     public void run() {
53         IWorkbenchPage page = window.getActivePage();
54         if (page == null) {
55             beep();
56             return;
57         }
58
59         IWorkbenchPart sourcePart = page.getActivePart();
60         if (sourcePart == null) {
61             beep();
62             return;
63         }
64
65         ShowInContext context = getContext(sourcePart);
66         if (context == null) {
67             beep();
68             return;
69         }
70
71         try {
72             IViewPart view = page.showView(desc.getId());
73             IShowInTarget target = getShowInTarget(view);
74             if (target != null && target.show(context)) {
75                 // success
76
} else {
77                 beep();
78             }
79             ((WorkbenchPage) page).performedShowIn(desc.getId()); // TODO: move back up
80
} catch (PartInitException e) {
81             WorkbenchPlugin.log(
82                     "Error showing view in ShowInAction.run", e.getStatus()); //$NON-NLS-1$
83
}
84     }
85
86     /**
87      * Returns the <code>IShowInSource</code> provided by the source part,
88      * or <code>null</code> if it does not provide one.
89      *
90      * @param sourcePart the source part
91      * @return an <code>IShowInSource</code> or <code>null</code>
92      */

93     private IShowInSource getShowInSource(IWorkbenchPart sourcePart) {
94         return (IShowInSource)Util.getAdapter(sourcePart, IShowInSource.class);
95     }
96
97     /**
98      * Returns the <code>IShowInTarget</code> for the given part,
99      * or <code>null</code> if it does not provide one.
100      *
101      * @param targetPart the target part
102      * @return the <code>IShowInTarget</code> or <code>null</code>
103      */

104     private IShowInTarget getShowInTarget(IWorkbenchPart targetPart) {
105         return (IShowInTarget)Util.getAdapter(targetPart, IShowInTarget.class);
106     }
107
108     /**
109      * Returns the <code>ShowInContext</code> to show in the selected target,
110      * or <code>null</code> if there is no valid context to show.
111      * <p>
112      * This implementation obtains the context from the <code>IShowInSource</code>
113      * of the source part (if provided), or, if the source part is an editor,
114      * it creates the context from the editor's input and selection.
115      * <p>
116      * Subclasses may extend or reimplement.
117      *
118      * @return the <code>ShowInContext</code> to show or <code>null</code>
119      */

120     private ShowInContext getContext(IWorkbenchPart sourcePart) {
121         IShowInSource source = getShowInSource(sourcePart);
122         if (source != null) {
123             ShowInContext context = source.getShowInContext();
124             if (context != null) {
125                 return context;
126             }
127         } else if (sourcePart instanceof IEditorPart) {
128             Object JavaDoc input = ((IEditorPart) sourcePart).getEditorInput();
129             ISelectionProvider sp = sourcePart.getSite().getSelectionProvider();
130             ISelection sel = sp == null ? null : sp.getSelection();
131             return new ShowInContext(input, sel);
132         }
133         return null;
134     }
135
136     /**
137      * Generates a system beep.
138      */

139     private void beep() {
140         window.getShell().getDisplay().beep();
141     }
142
143 }
144
Popular Tags