KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.Arrays JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.core.commands.Command;
24 import org.eclipse.core.commands.ExecutionException;
25 import org.eclipse.core.commands.IParameter;
26 import org.eclipse.core.commands.NotEnabledException;
27 import org.eclipse.core.commands.NotHandledException;
28 import org.eclipse.core.commands.Parameterization;
29 import org.eclipse.core.commands.ParameterizedCommand;
30 import org.eclipse.core.commands.common.NotDefinedException;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.action.ContributionItem;
33 import org.eclipse.jface.action.IAction;
34 import org.eclipse.jface.action.IContributionItem;
35 import org.eclipse.jface.action.IMenuListener;
36 import org.eclipse.jface.action.IMenuManager;
37 import org.eclipse.jface.action.MenuManager;
38 import org.eclipse.jface.action.Separator;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.widgets.Menu;
41 import org.eclipse.swt.widgets.MenuItem;
42 import org.eclipse.ui.IWorkbenchPage;
43 import org.eclipse.ui.IWorkbenchWindow;
44 import org.eclipse.ui.activities.WorkbenchActivityHelper;
45 import org.eclipse.ui.commands.ICommandService;
46 import org.eclipse.ui.handlers.IHandlerService;
47 import org.eclipse.ui.internal.intro.IIntroConstants;
48 import org.eclipse.ui.views.IViewDescriptor;
49 import org.eclipse.ui.views.IViewRegistry;
50
51 import com.ibm.icu.text.Collator;
52
53 /**
54  * A <code>ShowViewMenu</code> is used to populate a menu manager with Show
55  * View actions. The visible views are determined by user preference from the
56  * Perspective Customize dialog.
57  */

58 public class ShowViewMenu extends ContributionItem {
59     private static final String JavaDoc SHOW_VIEW_ID = "org.eclipse.ui.views.showView"; //$NON-NLS-1$
60
private static final String JavaDoc PARAMETER_MAKE_FAST = "org.eclipse.ui.views.showView.makeFast"; //$NON-NLS-1$
61

62     private IWorkbenchWindow window;
63
64     private static final String JavaDoc NO_TARGETS_MSG = WorkbenchMessages.Workbench_showInNoTargets;
65
66     private Comparator JavaDoc actionComparator = new Comparator JavaDoc() {
67         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
68             if (collator == null) {
69                 collator = Collator.getInstance();
70             }
71             IAction a1 = (IAction) o1;
72             IAction a2 = (IAction) o2;
73             return collator.compare(a1.getText(), a2.getText());
74         }
75     };
76
77     private Action showDlgAction;
78
79     private Map JavaDoc actions = new HashMap JavaDoc(21);
80
81     // Maps pages to a list of opened views
82
private Map JavaDoc openedViews = new HashMap JavaDoc();
83
84     protected boolean dirty = true;
85
86     private IMenuListener menuListener = new IMenuListener() {
87         public void menuAboutToShow(IMenuManager manager) {
88             manager.markDirty();
89             dirty = true;
90         }
91     };
92
93     private static Collator collator;
94     private boolean makeFast;
95
96     /**
97      * Creates a Show View menu.
98      *
99      * @param window
100      * the window containing the menu
101      * @param id
102      * the id
103      */

104     public ShowViewMenu(IWorkbenchWindow window, String JavaDoc id) {
105         this(window, id, false);
106     }
107     
108     /**
109      * Creates a Show View menu.
110      *
111      * @param window
112      * the window containing the menu
113      * @param id
114      * the id
115      */

116     public ShowViewMenu(IWorkbenchWindow window, String JavaDoc id, final boolean makeFast) {
117         super(id);
118         this.window = window;
119         final IHandlerService handlerService = (IHandlerService) window
120                 .getService(IHandlerService.class);
121         final ICommandService commandService = (ICommandService) window
122                 .getService(ICommandService.class);
123         final ParameterizedCommand cmd = getCommand(commandService, makeFast);
124         
125         showDlgAction = new Action(WorkbenchMessages.ShowView_title) {
126             public void run() {
127                 try {
128                     handlerService.executeCommand(
129                             cmd, null);
130                 } catch (final ExecutionException e) {
131                     // Do nothing.
132
} catch (NotDefinedException e) {
133                     // Do nothing.
134
} catch (NotEnabledException e) {
135                     // Do nothing.
136
} catch (NotHandledException e) {
137                     // Do nothing.
138
}
139             }
140         };
141         
142         window.getWorkbench().getHelpSystem().setHelp(showDlgAction,
143                 IWorkbenchHelpContextIds.SHOW_VIEW_OTHER_ACTION);
144         // indicate that a show views submenu has been created
145
((WorkbenchWindow) window)
146                 .addSubmenu(WorkbenchWindow.SHOW_VIEW_SUBMENU);
147         
148         showDlgAction.setActionDefinitionId(SHOW_VIEW_ID);
149         this.makeFast = makeFast;
150     }
151
152     public boolean isDirty() {
153         return dirty;
154     }
155
156     /**
157      * Overridden to always return true and force dynamic menu building.
158      */

