KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > WorkingSetConfigurationDialog


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.jdt.internal.ui.workingsets;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.eclipse.core.runtime.Assert;
26 import org.eclipse.core.runtime.IAdaptable;
27
28 import org.eclipse.core.resources.IProject;
29
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Shell;
40
41 import org.eclipse.jface.dialogs.IDialogConstants;
42 import org.eclipse.jface.resource.ImageDescriptor;
43 import org.eclipse.jface.viewers.CheckStateChangedEvent;
44 import org.eclipse.jface.viewers.CheckboxTableViewer;
45 import org.eclipse.jface.viewers.ICheckStateListener;
46 import org.eclipse.jface.viewers.ISelection;
47 import org.eclipse.jface.viewers.ISelectionChangedListener;
48 import org.eclipse.jface.viewers.IStructuredContentProvider;
49 import org.eclipse.jface.viewers.IStructuredSelection;
50 import org.eclipse.jface.viewers.LabelProvider;
51 import org.eclipse.jface.viewers.SelectionChangedEvent;
52 import org.eclipse.jface.viewers.StructuredSelection;
53 import org.eclipse.jface.viewers.Viewer;
54 import org.eclipse.jface.viewers.ViewerFilter;
55 import org.eclipse.jface.window.Window;
56 import org.eclipse.jface.wizard.WizardDialog;
57
58 import org.eclipse.ui.IWorkingSet;
59 import org.eclipse.ui.IWorkingSetManager;
60 import org.eclipse.ui.PlatformUI;
61 import org.eclipse.ui.dialogs.IWorkingSetEditWizard;
62 import org.eclipse.ui.dialogs.IWorkingSetNewWizard;
63 import org.eclipse.ui.dialogs.SelectionDialog;
64
65 public class WorkingSetConfigurationDialog extends SelectionDialog {
66
67     private static class WorkingSetLabelProvider extends LabelProvider {
68         private Map JavaDoc fIcons;
69         public WorkingSetLabelProvider() {
70             fIcons= new Hashtable JavaDoc();
71         }
72         public void dispose() {
73             Iterator JavaDoc iterator= fIcons.values().iterator();
74             while (iterator.hasNext()) {
75                 Image icon= (Image)iterator.next();
76                 icon.dispose();
77             }
78             super.dispose();
79         }
80         public Image getImage(Object JavaDoc object) {
81             Assert.isTrue(object instanceof IWorkingSet);
82             IWorkingSet workingSet= (IWorkingSet)object;
83             ImageDescriptor imageDescriptor= workingSet.getImage();
84             if (imageDescriptor == null)
85                 return null;
86             Image icon= (Image)fIcons.get(imageDescriptor);
87             if (icon == null) {
88                 icon= imageDescriptor.createImage();
89                 fIcons.put(imageDescriptor, icon);
90             }
91             return icon;
92         }
93         public String JavaDoc getText(Object JavaDoc object) {
94             Assert.isTrue(object instanceof IWorkingSet);
95             IWorkingSet workingSet= (IWorkingSet)object;
96             return workingSet.getName();
97         }
98     }
99     
100     private class Filter extends ViewerFilter {
101         public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
102             IWorkingSet ws= (IWorkingSet)element;
103             String JavaDoc id= ws.getId();
104             return OthersWorkingSetUpdater.ID.equals(id) ||
105                 JavaWorkingSetUpdater.ID.equals(id) || isCompatible(ws) || isActive(ws);
106         }
107         private boolean isCompatible(IWorkingSet set) {
108             if (!set.isSelfUpdating() || set.isAggregateWorkingSet())
109                 return false;
110             IAdaptable[] elements= set.getElements();
111             if (elements.length == 0)
112                 return false;
113             for (int i= 0; i < elements.length; i++) {
114                 IAdaptable element= elements[i];
115                 IProject p= (IProject)element.getAdapter(IProject.class);
116                 if (p == null || !p.exists())
117                     return false;
118             }
119             return true;
120         }
121         private boolean isActive(IWorkingSet workingSet) {
122             return fActiveWorkingSets.contains(workingSet);
123         }
124     }
125
126     private List JavaDoc fAllWorkingSets;
127     private List JavaDoc fActiveWorkingSets;
128     private CheckboxTableViewer fTableViewer;
129
130     private Button fNewButton;
131     private Button fEditButton;
132     private Button fRemoveButton;
133     private Button fUpButton;
134     private Button fDownButton;
135     private Button fSelectAll;
136     private Button fDeselectAll;
137
138     private IWorkingSet[] fResult;
139     private List JavaDoc fAddedWorkingSets;
140     private List JavaDoc fRemovedWorkingSets;
141     private Map JavaDoc fEditedWorkingSets;
142     private List JavaDoc fRemovedMRUWorkingSets;
143
144     private int nextButtonId= IDialogConstants.CLIENT_ID + 1;
145
146     public WorkingSetConfigurationDialog(Shell parentShell, IWorkingSet[] allWorkingSets, IWorkingSet[] activeWorkingSets) {
147         super(parentShell);
148         setTitle(WorkingSetMessages.WorkingSetConfigurationDialog_title);
149         setMessage(WorkingSetMessages.WorkingSetConfigurationDialog_message);
150         fAllWorkingSets= new ArrayList JavaDoc(allWorkingSets.length);
151         fActiveWorkingSets= Arrays.asList(activeWorkingSets);
152         Filter filter= new Filter();
153         for (int i= 0; i < allWorkingSets.length; i++) {
154             if (filter.select(null, null, allWorkingSets[i]))
155                 fAllWorkingSets.add(allWorkingSets[i]);
156         }
157         setShellStyle(getShellStyle() | SWT.RESIZE);
158     }
159
160     /**
161      * Returns the selected working sets
162      *
163      * @return the selected working sets
164      */

