KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > util > PluginWorkingSet


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.Preferences;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.viewers.CheckStateChangedEvent;
20 import org.eclipse.jface.viewers.CheckboxTreeViewer;
21 import org.eclipse.jface.viewers.ICheckStateListener;
22 import org.eclipse.jface.viewers.IStructuredContentProvider;
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.LabelProvider;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.wizard.WizardPage;
27 import org.eclipse.pde.core.plugin.IPluginBase;
28 import org.eclipse.pde.core.plugin.IPluginModelBase;
29 import org.eclipse.pde.core.plugin.PluginRegistry;
30 import org.eclipse.pde.internal.core.PDECore;
31 import org.eclipse.pde.internal.ui.IHelpContextIds;
32 import org.eclipse.pde.internal.ui.IPreferenceConstants;
33 import org.eclipse.pde.internal.ui.PDEPlugin;
34 import org.eclipse.pde.internal.ui.PDEPluginImages;
35 import org.eclipse.pde.internal.ui.PDEUIMessages;
36 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.events.SelectionAdapter;
41 import org.eclipse.swt.events.SelectionEvent;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Button;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Text;
49 import org.eclipse.ui.IWorkingSet;
50 import org.eclipse.ui.IWorkingSetManager;
51 import org.eclipse.ui.PlatformUI;
52 import org.eclipse.ui.dialogs.FilteredTree;
53 import org.eclipse.ui.dialogs.IWorkingSetPage;
54 import org.eclipse.ui.dialogs.PatternFilter;
55
56 public class PluginWorkingSet extends WizardPage implements IWorkingSetPage {
57
58     class ContentProvider extends DefaultContentProvider implements ITreeContentProvider {
59         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
60             return PluginRegistry.getAllModels();
61         }
62
63         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
64             return null;
65         }
66
67         public Object JavaDoc getParent(Object JavaDoc element) {
68             return null;
69         }
70
71         public boolean hasChildren(Object JavaDoc element) {
72             return false;
73         }
74     }
75
76     class WorkingSetLabelProvider extends LabelProvider {
77
78         Preferences pref = PDEPlugin.getDefault().getPluginPreferences();
79
80         public WorkingSetLabelProvider() {
81             PDEPlugin.getDefault().getLabelProvider().connect(this);
82         }
83         /* (non-Javadoc)
84          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
85          */

86         public String JavaDoc getText(Object JavaDoc element) {
87             if (element instanceof IPluginModelBase) {
88                 IPluginBase plugin = ((IPluginModelBase)element).getPluginBase();
89                 String JavaDoc showType = pref.getString(IPreferenceConstants.PROP_SHOW_OBJECTS);
90                 if (showType.equals(IPreferenceConstants.VALUE_USE_IDS))
91                     return plugin.getId();
92                 return plugin.getTranslatedName();
93             }
94             return super.getText(element);
95         }
96
97         /* (non-Javadoc)
98          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
99          */

100         public Image getImage(Object JavaDoc element) {
101             return PDEPlugin.getDefault().getLabelProvider().getImage(element);
102         }
103
104         /* (non-Javadoc)
105          * @see org.eclipse.jface.viewers.LabelProvider#dispose()
106          */

107         public void dispose() {
108             super.dispose();
109             PDEPlugin.getDefault().getLabelProvider().disconnect(this);
110         }
111
112     }
113     
114     class CheckboxFilteredTree extends FilteredTree {
115         
116         public CheckboxFilteredTree(Composite parent, int treeStyle,
117                 PatternFilter filter) {
118             super(parent, treeStyle, filter);
119         }
120
121         protected TreeViewer doCreateTreeViewer(Composite parent, int style) {
122             return new CheckboxTreeViewer(parent, style);
123         }
124         
125         public CheckboxTreeViewer getCheckboxTreeViewer() {
126             return (CheckboxTreeViewer) getViewer();
127         }
128         
129         
130     }
131
132     private IWorkingSet fWorkingSet;
133     private Text fWorkingSetName;
134     private CheckboxFilteredTree fTree;
135     private boolean fFirstCheck;
136
137     public PluginWorkingSet() {
138         super("page1", PDEUIMessages.PluginWorkingSet_title, PDEPluginImages.DESC_DEFCON_WIZ); //$NON-NLS-1$
139
PDEPlugin.getDefault().getLabelProvider().connect(this);
140     }
141
142     /* (non-Javadoc)
143      * @see org.eclipse.ui.dialogs.IWorkingSetPage#finish()
144      */