159     public boolean isDynamic() {
160         return true;
161     }
162
163     /**
164      * Fills the menu with Show View actions.
165      */

166     private void fillMenu(IMenuManager innerMgr) {
167         // Remove all.
168
innerMgr.removeAll();
169
170         // If no page disable all.
171
IWorkbenchPage page = window.getActivePage();
172         if (page == null) {
173             return;
174         }
175
176         // If no active perspective disable all
177
if (page.getPerspective() == null) {
178             return;
179         }
180
181         // Get visible actions.
182
List JavaDoc viewIds = Arrays.asList(page.getShowViewShortcuts());
183
184         // add all open views
185
viewIds = addOpenedViews(page, viewIds);
186
187         List JavaDoc actions = new ArrayList JavaDoc(viewIds.size());
188         for (Iterator JavaDoc i = viewIds.iterator(); i.hasNext();) {
189             String JavaDoc id = (String JavaDoc) i.next();
190             if (id.equals(IIntroConstants.INTRO_VIEW_ID)) {
191                 continue;
192             }
193             IAction action = getAction(id);
194             if (action != null) {
195                 if (WorkbenchActivityHelper.filterItem(action)) {
196                     continue;
197                 }
198                 actions.add(action);
199             }
200         }
201         Collections.sort(actions, actionComparator);
202         for (Iterator JavaDoc i = actions.iterator(); i.hasNext();) {
203             innerMgr.add((IAction) i.next());
204         }
205
206         // Add Other ..
207
innerMgr.add(new Separator());
208         innerMgr.add(showDlgAction);
209     }
210
211     private List JavaDoc addOpenedViews(IWorkbenchPage page, List JavaDoc actions) {
212         ArrayList JavaDoc views = getParts(page);
213         ArrayList JavaDoc result = new ArrayList JavaDoc(views.size() + actions.size());
214
215         for (int i = 0; i < actions.size(); i++) {
216             Object JavaDoc element = actions.get(i);
217             if (result.indexOf(element) < 0) {
218                 result.add(element);
219             }
220         }
221         for (int i = 0; i < views.size(); i++) {
222             Object JavaDoc element = views.get(i);
223             if (result.indexOf(element) < 0) {
224                 result.add(element);
225             }
226         }
227         return result;
228     }
229
230     /**
231      * Returns the action for the given view id, or null if not found.
232      */

233     private IAction getAction(String JavaDoc id) {
234         // Keep a cache, rather than creating a new action each time,
235
// so that image caching in ActionContributionItem works.
236
IAction action = (IAction) actions.get(id);
237         if (action == null) {
238             IViewRegistry reg = WorkbenchPlugin.getDefault().getViewRegistry();
239             IViewDescriptor desc = reg.find(id);
240             if (desc != null) {
241                 action = new ShowViewAction(window, desc, makeFast);
242                 action.setActionDefinitionId(id);
243                 actions.put(id, action);
244             }
245         }
246         return action;
247     }
248
249     private ArrayList JavaDoc getParts(IWorkbenchPage page) {
250         ArrayList JavaDoc parts = (ArrayList JavaDoc) openedViews.get(page);
251         if (parts == null) {
252             parts = new ArrayList JavaDoc();
253             openedViews.put(page, parts);
254         }
255         return parts;
256     }
257
258     public void fill(Menu menu, int index) {
259         if (getParent() instanceof MenuManager) {
260             ((MenuManager) getParent()).addMenuListener(menuListener);
261         }
262
263         if (!dirty) {
264             return;
265         }
266
267         MenuManager manager = new MenuManager();
268         fillMenu(manager);
269         IContributionItem items[] = manager.getItems();
270         if (items.length == 0) {
271             MenuItem item = new MenuItem(menu, SWT.NONE, index++);
272             item.setText(NO_TARGETS_MSG);
273             item.setEnabled(false);
274         } else {
275             for (int i = 0; i < items.length; i++) {
276                 items[i].fill(menu, index++);
277             }
278         }
279         dirty = false;
280     }
281
282     // for dynamic UI
283
protected void removeAction(String JavaDoc viewId) {
284         actions.remove(viewId);
285     }
286
287
288     /**
289      * @param commandService
290      * @param makeFast
291      */

292     private ParameterizedCommand getCommand(ICommandService commandService,
293             final boolean makeFast) {
294         Command c = commandService.getCommand(SHOW_VIEW_ID);
295         Parameterization[] parms = null;
296         if (makeFast) {
297             try {
298                 IParameter parmDef = c.getParameter(PARAMETER_MAKE_FAST);
299                 parms = new Parameterization[] {
300                         new Parameterization(parmDef, "true") //$NON-NLS-1$
301
};
302             } catch (NotDefinedException e) {
303                 // this should never happen
304
}
305         }
306         return new ParameterizedCommand(c, parms);
307     }
308 }
309
Popular Tags