KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > WorkingSetSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font
11  * should be activated and used by other components.
12  *******************************************************************************/

13 package org.eclipse.ui.internal.dialogs;
14
15 import java.util.Arrays JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.viewers.ArrayContentProvider;
23 import org.eclipse.jface.viewers.CheckStateChangedEvent;
24 import org.eclipse.jface.viewers.CheckboxTableViewer;
25 import org.eclipse.jface.viewers.DoubleClickEvent;
26 import org.eclipse.jface.viewers.ICheckStateListener;
27 import org.eclipse.jface.viewers.IDoubleClickListener;
28 import org.eclipse.jface.viewers.ILabelProvider;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.ISelectionChangedListener;
31 import org.eclipse.jface.viewers.IStructuredContentProvider;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Shell;
45 import org.eclipse.ui.IWorkbenchWindow;
46 import org.eclipse.ui.IWorkingSet;
47 import org.eclipse.ui.IWorkingSetManager;
48 import org.eclipse.ui.PlatformUI;
49 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
50 import org.eclipse.ui.internal.AggregateWorkingSet;
51 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
52 import org.eclipse.ui.internal.WorkbenchMessages;
53 import org.eclipse.ui.internal.WorkbenchPlugin;
54 import org.eclipse.ui.internal.util.Util;
55 import org.eclipse.ui.model.WorkbenchViewerComparator;
56
57 /**
58  * A working set selection dialog displays a list of working
59  * sets available in the workbench.
60  *
61  * @see IWorkingSetSelectionDialog
62  * @since 2.0
63  */

64 public class WorkingSetSelectionDialog extends AbstractWorkingSetDialog {
65     private final static int SIZING_SELECTION_WIDGET_HEIGHT = 200;
66
67     private final static int SIZING_SELECTION_WIDGET_WIDTH = 50;
68
69     private ILabelProvider labelProvider;
70
71     private IStructuredContentProvider contentProvider;
72
73     private CheckboxTableViewer listViewer;
74
75     private boolean multiSelect;
76     
77     private IWorkbenchWindow workbenchWindow;
78
79     private Button buttonWindowSet;
80
81     private Button buttonNoSet;
82
83     private Button buttonSelectedSets;
84
85     /**
86      * Creates a working set selection dialog.
87      *
88      * @param parentShell the parent shell
89      * @param multi true=more than one working set can be chosen
90      * in the dialog. false=only one working set can be chosen. Multiple
91      * working sets can still be selected and removed from the list but
92      * the dialog can only be closed when a single working set is selected.
93      * @param workingSetIds a list of working set ids which are valid workings sets
94      * to be selected, created, removed or edited, or <code>null</code> if all currently
95      * available working set types are valid
96      */

97     public WorkingSetSelectionDialog(Shell parentShell, boolean multi, String JavaDoc[] workingSetIds) {
98         super(parentShell, workingSetIds);
99         setShellStyle(getShellStyle() | SWT.RESIZE);
100         initWorkbenchWindow();
101         
102         contentProvider = new ArrayContentProvider();
103         labelProvider = new WorkingSetLabelProvider();
104         multiSelect = multi;
105         if (multiSelect) {
106             setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title_multiSelect);
107             setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message_multiSelect);
108         } else {
109             setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title);
110             setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message);
111         }
112                         
113     }
114
115     /**
116      * Determine what window this dialog is being opened on. This impacts the
117      * returned working set in the case where the user chooses the window set.
118      *
119      * @since 3.2
120      */

121     private void initWorkbenchWindow() {
122         Shell shellToCheck = getShell();
123
124         workbenchWindow = Util.getWorkbenchWindowForShell(shellToCheck);
125     }
126
127     /**
128      * Overrides method from Dialog.
129      *
130      * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
131      */

132     protected void cancelPressed() {
133         restoreAddedWorkingSets();
134         restoreChangedWorkingSets();
135         restoreRemovedWorkingSets();
136         setSelection(null);
137         super.cancelPressed();
138     }
139
140     /**
141      * Overrides method from Window.
142      *
143      * @see org.eclipse.jface.window.Window#configureShell(Shell)
144      */

