KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > EquinoxPluginBlock


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  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.launcher;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16 import java.util.TreeMap JavaDoc;
17 import java.util.TreeSet JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.debug.core.ILaunchConfiguration;
21 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
22 import org.eclipse.jface.viewers.CheckStateChangedEvent;
23 import org.eclipse.jface.viewers.ILabelProvider;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.core.PDECore;
26 import org.eclipse.pde.internal.ui.PDELabelProvider;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.pde.internal.ui.elements.NamedElement;
29 import org.eclipse.pde.ui.launcher.EquinoxPluginsTab;
30 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.custom.CCombo;
33 import org.eclipse.swt.custom.TreeEditor;
34 import org.eclipse.swt.events.ModifyEvent;
35 import org.eclipse.swt.events.ModifyListener;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Spinner;
42 import org.eclipse.swt.widgets.Tree;
43 import org.eclipse.swt.widgets.TreeColumn;
44 import org.eclipse.swt.widgets.TreeItem;
45 import org.eclipse.swt.widgets.Widget;
46
47 public class EquinoxPluginBlock extends AbstractPluginBlock {
48     
49     class EquinoxLabelProvider extends PDELabelProvider {
50         public Image getColumnImage(Object JavaDoc obj, int index) {
51             return index == 0 ? super.getColumnImage(obj, index) : null;
52         }
53         public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
54             switch (index) {
55             case 0:
56                 return super.getColumnText(obj, index);
57             default:
58                 return ""; //$NON-NLS-1$
59
}
60         }
61     }
62
63     public EquinoxPluginBlock(EquinoxPluginsTab tab) {
64         super(tab);
65     }
66     
67     protected void createPluginViewer(Composite composite) {
68         super.createPluginViewer(composite);
69         Tree tree = fPluginTreeViewer.getTree();
70  
71         TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
72         column1.setText(PDEUIMessages.EquinoxPluginBlock_pluginsColumn);
73         column1.setWidth(300);
74
75         TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
76         column2.setText(PDEUIMessages.EquinoxPluginBlock_levelColumn);
77         column2.setWidth(80);
78          
79         TreeColumn column3 = new TreeColumn(tree, SWT.CENTER);
80         column3.setText(PDEUIMessages.EquinoxPluginBlock_autoColumn);
81         column3.setWidth(80);
82         tree.setHeaderVisible(true);
83
84         createEditors();
85     }
86     
87     private void createEditors() {
88         final Tree tree = fPluginTreeViewer.getTree();
89
90         final TreeEditor editor1 = new TreeEditor(tree);
91         editor1.horizontalAlignment = SWT.CENTER;
92         editor1.minimumWidth = 60;
93
94         final TreeEditor editor2 = new TreeEditor(tree);
95         editor2.horizontalAlignment = SWT.CENTER;
96         editor2.grabHorizontal = true;
97         editor2.minimumWidth = 60;
98
99         tree.addSelectionListener(new SelectionAdapter() {
100             public void widgetSelected(SelectionEvent e) {
101                 // Clean up any previous editor control
102
Control oldEditor = editor1.getEditor();
103                 if (oldEditor != null)
104                     oldEditor.dispose();
105
106                 oldEditor = editor2.getEditor();
107                 if (oldEditor != null)
108                     oldEditor.dispose();
109
110                 // Identify the selected row
111
final TreeItem item = (TreeItem) e.item;
112                 if (!isEditable(item))
113                     return;
114
115                 final Spinner spinner = new Spinner(tree, SWT.BORDER);
116                 spinner.setMinimum(0);
117                 String JavaDoc level = item.getText(1);
118                 int defaultLevel = level.length() == 0 || "default".equals(level) ? 0 : Integer.parseInt(level); //$NON-NLS-1$
119
spinner.setSelection(defaultLevel);
120                 spinner.addModifyListener(new ModifyListener() {
121                     public void modifyText(ModifyEvent e) {
122                         if (item.getChecked()) {
123                             int selection = spinner.getSelection();
124                             item.setText(1, selection == 0
125                                                 ? "default" //$NON-NLS-1$
126
: Integer.toString(selection));
127                             fTab.updateLaunchConfigurationDialog();
128                         }
129                     }
130                 });
131                 editor1.setEditor(spinner, item, 1);
132
133                 final CCombo combo = new CCombo(tree, SWT.BORDER | SWT.READ_ONLY);
134                 combo.setItems(new String JavaDoc[] { "default", Boolean.toString(true), Boolean.toString(false) }); //$NON-NLS-1$
135
combo.setText(item.getText(2));
136                 combo.pack();
137                 combo.addSelectionListener(new SelectionAdapter() {
138                     public void widgetSelected(SelectionEvent e) {
139                         if (item.getChecked()) {
140                             item.setText(2, combo.getText());
141                             fTab.updateLaunchConfigurationDialog();
142                         }
143                     }
144                 });
145                 editor2.setEditor(combo, item, 2);
146
147             }
148         });
149     }
150     
151     private boolean isEditable(TreeItem item) {
152         Object JavaDoc obj = item.getData();
153         if (obj instanceof IPluginModelBase) {
154             IPluginModelBase model = (IPluginModelBase)obj;
155             if (!"org.eclipse.osgi".equals(model.getPluginBase().getId())) //$NON-NLS-1$
156
return fPluginTreeViewer.getChecked(model);
157         }
158         return false;
159     }
160     
161     protected ILabelProvider getLabelProvider() {
162         return new EquinoxLabelProvider();
163     }
164     
165     protected void savePluginState(ILaunchConfigurationWorkingCopy config) {
166         Object JavaDoc[] selected = fPluginTreeViewer.getCheckedElements();
167         StringBuffer JavaDoc wBuffer = new StringBuffer JavaDoc();
168         StringBuffer JavaDoc tBuffer = new StringBuffer JavaDoc();
169         for (int i = 0; i < selected.length; i++) {
170             if (selected[i] instanceof IPluginModelBase) {
171                 IPluginModelBase model = (IPluginModelBase)selected[i];
172                 String JavaDoc id = model.getPluginBase().getId();
173                 TreeItem item = (TreeItem)fPluginTreeViewer.testFindItem(model);
174                 if (model.getUnderlyingResource() == null) {
175                     appendToBuffer(tBuffer, id, item);
176                 } else {
177                     appendToBuffer(wBuffer, id, item);
178                 }
179             }
180         }
181         config.setAttribute(IPDELauncherConstants.WORKSPACE_BUNDLES,
182                             wBuffer.length() == 0 ? (String JavaDoc)null : wBuffer.toString());
183         config.setAttribute(IPDELauncherConstants.TARGET_BUNDLES,
184                             tBuffer.length() == 0 ? (String JavaDoc)null : tBuffer.toString());
185         
186         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
187         if (fAddWorkspaceButton.getSelection()) {
188             for (int i = 0; i < fWorkspaceModels.length; i++) {
189                 if (!fPluginTreeViewer.getChecked(fWorkspaceModels[i])) {
190                     if (buffer.length() > 0)
191                         buffer.append(","); //$NON-NLS-1$
192
buffer.append(fWorkspaceModels[i].getPluginBase().getId());
193                 }
194             }
195         }
196         config.setAttribute(IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS,
197                             buffer.length() > 0 ? buffer.toString() : (String JavaDoc)null);
198     }
199     
200     private void appendToBuffer(StringBuffer JavaDoc buffer, String JavaDoc id, TreeItem item) {
201         if (buffer.length() > 0)
202             buffer.append(","); //$NON-NLS-1$
203
buffer.append(id);
204         buffer.append("@"); //$NON-NLS-1$
205
buffer.append(item.getText(1));
206         buffer.append(":"); //$NON-NLS-1$
207
buffer.append(item.getText(2));
208     }
209     
210     private static Map JavaDoc retrieveMap(ILaunchConfiguration configuration, String JavaDoc attribute) {
211         Map JavaDoc map = new TreeMap JavaDoc();
212         try {
213             String JavaDoc value = configuration.getAttribute(attribute, ""); //$NON-NLS-1$
214
StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
215
while (tok.hasMoreTokens()) {
216                 String JavaDoc token = tok.nextToken();
217                 int index = token.indexOf('@');
218                 map.put(token.substring(0, index), token.substring(index + 1));
219             }
220         } catch (CoreException e) {
221         }
222         return map;
223     }
224     
225     public static Map JavaDoc retrieveTargetMap(ILaunchConfiguration configuration) {
226         return retrieveMap(configuration, IPDELauncherConstants.TARGET_BUNDLES);
227     }
228     
229     public static Map JavaDoc retrieveWorkspaceMap(ILaunchConfiguration configuration) throws CoreException {
230         Map JavaDoc map = retrieveMap(configuration, IPDELauncherConstants.WORKSPACE_BUNDLES);
231         if (configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_ADD, true)) {
232             TreeSet JavaDoc deselectedPlugins = LaunchPluginValidator.parsePlugins(configuration, IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS);
233             IPluginModelBase[] models = PDECore.getDefault().getModelManager().getWorkspaceModels();
234             for (int i = 0; i < models.length; i++) {
235                 String JavaDoc id = models[i].getPluginBase().getId();
236                 if (id != null && !map.containsKey(id) && !deselectedPlugins.contains(id)) {
237                     map.put(id, "default:default"); //$NON-NLS-1$
238
}
239             }
240         }
241         return map;
242     }
243
244     public void initializeFrom(ILaunchConfiguration configuration) throws CoreException {
245         super.initializeFrom(configuration);
246         initWorkspacePluginsState(configuration);
247         initExternalPluginsState(configuration);
248         updateCounter();
249     }
250         
251     private void initExternalPluginsState(ILaunchConfiguration configuration)
252             throws CoreException {
253         fNumExternalChecked = 0;
254         fPluginTreeViewer.setSubtreeChecked(fExternalPlugins, false);
255         
256         Map JavaDoc map = retrieveTargetMap(configuration);
257         for (int i = 0; i < fExternalModels.length; i++) {
258             IPluginModelBase model = fExternalModels[i];
259             if (map.containsKey(model.getPluginBase().getId())) {
260                 if (fPluginTreeViewer.setChecked(model, true)) {
261                     fNumExternalChecked += 1;
262                     setText(model, (String JavaDoc)map.get(model.getPluginBase().getId()));
263                 }
264             }
265         }
266         
267         resetGroup(fExternalPlugins);
268         fPluginTreeViewer.setChecked(fExternalPlugins, fNumExternalChecked > 0);
269         fPluginTreeViewer.setGrayed(fExternalPlugins, fNumExternalChecked > 0
270                 && fNumExternalChecked < fExternalModels.length);
271     }
272
273     private void resetGroup(NamedElement group) {
274         Widget widget = fPluginTreeViewer.testFindItem(group);
275         if (widget instanceof TreeItem) {
276             TreeItem[] items = ((TreeItem)widget).getItems();
277             for (int i = 0; i < items.length; i++) {
278                 if (!items[i].getChecked()) {
279                     resetText(items[i]);
280                 }
281             }
282         }
283     }
284
285     private void initWorkspacePluginsState(ILaunchConfiguration configuration)
286             throws CoreException {
287         fNumWorkspaceChecked = 0;
288         fPluginTreeViewer.setSubtreeChecked(fWorkspacePlugins, false);
289         
290         Map JavaDoc map = retrieveWorkspaceMap(configuration);
291         for (int i = 0; i < fWorkspaceModels.length; i++) {
292             IPluginModelBase model = fWorkspaceModels[i];
293             String JavaDoc id = model.getPluginBase().getId();
294             if (id != null && map.containsKey(id)) {
295                 if (fPluginTreeViewer.setChecked(model, true)) {
296                     fNumWorkspaceChecked += 1;
297                     setText(model, (String JavaDoc)map.get(model.getPluginBase().getId()));
298                 }
299             }
300         }
301         
302         resetGroup(fWorkspacePlugins);
303
304         fPluginTreeViewer.setChecked(fWorkspacePlugins, fNumWorkspaceChecked > 0);
305         fPluginTreeViewer.setGrayed(
306             fWorkspacePlugins,
307             fNumWorkspaceChecked > 0 && fNumWorkspaceChecked < fWorkspaceModels.length);
308     }
309     
310     protected void handleGroupStateChanged(Object JavaDoc group, boolean checked) {
311         super.handleGroupStateChanged(group, checked);
312         Widget item = fPluginTreeViewer.testFindItem(group);
313         if (item instanceof TreeItem) {
314             TreeItem[] items = ((TreeItem)item).getItems();
315             for (int i = 0; i < items.length; i++) {
316                 TreeItem child = items[i];
317                 if (child.getChecked() == (child.getText(1).length() == 0))
318                     resetText(items[i]);
319             }
320         }
321     }
322     
323     protected void handleCheckStateChanged(CheckStateChangedEvent event) {
324         super.handleCheckStateChanged(event);
325         resetText((IPluginModelBase)event.getElement());
326     }
327     
328     protected void setChecked(IPluginModelBase model, boolean checked) {
329         super.setChecked(model, checked);
330         resetText(model);
331     }
332     
333     protected void setCheckedElements(Object JavaDoc[] checked) {
334         super.setCheckedElements(checked);
335         updateGroup(fWorkspacePlugins);
336         updateGroup(fExternalPlugins);
337     }
338     
339     private void updateGroup(Object JavaDoc group) {
340         Widget item = fPluginTreeViewer.testFindItem(group);
341         if (item instanceof TreeItem) {
342             TreeItem[] items = ((TreeItem)item).getItems();
343             for (int i = 0; i < items.length; i++) {
344                 TreeItem child = items[i];
345                 if (child.getChecked() == (child.getText(1).length() == 0))
346                     resetText(items[i]);
347             }
348         }
349     }
350     
351     private void setText(IPluginModelBase model, String JavaDoc value) {
352         Widget widget = fPluginTreeViewer.testFindItem(model);
353         if (widget instanceof TreeItem) {
354             TreeItem item = (TreeItem)widget;
355             int index = value == null ? -1 : value.indexOf(':');
356             item.setText(1, index == -1 ? "" : value.substring(0, index)); //$NON-NLS-1$
357
item.setText(2, index == -1 ? "" : value.substring(index + 1)); //$NON-NLS-1$
358
}
359     }
360     
361     private void resetText(IPluginModelBase model) {
362         Widget widget = fPluginTreeViewer.testFindItem(model);
363         if (widget instanceof TreeItem) {
364             resetText((TreeItem)widget);
365         }
366     }
367     
368     private void resetText(TreeItem item) {
369         if (item.getChecked()) {
370             IPluginModelBase model = (IPluginModelBase)item.getData();
371             boolean isSystemBundle = "org.eclipse.osgi".equals(model.getPluginBase().getId()); //$NON-NLS-1$
372
if (!"default".equals(item.getText(1))) //$NON-NLS-1$
373
item.setText(1, isSystemBundle ? "" : "default"); //$NON-NLS-1$ //$NON-NLS-2$
374
if (!"default".equals(item.getText(2))) //$NON-NLS-1$
375
item.setText(2, isSystemBundle ? "" : "default"); //$NON-NLS-1$ //$NON-NLS-2$
376
} else {
377             if (item.getText(1).length() > 0)
378                 item.setText(1, ""); //$NON-NLS-1$
379
if (item.getText(2).length() > 0)
380                 item.setText(2, ""); //$NON-NLS-1$
381
}
382     }
383     
384     protected void handleRestoreDefaults() {
385         Object JavaDoc[] selected = fPluginTreeViewer.getCheckedElements();
386         for (int i = 0; i < selected.length; i++) {
387             if (selected[i] instanceof IPluginModelBase) {
388                 resetText((IPluginModelBase)selected[i]);
389             }
390         }
391     }
392     
393     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
394         super.setDefaults(config);
395         EquinoxLaunchShortcut.initializePluginState(config,
396                 PDECore.getDefault().getModelManager().getWorkspaceModels());
397     }
398     
399     protected PluginValidationOperation createValidationOperation() {
400         return new PluginValidationOperation(getPluginsToValidate());
401     }
402     
403     protected IPluginModelBase[] getPluginsToValidate() {
404         if (!fPluginTreeViewer.getTree().isEnabled())
405             return PDECore.getDefault().getModelManager().getPlugins();
406         
407         Map JavaDoc map = new HashMap JavaDoc();
408         Object JavaDoc[] objects = fPluginTreeViewer.getCheckedElements();
409         for (int i = 0; i < objects.length; i++) {
410             if (objects[i] instanceof IPluginModelBase) {
411                 IPluginModelBase model = (IPluginModelBase)objects[i];
412                 String JavaDoc id = model.getPluginBase().getId();
413                 if (id == null)
414                     continue;
415                 if (!map.containsKey(id) || model.getUnderlyingResource() != null)
416                     map.put(id, model);
417             }
418         }
419         return (IPluginModelBase[])map.values().toArray(new IPluginModelBase[map.size()]);
420     }
421     
422     protected int getTreeViewerStyle() {
423         return super.getTreeViewerStyle() | SWT.FULL_SELECTION;
424     }
425
426
427 }
428
Popular Tags