165     public IWorkingSet[] getSelection() {
166         return fResult;
167     }
168
169     /**
170      * Sets the initial selection
171      *
172      * @param workingSets the initial selection
173      */

174     public void setSelection(IWorkingSet[] workingSets) {
175         fResult= workingSets;
176         setInitialSelections(workingSets);
177     }
178     
179     /**
180      * {@inheritDoc}
181      */

182     protected Control createContents(Composite parent) {
183         Control control= super.createContents(parent);
184         setInitialSelection();
185         updateButtonAvailability();
186         return control;
187     }
188     
189     /**
190      * {@inheritDoc}
191      */

192     protected Control createDialogArea(Composite parent) {
193         Composite composite= (Composite)super.createDialogArea(parent);
194         composite.setFont(parent.getFont());
195
196         createMessageArea(composite);
197         Composite inner= new Composite(composite, SWT.NONE);
198         inner.setFont(composite.getFont());
199         inner.setLayoutData(new GridData(GridData.FILL_BOTH));
200         GridLayout layout= new GridLayout();
201         layout.numColumns= 2;
202         layout.marginHeight= 0;
203         layout.marginWidth= 0;
204         inner.setLayout(layout);
205         createTableViewer(inner);
206         createOrderButtons(inner);
207         createModifyButtons(composite);
208         fTableViewer.setInput(fAllWorkingSets);
209
210         return composite;
211     }
212
213     private void createTableViewer(Composite parent) {
214         fTableViewer= CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.MULTI);
215         fTableViewer.addCheckStateListener(new ICheckStateListener() {
216             public void checkStateChanged(CheckStateChangedEvent event) {
217                 updateButtonAvailability();
218             }
219         });
220         GridData data= new GridData(GridData.FILL_BOTH);
221         data.heightHint= convertHeightInCharsToPixels(20);
222         data.widthHint= convertWidthInCharsToPixels(50);
223         fTableViewer.getTable().setLayoutData(data);
224         fTableViewer.getTable().setFont(parent.getFont());
225
226         fTableViewer.addFilter(new Filter());
227         fTableViewer.setLabelProvider(new WorkingSetLabelProvider());
228         fTableViewer.setContentProvider(new IStructuredContentProvider() {
229             public Object JavaDoc[] getElements(Object JavaDoc element) {
230                 return ((List JavaDoc)element).toArray();
231             }
232             public void dispose() {
233             }
234             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
235             }
236         });
237         fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
238             public void selectionChanged(SelectionChangedEvent event) {
239                 handleSelectionChanged();
240             }
241         });
242     }
243
244     private void createModifyButtons(Composite composite) {
245         Composite buttonComposite= new Composite(composite, SWT.RIGHT);
246         GridLayout layout= new GridLayout();
247         layout.numColumns= 2;
248         buttonComposite.setLayout(layout);
249         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL);
250         data.grabExcessHorizontalSpace= true;
251         composite.setData(data);
252
253         fNewButton= createButton(buttonComposite, nextButtonId++,
254             WorkingSetMessages.WorkingSetConfigurationDialog_new_label, false);
255         fNewButton.setFont(composite.getFont());
256         fNewButton.addSelectionListener(new SelectionAdapter() {
257             public void widgetSelected(SelectionEvent e) {
258                 createWorkingSet();
259             }
260         });
261
262         fEditButton= createButton(buttonComposite, nextButtonId++,
263             WorkingSetMessages.WorkingSetConfigurationDialog_edit_label, false);
264         fEditButton.setFont(composite.getFont());
265         fEditButton.addSelectionListener(new SelectionAdapter() {
266             public void widgetSelected(SelectionEvent e) {
267                 editSelectedWorkingSet();
268             }
269         });
270
271         fRemoveButton= createButton(buttonComposite, nextButtonId++,
272             WorkingSetMessages.WorkingSetConfigurationDialog_remove_label, false);
273         fRemoveButton.setFont(composite.getFont());
274         fRemoveButton.addSelectionListener(new SelectionAdapter() {
275             public void widgetSelected(SelectionEvent e) {
276                 removeSelectedWorkingSets();
277             }
278         });
279     }
280
281     private void createOrderButtons(Composite parent) {
282         Composite buttons= new Composite(parent, SWT.NONE);
283         buttons.setFont(parent.getFont());
284         buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL));
285         GridLayout layout= new GridLayout();
286         layout.marginHeight= 0;
287         layout.marginWidth= 0;
288         buttons.setLayout(layout);
289
290         fUpButton= new Button(buttons, SWT.PUSH);
291         fUpButton.setText(WorkingSetMessages.WorkingSetConfigurationDialog_up_label);
292         fUpButton.setFont(parent.getFont());
293         setButtonLayoutData(fUpButton);
294         fUpButton.addSelectionListener(new SelectionAdapter() {
295             public void widgetSelected(SelectionEvent e) {
296                 moveUp(((IStructuredSelection)fTableViewer.getSelection()).toList());
297             }
298         });
299
300         fDownButton= new Button(buttons, SWT.PUSH);
301         fDownButton.setText(WorkingSetMessages.WorkingSetConfigurationDialog_down_label);
302         fDownButton.setFont(parent.getFont());
303         setButtonLayoutData(fDownButton);
304         fDownButton.addSelectionListener(new SelectionAdapter() {
305             public void widgetSelected(SelectionEvent e) {
306                 moveDown(((IStructuredSelection)fTableViewer.getSelection()).toList());
307             }
308         });
309         
310         fSelectAll= new Button(buttons, SWT.PUSH);
311         fSelectAll.setText(WorkingSetMessages.WorkingSetConfigurationDialog_selectAll_label);
312         fSelectAll.setFont(parent.getFont());
313         setButtonLayoutData(fSelectAll);
314         fSelectAll.addSelectionListener(new SelectionAdapter() {
315             public void widgetSelected(SelectionEvent e) {
316                 selectAll();
317             }
318         });
319         
320         fDeselectAll= new Button(buttons, SWT.PUSH);
321         fDeselectAll.setText(WorkingSetMessages.WorkingSetConfigurationDialog_deselectAll_label);
322         fDeselectAll.setFont(parent.getFont());
323         setButtonLayoutData(fDeselectAll);
324         fDeselectAll.addSelectionListener(new SelectionAdapter() {
325             public void widgetSelected(SelectionEvent e) {
326                 deselectAll();
327             }
328         });
329     }
330
331     /**
332      * {@inheritDoc}
333      */

