KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > WorkingSetFilterActionGroup


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.actions;
13
14 import java.util.Arrays JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.jface.action.IContributionItem;
21 import org.eclipse.jface.action.IMenuListener;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.Separator;
24 import org.eclipse.jface.util.IPropertyChangeListener;
25 import org.eclipse.jface.util.PropertyChangeEvent;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IMemento;
29 import org.eclipse.ui.IWorkbenchActionConstants;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.IWorkbenchPreferenceConstants;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.IWorkingSet;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.internal.WorkingSetComparator;
36 import org.eclipse.ui.internal.WorkingSetMenuContributionItem;
37 import org.eclipse.ui.internal.actions.ClearWorkingSetAction;
38 import org.eclipse.ui.internal.actions.EditWorkingSetAction;
39 import org.eclipse.ui.internal.actions.SelectWorkingSetAction;
40 import org.eclipse.ui.internal.util.Util;
41
42 /**
43  * Adds working set filter actions (set / clear / edit)
44  *
45  * @since 2.1
46  */

47 public class WorkingSetFilterActionGroup extends ActionGroup {
48     private static final String JavaDoc TAG_WORKING_SET_NAME = "workingSetName"; //$NON-NLS-1$
49

50     private static final String JavaDoc TAG_IS_WINDOW_WORKING_SET = "isWindowWorkingSet"; //$NON-NLS-1$
51

52     /**
53      * Indicates if working set was changed
54      */

55     public static final String JavaDoc CHANGE_WORKING_SET = "changeWorkingSet"; //$NON-NLS-1$
56

57     private static final String JavaDoc START_SEPARATOR_ID = "workingSetGroupStartSeparator"; //$NON-NLS-1$
58

59     private static final String JavaDoc SEPARATOR_ID = "workingSetGroupSeparator"; //$NON-NLS-1$
60

61     private static final String JavaDoc WORKING_SET_ACTION_GROUP = "workingSetActionGroup"; //$NON-NLS-1$
62

63     private IWorkingSet workingSet = null;
64
65     private ClearWorkingSetAction clearWorkingSetAction;
66
67     private SelectWorkingSetAction selectWorkingSetAction;
68
69     private EditWorkingSetAction editWorkingSetAction;
70
71     private IPropertyChangeListener workingSetUpdater;
72
73     private int mruMenuCount;
74
75     private IMenuManager menuManager;
76
77     private IMenuListener menuListener;
78
79     private IWorkbenchWindow workbenchWindow;
80
81     private IWorkbenchPage page;
82
83     private boolean allowWindowWorkingSetByDefault;
84
85     /**
86      * Creates a new instance of the receiver.
87      *
88      * @param shell
89      * shell to open dialogs and wizards on
90      * @param workingSetUpdater
91      * property change listener notified when a working set is set
92      * @since 3.2 Please note that it is expected that clients treat any
93      * selected working sets whose
94      * {@link IWorkingSet#isAggregateWorkingSet()} method returns
95      * <code>true</code> somewhat differently from traditional working
96      * sets. Please see the documentation for
97      * {@link IWorkbenchPage#getAggregateWorkingSet()} for details.
98      */

99     public WorkingSetFilterActionGroup(Shell shell,
100             IPropertyChangeListener workingSetUpdater) {
101         Assert.isNotNull(shell);
102
103         this.workingSetUpdater = workingSetUpdater;
104         clearWorkingSetAction = new ClearWorkingSetAction(this);
105         selectWorkingSetAction = new SelectWorkingSetAction(this, shell);
106         editWorkingSetAction = new EditWorkingSetAction(this, shell);
107         
108         workbenchWindow = Util.getWorkbenchWindowForShell(shell);
109         allowWindowWorkingSetByDefault = false;
110         // set the default working set to be that of the window.
111
page = workbenchWindow.getActivePage();
112         if (page == null) {
113             IWorkbenchPage[] pages = workbenchWindow.getPages();
114             if (pages.length > 0) {
115                 page = pages[0];
116             }
117         }
118     }
119
120     /**
121      * Adds actions for the most recently used working sets to the specified
122      * menu manager.
123      *
124      * @param menuManager
125      * menu manager to add actions to
126      */

127     private void addMruWorkingSetActions(IMenuManager menuManager) {
128         IWorkingSet[] workingSets = PlatformUI.getWorkbench()
129                 .getWorkingSetManager().getRecentWorkingSets();
130         List JavaDoc sortedWorkingSets = Arrays.asList(workingSets);
131         Collections.sort(sortedWorkingSets, new WorkingSetComparator());
132
133         Iterator JavaDoc iter = sortedWorkingSets.iterator();
134         mruMenuCount = 0;
135         while (iter.hasNext()) {
136             IWorkingSet workingSet = (IWorkingSet) iter.next();
137             if (workingSet != null) {
138                 IContributionItem item = new WorkingSetMenuContributionItem(
139                         ++mruMenuCount, this, workingSet);
140                 menuManager.insertBefore(SEPARATOR_ID, item);
141             }
142         }
143     }
144
145  
146     /* (non-Javadoc)
147      * @see org.eclipse.ui.actions.ActionGroup#dispose()
148      */

149     public void dispose() {
150         if (menuManager != null) {
151             menuManager.removeMenuListener(menuListener);
152         }
153         super.dispose();
154     }
155
156     
157     /* (non-Javadoc)
158      * @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
159      */

160     public void fillActionBars(IActionBars actionBars) {
161         menuManager = actionBars.getMenuManager();
162         
163         if(menuManager.find(IWorkbenchActionConstants.MB_ADDITIONS) != null)
164             menuManager.insertAfter(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(WORKING_SET_ACTION_GROUP));
165         else
166             menuManager.add(new Separator(WORKING_SET_ACTION_GROUP));
167         
168         menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, selectWorkingSetAction);
169         menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, clearWorkingSetAction);
170         menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, editWorkingSetAction);
171         menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, new Separator(START_SEPARATOR_ID));
172         menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, new Separator(SEPARATOR_ID));
173
174         menuListener = new IMenuListener() {
175             public void menuAboutToShow(IMenuManager manager) {
176                 removePreviousMruWorkingSetActions(manager);
177                 addMruWorkingSetActions(manager);
178             }
179         };
180         menuManager.addMenuListener(menuListener);
181     }
182     
183     
184     /* (non-Javadoc)
185      * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
186      */

