KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > actions > SelectWorkingSetsAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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 JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.ActionContributionItem;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.action.Separator;
22 import org.eclipse.jface.bindings.keys.IKeyLookup;
23 import org.eclipse.jface.bindings.keys.KeyLookupFactory;
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.dialogs.IDialogConstants;
26 import org.eclipse.jface.viewers.ArrayContentProvider;
27 import org.eclipse.jface.viewers.CheckboxTableViewer;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Event;
39 import org.eclipse.swt.widgets.Menu;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.IWorkingSet;
43 import org.eclipse.ui.internal.WorkbenchMessages;
44 import org.eclipse.ui.internal.dialogs.AbstractWorkingSetDialog;
45 import org.eclipse.ui.internal.dialogs.WorkingSetFilter;
46 import org.eclipse.ui.internal.dialogs.WorkingSetLabelProvider;
47
48 /**
49  * Action to select the visible working sets for a given workbench page.
50  *
51  * @since 3.2
52  */

53 public class SelectWorkingSetsAction extends AbstractWorkingSetPulldownDelegate {
54
55     private class ManageWorkingSetsAction extends Action {
56
57         ManageWorkingSetsAction() {
58             super(WorkbenchMessages.Edit);
59         }
60
61         public void run() {
62             SelectWorkingSetsAction.this.run(this);
63         }
64     }
65
66     private class ToggleWorkingSetAction extends Action {
67         private IWorkingSet set;
68
69         ToggleWorkingSetAction(IWorkingSet set) {
70             super(set.getLabel(), IAction.AS_CHECK_BOX);
71             setImageDescriptor(set.getImageDescriptor());
72             this.set = set;
73             setChecked(isWorkingSetEnabled(set));
74         }
75
76         public void runWithEvent(Event event) {
77             
78             Set JavaDoc newList = new HashSet JavaDoc(Arrays.asList(getWindow().getActivePage()
79                     .getWorkingSets()));
80
81             if (isChecked()) {
82                 // if the primary modifier key is down then clear the list
83
// first. this makes the selection exclusive rather than
84
// additive.
85
boolean modified = (event.stateMask & KeyLookupFactory
86                         .getDefault().formalModifierLookup(IKeyLookup.M1_NAME)) != 0;
87                 
88                 if (modified)
89                     newList.clear();
90                 newList.add(set);
91             } else {
92                 newList.remove(set);
93             }
94
95             getWindow().getActivePage().setWorkingSets(
96                     (IWorkingSet[]) newList.toArray(new IWorkingSet[newList
97                             .size()]));
98         }
99     }
100
101     protected void fillMenu(Menu menu) {
102         IWorkingSet[][] typedSets = splitSets();
103
104         for (int i = 0; i < typedSets.length; i++) {
105             IWorkingSet[] sets = typedSets[i];
106             for (int j = 0; j < sets.length; j++) {
107                 IWorkingSet set = sets[j];
108
109                 // only add visible sets
110
// if (set.isVisible()) {
111
ActionContributionItem item = new ActionContributionItem(
112                         new ToggleWorkingSetAction(set));
113                 item.fill(menu, -1);
114                 // }
115
}
116             Separator separator = new Separator();
117             separator.fill(menu, -1);
118         }
119
120         ActionContributionItem item = new ActionContributionItem(
121                 new ManageWorkingSetsAction());
122         item.fill(menu, -1);
123
124     }
125
126     private IWorkingSet[] getEnabledSets() {
127         return getWindow().getActivePage().getWorkingSets();
128     }
129
130     private boolean isWorkingSetEnabled(IWorkingSet set) {
131         IWorkingSet[] enabledSets = getEnabledSets();
132         for (int i = 0; i < enabledSets.length; i++) {
133             if (enabledSets[i].equals(set)) {
134                 return true;
135             }
136         }
137         return false;
138     }
139
140     public void run(IAction action) {
141         ConfigureWindowWorkingSetsDialog dialog = new ConfigureWindowWorkingSetsDialog(
142                 getWindow());
143         if (dialog.open() == Window.OK) {
144
145         }
146
147     }
148 }
149
150 class ConfigureWindowWorkingSetsDialog extends AbstractWorkingSetDialog {
151
152     private final static int SIZING_SELECTION_WIDGET_HEIGHT = 200;
153
154     private final static int SIZING_SELECTION_WIDGET_WIDTH = 50;
155     
156     private IWorkbenchWindow window;
157
158     private CheckboxTableViewer viewer;
159
160     protected ConfigureWindowWorkingSetsDialog(IWorkbenchWindow window) {
161         super(window.getShell(), null);
162         setShellStyle(getShellStyle() | SWT.RESIZE);
163         this.window = window;
164         setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title_multiSelect);
165         setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message_multiSelect);
166     }
167
168     protected Control createDialogArea(Composite parent) {
169         initializeDialogUnits(parent);
170         
171         Composite composite = (Composite) super.createDialogArea(parent);
172         
173         Composite viewerComposite = new Composite(composite, SWT.NONE);
174         GridLayout layout = new GridLayout(2, false);
175         layout.marginHeight = layout.marginWidth = 0;
176         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
177         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
178         viewerComposite.setLayout(layout);
179         
180         GridData data = new GridData(GridData.FILL_BOTH);
181         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
182         data.widthHint = SIZING_SELECTION_WIDGET_WIDTH + 300; // fudge? I like fudge.
183
viewerComposite.setLayoutData(data);
184         
185         viewer = CheckboxTableViewer.newCheckList(viewerComposite, SWT.BORDER);
186         viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
187         viewer.setLabelProvider(new WorkingSetLabelProvider());
188         viewer.setContentProvider(new ArrayContentProvider());
189         viewer.addFilter(new WorkingSetFilter(null));
190         viewer.setInput(window.getWorkbench().getWorkingSetManager()
191                 .getWorkingSets());
192         
193         viewer.setCheckedElements(window.getActivePage().getWorkingSets());
194         
195         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
196             public void selectionChanged(SelectionChangedEvent event) {
197                 handleSelectionChanged();
198             }
199         });
200
201         data = new GridData(GridData.FILL_BOTH);
202         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
203         data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
204
205         viewer.getControl().setLayoutData(data);
206         addModifyButtons(viewerComposite);
207         
208         addSelectionButtons(composite);
209         
210         availableWorkingSetsChanged();
211         
212         Dialog.applyDialogFont(composite);
213         
214         return composite;
215     }
216
217     protected void okPressed() {
218         Object JavaDoc[] selection = viewer.getCheckedElements();
219         IWorkingSet[] workingSets = new IWorkingSet[selection.length];
220         System.arraycopy(selection, 0, workingSets, 0, selection.length);
221         window.getActivePage().setWorkingSets(workingSets);
222         super.okPressed();
223     }
224
225     protected List JavaDoc getSelectedWorkingSets() {
226         ISelection selection = viewer.getSelection();
227         if (selection instanceof IStructuredSelection) {
228             return ((IStructuredSelection) selection).toList();
229         }
230         return null;
231     }
232
233     protected void availableWorkingSetsChanged() {
234         viewer.setInput(window.getWorkbench().getWorkingSetManager()
235                 .getWorkingSets());
236         super.availableWorkingSetsChanged();
237     }
238     
239     /**
240      * Called when the selection has changed.
241      */

242     void handleSelectionChanged() {
243         updateButtonAvailability();
244     }
245     
246     protected void configureShell(Shell shell) {
247             super.configureShell(shell);
248     }
249
250     protected void selectAllSets() {
251         viewer.setCheckedElements(window.getWorkbench().getWorkingSetManager()
252                 .getWorkingSets());
253         updateButtonAvailability();
254     }
255
256     protected void deselectAllSets() {
257         viewer.setCheckedElements(new Object JavaDoc[0]);
258         updateButtonAvailability();
259     }
260 }
261
Popular Tags