334     protected void okPressed() {
335         List JavaDoc newResult= getResultWorkingSets();
336         fResult= (IWorkingSet[])newResult.toArray(new IWorkingSet[newResult.size()]);
337         setResult(newResult);
338         super.okPressed();
339     }
340
341     private List JavaDoc getResultWorkingSets() {
342         Object JavaDoc[] checked= fTableViewer.getCheckedElements();
343         return new ArrayList JavaDoc(Arrays.asList(checked));
344     }
345
346     /**
347      * {@inheritDoc}
348      */

349     protected void cancelPressed() {
350         restoreAddedWorkingSets();
351         restoreChangedWorkingSets();
352         restoreRemovedWorkingSets();
353         super.cancelPressed();
354     }
355
356     private void setInitialSelection() {
357         List JavaDoc selections= getInitialElementSelections();
358         if (!selections.isEmpty()) {
359             fTableViewer.setCheckedElements(selections.toArray());
360         }
361     }
362
363     private void createWorkingSet() {
364         IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
365         IWorkingSetNewWizard wizard= manager.createWorkingSetNewWizard(new String JavaDoc[] {JavaWorkingSetUpdater.ID});
366         // the wizard can't be null since we have at least the Java working set.
367
WizardDialog dialog= new WizardDialog(getShell(), wizard);
368         dialog.create();
369         if (dialog.open() == Window.OK) {
370             IWorkingSet workingSet= wizard.getSelection();
371             Filter filter= new Filter();
372             if (filter.select(null, null, workingSet)) {
373                 fAllWorkingSets.add(workingSet);
374                 fTableViewer.add(workingSet);
375                 fTableViewer.setSelection(new StructuredSelection(workingSet), true);
376                 fTableViewer.setChecked(workingSet, true);
377                 manager.addWorkingSet(workingSet);
378                 fAddedWorkingSets.add(workingSet);
379             }
380         }
381     }
382
383     private void editSelectedWorkingSet() {
384         IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
385         IWorkingSet editWorkingSet= (IWorkingSet)((IStructuredSelection)fTableViewer.getSelection()).getFirstElement();
386         IWorkingSetEditWizard wizard= manager.createWorkingSetEditWizard(editWorkingSet);
387         WizardDialog dialog= new WizardDialog(getShell(), wizard);
388         IWorkingSet originalWorkingSet= (IWorkingSet)fEditedWorkingSets.get(editWorkingSet);
389         boolean firstEdit= originalWorkingSet == null;
390
391         // save the original working set values for restoration when selection
392
// dialog is cancelled.
393
if (firstEdit) {
394             originalWorkingSet=
395                 PlatformUI.getWorkbench().getWorkingSetManager().
396                 createWorkingSet(editWorkingSet.getName(), editWorkingSet.getElements());
397         } else {
398             fEditedWorkingSets.remove(editWorkingSet);
399         }
400         dialog.create();
401         if (dialog.open() == Window.OK) {
402             editWorkingSet= wizard.getSelection();
403             fTableViewer.update(editWorkingSet, null);
404             // make sure ok button is enabled when the selected working set
405
// is edited. Fixes bug 33386.
406
updateButtonAvailability();
407         }
408         fEditedWorkingSets.put(editWorkingSet, originalWorkingSet);
409     }
410
411     /**
412      * Called when the selection has changed.
413      */

