KickJava   Java API By Example, From Geeks To Geeks.

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


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  * 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.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.jface.wizard.WizardDialog;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.IWorkingSet;
35 import org.eclipse.ui.IWorkingSetManager;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.dialogs.IWorkingSetEditWizard;
38 import org.eclipse.ui.dialogs.IWorkingSetNewWizard;
39 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
40 import org.eclipse.ui.dialogs.SelectionDialog;
41 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
42 import org.eclipse.ui.internal.WorkbenchMessages;
43 import org.eclipse.ui.internal.WorkbenchPlugin;
44 import org.eclipse.ui.internal.WorkingSet;
45 import org.eclipse.ui.internal.registry.WorkingSetRegistry;
46
47 /**
48  * Abstract baseclass for various working set dialogs.
49  *
50  * @since 3.2
51  */

52 public abstract class AbstractWorkingSetDialog extends SelectionDialog
53         implements IWorkingSetSelectionDialog {
54
55     private static final int ID_NEW = IDialogConstants.CLIENT_ID + 1;
56     private static final int ID_DETAILS = ID_NEW + 1;
57     private static final int ID_REMOVE = ID_DETAILS + 1;
58     private static final int ID_SELECTALL = ID_REMOVE + 1;
59     private static final int ID_DESELECTALL = ID_SELECTALL + 1;
60     
61     private Button newButton;
62
63     private Button detailsButton;
64
65     private Button removeButton;
66     
67     private Button selectAllButton;
68     
69     private Button deselectAllButton;
70
71     private IWorkingSet[] result;
72
73     private List JavaDoc addedWorkingSets;
74
75     private List JavaDoc removedWorkingSets;
76
77     private Map JavaDoc editedWorkingSets;
78
79     private List JavaDoc removedMRUWorkingSets;
80
81     private Set JavaDoc workingSetIds;
82
83     protected AbstractWorkingSetDialog(Shell parentShell, String JavaDoc[] workingSetIds) {
84         super(parentShell);
85         if (workingSetIds != null) {
86             this.workingSetIds = new HashSet JavaDoc();
87             for (int i = 0; i < workingSetIds.length; i++) {
88                 this.workingSetIds.add(workingSetIds[i]);
89             }
90         }
91     }
92
93     /**
94      * Return the set of supported working set types.
95      *
96      * @return the supported working set types
97      */

98     protected Set JavaDoc getSupportedWorkingSetIds() {
99         return workingSetIds;
100     }
101
102     /**
103      * Adds the modify buttons to the dialog.
104      *
105      * @param composite
106      * Composite to add the buttons to
107      */

108     protected void addModifyButtons(Composite composite) {
109         Composite buttonComposite = new Composite(composite, SWT.RIGHT);
110         GridLayout layout = new GridLayout();
111         layout.marginHeight = layout.marginWidth = 0;
112         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
113         buttonComposite.setLayout(layout);
114         GridData data = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.GRAB_VERTICAL);
115         buttonComposite.setLayoutData(data);
116
117         newButton = createButton(buttonComposite, ID_NEW,
118                 WorkbenchMessages.WorkingSetSelectionDialog_newButton_label,
119                 false);
120         newButton.addSelectionListener(new SelectionAdapter() {
121             public void widgetSelected(SelectionEvent e) {
122                 createWorkingSet();
123             }
124         });
125
126         detailsButton = createButton(
127                 buttonComposite,
128                 ID_DETAILS,
129                 WorkbenchMessages.WorkingSetSelectionDialog_detailsButton_label,
130                 false);
131         detailsButton.setEnabled(false);
132         detailsButton.addSelectionListener(new SelectionAdapter() {
133             public void widgetSelected(SelectionEvent e) {
134                 editSelectedWorkingSet();
135             }
136         });
137
138         removeButton = createButton(buttonComposite, ID_REMOVE,
139                 WorkbenchMessages.WorkingSetSelectionDialog_removeButton_label,
140                 false);
141         removeButton.setEnabled(false);
142         removeButton.addSelectionListener(new SelectionAdapter() {
143             public void widgetSelected(SelectionEvent e) {
144                 removeSelectedWorkingSets();
145             }
146         });
147         
148         layout.numColumns = 1; // must manually reset the number of columns because createButton increments it - we want these buttons to be laid out vertically.
149
}
150
151     /**
152      * Add the select/deselect buttons.
153      *
154      * @param composite Composite to add the buttons to
155      */