145     public void finish() {
146         Object JavaDoc[] checked = fTree.getCheckboxTreeViewer().getCheckedElements();
147         ArrayList JavaDoc list = new ArrayList JavaDoc();
148         for (int i = 0; i < checked.length; i++) {
149             String JavaDoc id = ((IPluginModelBase)checked[i]).getPluginBase().getId();
150             if (id != null && id.length() > 0)
151                 list.add(new PersistablePluginObject(id));
152         }
153         PersistablePluginObject[] objects = (PersistablePluginObject[])list.toArray(new PersistablePluginObject[list.size()]);
154
155         String JavaDoc workingSetName = fWorkingSetName.getText().trim();
156         if (fWorkingSet == null) {
157             IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
158             fWorkingSet= workingSetManager.createWorkingSet(workingSetName, objects);
159         } else {
160             fWorkingSet.setName(workingSetName);
161             fWorkingSet.setElements(objects);
162         }
163     }
164
165     /* (non-Javadoc)
166      * @see org.eclipse.ui.dialogs.IWorkingSetPage#getSelection()
167      */

168     public IWorkingSet getSelection() {
169         return fWorkingSet;
170     }
171
172     /* (non-Javadoc)
173      * @see org.eclipse.ui.dialogs.IWorkingSetPage#setSelection(org.eclipse.ui.IWorkingSet)
174      */

175     public void setSelection(IWorkingSet workingSet) {
176         fWorkingSet = workingSet;
177     }
178
179     /* (non-Javadoc)
180      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
181      */

182     public void createControl(Composite parent) {
183         Composite composite= new Composite(parent, SWT.NONE);
184         composite.setLayout(new GridLayout());
185         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
186         setControl(composite);
187
188         Label label= new Label(composite, SWT.WRAP);
189         label.setText(PDEUIMessages.PluginWorkingSet_setName);
190         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
191
192         fWorkingSetName= new Text(composite, SWT.SINGLE | SWT.BORDER);
193         fWorkingSetName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
194         fWorkingSetName.addModifyListener(
195                 new ModifyListener() {
196                     public void modifyText(ModifyEvent e) {
197                         validatePage();
198                     }
199                 }
200         );
201         fWorkingSetName.setFocus();
202
203         label= new Label(composite, SWT.WRAP);
204         label.setText(PDEUIMessages.PluginWorkingSet_setContent);
205         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
206
207         fTree = new CheckboxFilteredTree(composite, SWT.BORDER, new PatternFilter());
208         GridData gd = new GridData(GridData.FILL_BOTH);
209         gd.heightHint = 250;
210         fTree.getViewer().getControl().setLayoutData(gd);
211         final IStructuredContentProvider fTableContentProvider = new ContentProvider();
212         fTree.getCheckboxTreeViewer().setContentProvider(fTableContentProvider);
213         fTree.getCheckboxTreeViewer().setLabelProvider(new WorkingSetLabelProvider());
214         fTree.getCheckboxTreeViewer().setUseHashlookup(true);
215         fTree.getCheckboxTreeViewer().setInput(PDECore.getDefault());
216
217         fTree.getCheckboxTreeViewer().addCheckStateListener(new ICheckStateListener() {
218             public void checkStateChanged(CheckStateChangedEvent event) {
219                 validatePage();
220             }
221         });
222
223         // Add select / deselect all buttons for bug 46669
224
Composite buttonComposite = new Composite(composite, SWT.NONE);
225         buttonComposite.setLayout(new GridLayout(2, true));
226         buttonComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
227
228         Button selectAllButton = new Button(buttonComposite, SWT.PUSH);
229         selectAllButton.setText(PDEUIMessages.PluginWorkingSet_selectAll_label);
230         selectAllButton.setToolTipText(PDEUIMessages.PluginWorkingSet_selectAll_toolTip);
231         selectAllButton.addSelectionListener(new SelectionAdapter() {
232             public void widgetSelected(SelectionEvent selectionEvent) {
233                 fTree.getCheckboxTreeViewer().setCheckedElements(fTableContentProvider.getElements(fTree.getCheckboxTreeViewer().getInput()));
234                 validatePage();
235             }
236         });
237         selectAllButton.setLayoutData(new GridData());
238         SWTUtil.setButtonDimensionHint(selectAllButton);
239
240         Button deselectAllButton = new Button(buttonComposite, SWT.PUSH);
241         deselectAllButton.setText(PDEUIMessages.PluginWorkingSet_deselectAll_label);
242         deselectAllButton.setToolTipText(PDEUIMessages.PluginWorkingSet_deselectAll_toolTip);
243         deselectAllButton.addSelectionListener(new SelectionAdapter() {
244             public void widgetSelected(SelectionEvent selectionEvent) {
245                 fTree.getCheckboxTreeViewer().setCheckedElements(new Object JavaDoc[0]);
246                 validatePage();
247             }
248         });
249         deselectAllButton.setLayoutData(new GridData());
250         SWTUtil.setButtonDimensionHint(deselectAllButton);
251         setPageComplete(false);
252         setMessage(PDEUIMessages.PluginWorkingSet_message);
253
254         initialize();
255         Dialog.applyDialogFont(composite);
256
257         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.PLUGIN_WORKING_SET);
258     }
259
260     /**
261      *
262      */

