KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.text.Collator;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
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.graphics.Image;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Shell;
35
36 import org.eclipse.jface.dialogs.Dialog;
37 import org.eclipse.jface.resource.ImageDescriptor;
38 import org.eclipse.jface.viewers.CheckStateChangedEvent;
39 import org.eclipse.jface.viewers.CheckboxTableViewer;
40 import org.eclipse.jface.viewers.ICheckStateListener;
41 import org.eclipse.jface.viewers.IStructuredContentProvider;
42 import org.eclipse.jface.viewers.LabelProvider;
43 import org.eclipse.jface.viewers.StructuredSelection;
44 import org.eclipse.jface.viewers.Viewer;
45 import org.eclipse.jface.viewers.ViewerFilter;
46 import org.eclipse.jface.viewers.ViewerSorter;
47 import org.eclipse.jface.window.Window;
48 import org.eclipse.jface.wizard.WizardDialog;
49
50 import org.eclipse.ui.IWorkingSet;
51 import org.eclipse.ui.IWorkingSetManager;
52 import org.eclipse.ui.PlatformUI;
53 import org.eclipse.ui.dialogs.IWorkingSetNewWizard;
54 import org.eclipse.ui.dialogs.SelectionDialog;
55
56 public class SimpleWorkingSetSelectionDialog extends SelectionDialog {
57     
58     private static class WorkingSetLabelProvider extends LabelProvider {
59         
60         private Map JavaDoc fIcons;
61         
62         public WorkingSetLabelProvider() {
63             fIcons= new Hashtable JavaDoc();
64         }
65         
66         public void dispose() {
67             Iterator JavaDoc iterator= fIcons.values().iterator();
68             while (iterator.hasNext()) {
69                 Image icon= (Image)iterator.next();
70                 icon.dispose();
71             }
72             super.dispose();
73         }
74         
75         public Image getImage(Object JavaDoc object) {
76             Assert.isTrue(object instanceof IWorkingSet);
77             IWorkingSet workingSet= (IWorkingSet)object;
78             ImageDescriptor imageDescriptor= workingSet.getImageDescriptor();
79             if (imageDescriptor == null)
80                 return null;
81             
82             Image icon= (Image)fIcons.get(imageDescriptor);
83             if (icon == null) {
84                 icon= imageDescriptor.createImage();
85                 fIcons.put(imageDescriptor, icon);
86             }
87             
88             return icon;
89         }
90         
91         public String JavaDoc getText(Object JavaDoc object) {
92             Assert.isTrue(object instanceof IWorkingSet);
93             IWorkingSet workingSet= (IWorkingSet)object;
94             return workingSet.getName();
95         }
96         
97     }
98     
99     private class Filter extends ViewerFilter {
100         
101         public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
102             return isCompatible((IWorkingSet)element);
103         }
104                 
105         private boolean isCompatible(IWorkingSet set) {
106             if (set.isAggregateWorkingSet() || !set.isSelfUpdating())
107                 return false;
108             
109             if (!set.isVisible())
110                 return false;
111             
112             if (!set.isEditable())
113                 return false;
114             
115             return true;
116         }
117         
118     }
119     
120     private final IWorkingSet[] fWorkingSets;
121     private final IWorkingSet[] fInitialSelection;
122     private final ArrayList JavaDoc fCreatedWorkingSets;
123     
124     private CheckboxTableViewer fTableViewer;
125     private IWorkingSet[] fCheckedElements;
126     
127     private Button fSelectAll;
128     private Button fDeselectAll;
129     private Button fNewWorkingSet;
130
131     public SimpleWorkingSetSelectionDialog(Shell shell, String JavaDoc[] workingSetIds, IWorkingSet[] initialSelection) {
132         super(shell);
133         
134         setTitle(WorkingSetMessages.SimpleWorkingSetSelectionDialog_SimpleSelectWorkingSetDialog_title);
135         setHelpAvailable(false);
136         setShellStyle(getShellStyle() | SWT.RESIZE);
137
138         fWorkingSets= WorkingSetConfigurationBlock.filter(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets(), workingSetIds);
139         fInitialSelection= initialSelection;
140         fCheckedElements= fInitialSelection;
141         fCreatedWorkingSets= new ArrayList JavaDoc();
142     }
143     
144     protected final Control createDialogArea(Composite parent) {
145         Composite composite= (Composite)super.createDialogArea(parent);
146         composite.setFont(parent.getFont());
147
148         createMessageArea(composite);
149         Composite inner= new Composite(composite, SWT.NONE);
150         inner.setFont(composite.getFont());
151         inner.setLayoutData(new GridData(GridData.FILL_BOTH));
152         GridLayout layout= new GridLayout();
153         layout.numColumns= 2;
154         layout.marginHeight= 0;
155         layout.marginWidth= 0;
156         inner.setLayout(layout);
157         
158         Composite tableComposite= new Composite(inner, SWT.NONE);
159         tableComposite.setFont(composite.getFont());
160         tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
161         layout= new GridLayout();
162         layout.marginHeight= 0;
163         layout.marginWidth= 0;
164         tableComposite.setLayout(layout);
165         
166         fTableViewer= createTableViewer(tableComposite);
167         createRightButtonBar(inner);
168         
169         createBottomButtonBar(composite);
170         
171         Dialog.applyDialogFont(composite);
172         
173         return composite;
174     }
175
176     public IWorkingSet[] getSelection() {
177         return fCheckedElements;
178     }
179     
180     protected CheckboxTableViewer createTableViewer(Composite parent) {
181         CheckboxTableViewer result= CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.MULTI);
182         result.addCheckStateListener(new ICheckStateListener() {
183             public void checkStateChanged(CheckStateChangedEvent event) {
184                 checkedStateChanged();
185             }
186         });
187         GridData data= new GridData(GridData.FILL_BOTH);
188         data.heightHint= convertHeightInCharsToPixels(20);
189         data.widthHint= convertWidthInCharsToPixels(50);
190         result.getTable().setLayoutData(data);
191         result.getTable().setFont(parent.getFont());
192
193         result.addFilter(createTableFilter());
194         result.setLabelProvider(createTableLabelProvider());
195         result.setSorter(createTableSorter());
196         result.setContentProvider(new IStructuredContentProvider() {
197             public Object JavaDoc[] getElements(Object JavaDoc element) {
198                 return (Object JavaDoc[])element;
199             }
200             public void dispose() {
201             }
202             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
203             }
204         });
205         
206         result.setInput(fWorkingSets);
207         result.setCheckedElements(fInitialSelection);
208         
209         return result;
210     }
211
212     protected ViewerSorter createTableSorter() {
213         return new ViewerSorter() {
214             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
215                 IWorkingSet w1= (IWorkingSet)e1;
216                 IWorkingSet w2= (IWorkingSet)e2;
217                 return Collator.getInstance().compare(w1.getLabel(), w2.getLabel());
218             }
219         };
220     }
221
222     protected LabelProvider createTableLabelProvider() {
223         return new WorkingSetLabelProvider();
224     }
225
226     protected ViewerFilter createTableFilter() {
227         return new Filter();
228     }
229     
230     protected void createRightButtonBar(Composite parent) {
231         Composite buttons= new Composite(parent, SWT.NONE);
232         buttons.setFont(parent.getFont());
233         buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL));
234         GridLayout layout= new GridLayout();
235         layout.marginHeight= 0;
236         layout.marginWidth= 0;
237         buttons.setLayout(layout);
238
239         createButtonsForRightButtonBar(buttons);
240     }
241
242     protected void createButtonsForRightButtonBar(Composite bar) {
243         fSelectAll= new Button(bar, SWT.PUSH);
244         fSelectAll.setText(WorkingSetMessages.SimpleWorkingSetSelectionDialog_SelectAll_button);
245         fSelectAll.setFont(bar.getFont());
246         setButtonLayoutData(fSelectAll);
247         fSelectAll.addSelectionListener(new SelectionAdapter() {
248             public void widgetSelected(SelectionEvent e) {
249                 selectAll();
250             }
251         });
252         
253         fDeselectAll= new Button(bar, SWT.PUSH);
254         fDeselectAll.setText(WorkingSetMessages.SimpleWorkingSetSelectionDialog_DeselectAll_button);
255         fDeselectAll.setFont(bar.getFont());
256         setButtonLayoutData(fDeselectAll);
257         fDeselectAll.addSelectionListener(new SelectionAdapter() {
258             public void widgetSelected(SelectionEvent e) {
259                 deselectAll();
260             }
261         });
262         
263         new Label(bar, SWT.NONE);
264         
265         fNewWorkingSet= new Button(bar, SWT.PUSH);
266         fNewWorkingSet.setText(WorkingSetMessages.SimpleWorkingSetSelectionDialog_New_button);
267         fNewWorkingSet.setFont(bar.getFont());
268         setButtonLayoutData(fNewWorkingSet);
269         fNewWorkingSet.addSelectionListener(new SelectionAdapter() {
270             public void widgetSelected(SelectionEvent e) {
271                 IWorkingSet workingSet= newWorkingSet();
272                 if (workingSet != null) {
273                     
274                 }
275             }
276         });
277     }
278     
279     protected void createBottomButtonBar(Composite parent) {
280     }
281     
282     protected void checkedStateChanged() {
283         List JavaDoc elements= Arrays.asList(fTableViewer.getCheckedElements());
284         fCheckedElements= (IWorkingSet[])elements.toArray(new IWorkingSet[elements.size()]);
285     }
286     
287     protected void selectAll() {
288         fTableViewer.setAllChecked(true);
289         checkedStateChanged();
290     }
291     
292     protected void deselectAll() {
293         fTableViewer.setAllChecked(false);
294         checkedStateChanged();
295     }
296     
297     protected IWorkingSet newWorkingSet() {
298         IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
299         
300         //can only allow to create java working sets at the moment, see bug 186762
301
// IWorkingSetNewWizard wizard= manager.createWorkingSetNewWizard(fWorkingSetIds);
302
// if (wizard == null)
303
// return;
304

305         IWorkingSetNewWizard wizard= manager.createWorkingSetNewWizard(new String JavaDoc[] {JavaWorkingSetUpdater.ID});
306         
307         WizardDialog dialog= new WizardDialog(getShell(), wizard);
308         dialog.create();
309         if (dialog.open() == Window.OK) {
310             IWorkingSet workingSet= wizard.getSelection();
311             Filter filter= new Filter();
312             if (filter.select(null, null, workingSet)) {
313                 addNewWorkingSet(workingSet);
314                 checkedStateChanged();
315                 manager.addWorkingSet(workingSet);
316                 fCreatedWorkingSets.add(workingSet);
317                 return workingSet;
318             }
319         }
320         
321         return null;
322     }
323
324     protected void addNewWorkingSet(IWorkingSet workingSet) {
325         fTableViewer.add(workingSet);
326         fTableViewer.setSelection(new StructuredSelection(workingSet), true);
327         fTableViewer.setChecked(workingSet, true);
328     }
329     
330     /**
331      * {@inheritDoc}
332      */

333     protected void cancelPressed() {
334         IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
335         for (int i= 0; i < fCreatedWorkingSets.size(); i++) {
336             manager.removeWorkingSet((IWorkingSet)fCreatedWorkingSets.get(i));
337         }
338         
339         super.cancelPressed();
340     }
341 }
Popular Tags