156     protected void addSelectionButtons(Composite composite) {
157         Composite buttonComposite = new Composite(composite, SWT.NONE);
158         GridLayout layout = new GridLayout(2, false);
159         layout.marginHeight = layout.marginWidth = 0;
160         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
161         buttonComposite.setLayout(layout);
162         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
163         buttonComposite.setLayoutData(data);
164         
165         selectAllButton = createButton(
166                 buttonComposite,
167                 ID_SELECTALL,
168                 WorkbenchMessages.SelectionDialog_selectLabel,
169                 false);
170         selectAllButton.addSelectionListener(new SelectionAdapter() {
171             public void widgetSelected(SelectionEvent e) {
172                 selectAllSets();
173             }
174         });
175         
176         deselectAllButton = createButton(
177                 buttonComposite,
178                 ID_DESELECTALL,
179                 WorkbenchMessages.SelectionDialog_deselectLabel,
180                 false);
181         deselectAllButton.addSelectionListener(new SelectionAdapter() {
182             public void widgetSelected(SelectionEvent e) {
183                 deselectAllSets();
184             }
185         });
186     }
187     
188     /**
189      * Select all working sets.
190      */

191     protected abstract void selectAllSets();
192     
193     /**
194      * Deselect all working sets.
195      */

196     protected abstract void deselectAllSets();
197
198     /**
199      * Opens a working set wizard for editing the currently selected working
200      * set.
201      *
202      * @see org.eclipse.ui.dialogs.IWorkingSetPage
203      */

204     void editSelectedWorkingSet() {
205         IWorkingSetManager manager = WorkbenchPlugin.getDefault()
206                 .getWorkingSetManager();
207         IWorkingSet editWorkingSet = (IWorkingSet) getSelectedWorkingSets()
208                 .get(0);
209         IWorkingSetEditWizard wizard = manager
210                 .createWorkingSetEditWizard(editWorkingSet);
211         WizardDialog dialog = new WizardDialog(getShell(), wizard);
212         IWorkingSet originalWorkingSet = (IWorkingSet) editedWorkingSets
213                 .get(editWorkingSet);
214         boolean firstEdit = originalWorkingSet == null;
215
216         // save the original working set values for restoration when selection
217
// dialog is cancelled.
218
if (firstEdit) {
219             originalWorkingSet = new WorkingSet(editWorkingSet.getName(),
220                     editWorkingSet.getLabel(), editWorkingSet.getElements());
221         } else {
222             editedWorkingSets.remove(editWorkingSet);
223         }
224         dialog.create();
225         PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
226                 IWorkbenchHelpContextIds.WORKING_SET_EDIT_WIZARD);
227         if (dialog.open() == Window.OK) {
228             editWorkingSet = wizard.getSelection();
229             availableWorkingSetsChanged();
230             // make sure ok button is enabled when the selected working set
231
// is edited. Fixes bug 33386.
232
updateButtonAvailability();
233         }
234         editedWorkingSets.put(editWorkingSet, originalWorkingSet);
235     }
236
237     /**
238      * Opens a working set wizard for creating a new working set.
239      */

240     void createWorkingSet() {
241         IWorkingSetManager manager = WorkbenchPlugin.getDefault()
242                 .getWorkingSetManager();
243         String JavaDoc ids[] = null;
244         if (workingSetIds != null) {
245             ids = (String JavaDoc[]) workingSetIds.toArray(new String JavaDoc[workingSetIds
246                     .size()]);
247         }
248         IWorkingSetNewWizard wizard = manager.createWorkingSetNewWizard(ids);
249         // the wizard can never be null since we have at least a resource
250
// working set
251
// creation page
252
WizardDialog dialog = new WizardDialog(getShell(), wizard);
253
254         dialog.create();
255         PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
256                 IWorkbenchHelpContextIds.WORKING_SET_NEW_WIZARD);
257         if (dialog.open() == Window.OK) {
258             IWorkingSet workingSet = wizard.getSelection();
259             manager.addWorkingSet(workingSet);
260             addedWorkingSets.add(workingSet);
261             availableWorkingSetsChanged();
262         }
263     }
264
265     protected abstract List JavaDoc getSelectedWorkingSets();
266
267     /**
268      * Notifies the dialog that there has been a change to the sets available
269      * for use. In other words, the user has either added, deleted or renamed a
270      * set.
271      * <p>
272      * Subclasses should override, but should call <code>super.availableWorkingSetsChanged</code>
273      * to update the selection button enablements.
274      * </p>
275      */