145     protected void configureShell(Shell shell) {
146         super.configureShell(shell);
147         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
148                 IWorkbenchHelpContextIds.WORKING_SET_SELECTION_DIALOG);
149     }
150
151     /**
152      * Overrides method from Dialog.
153      * Create the dialog widgets.
154      *
155      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
156      */

157     protected Control createDialogArea(Composite parent) {
158         initializeDialogUnits(parent);
159         
160         Composite composite = (Composite) super.createDialogArea(parent);
161
162         createMessageArea(composite);
163
164         SelectionListener listener = new SelectionAdapter() {
165             public void widgetSelected(SelectionEvent e) {
166                 updateButtonAvailability();
167             }
168         };
169         
170         buttonWindowSet = new Button(composite, SWT.RADIO);
171         buttonWindowSet.setText(WorkbenchMessages.WindowWorkingSets);
172         buttonWindowSet.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
173         buttonWindowSet.addSelectionListener(listener);
174
175         buttonNoSet = new Button(composite, SWT.RADIO);
176         buttonNoSet.setText(WorkbenchMessages.NoWorkingSet);
177         buttonNoSet.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
178         buttonNoSet.addSelectionListener(listener);
179
180         buttonSelectedSets = new Button(composite, SWT.RADIO);
181         buttonSelectedSets.setText(WorkbenchMessages.SelectedWorkingSets);
182         buttonSelectedSets.addSelectionListener(listener);
183
184         switch (getInitialRadioSelection()) {
185         case 0:
186             buttonWindowSet.setSelection(true);
187             break;
188         case 1:
189             buttonNoSet.setSelection(true);
190             break;
191         case 2:
192             buttonSelectedSets.setSelection(true);
193             break;
194         }
195         buttonSelectedSets
196                 .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
197         
198         Composite viewerComposite = new Composite(composite, SWT.NONE);
199         GridLayout layout = new GridLayout(2, false);
200         layout.marginHeight = layout.marginWidth = 0;
201         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
202         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
203         viewerComposite.setLayout(layout);
204         
205         GridData data = new GridData(GridData.FILL_BOTH);
206         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
207         data.widthHint = SIZING_SELECTION_WIDGET_WIDTH + 300; // fudge? I like fudge.
208
viewerComposite.setLayoutData(data);
209         
210         listViewer = CheckboxTableViewer.newCheckList(viewerComposite,
211                 SWT.BORDER | SWT.MULTI);
212         data = new GridData(GridData.FILL_BOTH);
213         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
214         data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
215         listViewer.getTable().setLayoutData(data);
216
217         listViewer.setLabelProvider(labelProvider);
218         listViewer.setContentProvider(contentProvider);
219         listViewer.setComparator(new WorkbenchViewerComparator());
220         
221         listViewer.addFilter(new WorkingSetFilter(getSupportedWorkingSetIds()));
222         
223         listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
224             public void selectionChanged(SelectionChangedEvent event) {
225                 handleSelectionChanged();
226             }
227         });
228         listViewer.addDoubleClickListener(new IDoubleClickListener() {
229             public void doubleClick(DoubleClickEvent event) {
230                 Object JavaDoc obj = ((IStructuredSelection) listViewer.getSelection())
231                         .getFirstElement();
232                 listViewer.setCheckedElements(new Object JavaDoc[] {obj});
233                 buttonWindowSet.setSelection(false);
234                 buttonNoSet.setSelection(false);
235                 buttonSelectedSets.setSelection(true);
236                 okPressed();
237             }
238         });
239         listViewer.addCheckStateListener(new ICheckStateListener() {
240             public void checkStateChanged(CheckStateChangedEvent event) {
241                 // implicitly select the third radio button
242
buttonWindowSet.setSelection(false);
243                 buttonNoSet.setSelection(false);
244                 buttonSelectedSets.setSelection(true);
245             }
246         });
247
248         addModifyButtons(viewerComposite);
249         
250         addSelectionButtons(composite);
251         
252
253         listViewer.setInput(Arrays.asList(WorkbenchPlugin.getDefault()
254                 .getWorkingSetManager().getWorkingSets()));
255         List JavaDoc initialElementSelections = getInitialElementSelections();
256         if (multiSelect) {
257             listViewer.setCheckedElements(initialElementSelections.toArray());
258         } else if (!initialElementSelections.isEmpty()) {
259             IWorkingSet set = (IWorkingSet) initialElementSelections.get(0);
260             if (set instanceof AggregateWorkingSet) {
261                 AggregateWorkingSet aggregate = (AggregateWorkingSet) set;
262                 listViewer.setCheckedElements(aggregate.getComponents());
263             }
264             else {
265                 listViewer.setCheckedElements(initialElementSelections.toArray());
266             }
267         }
268         
269         availableWorkingSetsChanged();
270         Dialog.applyDialogFont(composite);
271         
272         return composite;
273     }
274
275     private int getInitialRadioSelection() {
276             IWorkingSet windowSet = workbenchWindow.getActivePage().getAggregateWorkingSet();
277             
278             int selectionIndex;
279             if (getSelection() != null && getSelection().length > 0) {
280                 if (windowSet.equals(getSelection()[0])) {
281                     selectionIndex = 0;
282                 }
283                 else {
284                     selectionIndex = 2;
285                 }
286             }
287             else {
288                 selectionIndex = 1;
289             }
290             
291         return selectionIndex;
292     }
293
294     /**
295      * Overrides method from Dialog.
296      * Sets the initial selection, if any.
297      *
298      * @see org.eclipse.jface.dialogs.Dialog#createContents(Composite)
299      */