414     void handleSelectionChanged() {
415         updateButtonAvailability();
416     }
417
418     /**
419      * Overrides method in Dialog
420      *
421      * @see org.eclipse.jface.dialogs.Dialog#open()
422      */

423     public int open() {
424         fAddedWorkingSets= new ArrayList JavaDoc();
425         fRemovedWorkingSets= new ArrayList JavaDoc();
426         fEditedWorkingSets= new HashMap JavaDoc();
427         fRemovedMRUWorkingSets= new ArrayList JavaDoc();
428         return super.open();
429     }
430
431     /**
432      * Removes the selected working sets from the workbench.
433      */

434     private void removeSelectedWorkingSets() {
435         ISelection selection= fTableViewer.getSelection();
436         if (selection instanceof IStructuredSelection) {
437             IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
438             Iterator JavaDoc iter= ((IStructuredSelection)selection).iterator();
439             while (iter.hasNext()) {
440                 IWorkingSet workingSet= (IWorkingSet)iter.next();
441                 if (fAddedWorkingSets.contains(workingSet)) {
442                     fAddedWorkingSets.remove(workingSet);
443                 } else {
444                     IWorkingSet[] recentWorkingSets= manager.getRecentWorkingSets();
445                     for (int i= 0; i < recentWorkingSets.length; i++) {
446                         if (workingSet.equals(recentWorkingSets[i])) {
447                             fRemovedMRUWorkingSets.add(workingSet);
448                             break;
449                         }
450                     }
451                     fRemovedWorkingSets.add(workingSet);
452                 }
453                 fAllWorkingSets.remove(workingSet);
454                 manager.removeWorkingSet(workingSet);
455             }
456             fTableViewer.remove(((IStructuredSelection)selection).toArray());
457         }
458     }
459
460     /**
461      * Removes newly created working sets from the working set manager.
462      */

463     private void restoreAddedWorkingSets() {
464         IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
465         Iterator JavaDoc iterator= fAddedWorkingSets.iterator();
466
467         while (iterator.hasNext()) {
468             manager.removeWorkingSet(((IWorkingSet)iterator.next()));
469         }
470     }
471
472     /**
473      * Rolls back changes to working sets.
474      */

