| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2005, 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 | package org.eclipse.ui.internal.actions; |
| 12 | |
| 13 | import java.util.Arrays; |
| 14 | import java.util.HashSet; |
| 15 | import java.util.Set; |
| 16 | |
| 17 | import org.eclipse.jface.action.Action; |
| 18 | import org.eclipse.jface.action.ActionContributionItem; |
| 19 | import org.eclipse.jface.action.IAction; |
| 20 | import org.eclipse.jface.action.Separator; |
| 21 | import org.eclipse.jface.bindings.keys.IKeyLookup; |
| 22 | import org.eclipse.jface.bindings.keys.KeyLookupFactory; |
| 23 | import org.eclipse.jface.window.Window; |
| 24 | import org.eclipse.swt.widgets.Event; |
| 25 | import org.eclipse.swt.widgets.Menu; |
| 26 | import org.eclipse.ui.IWorkbenchWindow; |
| 27 | import org.eclipse.ui.IWorkingSet; |
| 28 | import org.eclipse.ui.internal.WorkbenchMessages; |
| 29 | import org.eclipse.ui.internal.dialogs.SimpleWorkingSetSelectionDialog; |
| 30 | |
| 31 | /** |
| 32 | * Action to select the visible working sets for a given workbench page. |
| 33 | * |
| 34 | * @since 3.2 |
| 35 | */ |
| 36 | public class SelectWorkingSetsAction extends AbstractWorkingSetPulldownDelegate { |
| 37 | |
| 38 | private class ManageWorkingSetsAction extends Action { |
| 39 | |
| 40 | ManageWorkingSetsAction() { |
| 41 | super(WorkbenchMessages.Edit); |
| 42 | } |
| 43 | |
| 44 | public void run() { |
| 45 | SelectWorkingSetsAction.this.run(this); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private class ToggleWorkingSetAction extends Action { |
| 50 | private IWorkingSet set; |
| 51 | |
| 52 | ToggleWorkingSetAction(IWorkingSet set) { |
| 53 | super(set.getLabel(), IAction.AS_CHECK_BOX); |
| 54 | setImageDescriptor(set.getImageDescriptor()); |
| 55 | this.set = set; |
| 56 | setChecked(isWorkingSetEnabled(set)); |
| 57 | } |
| 58 | |
| 59 | public void runWithEvent(Event event) { |
| 60 | |
| 61 | Set newList = new HashSet(Arrays.asList(getWindow().getActivePage() |
| 62 | .getWorkingSets())); |
| 63 | |
| 64 | if (isChecked()) { |
| 65 | // if the primary modifier key is down then clear the list |
| 66 | // first. this makes the selection exclusive rather than |
| 67 | // additive. |
| 68 | boolean modified = (event.stateMask & KeyLookupFactory |
| 69 | .getDefault().formalModifierLookup(IKeyLookup.M1_NAME)) != 0; |
| 70 | |
| 71 | if (modified) |
| 72 | newList.clear(); |
| 73 | newList.add(set); |
| 74 | } else { |
| 75 | newList.remove(set); |
| 76 | } |
| 77 | |
| 78 | getWindow().getActivePage().setWorkingSets( |
| 79 | (IWorkingSet[]) newList.toArray(new IWorkingSet[newList |
| 80 | .size()])); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | protected void fillMenu(Menu menu) { |
| 85 | IWorkingSet[][] typedSets = splitSets(); |
| 86 | |
| 87 | for (int i = 0; i < typedSets.length; i++) { |
| 88 | IWorkingSet[] sets = typedSets[i]; |
| 89 | for (int j = 0; j < sets.length; j++) { |
| 90 | IWorkingSet set = sets[j]; |
| 91 | |
| 92 | // only add visible sets |
| 93 | // if (set.isVisible()) { |
| 94 | ActionContributionItem item = new ActionContributionItem( |
| 95 | new ToggleWorkingSetAction(set)); |
| 96 | item.fill(menu, -1); |
| 97 | // } |
| 98 | } |
| 99 | Separator separator = new Separator(); |
| 100 | separator.fill(menu, -1); |
| 101 | } |
| 102 | |
| 103 | ActionContributionItem item = new ActionContributionItem( |
| 104 | new ManageWorkingSetsAction()); |
| 105 | item.fill(menu, -1); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | private IWorkingSet[] getEnabledSets() { |
| 110 | return getWindow().getActivePage().getWorkingSets(); |
| 111 | } |
| 112 | |
| 113 | private boolean isWorkingSetEnabled(IWorkingSet set) { |
| 114 | IWorkingSet[] enabledSets = getEnabledSets(); |
| 115 | for (int i = 0; i < enabledSets.length; i++) { |
| 116 | if (enabledSets[i].equals(set)) { |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | public void run(IAction action) { |
| 124 | ConfigureWindowWorkingSetsDialog dialog = new ConfigureWindowWorkingSetsDialog( |
| 125 | getWindow()); |
| 126 | if (dialog.open() == Window.OK) { |
| 127 | |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | class ConfigureWindowWorkingSetsDialog extends SimpleWorkingSetSelectionDialog { |
| 134 | |
| 135 | private IWorkbenchWindow window; |
| 136 | |
| 137 | protected ConfigureWindowWorkingSetsDialog(IWorkbenchWindow window) { |
| 138 | super(window.getShell(), null, window.getActivePage().getWorkingSets(), true); |
| 139 | this.window = window; |
| 140 | setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title_multiSelect); |
| 141 | setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message_multiSelect); |
| 142 | } |
| 143 | |
| 144 | protected void okPressed() { |
| 145 | super.okPressed(); |
| 146 | window.getActivePage().setWorkingSets(getSelection()); |
| 147 | } |
| 148 | } |