276     protected void availableWorkingSetsChanged() {
277         boolean enable = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets().length > 0;
278         if (!(selectAllButton == null || selectAllButton.isDisposed())){
279             selectAllButton.setEnabled(enable);
280         }
281         if (!(deselectAllButton == null || deselectAllButton.isDisposed())){
282             deselectAllButton.setEnabled(enable);
283         }
284     }
285
286     /* (non-Javadoc)
287      * @see org.eclipse.ui.dialogs.IWorkingSetSelectionDialog#getSelection()
288      */

289     public IWorkingSet[] getSelection() {
290         return result;
291     }
292
293     /* (non-Javadoc)
294      * @see org.eclipse.ui.dialogs.IWorkingSetSelectionDialog#setSelection(org.eclipse.ui.IWorkingSet[])
295      */

296     public void setSelection(IWorkingSet[] selection) {
297         result = selection;
298     }
299
300     /**
301      * Overrides method in Dialog
302      *
303      * @see org.eclipse.jface.dialogs.Dialog#open()
304      */

305     public int open() {
306         addedWorkingSets = new ArrayList JavaDoc();
307         removedWorkingSets = new ArrayList JavaDoc();
308         editedWorkingSets = new HashMap JavaDoc();
309         removedMRUWorkingSets = new ArrayList JavaDoc();
310         return super.open();
311     }
312
313     /**
314      * Return the list of working sets that were added during the life of this
315      * dialog.
316      *
317      * @return the working sets
318      */

319     protected final List JavaDoc getAddedWorkingSets() {
320         return addedWorkingSets;
321     }
322
323     /**
324      * Return the map of working sets that were edited during the life of this
325      * dialog.
326      *
327      * @return the working sets
328      */

329     protected final Map JavaDoc getEditedWorkingSets() {
330         return editedWorkingSets;
331     }
332
333     /**
334      * Return the list of working sets that were removed from the MRU list
335      * during the life of this dialog.
336      *
337      * @return the working sets
338      */

339     protected final List JavaDoc getRemovedMRUWorkingSets() {
340         return removedMRUWorkingSets;
341     }
342
343     /**
344      * Return the list of working sets that were removed during the life of this
345      * dialog.
346      *
347      * @return the working sets
348      */

349     protected final List JavaDoc getRemovedWorkingSets() {
350         return removedWorkingSets;
351     }
352
353     /**
354      * Updates the modify buttons' enabled state based on the current seleciton.
355      */

356     protected void updateButtonAvailability() {
357         List JavaDoc selection = getSelectedWorkingSets();
358         boolean hasSelection = selection != null && !selection.isEmpty();
359         boolean hasSingleSelection = hasSelection;
360         WorkingSetRegistry registry = WorkbenchPlugin.getDefault()
361                 .getWorkingSetRegistry();
362
363         newButton.setEnabled(registry.hasNewPageWorkingSetDescriptor());
364
365         removeButton.setEnabled(hasSelection);
366
367         IWorkingSet selectedWorkingSet = null;
368         if (hasSelection) {
369             hasSingleSelection = selection.size() == 1;
370             if (hasSingleSelection) {
371                 selectedWorkingSet = (IWorkingSet) selection
372                         .get(0);
373             }
374         }
375         detailsButton.setEnabled(hasSingleSelection
376                 && selectedWorkingSet.isEditable());
377
378         getOkButton().setEnabled(true);
379     }
380
381     /**
382      * Removes the selected working sets from the workbench.
383      */

384     protected void removeSelectedWorkingSets() {
385         List JavaDoc selection = getSelectedWorkingSets();
386         removeSelectedWorkingSets(selection);
387     }
388
389     /**
390      * Remove the working sets contained in the provided selection from the
391      * working set manager.
392      *
393      * @param selection
394      * the sets
395      */

396     protected void removeSelectedWorkingSets(List JavaDoc selection) {
397         IWorkingSetManager manager = WorkbenchPlugin.getDefault()
398                 .getWorkingSetManager();
399         Iterator JavaDoc iter = selection.iterator();
400         while (iter.hasNext()) {
401             IWorkingSet workingSet = (IWorkingSet) iter.next();
402             if (getAddedWorkingSets().contains(workingSet)) {
403                 getAddedWorkingSets().remove(workingSet);
404             } else {
405                 IWorkingSet[] recentWorkingSets = manager
406                         .getRecentWorkingSets();
407                 for (int i = 0; i < recentWorkingSets.length; i++) {
408                     if (workingSet.equals(recentWorkingSets[i])) {
409                         getRemovedMRUWorkingSets().add(workingSet);
410                         break;
411                     }
412                 }
413                 getRemovedWorkingSets().add(workingSet);
414             }
415             manager.removeWorkingSet(workingSet);
416         }
417         availableWorkingSetsChanged();
418     }
419 }
420
Popular Tags