001: /*******************************************************************************
002: * Copyright (c) 2006 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.actions;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Map;
017:
018: import org.eclipse.jface.action.IAction;
019: import org.eclipse.jface.viewers.ISelection;
020: import org.eclipse.swt.events.MenuAdapter;
021: import org.eclipse.swt.events.MenuEvent;
022: import org.eclipse.swt.widgets.Control;
023: import org.eclipse.swt.widgets.Menu;
024: import org.eclipse.swt.widgets.MenuItem;
025: import org.eclipse.ui.IWorkbenchWindow;
026: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
027: import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
028: import org.eclipse.ui.IWorkingSet;
029: import org.eclipse.ui.activities.WorkbenchActivityHelper;
030: import org.eclipse.ui.internal.WorkbenchPlugin;
031: import org.eclipse.ui.internal.registry.WorkingSetRegistry;
032:
033: /**
034: * Baseclass for working set pulldown actions.
035: *
036: * @since 3.3
037: */
038: public abstract class AbstractWorkingSetPulldownDelegate implements
039: IWorkbenchWindowActionDelegate,
040: IWorkbenchWindowPulldownDelegate2 {
041:
042: private Menu menubarMenu;
043:
044: private Menu toolbarMenu;
045:
046: private ISelection selection;
047:
048: private IWorkbenchWindow window;
049:
050: /**
051: *
052: */
053: public AbstractWorkingSetPulldownDelegate() {
054: super ();
055: }
056:
057: public void dispose() {
058: if (menubarMenu != null) {
059: menubarMenu.dispose();
060: menubarMenu = null;
061: }
062: if (toolbarMenu != null) {
063: toolbarMenu.dispose();
064: toolbarMenu = null;
065: }
066: }
067:
068: public Menu getMenu(Control parent) {
069: if (toolbarMenu != null) {
070: toolbarMenu.dispose();
071: }
072: toolbarMenu = new Menu(parent);
073: initMenu(toolbarMenu);
074: return toolbarMenu;
075: }
076:
077: public Menu getMenu(Menu parent) {
078: if (menubarMenu != null) {
079: menubarMenu.dispose();
080: }
081: menubarMenu = new Menu(parent);
082: initMenu(menubarMenu);
083: return menubarMenu;
084: }
085:
086: /**
087: * Creates the menu for the action
088: */
089: private void initMenu(Menu menu) {
090: menu.addMenuListener(new MenuAdapter() {
091: public void menuShown(MenuEvent e) {
092: Menu m = (Menu) e.widget;
093: MenuItem[] items = m.getItems();
094: for (int i = 0; i < items.length; i++) {
095: items[i].dispose();
096: }
097: fillMenu(m);
098: }
099:
100: });
101: }
102:
103: /**
104: * @param m
105: */
106: protected abstract void fillMenu(Menu m);
107:
108: /**
109: * Split the working sets known by the manager into arrays based on their
110: * defining page Id.
111: *
112: * @return an array of arrays
113: */
114: protected IWorkingSet[][] splitSets() {
115: IWorkingSet[] allSets = getWindow().getWorkbench()
116: .getWorkingSetManager().getWorkingSets();
117:
118: Map map = new HashMap();
119: WorkingSetRegistry registry = WorkbenchPlugin.getDefault()
120: .getWorkingSetRegistry();
121:
122: for (int i = 0; i < allSets.length; i++) {
123: String setType = allSets[i].getId();
124: if (WorkbenchActivityHelper.filterItem(registry
125: .getWorkingSetDescriptor(setType))) {
126: continue;
127: }
128: List setsOfType = (List) map.get(setType);
129: if (setsOfType == null) {
130: setsOfType = new ArrayList();
131: map.put(setType, setsOfType);
132: }
133: setsOfType.add(allSets[i]);
134: }
135:
136: IWorkingSet[][] typedSets = new IWorkingSet[map.keySet().size()][];
137: int i = 0;
138: for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
139: List setsOfType = (List) map.get(iter.next());
140: typedSets[i] = new IWorkingSet[setsOfType.size()];
141: setsOfType.toArray(typedSets[i++]);
142: }
143: return typedSets;
144: }
145:
146: public void init(IWorkbenchWindow window) {
147: this .window = window;
148: }
149:
150: protected IWorkbenchWindow getWindow() {
151: return window;
152: }
153:
154: public void selectionChanged(IAction action, ISelection selection) {
155: this .selection = selection;
156: }
157:
158: protected ISelection getSelection() {
159: return selection;
160: }
161: }
|