KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.jface.action.ContributionItem;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.action.IContributionItem;
22 import org.eclipse.jface.action.IMenuListener;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.MenuManager;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionProvider;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.widgets.Menu;
29 import org.eclipse.swt.widgets.MenuItem;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.IWorkbenchPart;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.internal.util.Util;
35 import org.eclipse.ui.part.IShowInSource;
36 import org.eclipse.ui.part.IShowInTargetList;
37 import org.eclipse.ui.part.ShowInContext;
38 import org.eclipse.ui.views.IViewDescriptor;
39 import org.eclipse.ui.views.IViewRegistry;
40
41 /**
42  * A <code>ShowInMenu</code> is used to populate a menu manager with
43  * Show In actions. The items to show are determined from the active perspective
44  * and active part.
45  */

46 public class ShowInMenu extends ContributionItem {
47
48     private static final String JavaDoc NO_TARGETS_MSG = WorkbenchMessages.Workbench_showInNoTargets;
49
50     private IWorkbenchWindow window;
51
52     private Map JavaDoc actions = new HashMap JavaDoc(21);
53
54     private boolean dirty = true;
55
56     private IMenuListener menuListener = new IMenuListener() {
57         public void menuAboutToShow(IMenuManager manager) {
58             manager.markDirty();
59             dirty = true;
60         }
61     };
62
63     /**
64      * Creates a Show In menu.
65      *
66      * @param window the window containing the menu
67      */

68     public ShowInMenu(IWorkbenchWindow window, String JavaDoc id) {
69         super(id);
70         this.window = window;
71     }
72
73     protected IWorkbenchWindow getWindow() {
74         return window;
75     }
76
77     public boolean isDirty() {
78         return dirty;
79     }
80
81     /**
82      * Overridden to always return true and force dynamic menu building.
83      */

84     public boolean isDynamic() {
85         return true;
86     }
87
88     public void fill(Menu menu, int index) {
89         if (getParent() instanceof MenuManager) {
90             ((MenuManager) getParent()).addMenuListener(menuListener);
91         }
92
93         if (!dirty) {
94             return;
95         }
96
97         MenuManager manager = new MenuManager();
98         fillMenu(manager);
99         IContributionItem[] items = manager.getItems();
100         if (items.length == 0) {
101             MenuItem item = new MenuItem(menu, SWT.NONE, index++);
102             item.setText(NO_TARGETS_MSG);
103             item.setEnabled(false);
104         } else {
105             for (int i = 0; i < items.length; i++) {
106                 items[i].fill(menu, index++);
107             }
108         }
109         dirty = false;
110     }
111
112     /**
113      * Fills the menu with Show In actions.
114      */

115     private void fillMenu(IMenuManager innerMgr) {
116         // Remove all.
117
innerMgr.removeAll();
118
119         IWorkbenchPart sourcePart = getSourcePart();
120         if (sourcePart == null) {
121             return;
122         }
123         ShowInContext context = getContext(sourcePart);
124         if (context == null) {
125             return;
126         }
127         if (context.getInput() == null
128                 && (context.getSelection() == null || context.getSelection()
129                         .isEmpty())) {
130             return;
131         }
132
133         IViewDescriptor[] viewDescs = getViewDescriptors(sourcePart);
134         if (viewDescs.length == 0) {
135             return;
136         }
137
138         for (int i = 0; i < viewDescs.length; ++i) {
139             IAction action = getAction(viewDescs[i]);
140             if (action != null) {
141                 innerMgr.add(action);
142             }
143         }
144     }
145
146     /**
147      * Returns the action for the given view id, or null if not found.
148      */

149     private IAction getAction(IViewDescriptor desc) {
150         // Keep a cache, rather than creating a new action each time,
151
// so that image caching in ActionContributionItem works.
152
IAction action = (IAction) actions.get(desc.getId());
153         if (action == null) {
154             if (desc != null) {
155                 action = new ShowInAction(window, desc);
156                 actions.put(desc.getId(), action);
157             }
158         }
159         return action;
160     }
161
162     /**
163      * Returns the Show In... target part ids for the given source part.
164      * Merges the contributions from the current perspective and the source part.
165      */

166     private ArrayList JavaDoc getShowInPartIds(IWorkbenchPart sourcePart) {
167         ArrayList JavaDoc targetIds = new ArrayList JavaDoc();
168         WorkbenchPage page = (WorkbenchPage) getWindow().getActivePage();
169         if (page != null) {
170             targetIds.addAll(page.getShowInPartIds());
171         }
172         IShowInTargetList targetList = getShowInTargetList(sourcePart);
173         if (targetList != null) {
174             String JavaDoc[] partIds = targetList.getShowInTargetIds();
175             if (partIds != null) {
176                 for (int i = 0; i < partIds.length; ++i) {
177                     if (!targetIds.contains(partIds[i])) {
178                         targetIds.add(partIds[i]);
179                     }
180                 }
181             }
182         }
183         page.sortShowInPartIds(targetIds);
184         return targetIds;
185     }
186
187     /**
188      * Returns the source part, or <code>null</code> if there is no applicable
189      * source part
190      * <p>
191      * This implementation returns the current part in the window.
192      * Subclasses may extend or reimplement.
193      *
194      * @return the source part or <code>null</code>
195      */

196     private IWorkbenchPart getSourcePart() {
197         IWorkbenchPage page = getWindow().getActivePage();
198         if (page != null) {
199             return page.getActivePart();
200         }
201         return null;
202     }
203
204     /**
205      * Returns the <code>IShowInSource</code> provided by the source part,
206      * or <code>null</code> if it does not provide one.
207      *
208      * @param sourcePart the source part
209      * @return an <code>IShowInSource</code> or <code>null</code>
210      */

211     private IShowInSource getShowInSource(IWorkbenchPart sourcePart) {
212         return (IShowInSource)Util.getAdapter(sourcePart, IShowInSource.class);
213     }
214
215     /**
216      * Returns the <code>IShowInTargetList</code> for the given source part,
217      * or <code>null</code> if it does not provide one.
218      *
219      * @param sourcePart the source part
220      * @return the <code>IShowInTargetList</code> or <code>null</code>
221      */

222     private IShowInTargetList getShowInTargetList(IWorkbenchPart sourcePart) {
223         return (IShowInTargetList)Util.getAdapter(sourcePart, IShowInTargetList.class);
224     }
225
226     /**
227      * Returns the <code>ShowInContext</code> to show in the selected target,
228      * or <code>null</code> if there is no valid context to show.
229      * <p>
230      * This implementation obtains the context from the <code>IShowInSource</code>
231      * of the source part (if provided), or, if the source part is an editor,
232      * it creates the context from the editor's input and selection.
233      * <p>
234      * Subclasses may extend or reimplement.
235      *
236      * @return the <code>ShowInContext</code> to show or <code>null</code>
237      */

238     private ShowInContext getContext(IWorkbenchPart sourcePart) {
239         IShowInSource source = getShowInSource(sourcePart);
240         if (source != null) {
241             ShowInContext context = source.getShowInContext();
242             if (context != null) {
243                 return context;
244             }
245         } else if (sourcePart instanceof IEditorPart) {
246             Object JavaDoc input = ((IEditorPart) sourcePart).getEditorInput();
247             ISelectionProvider sp = sourcePart.getSite().getSelectionProvider();
248             ISelection sel = sp == null ? null : sp.getSelection();
249             return new ShowInContext(input, sel);
250         }
251         return null;
252     }
253
254     /**
255      * Returns the view descriptors to show in the dialog.
256      */

257     private IViewDescriptor[] getViewDescriptors(IWorkbenchPart sourcePart) {
258         String JavaDoc srcId = sourcePart.getSite().getId();
259         ArrayList JavaDoc ids = getShowInPartIds(sourcePart);
260         ArrayList JavaDoc descs = new ArrayList JavaDoc();
261         IViewRegistry reg = WorkbenchPlugin.getDefault().getViewRegistry();
262         for (Iterator JavaDoc i = ids.iterator(); i.hasNext();) {
263             String JavaDoc id = (String JavaDoc) i.next();
264             if (!id.equals(srcId)) {
265                 IViewDescriptor desc = reg.find(id);
266                 if (desc != null) {
267                     descs.add(desc);
268                 }
269             }
270         }
271         return (IViewDescriptor[]) descs.toArray(new IViewDescriptor[descs
272                 .size()]);
273     }
274
275 }
276
Popular Tags