1 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.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 47 public class WorkingSetFilterActionGroup extends ActionGroup { 48 private static final String TAG_WORKING_SET_NAME = "workingSetName"; 50 private static final String TAG_IS_WINDOW_WORKING_SET = "isWindowWorkingSet"; 52 55 public static final String CHANGE_WORKING_SET = "changeWorkingSet"; 57 private static final String START_SEPARATOR_ID = "workingSetGroupStartSeparator"; 59 private static final String SEPARATOR_ID = "workingSetGroupSeparator"; 61 private static final String WORKING_SET_ACTION_GROUP = "workingSetActionGroup"; 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 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 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 127 private void addMruWorkingSetActions(IMenuManager menuManager) { 128 IWorkingSet[] workingSets = PlatformUI.getWorkbench() 129 .getWorkingSetManager().getRecentWorkingSets(); 130 List sortedWorkingSets = Arrays.asList(workingSets); 131 Collections.sort(sortedWorkingSets, new WorkingSetComparator()); 132 133 Iterator 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 149 public void dispose() { 150 if (menuManager != null) { 151 menuManager.removeMenuListener(menuListener); 152 } 153 super.dispose(); 154 } 155 156 157 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 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 208 public IWorkingSet getWorkingSet() { 209 return workingSet; 210 } 211 212 218 private void removePreviousMruWorkingSetActions(IMenuManager menuManager) { 219 for (int i = 1; i <= mruMenuCount; i++) { 220 menuManager.remove(WorkingSetMenuContributionItem.getId(i)); 221 } 222 } 223 224 229 public void setWorkingSet(IWorkingSet newWorkingSet) { 230 IWorkingSet oldWorkingSet = workingSet; 231 232 workingSet = newWorkingSet; 233 clearWorkingSetAction.setEnabled(newWorkingSet != null); 235 editWorkingSetAction.setEnabled(newWorkingSet != null && newWorkingSet.isEditable()); 236 237 firePropertyChange(newWorkingSet, oldWorkingSet); 238 } 239 240 247 private void firePropertyChange(IWorkingSet newWorkingSet, IWorkingSet oldWorkingSet) { 248 if (workingSetUpdater != null) { 250 workingSetUpdater.propertyChange(new PropertyChangeEvent(this, 251 WorkingSetFilterActionGroup.CHANGE_WORKING_SET, 252 oldWorkingSet, newWorkingSet)); 253 } 254 } 255 256 263 public void saveState(IMemento memento) { 264 String workingSetName = ""; 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"); memento.putString(TAG_WORKING_SET_NAME, workingSetName); 276 } 277 278 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 workingSetName = memento.getString(TAG_WORKING_SET_NAME); 297 boolean hasWorkingSetName = workingSetName != null 298 && workingSetName.length() > 0; 299 300 IWorkingSet ws = null; 301 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 |