KickJava   Java API By Example, From Geeks To Geeks.

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


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