KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > actions > AbstractWorkingSetPulldownDelegate


1 /*******************************************************************************
2  * Copyright (c) 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.actions;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.swt.events.MenuAdapter;
23 import org.eclipse.swt.events.MenuEvent;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.swt.widgets.MenuItem;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
29 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
30 import org.eclipse.ui.IWorkingSet;
31 import org.eclipse.ui.activities.WorkbenchActivityHelper;
32 import org.eclipse.ui.internal.WorkbenchPlugin;
33 import org.eclipse.ui.internal.registry.WorkingSetRegistry;
34
35 /**
36  * Baseclass for working set pulldown actions.
37  *
38  * @since 3.3
39  */

40 public abstract class AbstractWorkingSetPulldownDelegate implements
41         IWorkbenchWindowActionDelegate, IWorkbenchWindowPulldownDelegate2 {
42
43     private Menu menubarMenu;
44
45     private Menu toolbarMenu;
46
47     private ISelection selection;
48
49     private IWorkbenchWindow window;
50
51     /**
52      *
53      */

54     public AbstractWorkingSetPulldownDelegate() {
55         super();
56     }
57
58     public void dispose() {
59         if (menubarMenu != null) {
60             menubarMenu.dispose();
61             menubarMenu = null;
62         }
63         if (toolbarMenu != null) {
64             toolbarMenu.dispose();
65             toolbarMenu = null;
66         }
67     }
68
69     public Menu getMenu(Control parent) {
70         if (toolbarMenu != null) {
71             toolbarMenu.dispose();
72         }
73         toolbarMenu = new Menu(parent);
74         initMenu(toolbarMenu);
75         return toolbarMenu;
76     }
77
78     public Menu getMenu(Menu parent) {
79         if (menubarMenu != null) {
80             menubarMenu.dispose();
81         }
82         menubarMenu = new Menu(parent);
83         initMenu(menubarMenu);
84         return menubarMenu;
85     }
86
87     /**
88      * Creates the menu for the action
89      */

90     private void initMenu(Menu menu) {
91         menu.addMenuListener(new MenuAdapter() {
92             public void menuShown(MenuEvent e) {
93                 Menu m = (Menu) e.widget;
94                 MenuItem[] items = m.getItems();
95                 for (int i = 0; i < items.length; i++) {
96                     items[i].dispose();
97                 }
98                 fillMenu(m);
99             }
100
101         });
102     }
103
104     /**
105      * @param m
106      */

107     protected abstract void fillMenu(Menu m);
108
109     /**
110      * Split the working sets known by the manager into arrays based on their
111      * defining page Id.
112      *
113      * @return an array of arrays
114      */

115     protected IWorkingSet[][] splitSets() {
116         IWorkingSet[] allSets = getWindow().getWorkbench().getWorkingSetManager()
117                 .getWorkingSets();
118
119         Map JavaDoc map = new HashMap JavaDoc();
120         WorkingSetRegistry registry = WorkbenchPlugin.getDefault()
121                 .getWorkingSetRegistry();
122
123         for (int i = 0; i < allSets.length; i++) {
124             String JavaDoc setType = allSets[i].getId();
125             if (WorkbenchActivityHelper.filterItem(registry
126                     .getWorkingSetDescriptor(setType))) {
127                 continue;
128             }
129             List JavaDoc setsOfType = (List JavaDoc) map.get(setType);
130             if (setsOfType == null) {
131                 setsOfType = new ArrayList JavaDoc();
132                 map.put(setType, setsOfType);
133             }
134             setsOfType.add(allSets[i]);
135         }
136
137         IWorkingSet[][] typedSets = new IWorkingSet[map.keySet().size()][];
138         int i = 0;
139         for (Iterator JavaDoc iter = map.keySet().iterator(); iter.hasNext();) {
140             List JavaDoc setsOfType = (List JavaDoc) map.get(iter.next());
141             typedSets[i] = new IWorkingSet[setsOfType.size()];
142             setsOfType.toArray(typedSets[i++]);
143         }
144         return typedSets;
145     }
146
147     public void init(IWorkbenchWindow window) {
148         this.window = window;
149     }
150
151     protected IWorkbenchWindow getWindow() {
152         return window;
153     }
154
155     public void selectionChanged(IAction action, ISelection selection) {
156         this.selection = selection;
157     }
158
159     protected ISelection getSelection() {
160         return selection;
161     }
162 }
Popular Tags