001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.launcher;
011:
012: import java.util.Iterator;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.debug.core.ILaunchConfiguration;
017: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
018: import org.eclipse.jface.viewers.CheckStateChangedEvent;
019: import org.eclipse.jface.viewers.ILabelProvider;
020: import org.eclipse.pde.core.plugin.IPluginModelBase;
021: import org.eclipse.pde.internal.ui.PDELabelProvider;
022: import org.eclipse.pde.internal.ui.PDEUIMessages;
023: import org.eclipse.pde.internal.ui.elements.NamedElement;
024: import org.eclipse.pde.ui.launcher.BundlesTab;
025: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.custom.CCombo;
028: import org.eclipse.swt.custom.TreeEditor;
029: import org.eclipse.swt.events.ModifyEvent;
030: import org.eclipse.swt.events.ModifyListener;
031: import org.eclipse.swt.events.SelectionAdapter;
032: import org.eclipse.swt.events.SelectionEvent;
033: import org.eclipse.swt.graphics.Image;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Control;
036: import org.eclipse.swt.widgets.Spinner;
037: import org.eclipse.swt.widgets.Tree;
038: import org.eclipse.swt.widgets.TreeColumn;
039: import org.eclipse.swt.widgets.TreeItem;
040: import org.eclipse.swt.widgets.Widget;
041:
042: public class OSGiBundleBlock extends AbstractPluginBlock {
043:
044: class OSGiLabelProvider extends PDELabelProvider {
045: public Image getColumnImage(Object obj, int index) {
046: return index == 0 ? super .getColumnImage(obj, index) : null;
047: }
048:
049: public String getColumnText(Object obj, int index) {
050: switch (index) {
051: case 0:
052: return super .getColumnText(obj, index);
053: default:
054: return ""; //$NON-NLS-1$
055: }
056: }
057: }
058:
059: private ILaunchConfiguration fLaunchConfiguration;
060:
061: public OSGiBundleBlock(BundlesTab tab) {
062: super (tab);
063: }
064:
065: protected void createPluginViewer(Composite composite, int span,
066: int indent) {
067: super .createPluginViewer(composite, span, indent);
068: Tree tree = fPluginTreeViewer.getTree();
069:
070: TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
071: column1.setText(fTab.getName());
072: column1.setWidth(300);
073:
074: TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
075: column2.setText(PDEUIMessages.EquinoxPluginBlock_levelColumn);
076: column2.setWidth(80);
077:
078: TreeColumn column3 = new TreeColumn(tree, SWT.CENTER);
079: column3.setText(PDEUIMessages.EquinoxPluginBlock_autoColumn);
080: column3.setWidth(80);
081: tree.setHeaderVisible(true);
082:
083: createEditors();
084: }
085:
086: private void createEditors() {
087: final Tree tree = fPluginTreeViewer.getTree();
088:
089: final TreeEditor editor1 = new TreeEditor(tree);
090: editor1.horizontalAlignment = SWT.CENTER;
091: editor1.minimumWidth = 60;
092:
093: final TreeEditor editor2 = new TreeEditor(tree);
094: editor2.horizontalAlignment = SWT.CENTER;
095: editor2.grabHorizontal = true;
096: editor2.minimumWidth = 60;
097:
098: tree.addSelectionListener(new SelectionAdapter() {
099: public void widgetSelected(SelectionEvent e) {
100: // Clean up any previous editor control
101: Control oldEditor = editor1.getEditor();
102: if (oldEditor != null)
103: oldEditor.dispose();
104:
105: oldEditor = editor2.getEditor();
106: if (oldEditor != null)
107: oldEditor.dispose();
108:
109: // Identify the selected row
110: final TreeItem item = (TreeItem) e.item;
111: if (!isEditable(item))
112: return;
113:
114: final Spinner spinner = new Spinner(tree, SWT.BORDER);
115: spinner.setMinimum(0);
116: String level = item.getText(1);
117: int defaultLevel = level.length() == 0
118: || "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 ? "default" //$NON-NLS-1$
125: : Integer.toString(selection));
126: fTab.updateLaunchConfigurationDialog();
127: }
128: }
129: });
130: editor1.setEditor(spinner, item, 1);
131:
132: final CCombo combo = new CCombo(tree, SWT.BORDER
133: | SWT.READ_ONLY);
134: combo
135: .setItems(new String[] {
136: "default", Boolean.toString(true), Boolean.toString(false) }); //$NON-NLS-1$
137: combo.setText(item.getText(2));
138: combo.pack();
139: combo.addSelectionListener(new SelectionAdapter() {
140: public void widgetSelected(SelectionEvent e) {
141: if (item.getChecked()) {
142: item.setText(2, combo.getText());
143: fTab.updateLaunchConfigurationDialog();
144: }
145: }
146: });
147: editor2.setEditor(combo, item, 2);
148:
149: }
150: });
151: }
152:
153: private boolean isEditable(TreeItem item) {
154: Object obj = item.getData();
155: if (obj instanceof IPluginModelBase) {
156: IPluginModelBase model = (IPluginModelBase) obj;
157: if (!"org.eclipse.osgi".equals(model.getPluginBase().getId())) //$NON-NLS-1$
158: return fPluginTreeViewer.getChecked(model);
159: }
160: return false;
161: }
162:
163: protected ILabelProvider getLabelProvider() {
164: return new OSGiLabelProvider();
165: }
166:
167: protected void savePluginState(
168: ILaunchConfigurationWorkingCopy config) {
169: Object[] selected = fPluginTreeViewer.getCheckedElements();
170: StringBuffer wBuffer = new StringBuffer();
171: StringBuffer tBuffer = new StringBuffer();
172: for (int i = 0; i < selected.length; i++) {
173: if (selected[i] instanceof IPluginModelBase) {
174: IPluginModelBase model = (IPluginModelBase) selected[i];
175: String id = model.getPluginBase().getId();
176: TreeItem item = (TreeItem) fPluginTreeViewer
177: .testFindItem(model);
178: if (model.getUnderlyingResource() == null) {
179: appendToBuffer(tBuffer, id, item);
180: } else {
181: appendToBuffer(wBuffer, id, item);
182: }
183: }
184: }
185: config.setAttribute(IPDELauncherConstants.WORKSPACE_BUNDLES,
186: wBuffer.length() == 0 ? (String) null : wBuffer
187: .toString());
188: config.setAttribute(IPDELauncherConstants.TARGET_BUNDLES,
189: tBuffer.length() == 0 ? (String) null : tBuffer
190: .toString());
191:
192: StringBuffer buffer = new StringBuffer();
193: if (fAddWorkspaceButton.getSelection()) {
194: for (int i = 0; i < fWorkspaceModels.length; i++) {
195: if (!fPluginTreeViewer.getChecked(fWorkspaceModels[i])) {
196: if (buffer.length() > 0)
197: buffer.append(","); //$NON-NLS-1$
198: buffer.append(fWorkspaceModels[i].getPluginBase()
199: .getId());
200: }
201: }
202: }
203: config
204: .setAttribute(
205: IPDELauncherConstants.DESELECTED_WORKSPACE_PLUGINS,
206: buffer.length() > 0 ? buffer.toString()
207: : (String) null);
208: }
209:
210: private void appendToBuffer(StringBuffer buffer, String id,
211: TreeItem item) {
212: if (buffer.length() > 0)
213: buffer.append(","); //$NON-NLS-1$
214: buffer.append(id);
215: buffer.append("@"); //$NON-NLS-1$
216: buffer.append(item.getText(1));
217: buffer.append(":"); //$NON-NLS-1$
218: buffer.append(item.getText(2));
219: }
220:
221: public void initializeFrom(ILaunchConfiguration configuration)
222: throws CoreException {
223: super .initializeFrom(configuration);
224: initWorkspacePluginsState(configuration);
225: initExternalPluginsState(configuration);
226: updateCounter();
227: fLaunchConfiguration = configuration;
228: }
229:
230: private void initExternalPluginsState(
231: ILaunchConfiguration configuration) throws CoreException {
232: Map map = BundleLauncherHelper
233: .getTargetBundleMap(configuration);
234: Iterator iter = map.keySet().iterator();
235: while (iter.hasNext()) {
236: IPluginModelBase model = (IPluginModelBase) iter.next();
237: if (fPluginTreeViewer.setChecked(model, true)) {
238: setText(model, map.get(model).toString());
239: }
240: }
241: fNumExternalChecked = map.size();
242: resetGroup(fExternalPlugins);
243: fPluginTreeViewer.setChecked(fExternalPlugins,
244: fNumExternalChecked > 0);
245: fPluginTreeViewer
246: .setGrayed(fExternalPlugins, fNumExternalChecked > 0
247: && fNumExternalChecked < fExternalModels.length);
248: }
249:
250: private void resetGroup(NamedElement group) {
251: Widget widget = fPluginTreeViewer.testFindItem(group);
252: if (widget instanceof TreeItem) {
253: TreeItem[] items = ((TreeItem) widget).getItems();
254: for (int i = 0; i < items.length; i++) {
255: if (!items[i].getChecked()) {
256: resetText(items[i]);
257: }
258: }
259: }
260: }
261:
262: private void initWorkspacePluginsState(
263: ILaunchConfiguration configuration) throws CoreException {
264: Map map = BundleLauncherHelper
265: .getWorkspaceBundleMap(configuration);
266: Iterator iter = map.keySet().iterator();
267: while (iter.hasNext()) {
268: IPluginModelBase model = (IPluginModelBase) iter.next();
269: if (fPluginTreeViewer.setChecked(model, true)) {
270: setText(model, map.get(model).toString());
271: }
272: }
273: fNumWorkspaceChecked = map.size();
274: resetGroup(fWorkspacePlugins);
275:
276: fPluginTreeViewer.setChecked(fWorkspacePlugins,
277: fNumWorkspaceChecked > 0);
278: fPluginTreeViewer
279: .setGrayed(
280: fWorkspacePlugins,
281: fNumWorkspaceChecked > 0
282: && fNumWorkspaceChecked < fWorkspaceModels.length);
283: }
284:
285: protected void handleGroupStateChanged(Object group, boolean checked) {
286: super .handleGroupStateChanged(group, checked);
287: Widget item = fPluginTreeViewer.testFindItem(group);
288: if (item instanceof TreeItem) {
289: TreeItem[] items = ((TreeItem) item).getItems();
290: for (int i = 0; i < items.length; i++) {
291: TreeItem child = items[i];
292: if (child.getChecked() == (child.getText(1).length() == 0))
293: resetText(items[i]);
294: }
295: }
296: }
297:
298: protected void handleCheckStateChanged(CheckStateChangedEvent event) {
299: super .handleCheckStateChanged(event);
300: resetText((IPluginModelBase) event.getElement());
301: }
302:
303: protected void setChecked(IPluginModelBase model, boolean checked) {
304: super .setChecked(model, checked);
305: resetText(model);
306: }
307:
308: protected void setCheckedElements(Object[] checked) {
309: super .setCheckedElements(checked);
310: updateGroup(fWorkspacePlugins);
311: updateGroup(fExternalPlugins);
312: }
313:
314: private void updateGroup(Object group) {
315: Widget item = fPluginTreeViewer.testFindItem(group);
316: if (item instanceof TreeItem) {
317: TreeItem[] items = ((TreeItem) item).getItems();
318: for (int i = 0; i < items.length; i++) {
319: TreeItem child = items[i];
320: if (child.getChecked() == (child.getText(1).length() == 0))
321: resetText(items[i]);
322: }
323: }
324: }
325:
326: private void setText(IPluginModelBase model, String value) {
327: Widget widget = fPluginTreeViewer.testFindItem(model);
328: if (widget instanceof TreeItem) {
329: TreeItem item = (TreeItem) widget;
330: int index = value == null ? -1 : value.indexOf(':');
331: item.setText(1,
332: index == -1 ? "" : value.substring(0, index)); //$NON-NLS-1$
333: item.setText(2,
334: index == -1 ? "" : value.substring(index + 1)); //$NON-NLS-1$
335: }
336: }
337:
338: private void resetText(IPluginModelBase model) {
339: Widget widget = fPluginTreeViewer.testFindItem(model);
340: if (widget instanceof TreeItem) {
341: resetText((TreeItem) widget);
342: }
343: }
344:
345: private void resetText(TreeItem item) {
346: if (item.getChecked()) {
347: IPluginModelBase model = (IPluginModelBase) item.getData();
348: boolean isSystemBundle = "org.eclipse.osgi".equals(model.getPluginBase().getId()); //$NON-NLS-1$
349: if (!"default".equals(item.getText(1))) //$NON-NLS-1$
350: item.setText(1, isSystemBundle ? "" : "default"); //$NON-NLS-1$ //$NON-NLS-2$
351: if (!"default".equals(item.getText(2))) //$NON-NLS-1$
352: item.setText(2, isSystemBundle ? "" : "default"); //$NON-NLS-1$ //$NON-NLS-2$
353: } else {
354: if (item.getText(1).length() > 0)
355: item.setText(1, ""); //$NON-NLS-1$
356: if (item.getText(2).length() > 0)
357: item.setText(2, ""); //$NON-NLS-1$
358: }
359: }
360:
361: protected void handleRestoreDefaults() {
362: Object[] selected = fPluginTreeViewer.getCheckedElements();
363: for (int i = 0; i < selected.length; i++) {
364: if (selected[i] instanceof IPluginModelBase) {
365: resetText((IPluginModelBase) selected[i]);
366: }
367: }
368: }
369:
370: protected int getTreeViewerStyle() {
371: return super .getTreeViewerStyle() | SWT.FULL_SELECTION;
372: }
373:
374: protected LaunchValidationOperation createValidationOperation() {
375: return new OSGiValidationOperation(fLaunchConfiguration);
376: }
377:
378: }
|