1 13 package org.eclipse.ui.internal.actions; 14 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Collection ; 18 import java.util.HashSet ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 import org.eclipse.core.runtime.IAdaptable; 23 import org.eclipse.core.runtime.IConfigurationElement; 24 import org.eclipse.core.runtime.IExecutableExtension; 25 import org.eclipse.jface.action.Action; 26 import org.eclipse.jface.action.ActionContributionItem; 27 import org.eclipse.jface.action.IAction; 28 import org.eclipse.jface.action.IContributionItem; 29 import org.eclipse.jface.action.IMenuManager; 30 import org.eclipse.jface.action.Separator; 31 import org.eclipse.jface.util.IPropertyChangeListener; 32 import org.eclipse.jface.util.PropertyChangeEvent; 33 import org.eclipse.jface.viewers.ISelection; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.window.Window; 36 import org.eclipse.jface.wizard.WizardDialog; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.widgets.Event; 39 import org.eclipse.swt.widgets.Menu; 40 import org.eclipse.ui.IActionDelegate2; 41 import org.eclipse.ui.IWorkbenchWindow; 42 import org.eclipse.ui.IWorkingSet; 43 import org.eclipse.ui.IWorkingSetManager; 44 import org.eclipse.ui.PlatformUI; 45 import org.eclipse.ui.actions.QuickMenuCreator; 46 import org.eclipse.ui.dialogs.IWorkingSetNewWizard; 47 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 48 import org.eclipse.ui.internal.WorkbenchMessages; 49 import org.eclipse.ui.internal.WorkbenchPlugin; 50 51 55 public class ModifyWorkingSetDelegate extends 56 AbstractWorkingSetPulldownDelegate implements IExecutableExtension, IActionDelegate2 { 57 58 public class NewWorkingSetAction extends Action { 59 60 63 public NewWorkingSetAction() { 64 super(WorkbenchMessages.NewWorkingSet); 65 } 66 67 public void run() { 68 IWorkingSetManager manager = WorkbenchPlugin.getDefault() 69 .getWorkingSetManager(); 70 IWorkingSetNewWizard wizard = manager.createWorkingSetNewWizard(null); 71 WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench() 75 .getDisplay().getActiveShell(), wizard); 76 77 dialog.create(); 78 PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), 79 IWorkbenchHelpContextIds.WORKING_SET_NEW_WIZARD); 80 if (dialog.open() == Window.OK) { 81 IWorkingSet workingSet = wizard.getSelection(); 82 if(workingSet != null) { 83 manager.addWorkingSet(workingSet); 84 } 85 } 86 } 87 88 } 89 90 private class ModifyAction extends Action { 91 92 private IWorkingSet set; 93 94 private IAdaptable [] selectedElements; 95 96 101 private ModifyAction(IWorkingSet set, IAdaptable [] selectedElements) { 102 super(set.getLabel(), IAction.AS_CHECK_BOX); 103 this.set = set; 104 this.selectedElements = selectedElements; 105 setImageDescriptor(set.getImageDescriptor()); 106 } 107 108 public void run() { 109 110 Collection oldElements = Arrays.asList(set.getElements()); 111 Set newElements = new HashSet (oldElements.size() 112 + selectedElements.length); 113 newElements.addAll(oldElements); 114 List selectedAsList = Arrays.asList(selectedElements); 115 if (add) { 116 newElements.addAll(selectedAsList); 117 } else { 118 newElements.removeAll(selectedAsList); 119 } 120 set.setElements((IAdaptable[]) newElements 121 .toArray(new IAdaptable[newElements.size()])); 122 } 123 } 124 125 private QuickMenuCreator contextMenuCreator = new QuickMenuCreator() { 126 protected void fillMenu(IMenuManager menu) { 127 ModifyWorkingSetDelegate.this.fillMenu(menu); 128 } 129 }; 130 131 private boolean add = true; 132 133 private IPropertyChangeListener listener = new IPropertyChangeListener() { 134 135 public void propertyChange(PropertyChangeEvent event) { 136 refreshEnablement(); 137 } 138 139 142 private void refreshEnablement() { 143 selectionChanged(actionProxy, getSelection()); 144 } 145 }; 146 147 private IAction actionProxy; 148 149 152 public ModifyWorkingSetDelegate() { 153 } 154 155 160 public void run(IAction action) { 161 contextMenuCreator.createMenu(); 162 } 163 164 167 public void runWithEvent(IAction action, Event event) { 168 if (event.type == SWT.KeyDown || event.type == SWT.KeyUp) 169 run(action); 170 } 171 172 177 public void init(IWorkbenchWindow window) { 178 super.init(window); 179 getWindow().getWorkbench().getWorkingSetManager() 180 .addPropertyChangeListener(listener); 181 } 182 183 188 public void dispose() { 189 getWindow().getWorkbench().getWorkingSetManager() 190 .removePropertyChangeListener(listener); 191 super.dispose(); 192 contextMenuCreator.dispose(); 193 } 194 195 public void fillMenu(Menu menu) { 196 List menuItems = getItems(); 197 for (int i = 0; i < menuItems.size(); i++) { 198 Object object = menuItems.get(i); 199 if (object instanceof IAction) { 200 ActionContributionItem item = new ActionContributionItem((IAction) object); 201 item.fill(menu, -1); 202 } else { 203 IContributionItem item = (IContributionItem) object; 204 item.fill(menu, -1); 205 } 206 } 207 if(add) { 209 IContributionItem item = null; 210 if (menu.getItemCount() > 0) { 211 item = new Separator(); 212 item.fill(menu, -1); 213 } 214 215 item = new ActionContributionItem(new NewWorkingSetAction()); 216 item.fill(menu, -1); 217 } 218 } 219 224 private List getItems() { 225 List menuItems = new ArrayList (); 226 ISelection selection = getSelection(); 227 if (!(selection instanceof IStructuredSelection)) { 228 if(!add) { 229 IAction emptyAction = new Action(WorkbenchMessages.NoApplicableWorkingSets) {}; 230 emptyAction.setEnabled(false); 231 menuItems.add(emptyAction); 232 } 233 return menuItems; 234 } 235 236 IWorkingSet[][] typedSets = splitSets(); 237 Object [] selectedElements = ((IStructuredSelection)selection).toArray(); 238 239 for (int i = 0; i < typedSets.length; i++) { 240 if (menuItems.size() > 0 242 && !(menuItems.get(menuItems.size() - 1) instanceof Separator)) { 243 Separator item = new Separator(); 244 menuItems.add(item); 245 } 246 IWorkingSet[] sets = typedSets[i]; 247 for (int j = 0; j < sets.length; j++) { 248 IWorkingSet set = sets[j]; 249 250 Set existingElements = new HashSet (); 251 existingElements.addAll(Arrays 252 .asList(set.getElements())); 253 254 boolean visible = false; 255 IAdaptable [] adaptables = new IAdaptable[selectedElements.length]; 256 System.arraycopy(selectedElements, 0, adaptables, 0, selectedElements.length); 257 adaptables = set.adaptElements(adaptables); 258 if (adaptables.length > 0 && add) { 259 for (int k = 0; k < adaptables.length; k++) { 260 if (!existingElements.contains(adaptables[k])) { 261 visible = true; 264 break; 265 } 266 } 267 } 268 else if (adaptables.length > 0) { 269 for (int k = 0; k < adaptables.length; k++) { 270 if (existingElements.contains(adaptables[k])) 271 visible = true; break; 274 } 275 } 276 277 if (visible) { 278 ModifyAction action = new ModifyAction(set, 279 adaptables); 280 menuItems.add(action); 281 } 282 } 283 } 284 if (menuItems.isEmpty() && !add) { 285 IAction emptyAction = new Action( 286 WorkbenchMessages.NoApplicableWorkingSets) { 287 }; 288 emptyAction.setEnabled(false); 289 menuItems.add(emptyAction); 290 } 291 return menuItems; 292 } 293 294 private void fillMenu(IMenuManager menu) { 295 List menuItems = getItems(); 296 for (int i = 0; i < menuItems.size(); i++) { 297 Object object = menuItems.get(i); 298 if (object instanceof IAction) { 299 menu.add((IAction) object); 300 } else { 301 IContributionItem item = (IContributionItem) object; 302 menu.add(item); 303 } 304 } 305 } 306 307 313 public void selectionChanged(IAction actionProxy, ISelection selection) { 314 super.selectionChanged(actionProxy, selection); 315 if (selection instanceof IStructuredSelection) { 316 Object [] selectedElements = ((IStructuredSelection) getSelection()) 317 .toArray(); 318 boolean minimallyOkay = true; 320 for (int i = 0; i < selectedElements.length; i++) { 321 Object object = selectedElements[i]; 322 if (!(object instanceof IAdaptable) || object instanceof IWorkingSet) { 323 minimallyOkay = false; 324 break; 325 } 326 } 327 actionProxy.setEnabled(minimallyOkay); 328 329 } 330 else 331 actionProxy.setEnabled(false); 332 } 333 334 340 public void setInitializationData(IConfigurationElement config, 341 String propertyName, Object data) { 342 if (data instanceof String ) { 343 add = Boolean.valueOf((String ) data).booleanValue(); 344 } 345 } 346 347 350 public void init(IAction action) { 351 this.actionProxy = action; 352 } 353 } 354 | Popular Tags |