| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 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; |
| 15 | import java.util.Collections; |
| 16 | import java.util.Iterator; |
| 17 | import java.util.List; |
| 18 | |
| 19 | import org.eclipse.swt.widgets.Shell; |
| 20 | |
| 21 | import org.eclipse.core.runtime.Assert; |
| 22 | |
| 23 | import org.eclipse.jface.action.IContributionItem; |
| 24 | import org.eclipse.jface.action.IMenuListener; |
| 25 | import org.eclipse.jface.action.IMenuManager; |
| 26 | import org.eclipse.jface.action.Separator; |
| 27 | import org.eclipse.jface.util.IPropertyChangeListener; |
| 28 | import org.eclipse.jface.util.PropertyChangeEvent; |
| 29 | |
| 30 | import org.eclipse.ui.IActionBars; |
| 31 | import org.eclipse.ui.IMemento; |
| 32 | import org.eclipse.ui.IWorkbenchActionConstants; |
| 33 | import org.eclipse.ui.IWorkbenchPage; |
| 34 | import org.eclipse.ui.IWorkbenchPreferenceConstants; |
| 35 | import org.eclipse.ui.IWorkbenchWindow; |
| 36 | import org.eclipse.ui.IWorkingSet; |
| 37 | import org.eclipse.ui.PlatformUI; |
| 38 | import org.eclipse.ui.internal.WorkingSetComparator; |
| 39 | import org.eclipse.ui.internal.WorkingSetMenuContributionItem; |
| 40 | import org.eclipse.ui.internal.actions.ClearWorkingSetAction; |
| 41 | import org.eclipse.ui.internal.actions.EditWorkingSetAction; |
| 42 | import org.eclipse.ui.internal.actions.SelectWorkingSetAction; |
| 43 | import org.eclipse.ui.internal.util.Util; |
| 44 | |
| 45 | /** |
| 46 | * Adds working set filter actions (set / clear / edit) |
| 47 | * |
| 48 | * @since 2.1 |
| 49 | */ |
| 50 | public class WorkingSetFilterActionGroup extends ActionGroup { |
| 51 | private static final String TAG_WORKING_SET_NAME = "workingSetName"; //$NON-NLS-1$ |
| 52 | |
| 53 | /** |
| 54 | * Indicates if working set was changed |
| 55 | */ |
| 56 | public static final String CHANGE_WORKING_SET = "changeWorkingSet"; //$NON-NLS-1$ |
| 57 | |
| 58 | private static final String START_SEPARATOR_ID = "workingSetGroupStartSeparator"; //$NON-NLS-1$ |
| 59 | |
| 60 | private static final String SEPARATOR_ID = "workingSetGroupSeparator"; //$NON-NLS-1$ |
| 61 | |
| 62 | private static final String WORKING_SET_ACTION_GROUP = "workingSetActionGroup"; //$NON-NLS-1$ |
| 63 | |
| 64 | private IWorkingSet workingSet = null; |
| 65 | |
| 66 | private ClearWorkingSetAction clearWorkingSetAction; |
| 67 | |
| 68 | private SelectWorkingSetAction selectWorkingSetAction; |
| 69 | |
| 70 | private EditWorkingSetAction editWorkingSetAction; |
| 71 | |
| 72 | private IPropertyChangeListener workingSetUpdater; |
| 73 | |
| 74 | private int mruMenuCount; |
| 75 | |
| 76 | private IMenuManager menuManager; |
| 77 | |
| 78 | private IMenuListener menuListener; |
| 79 | |
| 80 | private IWorkbenchWindow workbenchWindow; |
| 81 | |
| 82 | private IWorkbenchPage page; |
| 83 | |
| 84 | private boolean allowWindowWorkingSetByDefault; |
| 85 | |
| 86 | /** |
| 87 | * Creates a new instance of the receiver. |
| 88 | * |
| 89 | * @param shell |
| 90 | * shell to open dialogs and wizards on |
| 91 | * @param workingSetUpdater |
| 92 | * property change listener notified when a working set is set |
| 93 | * @since 3.2 Please note that it is expected that clients treat any |
| 94 | * selected working sets whose |
| 95 | * {@link IWorkingSet#isAggregateWorkingSet()} method returns |
| 96 | * <code>true</code> somewhat differently from traditional working |
| 97 | * sets. Please see the documentation for |
| 98 | * {@link IWorkbenchPage#getAggregateWorkingSet()} for details. |
| 99 | */ |
| 100 | public WorkingSetFilterActionGroup(Shell shell, |
| 101 | IPropertyChangeListener workingSetUpdater) { |
| 102 | Assert.isNotNull(shell); |
| 103 | |
| 104 | this.workingSetUpdater = workingSetUpdater; |
| 105 | clearWorkingSetAction = new ClearWorkingSetAction(this); |
| 106 | selectWorkingSetAction = new SelectWorkingSetAction(this, shell); |
| 107 | editWorkingSetAction = new EditWorkingSetAction(this, shell); |
| 108 | |
| 109 | workbenchWindow = Util.getWorkbenchWindowForShell(shell); |
| 110 | allowWindowWorkingSetByDefault = false; |
| 111 | // set the default working set to be that of the window. |
| 112 | page = workbenchWindow.getActivePage(); |
| 113 | if (page == null) { |
| 114 | IWorkbenchPage[] pages = workbenchWindow.getPages(); |
| 115 | if (pages.length > 0) { |
| 116 | page = pages[0]; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Adds actions for the most recently used working sets to the specified |
| 123 | * menu manager. |
| 124 | * |
| 125 | * @param menuManager |
| 126 | * menu manager to add actions to |
| 127 | */ |
| 128 | private void addMruWorkingSetActions(IMenuManager menuManager) { |
| 129 | IWorkingSet[] workingSets = PlatformUI.getWorkbench() |
| 130 | .getWorkingSetManager().getRecentWorkingSets(); |
| 131 | List sortedWorkingSets = Arrays.asList(workingSets); |
| 132 | Collections.sort(sortedWorkingSets, new WorkingSetComparator()); |
| 133 | |
| 134 | Iterator iter = sortedWorkingSets.iterator(); |
| 135 | mruMenuCount = 0; |
| 136 | while (iter.hasNext()) { |
| 137 | IWorkingSet workingSet = (IWorkingSet) iter.next(); |
| 138 | if (workingSet != null) { |
| 139 | IContributionItem item = new WorkingSetMenuContributionItem( |
| 140 | ++mruMenuCount, this, workingSet); |
| 141 | menuManager.insertBefore(SEPARATOR_ID, item); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* (non-Javadoc) |
| 148 | * @see org.eclipse.ui.actions.ActionGroup#dispose() |
| 149 | */ |
| 150 | public void dispose() { |
| 151 | if (menuManager != null) { |
| 152 | menuManager.removeMenuListener(menuListener); |
| 153 | } |
| 154 | super.dispose(); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /* (non-Javadoc) |
| 159 | * @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars) |
| 160 | */ |
| 161 | public void fillActionBars(IActionBars actionBars) { |
| 162 | menuManager = actionBars.getMenuManager(); |
| 163 | |
| 164 | if(menuManager.find(IWorkbenchActionConstants.MB_ADDITIONS) != null) |
| 165 | menuManager.insertAfter(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(WORKING_SET_ACTION_GROUP)); |
| 166 | else |
| 167 | menuManager.add(new Separator(WORKING_SET_ACTION_GROUP)); |
| 168 | |
| 169 | menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, selectWorkingSetAction); |
| 170 | menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, clearWorkingSetAction); |
| 171 | menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, editWorkingSetAction); |
| 172 | menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, new Separator(START_SEPARATOR_ID)); |
| 173 | menuManager.appendToGroup(WORKING_SET_ACTION_GROUP, new Separator(SEPARATOR_ID)); |
| 174 | |
| 175 | menuListener = new IMenuListener() { |
| 176 | public void menuAboutToShow(IMenuManager manager) { |
| 177 | removePreviousMruWorkingSetActions(manager); |
| 178 | addMruWorkingSetActions(manager); |
| 179 | } |
| 180 | }; |
| 181 | menuManager.addMenuListener(menuListener); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /* (non-Javadoc) |
| 186 | * @see org.eclipse.ui.actions.ActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager) |
| 187 | */ |
| 188 | public void fillContextMenu(IMenuManager menuManager) { |
| 189 | menuManager.add(selectWorkingSetAction); |
| 190 | menuManager.add(clearWorkingSetAction); |
| 191 | menuManager.add(editWorkingSetAction); |
| 192 | menuManager.add(new Separator()); |
| 193 | menuManager.add(new Separator(SEPARATOR_ID)); |
| 194 | |
| 195 | menuListener = new IMenuListener() { |
| 196 | public void menuAboutToShow(IMenuManager manager) { |
| 197 | removePreviousMruWorkingSetActions(manager); |
| 198 | addMruWorkingSetActions(manager); |
| 199 | } |
| 200 | }; |
| 201 | menuManager.addMenuListener(menuListener); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns the working set which is currently selected. |
| 206 | * |
| 207 | * @return the working set which is currently selected. |
| 208 | */ |
| 209 | public IWorkingSet getWorkingSet() { |
| 210 | return workingSet; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Removes the most recently used working set actions that were |
| 215 | * added to the specified menu. |
| 216 | * |
| 217 | * @param menuManager menu manager to remove actions from |
| 218 | */ |
| 219 | private void removePreviousMruWorkingSetActions(IMenuManager menuManager) { |
| 220 | for (int i = 1; i <= mruMenuCount; i++) { |
| 221 | menuManager.remove(WorkingSetMenuContributionItem.getId(i)); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Sets the current working set. |
| 227 | * |
| 228 | * @param newWorkingSet the new working set |
| 229 | */ |
| 230 | public void setWorkingSet(IWorkingSet newWorkingSet) { |
| 231 | IWorkingSet oldWorkingSet = workingSet; |
| 232 | |
| 233 | workingSet = newWorkingSet; |
| 234 | // Update action |
| 235 | clearWorkingSetAction.setEnabled(newWorkingSet != null); |
| 236 | editWorkingSetAction.setEnabled(newWorkingSet != null && newWorkingSet.isEditable()); |
| 237 | |
| 238 | firePropertyChange(newWorkingSet, oldWorkingSet); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Fire the property change to the updater if there is one available. |
| 243 | * |
| 244 | * @param newWorkingSet the new working set |
| 245 | * @param oldWorkingSet the previous working set |
| 246 | * @since 3.2 |
| 247 | */ |
| 248 | private void firePropertyChange(IWorkingSet newWorkingSet, IWorkingSet oldWorkingSet) { |
| 249 | // Update viewer |
| 250 | if (workingSetUpdater != null) { |
| 251 | workingSetUpdater.propertyChange(new PropertyChangeEvent(this, |
| 252 | WorkingSetFilterActionGroup.CHANGE_WORKING_SET, |
| 253 | oldWorkingSet, newWorkingSet)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Saves the state of the filter actions in a memento. |
| 259 | * |
| 260 | * @param memento |
| 261 | * the memento |
| 262 | * @since 3.3 |
| 263 | */ |
| 264 | public void saveState(IMemento memento) { |
| 265 | if (workingSet != null) { |
| 266 | memento.putString(TAG_WORKING_SET_NAME, workingSet.getName()); |
| 267 | } else { |
| 268 | memento.putString(TAG_WORKING_SET_NAME, ""); //$NON-NLS-1$ |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Restores the state of the filter actions from a memento. |
| 274 | * <p> |
| 275 | * Note: This method does not refresh the viewer. |
| 276 | * </p> |
| 277 | * |
| 278 | * @param memento |
| 279 | * @since 3.3 |
| 280 | */ |
| 281 | public void restoreState(IMemento memento) { |
| 282 | String workingSetName = memento.getString(TAG_WORKING_SET_NAME); |
| 283 | if (workingSetName != null && workingSetName.length() > 0) { |
| 284 | setWorkingSet(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(workingSetName)); |
| 285 | } else if (page != null && useWindowWorkingSetByDefault()) { |
| 286 | setWorkingSet(page.getAggregateWorkingSet()); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private boolean useWindowWorkingSetByDefault() { |
| 291 | return allowWindowWorkingSetByDefault |
| 292 | && PlatformUI |
| 293 | .getPreferenceStore() |
| 294 | .getBoolean( |
| 295 | IWorkbenchPreferenceConstants.USE_WINDOW_WORKING_SET_BY_DEFAULT); |
| 296 | } |
| 297 | } |