KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.workingsets;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.runtime.IAdaptable;
21
22 import org.eclipse.core.resources.IResource;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Link;
32 import org.eclipse.swt.widgets.Shell;
33
34 import org.eclipse.jface.dialogs.IDialogConstants;
35 import org.eclipse.jface.viewers.CheckStateChangedEvent;
36 import org.eclipse.jface.viewers.CheckboxTableViewer;
37 import org.eclipse.jface.viewers.ICheckStateListener;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.LabelProvider;
40 import org.eclipse.jface.viewers.StructuredSelection;
41 import org.eclipse.jface.viewers.Viewer;
42 import org.eclipse.jface.viewers.ViewerFilter;
43 import org.eclipse.jface.viewers.ViewerSorter;
44 import org.eclipse.jface.window.Window;
45
46 import org.eclipse.ui.IWorkbenchPage;
47 import org.eclipse.ui.IWorkbenchPart;
48 import org.eclipse.ui.IWorkbenchSite;
49 import org.eclipse.ui.IWorkingSet;
50 import org.eclipse.ui.PlatformUI;
51
52 import org.eclipse.jdt.core.IJavaElement;
53
54 import org.eclipse.jdt.internal.corext.util.Messages;
55
56 import org.eclipse.jdt.ui.JavaElementLabels;
57 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
58
59 import org.eclipse.jdt.internal.ui.JavaPlugin;
60 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
61
62 public final class ConfigureWorkingSetAssignementAction extends SelectionDispatchAction {
63     
64     private static final class GrayedCheckedModel {
65
66         private GrayedCheckedModelElement[] fElements;
67         private final Hashtable JavaDoc fLookup;
68         
69         public GrayedCheckedModel(GrayedCheckedModelElement[] elements) {
70             fElements= elements;
71             fLookup= new Hashtable JavaDoc();
72             for (int i= 0; i < elements.length; i++) {
73                 fLookup.put(elements[i].getWorkingSet(), elements[i]);
74             }
75         }
76         
77         public void addElement(GrayedCheckedModelElement element) {
78             ArrayList JavaDoc list= new ArrayList JavaDoc(Arrays.asList(fElements));
79             list.add(element);
80             fElements= (GrayedCheckedModelElement[])list.toArray(new GrayedCheckedModelElement[list.size()]);
81             
82             fLookup.put(element.getWorkingSet(), element);
83         }
84
85         public IWorkingSet[] getAll() {
86             IWorkingSet[] result= new IWorkingSet[fElements.length];
87             for (int i= 0; i < fElements.length; i++) {
88                 result[i]= fElements[i].getWorkingSet();
89             }
90             return result;
91         }
92         
93         public GrayedCheckedModelElement getModelElement(IWorkingSet element) {
94             return (GrayedCheckedModelElement)fLookup.get(element);
95         }
96
97         public IWorkingSet[] getChecked() {
98             ArrayList JavaDoc result= new ArrayList JavaDoc();
99             for (int i= 0; i < fElements.length; i++) {
100                 if (fElements[i].isChecked())
101                     result.add(fElements[i].getWorkingSet());
102             }
103             return (IWorkingSet[])result.toArray(new IWorkingSet[result.size()]);
104         }
105
106         public IWorkingSet[] getGrayed() {
107             ArrayList JavaDoc result= new ArrayList JavaDoc();
108             for (int i= 0; i < fElements.length; i++) {
109                 if (fElements[i].isGrayed())
110                     result.add(fElements[i].getWorkingSet());
111             }
112             return (IWorkingSet[])result.toArray(new IWorkingSet[result.size()]);
113         }
114
115         public void selectAll() {
116             for (int i= 0; i < fElements.length; i++) {
117                 fElements[i].select();
118             }
119         }
120
121         public void deselectAll() {
122             for (int i= 0; i < fElements.length; i++) {
123                 fElements[i].deselect();
124             }
125         }
126         
127     }
128
129     private final static class GrayedCheckedModelElement {
130         
131         private final IWorkingSet fWorkingSet;
132         private final int fElementCount;
133         private int fCheckCount;
134         
135         public GrayedCheckedModelElement(IWorkingSet workingSet, int checkCount, int elementCount) {
136             fWorkingSet= workingSet;
137             fCheckCount= checkCount;
138             fElementCount= elementCount;
139         }
140         
141         public IWorkingSet getWorkingSet() {
142             return fWorkingSet;
143         }
144         
145         public int getCheckCount() {
146             return fCheckCount;
147         }
148         
149         public boolean isGrayed() {
150             return isChecked() && fCheckCount < fElementCount;
151         }
152         
153         public boolean isChecked() {
154             return fCheckCount > 0;
155         }
156
157         public void deselect() {
158             fCheckCount= 0;
159         }
160         
161         public void select() {
162             fCheckCount= fElementCount;
163         }
164         
165     }
166     
167     private final class WorkingSetModelAwareSelectionDialog extends SimpleWorkingSetSelectionDialog {
168         
169         private CheckboxTableViewer fTableViewer;
170         private boolean fShowVisibleOnly;
171         private GrayedCheckedModel fModel;
172         private final IAdaptable[] fElements;
173         
174         private WorkingSetModelAwareSelectionDialog(Shell shell, GrayedCheckedModel model, IAdaptable[] elements) {
175             super(shell, VALID_WORKING_SET_IDS, model.getChecked());
176             setTitle(WorkingSetMessages.ConfigureWorkingSetAssignementAction_WorkingSetAssignments_title);
177             fModel= model;
178             fShowVisibleOnly= true;
179             fElements= elements;
180         }
181         
182         public IWorkingSet[] getGrayed() {
183             return fModel.getGrayed();
184         }
185         
186         public IWorkingSet[] getSelection() {
187             return fModel.getChecked();
188         }
189
190         protected CheckboxTableViewer createTableViewer(Composite parent) {
191             fTableViewer= super.createTableViewer(parent);
192             fTableViewer.setGrayedElements(fModel.getGrayed());
193             fTableViewer.addCheckStateListener(new ICheckStateListener() {
194                 public void checkStateChanged(CheckStateChangedEvent event) {
195                     IWorkingSet element= (IWorkingSet)event.getElement();
196                     fTableViewer.setGrayed(element, false);
197                     GrayedCheckedModelElement modelElement= fModel.getModelElement(element);
198                     if (event.getChecked()) {
199                         modelElement.select();
200                     } else {
201                         modelElement.deselect();
202                     }
203                     fTableViewer.update(element, null);
204                 }
205             });
206             
207             createShowVisibleOnly(parent);
208             
209             return fTableViewer;
210         }
211         
212         protected void selectAll() {
213             super.selectAll();
214             fModel.selectAll();
215             fTableViewer.setGrayedElements(new Object JavaDoc[0]);
216             fTableViewer.refresh();
217         }
218
219         protected void deselectAll() {
220             super.deselectAll();
221             fModel.deselectAll();
222             fTableViewer.setGrayedElements(new Object JavaDoc[0]);
223             fTableViewer.refresh();
224         }
225         
226         /**
227          * {@inheritDoc}
228          */

229         protected void addNewWorkingSet(IWorkingSet workingSet) {
230             if (fWorkingSetModel != null) {
231                 IWorkingSet[] workingSets= fWorkingSetModel.getActiveWorkingSets();
232                 IWorkingSet[] activeWorkingSets= new IWorkingSet[workingSets.length+ 1];
233                 activeWorkingSets[0]= workingSet;
234                 System.arraycopy(workingSets, 0, activeWorkingSets, 1, workingSets.length);
235                 fWorkingSetModel.setActiveWorkingSets(activeWorkingSets);
236             }
237             
238             int checkCount= 0;
239             for (int i= 0; i < fElements.length; i++) {
240                 IAdaptable adapted= adapt(workingSet, fElements[i]);
241                 if (adapted != null && contains(workingSet, adapted)) {
242                     checkCount++;
243                 }
244             }
245             
246             fModel.addElement(new GrayedCheckedModelElement(workingSet, checkCount, fElements.length));
247             
248             fTableViewer.setInput(fModel.getAll());
249             fTableViewer.refresh();
250             
251             fTableViewer.setCheckedElements(fModel.getChecked());
252             fTableViewer.setGrayedElements(fModel.getGrayed());
253             
254             fTableViewer.setSelection(new StructuredSelection(workingSet));
255         }
256
257         protected ViewerFilter createTableFilter() {
258             final ViewerFilter superFilter= super.createTableFilter();
259             return new ViewerFilter() {
260                 public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
261                     if (!superFilter.select(viewer, parentElement, element))
262                         return false;
263                     
264                     IWorkingSet set= (IWorkingSet)element;
265                     if (!isValidWorkingSet(set))
266                         return false;
267                     
268                     if (fWorkingSetModel == null)
269                         return true;
270                     
271                     if (fShowVisibleOnly) {
272                         if (!fWorkingSetModel.isActiveWorkingSet(set))
273                             return false;
274                         
275                         return true;
276                     } else {
277                         return true;
278                     }
279                 }
280             };
281         }
282         
283         protected ViewerSorter createTableSorter() {
284             final ViewerSorter superSorter= super.createTableSorter();
285             return new ViewerSorter() {
286                 public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
287                     if (fWorkingSetModel == null)
288                         return superSorter.compare(viewer, e1, e2);
289                     
290                     IWorkingSet[] activeWorkingSets= fWorkingSetModel.getActiveWorkingSets();
291                     for (int i= 0; i < activeWorkingSets.length; i++) {
292                         IWorkingSet active= activeWorkingSets[i];
293                         if (active == e1) {
294                             return -1;
295                         } else if (active == e2) {
296                             return 1;
297                         }
298                     }
299                     
300                     return superSorter.compare(viewer, e1, e2);
301                 }
302             };
303         }
304         
305         protected LabelProvider createTableLabelProvider() {
306             final LabelProvider superLabelProvider= super.createTableLabelProvider();
307             return new LabelProvider() {
308                 public String JavaDoc getText(Object JavaDoc element) {
309                     String JavaDoc superText= superLabelProvider.getText(element);
310                     if (superText == null)
311                         return null;
312                     
313                     GrayedCheckedModelElement modelElement= fModel.getModelElement((IWorkingSet)element);
314                     if (!modelElement.isGrayed())
315                         return superText;
316                     
317                     return superText + Messages.format(WorkingSetMessages.ConfigureWorkingSetAssignementAction_XofY_label, new Object JavaDoc[] {new Integer JavaDoc(modelElement.getCheckCount()), new Integer JavaDoc(fElements.length)});
318                 }
319             };
320         }
321     
322         private void createShowVisibleOnly(Composite parent) {
323             if (fWorkingSetModel == null)
324                 return;
325             
326             Composite bar= new Composite(parent, SWT.NONE);
327             bar.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
328             GridLayout gridLayout= new GridLayout(2, false);
329             gridLayout.marginHeight= 0;
330             gridLayout.marginWidth= 0;
331             bar.setLayout(gridLayout);
332             
333             final Button showVisibleOnly= new Button(bar, SWT.CHECK);
334             showVisibleOnly.setSelection(fShowVisibleOnly);
335             showVisibleOnly.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, true));
336             showVisibleOnly.addSelectionListener(new SelectionAdapter() {
337                 public void widgetSelected(SelectionEvent e) {
338                     fShowVisibleOnly= showVisibleOnly.getSelection();
339                     
340                     fTableViewer.refresh();
341                     
342                     fTableViewer.setCheckedElements(fModel.getChecked());
343                     fTableViewer.setGrayedElements(fModel.getGrayed());
344                 }
345             });
346             
347             Link ppwsLink= new Link(bar, SWT.NONE);
348             ppwsLink.setText(WorkingSetMessages.ConfigureWorkingSetAssignementAction_OnlyShowVisible_link);
349             ppwsLink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
350             ppwsLink.addSelectionListener(new SelectionAdapter() {
351                 public void widgetSelected(SelectionEvent e) {
352                     
353                     List JavaDoc workingSets= new ArrayList JavaDoc(Arrays.asList(fWorkingSetModel.getAllWorkingSets()));
354                     IWorkingSet[] activeWorkingSets= fWorkingSetModel.getActiveWorkingSets();
355                     WorkingSetConfigurationDialog dialog= new WorkingSetConfigurationDialog(
356                         getShell(),
357                         (IWorkingSet[])workingSets.toArray(new IWorkingSet[workingSets.size()]),
358                         activeWorkingSets);
359                     dialog.setSelection(activeWorkingSets);
360                     if (dialog.open() == IDialogConstants.OK_ID) {
361                         IWorkingSet[] selection= dialog.getSelection();
362                         fWorkingSetModel.setActiveWorkingSets(selection);
363                     }
364                     
365                     recalculateCheckedState();
366                 }
367             });
368         }
369         
370         private void recalculateCheckedState() {
371             fModel= createGrayedCheckedModel(fElements, getAllWorkingSets());
372             
373             fTableViewer.setInput(fModel.getAll());
374             fTableViewer.refresh();
375             fTableViewer.setCheckedElements(fModel.getChecked());
376             fTableViewer.setGrayedElements(fModel.getGrayed());
377         }
378     }
379
380     private static final String JavaDoc[] VALID_WORKING_SET_IDS= new String JavaDoc[] {
381             JavaWorkingSetUpdater.ID,
382             "org.eclipse.ui.resourceWorkingSetPage" //$NON-NLS-1$
383
};
384     
385     private WorkingSetModel fWorkingSetModel;
386     private final IWorkbenchSite fSite;
387
388     public ConfigureWorkingSetAssignementAction(IWorkbenchSite site) {
389         super(site);
390         fSite= site;
391         setText(WorkingSetMessages.ConfigureWorkingSetAssignementAction_WorkingSets_actionLabel);
392         setEnabled(false);
393     }
394     
395     public void setWorkingSetModel(WorkingSetModel workingSetModel) {
396         fWorkingSetModel= workingSetModel;
397     }
398     
399     public void selectionChanged(IStructuredSelection selection) {
400         setEnabled(canEnable(selection));
401     }
402
403     private boolean canEnable(IStructuredSelection selection) {
404         if (selection.isEmpty())
405             return false;
406         
407         List JavaDoc list= selection.toList();
408         for (Iterator JavaDoc iterator= list.iterator(); iterator.hasNext();) {
409             Object JavaDoc object= iterator.next();
410             if (!(object instanceof IResource) && !(object instanceof IJavaElement))
411                 return false;
412         }
413         
414         return true;
415     }
416
417     private IAdaptable[] getSelectedElements(IStructuredSelection selection) {
418         ArrayList JavaDoc result= new ArrayList JavaDoc();
419         
420         List JavaDoc list= selection.toList();
421         for (Iterator JavaDoc iterator= list.iterator(); iterator.hasNext();) {
422             Object JavaDoc object= iterator.next();
423             if (object instanceof IResource || object instanceof IJavaElement) {
424                 result.add(object);
425             }
426         }
427         
428         return (IAdaptable[])result.toArray(new IAdaptable[result.size()]);
429     }
430     
431     public void run(IStructuredSelection selection) {
432         IAdaptable[] elements= getSelectedElements(selection);
433         GrayedCheckedModel model= createGrayedCheckedModel(elements, getAllWorkingSets());
434         WorkingSetModelAwareSelectionDialog dialog= new WorkingSetModelAwareSelectionDialog(fSite.getShell(), model, elements);
435         
436         if (elements.length == 1) {
437             IAdaptable element= elements[0];
438             String JavaDoc elementName;
439             if (element instanceof IResource) {
440                 elementName= ((IResource)element).getName();
441             } else {
442                 elementName= JavaElementLabels.getElementLabel((IJavaElement)element, JavaElementLabels.ALL_DEFAULT);
443             }
444             dialog.setMessage(Messages.format(WorkingSetMessages.ConfigureWorkingSetAssignementAction_DialogMessage_specific, elementName));
445         } else {
446             dialog.setMessage(Messages.format(WorkingSetMessages.ConfigureWorkingSetAssignementAction_DialogMessage_multi, new Integer JavaDoc(elements.length)));
447         }
448         if (dialog.open() == Window.OK) {
449             updateWorkingSets(dialog.getSelection(), dialog.getGrayed(), elements);
450             selectAndReveal(elements);
451         }
452     }
453     
454     private static GrayedCheckedModel createGrayedCheckedModel(IAdaptable[] elements, IWorkingSet[] workingSets) {
455         GrayedCheckedModelElement[] result= new GrayedCheckedModelElement[workingSets.length];
456         
457         for (int i= 0; i < workingSets.length; i++) {
458             IWorkingSet set= workingSets[i];
459             
460             int checkCount= 0;
461             for (int j= 0; j < elements.length; j++) {
462                 IAdaptable adapted= adapt(set, elements[j]);
463                 if (adapted != null && contains(set, adapted)) {
464                     checkCount++;
465                 }
466             }
467             
468             result[i]= new GrayedCheckedModelElement(set, checkCount, elements.length);
469         }
470         
471         return new GrayedCheckedModel(result);
472     }
473
474     private void updateWorkingSets(IWorkingSet[] newWorkingSets, IWorkingSet[] grayedWorkingSets, IAdaptable[] elements) {
475         HashSet JavaDoc selectedSets= new HashSet JavaDoc(Arrays.asList(newWorkingSets));
476         HashSet JavaDoc grayedSets= new HashSet JavaDoc(Arrays.asList(grayedWorkingSets));
477         IWorkingSet[] workingSets= getAllWorkingSets();
478         
479         for (int i= 0; i < workingSets.length; i++) {
480             IWorkingSet workingSet= workingSets[i];
481             if (isValidWorkingSet(workingSet) && !selectedSets.contains(workingSet) && !grayedSets.contains(workingSet)) {
482                 for (int j= 0; j < elements.length; j++) {
483                     IAdaptable adapted= adapt(workingSet, elements[j]);
484                     if (adapted != null && contains(workingSet, adapted)) {
485                         remove(workingSet, adapted);
486                     }
487                 }
488             }
489         }
490
491         for (int i= 0; i < newWorkingSets.length; i++) {
492             IWorkingSet set= newWorkingSets[i];
493             if (isValidWorkingSet(set) && !grayedSets.contains(set)) {
494                 for (int j= 0; j < elements.length; j++) {
495                     IAdaptable adapted= adapt(set, elements[j]);
496                     if (adapted != null && !contains(set, adapted)) {
497                         add(set, adapted);
498                     }
499                 }
500             }
501         }
502     }
503     
504     private IWorkingSet[] getAllWorkingSets() {
505         if (fWorkingSetModel != null) {
506             return fWorkingSetModel.getAllWorkingSets();
507         } else {
508             return PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets();
509         }
510     }
511
512     private static boolean isValidWorkingSet(IWorkingSet set) {
513         if (set.isAggregateWorkingSet() || !set.isSelfUpdating())
514             return false;
515         
516         if (!set.isVisible())
517             return false;
518         
519         if (!set.isEditable())
520             return false;
521
522         for (int i= 0; i < VALID_WORKING_SET_IDS.length; i++) {
523             if (VALID_WORKING_SET_IDS[i].equals(set.getId()))
524                 return true;
525         }
526
527         return false;
528     }
529
530     private static IAdaptable adapt(IWorkingSet set, IAdaptable element) {
531         IAdaptable[] adaptedElements= set.adaptElements(new IAdaptable[] {
532             element
533         });
534         if (adaptedElements.length != 1)
535             return null;
536
537         return adaptedElements[0];
538     }
539
540     private static boolean contains(IWorkingSet set, IAdaptable adaptedElement) {
541         IAdaptable[] elements= set.getElements();
542         for (int i= 0; i < elements.length; i++) {
543             if (elements[i].equals(adaptedElement))
544                 return true;
545         }
546
547         return false;
548     }
549
550     private static void remove(IWorkingSet workingSet, IAdaptable adaptedElement) {
551         HashSet JavaDoc set= new HashSet JavaDoc(Arrays.asList(workingSet.getElements()));
552         set.remove(adaptedElement);
553         workingSet.setElements((IAdaptable[])set.toArray(new IAdaptable[set.size()]));
554     }
555
556     private static void add(IWorkingSet workingSet, IAdaptable adaptedElement) {
557         IAdaptable[] elements= workingSet.getElements();
558         IAdaptable[] newElements= new IAdaptable[elements.length + 1];
559         System.arraycopy(elements, 0, newElements, 0, elements.length);
560         newElements[elements.length]= adaptedElement;
561         workingSet.setElements(newElements);
562     }
563     
564     private void selectAndReveal(IAdaptable[] elements) {
565         PackageExplorerPart explorer= getActivePackageExplorer();
566         if (explorer != null)
567             explorer.selectReveal(new StructuredSelection(elements));
568     }
569     
570     private PackageExplorerPart getActivePackageExplorer() {
571         IWorkbenchPage page= JavaPlugin.getActivePage();
572         if (page != null) {
573             IWorkbenchPart activePart= page.getActivePart();
574             if (activePart instanceof PackageExplorerPart) {
575                 return (PackageExplorerPart) activePart;
576             }
577         }
578         return null;
579     }
580
581 }
Popular Tags