001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.Collections;
015: import java.util.Comparator;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.Map;
020:
021: import org.eclipse.core.commands.Command;
022: import org.eclipse.core.commands.ExecutionException;
023: import org.eclipse.core.commands.IParameter;
024: import org.eclipse.core.commands.NotEnabledException;
025: import org.eclipse.core.commands.NotHandledException;
026: import org.eclipse.core.commands.Parameterization;
027: import org.eclipse.core.commands.ParameterizedCommand;
028: import org.eclipse.core.commands.common.NotDefinedException;
029: import org.eclipse.jface.action.Action;
030: import org.eclipse.jface.action.ContributionItem;
031: import org.eclipse.jface.action.IAction;
032: import org.eclipse.jface.action.IContributionItem;
033: import org.eclipse.jface.action.IMenuListener;
034: import org.eclipse.jface.action.IMenuManager;
035: import org.eclipse.jface.action.MenuManager;
036: import org.eclipse.jface.action.Separator;
037: import org.eclipse.swt.SWT;
038: import org.eclipse.swt.widgets.Menu;
039: import org.eclipse.swt.widgets.MenuItem;
040: import org.eclipse.ui.IWorkbenchPage;
041: import org.eclipse.ui.IWorkbenchWindow;
042: import org.eclipse.ui.activities.WorkbenchActivityHelper;
043: import org.eclipse.ui.commands.ICommandService;
044: import org.eclipse.ui.handlers.IHandlerService;
045: import org.eclipse.ui.internal.intro.IIntroConstants;
046: import org.eclipse.ui.views.IViewDescriptor;
047: import org.eclipse.ui.views.IViewRegistry;
048:
049: import com.ibm.icu.text.Collator;
050:
051: /**
052: * A <code>ShowViewMenu</code> is used to populate a menu manager with Show
053: * View actions. The visible views are determined by user preference from the
054: * Perspective Customize dialog.
055: */
056: public class ShowViewMenu extends ContributionItem {
057: private static final String SHOW_VIEW_ID = "org.eclipse.ui.views.showView"; //$NON-NLS-1$
058: private static final String PARAMETER_MAKE_FAST = "org.eclipse.ui.views.showView.makeFast"; //$NON-NLS-1$
059:
060: private IWorkbenchWindow window;
061:
062: private static final String NO_TARGETS_MSG = WorkbenchMessages.Workbench_showInNoTargets;
063:
064: private Comparator actionComparator = new Comparator() {
065: public int compare(Object o1, Object o2) {
066: if (collator == null) {
067: collator = Collator.getInstance();
068: }
069: IAction a1 = (IAction) o1;
070: IAction a2 = (IAction) o2;
071: return collator.compare(a1.getText(), a2.getText());
072: }
073: };
074:
075: private Action showDlgAction;
076:
077: private Map actions = new HashMap(21);
078:
079: // Maps pages to a list of opened views
080: private Map openedViews = new HashMap();
081:
082: protected boolean dirty = true;
083:
084: private IMenuListener menuListener = new IMenuListener() {
085: public void menuAboutToShow(IMenuManager manager) {
086: manager.markDirty();
087: dirty = true;
088: }
089: };
090:
091: private static Collator collator;
092: private boolean makeFast;
093:
094: /**
095: * Creates a Show View menu.
096: *
097: * @param window
098: * the window containing the menu
099: * @param id
100: * the id
101: */
102: public ShowViewMenu(IWorkbenchWindow window, String id) {
103: this (window, id, false);
104: }
105:
106: /**
107: * Creates a Show View menu.
108: *
109: * @param window
110: * the window containing the menu
111: * @param id
112: * the id
113: */
114: public ShowViewMenu(IWorkbenchWindow window, String id,
115: final boolean makeFast) {
116: super (id);
117: this .window = window;
118: final IHandlerService handlerService = (IHandlerService) window
119: .getService(IHandlerService.class);
120: final ICommandService commandService = (ICommandService) window
121: .getService(ICommandService.class);
122: final ParameterizedCommand cmd = getCommand(commandService,
123: makeFast);
124:
125: showDlgAction = new Action(WorkbenchMessages.ShowView_title) {
126: public void run() {
127: try {
128: handlerService.executeCommand(cmd, null);
129: } catch (final ExecutionException e) {
130: // Do nothing.
131: } catch (NotDefinedException e) {
132: // Do nothing.
133: } catch (NotEnabledException e) {
134: // Do nothing.
135: } catch (NotHandledException e) {
136: // Do nothing.
137: }
138: }
139: };
140:
141: window.getWorkbench().getHelpSystem().setHelp(showDlgAction,
142: IWorkbenchHelpContextIds.SHOW_VIEW_OTHER_ACTION);
143: // indicate that a show views submenu has been created
144: ((WorkbenchWindow) window)
145: .addSubmenu(WorkbenchWindow.SHOW_VIEW_SUBMENU);
146:
147: showDlgAction.setActionDefinitionId(SHOW_VIEW_ID);
148: this .makeFast = makeFast;
149: }
150:
151: public boolean isDirty() {
152: return dirty;
153: }
154:
155: /**
156: * Overridden to always return true and force dynamic menu building.
157: */
158: public boolean isDynamic() {
159: return true;
160: }
161:
162: /**
163: * Fills the menu with Show View actions.
164: */
165: private void fillMenu(IMenuManager innerMgr) {
166: // Remove all.
167: innerMgr.removeAll();
168:
169: // If no page disable all.
170: IWorkbenchPage page = window.getActivePage();
171: if (page == null) {
172: return;
173: }
174:
175: // If no active perspective disable all
176: if (page.getPerspective() == null) {
177: return;
178: }
179:
180: // Get visible actions.
181: List viewIds = Arrays.asList(page.getShowViewShortcuts());
182:
183: // add all open views
184: viewIds = addOpenedViews(page, viewIds);
185:
186: List actions = new ArrayList(viewIds.size());
187: for (Iterator i = viewIds.iterator(); i.hasNext();) {
188: String id = (String) i.next();
189: if (id.equals(IIntroConstants.INTRO_VIEW_ID)) {
190: continue;
191: }
192: IAction action = getAction(id);
193: if (action != null) {
194: if (WorkbenchActivityHelper.filterItem(action)) {
195: continue;
196: }
197: actions.add(action);
198: }
199: }
200: Collections.sort(actions, actionComparator);
201: for (Iterator i = actions.iterator(); i.hasNext();) {
202: innerMgr.add((IAction) i.next());
203: }
204:
205: // Add Other ..
206: innerMgr.add(new Separator());
207: innerMgr.add(showDlgAction);
208: }
209:
210: private List addOpenedViews(IWorkbenchPage page, List actions) {
211: ArrayList views = getParts(page);
212: ArrayList result = new ArrayList(views.size() + actions.size());
213:
214: for (int i = 0; i < actions.size(); i++) {
215: Object element = actions.get(i);
216: if (result.indexOf(element) < 0) {
217: result.add(element);
218: }
219: }
220: for (int i = 0; i < views.size(); i++) {
221: Object element = views.get(i);
222: if (result.indexOf(element) < 0) {
223: result.add(element);
224: }
225: }
226: return result;
227: }
228:
229: /**
230: * Returns the action for the given view id, or null if not found.
231: */
232: private IAction getAction(String id) {
233: // Keep a cache, rather than creating a new action each time,
234: // so that image caching in ActionContributionItem works.
235: IAction action = (IAction) actions.get(id);
236: if (action == null) {
237: IViewRegistry reg = WorkbenchPlugin.getDefault()
238: .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 getParts(IWorkbenchPage page) {
250: ArrayList parts = (ArrayList) openedViews.get(page);
251: if (parts == null) {
252: parts = new ArrayList();
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 viewId) {
284: actions.remove(viewId);
285: }
286:
287: /**
288: * @param commandService
289: * @param makeFast
290: */
291: private ParameterizedCommand getCommand(
292: ICommandService commandService, final boolean makeFast) {
293: Command c = commandService.getCommand(SHOW_VIEW_ID);
294: Parameterization[] parms = null;
295: if (makeFast) {
296: try {
297: IParameter parmDef = c
298: .getParameter(PARAMETER_MAKE_FAST);
299: parms = new Parameterization[] { new Parameterization(
300: 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: }
|