475     private void restoreChangedWorkingSets() {
476         Iterator JavaDoc iterator= fEditedWorkingSets.keySet().iterator();
477
478         while (iterator.hasNext()) {
479             IWorkingSet editedWorkingSet= (IWorkingSet)iterator.next();
480             IWorkingSet originalWorkingSet= (IWorkingSet)fEditedWorkingSets.get(editedWorkingSet);
481
482             if (editedWorkingSet.getName().equals(originalWorkingSet.getName()) == false) {
483                 editedWorkingSet.setName(originalWorkingSet.getName());
484             }
485             if (editedWorkingSet.getElements().equals(originalWorkingSet.getElements()) == false) {
486                 editedWorkingSet.setElements(originalWorkingSet.getElements());
487             }
488         }
489     }
490
491     /**
492      * Adds back removed working sets to the working set manager.
493      */

494     private void restoreRemovedWorkingSets() {
495         IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
496         Iterator JavaDoc iterator= fRemovedWorkingSets.iterator();
497
498         while (iterator.hasNext()) {
499             manager.addWorkingSet(((IWorkingSet)iterator.next()));
500         }
501         iterator= fRemovedMRUWorkingSets.iterator();
502         while (iterator.hasNext()) {
503             manager.addRecentWorkingSet(((IWorkingSet)iterator.next()));
504         }
505     }
506
507     /**
508      * Updates the modify buttons' enabled state based on the current seleciton.
509      */

510     private void updateButtonAvailability() {
511         IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection();
512         boolean hasSelection= !selection.isEmpty();
513         boolean hasSingleSelection= selection.size() == 1;
514
515         fRemoveButton.setEnabled(hasSelection && areAllGlobalWorkingSets(selection));
516         fEditButton.setEnabled(hasSingleSelection && ((IWorkingSet)selection.getFirstElement()).isEditable());
517         if (fUpButton != null) {
518             fUpButton.setEnabled(canMoveUp());
519         }
520         if (fDownButton != null) {
521             fDownButton.setEnabled(canMoveDown());
522         }
523     }
524     
525     private boolean areAllGlobalWorkingSets(IStructuredSelection selection) {
526         Set JavaDoc globals= new HashSet JavaDoc(Arrays.asList(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets()));
527         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
528             if (!globals.contains(iter.next()))
529                 return false;
530         }
531         return true;
532     }
533     
534     private void moveUp(List JavaDoc toMoveUp) {
535         if (toMoveUp.size() > 0) {
536             setElements(moveUp(fAllWorkingSets, toMoveUp));
537             fTableViewer.reveal(toMoveUp.get(0));
538         }
539     }
540
541     private void moveDown(List JavaDoc toMoveDown) {
542         if (toMoveDown.size() > 0) {
543             setElements(reverse(moveUp(reverse(fAllWorkingSets), toMoveDown)));
544             fTableViewer.reveal(toMoveDown.get(toMoveDown.size() - 1));
545         }
546     }
547
548     private void setElements(List JavaDoc elements) {
549         fAllWorkingSets= elements;
550         fTableViewer.setInput(fAllWorkingSets);
551         updateButtonAvailability();
552     }
553
554     private List JavaDoc moveUp(List JavaDoc elements, List JavaDoc move) {
555         int nElements= elements.size();
556         List JavaDoc res= new ArrayList JavaDoc(nElements);
557         Object JavaDoc floating= null;
558         for (int i= 0; i < nElements; i++) {
559             Object JavaDoc curr= elements.get(i);
560             if (move.contains(curr)) {
561                 res.add(curr);
562             } else {
563                 if (floating != null) {
564                     res.add(floating);
565                 }
566                 floating= curr;
567             }
568         }
569         if (floating != null) {
570             res.add(floating);
571         }
572         return res;
573     }
574
575     private List JavaDoc reverse(List JavaDoc p) {
576         List JavaDoc reverse= new ArrayList JavaDoc(p.size());
577         for (int i= p.size() - 1; i >= 0; i--) {
578             reverse.add(p.get(i));
579         }
580         return reverse;
581     }
582
583     private boolean canMoveUp() {
584         int[] indc= fTableViewer.getTable().getSelectionIndices();
585         for (int i= 0; i < indc.length; i++) {
586             if (indc[i] != i) {
587                 return true;
588             }
589         }
590         return false;
591     }
592
593     private boolean canMoveDown() {
594         int[] indc= fTableViewer.getTable().getSelectionIndices();
595         int k= fAllWorkingSets.size() - 1;
596         for (int i= indc.length - 1; i >= 0; i--, k--) {
597             if (indc[i] != k) {
598                 return true;
599             }
600         }
601         return false;
602     }
603     
604     //---- select / deselect --------------------------------------------------
605

606     private void selectAll() {
607         fTableViewer.setAllChecked(true);
608     }
609     
610     private void deselectAll() {
611         fTableViewer.setAllChecked(false);
612     }
613 }
614
Popular Tags