001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.actions;
011:
012: import java.util.Arrays;
013: import java.util.HashSet;
014: import java.util.Set;
015:
016: import org.eclipse.jface.action.Action;
017: import org.eclipse.jface.action.ActionContributionItem;
018: import org.eclipse.jface.action.IAction;
019: import org.eclipse.jface.action.Separator;
020: import org.eclipse.jface.bindings.keys.IKeyLookup;
021: import org.eclipse.jface.bindings.keys.KeyLookupFactory;
022: import org.eclipse.jface.window.Window;
023: import org.eclipse.swt.widgets.Event;
024: import org.eclipse.swt.widgets.Menu;
025: import org.eclipse.ui.IWorkbenchWindow;
026: import org.eclipse.ui.IWorkingSet;
027: import org.eclipse.ui.internal.WorkbenchMessages;
028: import org.eclipse.ui.internal.dialogs.SimpleWorkingSetSelectionDialog;
029:
030: /**
031: * Action to select the visible working sets for a given workbench page.
032: *
033: * @since 3.2
034: */
035: public class SelectWorkingSetsAction extends
036: AbstractWorkingSetPulldownDelegate {
037:
038: private class ManageWorkingSetsAction extends Action {
039:
040: ManageWorkingSetsAction() {
041: super (WorkbenchMessages.Edit);
042: }
043:
044: public void run() {
045: SelectWorkingSetsAction.this .run(this );
046: }
047: }
048:
049: private class ToggleWorkingSetAction extends Action {
050: private IWorkingSet set;
051:
052: ToggleWorkingSetAction(IWorkingSet set) {
053: super (set.getLabel(), IAction.AS_CHECK_BOX);
054: setImageDescriptor(set.getImageDescriptor());
055: this .set = set;
056: setChecked(isWorkingSetEnabled(set));
057: }
058:
059: public void runWithEvent(Event event) {
060:
061: Set newList = new HashSet(Arrays.asList(getWindow()
062: .getActivePage().getWorkingSets()));
063:
064: if (isChecked()) {
065: // if the primary modifier key is down then clear the list
066: // first. this makes the selection exclusive rather than
067: // additive.
068: boolean modified = (event.stateMask & KeyLookupFactory
069: .getDefault().formalModifierLookup(
070: IKeyLookup.M1_NAME)) != 0;
071:
072: if (modified)
073: newList.clear();
074: newList.add(set);
075: } else {
076: newList.remove(set);
077: }
078:
079: getWindow().getActivePage().setWorkingSets(
080: (IWorkingSet[]) newList
081: .toArray(new IWorkingSet[newList.size()]));
082: }
083: }
084:
085: protected void fillMenu(Menu menu) {
086: IWorkingSet[][] typedSets = splitSets();
087:
088: for (int i = 0; i < typedSets.length; i++) {
089: IWorkingSet[] sets = typedSets[i];
090: for (int j = 0; j < sets.length; j++) {
091: IWorkingSet set = sets[j];
092:
093: // only add visible sets
094: // if (set.isVisible()) {
095: ActionContributionItem item = new ActionContributionItem(
096: new ToggleWorkingSetAction(set));
097: item.fill(menu, -1);
098: // }
099: }
100: Separator separator = new Separator();
101: separator.fill(menu, -1);
102: }
103:
104: ActionContributionItem item = new ActionContributionItem(
105: new ManageWorkingSetsAction());
106: item.fill(menu, -1);
107:
108: }
109:
110: private IWorkingSet[] getEnabledSets() {
111: return getWindow().getActivePage().getWorkingSets();
112: }
113:
114: private boolean isWorkingSetEnabled(IWorkingSet set) {
115: IWorkingSet[] enabledSets = getEnabledSets();
116: for (int i = 0; i < enabledSets.length; i++) {
117: if (enabledSets[i].equals(set)) {
118: return true;
119: }
120: }
121: return false;
122: }
123:
124: public void run(IAction action) {
125: ConfigureWindowWorkingSetsDialog dialog = new ConfigureWindowWorkingSetsDialog(
126: getWindow());
127: if (dialog.open() == Window.OK) {
128:
129: }
130:
131: }
132: }
133:
134: class ConfigureWindowWorkingSetsDialog extends
135: SimpleWorkingSetSelectionDialog {
136:
137: private IWorkbenchWindow window;
138:
139: protected ConfigureWindowWorkingSetsDialog(IWorkbenchWindow window) {
140: super (window.getShell(), null, window.getActivePage()
141: .getWorkingSets(), true);
142: this .window = window;
143: setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title_multiSelect);
144: setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message_multiSelect);
145: }
146:
147: protected void okPressed() {
148: super.okPressed();
149: window.getActivePage().setWorkingSets(getSelection());
150: }
151: }
|