263     private void initialize() {
264         if (fWorkingSet != null) {
265             HashSet JavaDoc set = new HashSet JavaDoc();
266             IAdaptable[] elements = fWorkingSet.getElements();
267             for (int i = 0; i < elements.length; i++) {
268                 if (elements[i] instanceof PersistablePluginObject)
269                     set.add(((PersistablePluginObject)elements[i]).getPluginID());
270             }
271
272             IPluginModelBase[] bases = PluginRegistry.getAllModels();
273             for (int i = 0; i < bases.length; i++) {
274
275                 String JavaDoc id = bases[i].getPluginBase().getId();
276                 if (id == null)
277                     continue;
278                 if (set.contains(id)) {
279                     fTree.getCheckboxTreeViewer().setChecked(bases[i], true);
280                     set.remove(id);
281                 }
282                 if (set.isEmpty())
283                     break;
284             }
285             fWorkingSetName.setText(fWorkingSet.getName());
286         }
287     }
288
289     /* (non-Javadoc)
290      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
291      */

292     public void dispose() {
293         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
294     }
295
296     private void validatePage() {
297         String JavaDoc errorMessage= null;
298         String JavaDoc newText= fWorkingSetName.getText();
299
300         if (newText.trim().length() == 0) {
301             errorMessage = PDEUIMessages.PluginWorkingSet_emptyName;
302             if (fFirstCheck) {
303                 setPageComplete(false);
304                 fFirstCheck= false;
305                 return;
306             }
307         }
308         if (errorMessage == null && fTree.getCheckboxTreeViewer().getCheckedElements().length == 0) {
309             errorMessage = PDEUIMessages.PluginWorkingSet_noPluginsChecked;
310         }
311
312         if (errorMessage == null && fWorkingSet == null) {
313             IWorkingSet[] workingSets = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets();
314             for (int i = 0; i < workingSets.length; i++) {
315                 if (newText.equals(workingSets[i].getName())) {
316                     errorMessage = PDEUIMessages.PluginWorkingSet_nameInUse;
317                     break;
318                 }
319             }
320         }
321         setErrorMessage(errorMessage);
322         setPageComplete(errorMessage == null);
323     }
324
325 }
326
Popular Tags