300     protected Control createContents(Composite parent) {
301         Control control = super.createContents(parent);
302         List JavaDoc selections = getInitialElementSelections();
303         if (!selections.isEmpty()) {
304             listViewer.setSelection(new StructuredSelection(selections), true);
305         }
306         updateButtonAvailability();
307         return control;
308     }
309
310     /**
311      * Returns the selected working sets.
312      *
313      * @return the selected working sets
314      */

315     protected List JavaDoc getSelectedWorkingSets() {
316         ISelection selection = listViewer.getSelection();
317         if (selection instanceof IStructuredSelection) {
318             return ((IStructuredSelection) selection).toList();
319         }
320         return null;
321     }
322
323     /**
324      * Called when the selection has changed.
325      */

326     void handleSelectionChanged() {
327         updateButtonAvailability();
328     }
329
330     /**
331      * Sets the selected working sets as the dialog result.
332      * Overrides method from Dialog
333      *
334      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
335      */

336     protected void okPressed() {
337             if (buttonWindowSet.getSelection()) {
338                 IWorkingSet [] windowSet = new IWorkingSet[] {workbenchWindow.getActivePage().getAggregateWorkingSet()};
339                 setSelection(windowSet);
340                 setResult(Arrays.asList(getSelection()));
341             }
342             else if (buttonNoSet.getSelection()) {
343             setSelection(new IWorkingSet[0]);
344             setResult(Arrays.asList(getSelection()));
345             }
346             else if (buttonSelectedSets.getSelection()) {
347             Object JavaDoc[] untypedResult = listViewer.getCheckedElements();
348             IWorkingSet[] typedResult = new IWorkingSet[untypedResult.length];
349             System.arraycopy(untypedResult, 0, typedResult, 0,
350                     untypedResult.length);
351             // if multiselect is allowed or there was only one selected then dont create
352
// an aggregate
353
if (multiSelect || typedResult.length <= 1) {
354                 setSelection(typedResult);
355                 setResult(Arrays.asList(typedResult));
356             }
357             else {
358                 String JavaDoc setId = getAggregateIdForSets(typedResult);
359                 IWorkingSetManager workingSetManager = workbenchWindow
360                         .getWorkbench().getWorkingSetManager();
361                 IWorkingSet aggregate = workingSetManager
362                         .getWorkingSet(setId);
363                 if (aggregate == null) {
364                     aggregate = workingSetManager
365                             .createAggregateWorkingSet(
366                                     setId,
367                                     WorkbenchMessages.WorkbenchPage_workingSet_multi_label,
368                                     typedResult);
369                     workingSetManager.addWorkingSet(aggregate);
370                 }
371                 setSelection(new IWorkingSet[] {aggregate});
372                 setResult(Collections.singletonList(aggregate));
373             }
374             }
375         
376         super.okPressed();
377     }
378
379     /**
380      * Create a string that represents the name of the aggregate set composed of
381      * the supplied working sets. It's very long and not printworthy.
382      *
383      * @param typedResult the sets
384      * @return the name
385      */