187     public void fillContextMenu(IMenuManager menuManager) {
188         menuManager.add(selectWorkingSetAction);
189         menuManager.add(clearWorkingSetAction);
190         menuManager.add(editWorkingSetAction);
191         menuManager.add(new Separator());
192         menuManager.add(new Separator(SEPARATOR_ID));
193
194         menuListener = new IMenuListener() {
195             public void menuAboutToShow(IMenuManager manager) {
196                 removePreviousMruWorkingSetActions(manager);
197                 addMruWorkingSetActions(manager);
198             }
199         };
200         menuManager.addMenuListener(menuListener);
201     }
202
203     /**
204      * Returns the working set which is currently selected.
205      *
206      * @return the working set which is currently selected.
207      */

208     public IWorkingSet getWorkingSet() {
209         return workingSet;
210     }
211
212     /**
213      * Removes the most recently used working set actions that were
214      * added to the specified menu.
215      *
216      * @param menuManager menu manager to remove actions from
217      */

218     private void removePreviousMruWorkingSetActions(IMenuManager menuManager) {
219         for (int i = 1; i <= mruMenuCount; i++) {
220             menuManager.remove(WorkingSetMenuContributionItem.getId(i));
221         }
222     }
223
224     /**
225      * Sets the current working set.
226      *
227      * @param newWorkingSet the new working set
228      */

229     public void setWorkingSet(IWorkingSet newWorkingSet) {
230         IWorkingSet oldWorkingSet = workingSet;
231
232         workingSet = newWorkingSet;
233         // Update action
234
clearWorkingSetAction.setEnabled(newWorkingSet != null);
235         editWorkingSetAction.setEnabled(newWorkingSet != null && newWorkingSet.isEditable());
236
237         firePropertyChange(newWorkingSet, oldWorkingSet);
238     }
239
240     /**
241      * Fire the property change to the updater if there is one available.
242      *
243      * @param newWorkingSet the new working set
244      * @param oldWorkingSet the previous working set
245      * @since 3.2
246      */

247     private void firePropertyChange(IWorkingSet newWorkingSet, IWorkingSet oldWorkingSet) {
248         // Update viewer
249
if (workingSetUpdater != null) {
250             workingSetUpdater.propertyChange(new PropertyChangeEvent(this,
251                     WorkingSetFilterActionGroup.CHANGE_WORKING_SET,
252                     oldWorkingSet, newWorkingSet));
253         }
254     }
255
256     /**
257      * Saves the state of the filter actions in a memento.
258      *
259      * @param memento
260      * the memento
261      * @since 3.3
262      */

263     public void saveState(IMemento memento) {
264         String JavaDoc workingSetName = ""; //$NON-NLS-1$
265
boolean isWindowWorkingSet = false;
266         if (workingSet != null) {
267             if (workingSet.isAggregateWorkingSet()) {
268                 isWindowWorkingSet = true;
269             } else {
270                 workingSetName = workingSet.getName();
271             }
272         }
273         memento.putString(TAG_IS_WINDOW_WORKING_SET,
274                 isWindowWorkingSet ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
275
memento.putString(TAG_WORKING_SET_NAME, workingSetName);
276     }
277
278     /**
279      * Restores the state of the filter actions from a memento.
280      * <p>
281      * Note: This method does not refresh the viewer.
282      * </p>
283      *
284      * @param memento
285      * @since 3.3
286      */

287     public void restoreState(IMemento memento) {
288         boolean isWindowWorkingSet;
289         if (memento.getString(TAG_IS_WINDOW_WORKING_SET) != null) {
290             isWindowWorkingSet = Boolean.valueOf(
291                     memento.getString(TAG_IS_WINDOW_WORKING_SET))
292                     .booleanValue();
293         } else {
294             isWindowWorkingSet = useWindowWorkingSetByDefault();
295         }
296         String JavaDoc workingSetName = memento.getString(TAG_WORKING_SET_NAME);
297         boolean hasWorkingSetName = workingSetName != null
298                 && workingSetName.length() > 0;
299
300         IWorkingSet ws = null;
301         // First handle name if present.
302
if (hasWorkingSetName) {
303             ws = PlatformUI.getWorkbench().getWorkingSetManager()
304                     .getWorkingSet(workingSetName);
305         } else if (isWindowWorkingSet && page != null) {
306             ws = page.getAggregateWorkingSet();
307         }
308
309         setWorkingSet(ws);
310     }
311
312     private boolean useWindowWorkingSetByDefault() {
313         return allowWindowWorkingSetByDefault
314                 && PlatformUI
315                         .getPreferenceStore()
316                         .getBoolean(
317                                 IWorkbenchPreferenceConstants.USE_WINDOW_WORKING_SET_BY_DEFAULT);
318     }
319 }
320
Popular Tags