386     private String JavaDoc getAggregateIdForSets(IWorkingSet[] typedResult) {
387             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
388             buffer.append("Aggregate:"); //$NON-NLS-1$
389
for (int i = 0; i < typedResult.length; i++) {
390             buffer.append(typedResult[i].getName()).append(':');
391         }
392         return buffer.toString();
393     }
394
395     /**
396      * Removes newly created working sets from the working set manager.
397      */

398     private void restoreAddedWorkingSets() {
399         IWorkingSetManager manager = WorkbenchPlugin.getDefault()
400                 .getWorkingSetManager();
401         Iterator JavaDoc iterator = getAddedWorkingSets().iterator();
402
403         while (iterator.hasNext()) {
404             manager.removeWorkingSet(((IWorkingSet) iterator.next()));
405         }
406     }
407
408     /**
409      * Rolls back changes to working sets.
410      */

411     private void restoreChangedWorkingSets() {
412         Iterator JavaDoc iterator = getEditedWorkingSets().keySet().iterator();
413
414         while (iterator.hasNext()) {
415             IWorkingSet editedWorkingSet = (IWorkingSet) iterator.next();
416             IWorkingSet originalWorkingSet = (IWorkingSet) getEditedWorkingSets()
417                     .get(editedWorkingSet);
418
419             if (editedWorkingSet.getName().equals(originalWorkingSet.getName()) == false) {
420                 editedWorkingSet.setName(originalWorkingSet.getName());
421             }
422             if (editedWorkingSet.getElements().equals(
423                     originalWorkingSet.getElements()) == false) {
424                 editedWorkingSet.setElements(originalWorkingSet.getElements());
425             }
426         }
427     }
428
429     /**
430      * Adds back removed working sets to the working set manager.
431      */

432     private void restoreRemovedWorkingSets() {
433         IWorkingSetManager manager = WorkbenchPlugin.getDefault()
434                 .getWorkingSetManager();
435         Iterator JavaDoc iterator = getRemovedWorkingSets().iterator();
436
437         while (iterator.hasNext()) {
438             manager.addWorkingSet(((IWorkingSet) iterator.next()));
439         }
440         iterator = getRemovedMRUWorkingSets().iterator();
441         while (iterator.hasNext()) {
442             manager.addRecentWorkingSet(((IWorkingSet) iterator.next()));
443         }
444     }
445
446     /**
447      * Implements IWorkingSetSelectionDialog.
448      *
449      * @see org.eclipse.ui.dialogs.IWorkingSetSelectionDialog#setSelection(IWorkingSet[])
450      */

451     public void setSelection(IWorkingSet[] workingSets) {
452         super.setSelection(workingSets);
453         setInitialSelections(workingSets == null ? new Object JavaDoc[0] : workingSets);
454     }
455
456     protected void availableWorkingSetsChanged() {
457         listViewer.setInput(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets());
458         super.availableWorkingSetsChanged();
459     }
460
461     protected void selectAllSets() {
462         listViewer.setCheckedElements(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets());
463         // implicitly select the third radio button
464
buttonWindowSet.setSelection(false);
465         buttonNoSet.setSelection(false);
466         buttonSelectedSets.setSelection(true);
467         updateButtonAvailability();
468     }
469
470     protected void deselectAllSets() {
471         listViewer.setCheckedElements(new Object JavaDoc[0]);
472         // implicitly select the third radio button
473
buttonWindowSet.setSelection(false);
474         buttonNoSet.setSelection(false);
475         buttonSelectedSets.setSelection(true);
476         updateButtonAvailability();
477     }
478 }
479
Popular Tags