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.ArrayList;
013: import java.util.Enumeration;
014: import java.util.Hashtable;
015: import java.util.Map;
016: import java.util.Properties;
017: import java.util.StringTokenizer;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.debug.core.ILaunchConfiguration;
021: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
022: import org.eclipse.jface.viewers.ArrayContentProvider;
023: import org.eclipse.jface.viewers.CheckStateChangedEvent;
024: import org.eclipse.jface.viewers.CheckboxTableViewer;
025: import org.eclipse.jface.viewers.ICheckStateListener;
026: import org.eclipse.jface.viewers.ISelectionChangedListener;
027: import org.eclipse.jface.viewers.IStructuredSelection;
028: import org.eclipse.jface.viewers.SelectionChangedEvent;
029: import org.eclipse.jface.viewers.StructuredSelection;
030: import org.eclipse.pde.core.plugin.IPluginModelBase;
031: import org.eclipse.pde.core.plugin.PluginRegistry;
032: import org.eclipse.pde.internal.core.PDECore;
033: import org.eclipse.pde.internal.core.TracingOptionsManager;
034: import org.eclipse.pde.internal.ui.PDEPlugin;
035: import org.eclipse.pde.internal.ui.PDEUIMessages;
036: import org.eclipse.pde.internal.ui.util.SWTUtil;
037: import org.eclipse.pde.internal.ui.wizards.ListUtil;
038: import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
039: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
040: import org.eclipse.pde.ui.launcher.TracingTab;
041: import org.eclipse.swt.SWT;
042: import org.eclipse.swt.custom.SashForm;
043: import org.eclipse.swt.events.SelectionAdapter;
044: import org.eclipse.swt.events.SelectionEvent;
045: import org.eclipse.swt.layout.FillLayout;
046: import org.eclipse.swt.layout.GridData;
047: import org.eclipse.swt.layout.GridLayout;
048: import org.eclipse.swt.widgets.Button;
049: import org.eclipse.swt.widgets.Composite;
050: import org.eclipse.swt.widgets.Control;
051: import org.eclipse.ui.forms.widgets.FormToolkit;
052: import org.eclipse.ui.forms.widgets.ScrolledPageBook;
053:
054: public class TracingBlock {
055:
056: private TracingTab fTab;
057: private Button fTracingCheck;
058: private CheckboxTableViewer fPluginViewer;
059: private IPluginModelBase[] fTraceableModels;
060: private Properties fMasterOptions = new Properties();
061: private Button fSelectAllButton;
062: private Button fDeselectAllButton;
063: private Hashtable fPropertySources = new Hashtable();
064: private FormToolkit fToolkit;
065: private ScrolledPageBook fPageBook;
066:
067: public TracingBlock(TracingTab tab) {
068: fTab = tab;
069: }
070:
071: public AbstractLauncherTab getTab() {
072: return fTab;
073: }
074:
075: public void createControl(Composite parent) {
076: fTracingCheck = new Button(parent, SWT.CHECK);
077: fTracingCheck.setText(PDEUIMessages.TracingLauncherTab_tracing);
078: fTracingCheck.setLayoutData(new GridData(
079: GridData.FILL_HORIZONTAL));
080: fTracingCheck.addSelectionListener(new SelectionAdapter() {
081: public void widgetSelected(SelectionEvent e) {
082: masterCheckChanged(true);
083: fTab.updateLaunchConfigurationDialog();
084: }
085: });
086:
087: createSashSection(parent);
088: createButtonSection(parent);
089: }
090:
091: private void createSashSection(Composite container) {
092: SashForm sashForm = new SashForm(container, SWT.HORIZONTAL);
093: sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
094: createPluginViewer(sashForm);
095: createPropertySheetClient(sashForm);
096: }
097:
098: private void createPluginViewer(Composite sashForm) {
099: Composite composite = new Composite(sashForm, SWT.NONE);
100: GridLayout layout = new GridLayout();
101: layout.marginWidth = layout.marginHeight = 1;
102: composite.setLayout(layout);
103:
104: fPluginViewer = CheckboxTableViewer.newCheckList(composite,
105: SWT.BORDER);
106: fPluginViewer.setContentProvider(new ArrayContentProvider());
107: fPluginViewer.setLabelProvider(PDEPlugin.getDefault()
108: .getLabelProvider());
109: fPluginViewer.setComparator(new ListUtil.PluginComparator());
110: fPluginViewer
111: .addSelectionChangedListener(new ISelectionChangedListener() {
112: public void selectionChanged(SelectionChangedEvent e) {
113: CheckboxTableViewer tableViewer = (CheckboxTableViewer) e
114: .getSource();
115: boolean selected = tableViewer
116: .getChecked(getSelectedModel());
117: pluginSelected(getSelectedModel(), selected);
118: }
119: });
120: fPluginViewer.addCheckStateListener(new ICheckStateListener() {
121: public void checkStateChanged(CheckStateChangedEvent event) {
122: CheckboxTableViewer tableViewer = (CheckboxTableViewer) event
123: .getSource();
124: tableViewer.setSelection(new StructuredSelection(event
125: .getElement()));
126: pluginSelected(getSelectedModel(), event.getChecked());
127: fTab.updateLaunchConfigurationDialog();
128: }
129: });
130: GridData gd = new GridData(GridData.FILL_BOTH);
131: gd.widthHint = 125;
132: gd.heightHint = 100;
133: fPluginViewer.getTable().setLayoutData(gd);
134: fPluginViewer.setInput(getTraceableModels());
135: }
136:
137: private void createPropertySheetClient(Composite sashForm) {
138: Composite tableChild = new Composite(sashForm, SWT.NULL);
139: GridLayout layout = new GridLayout();
140: layout.marginHeight = layout.marginWidth = 0;
141: tableChild.setLayout(layout);
142: int margin = createPropertySheet(tableChild);
143: layout.marginWidth = layout.marginHeight = margin;
144: }
145:
146: private void createButtonSection(Composite parent) {
147: Composite container = new Composite(parent, SWT.NONE);
148: GridLayout layout = new GridLayout();
149: layout.numColumns = 2;
150: container.setLayout(layout);
151:
152: fSelectAllButton = new Button(container, SWT.PUSH);
153: fSelectAllButton
154: .setText(PDEUIMessages.TracingLauncherTab_selectAll);
155: fSelectAllButton.setLayoutData(new GridData(
156: GridData.HORIZONTAL_ALIGN_BEGINNING));
157: SWTUtil.setButtonDimensionHint(fSelectAllButton);
158: fSelectAllButton.addSelectionListener(new SelectionAdapter() {
159: public void widgetSelected(SelectionEvent e) {
160: fPluginViewer.setAllChecked(true);
161: fTab.updateLaunchConfigurationDialog();
162: }
163: });
164:
165: fDeselectAllButton = new Button(container, SWT.PUSH);
166: fDeselectAllButton
167: .setText(PDEUIMessages.TracinglauncherTab_deselectAll);
168: fDeselectAllButton.setLayoutData(new GridData(
169: GridData.HORIZONTAL_ALIGN_BEGINNING));
170: SWTUtil.setButtonDimensionHint(fDeselectAllButton);
171: fDeselectAllButton.addSelectionListener(new SelectionAdapter() {
172: public void widgetSelected(SelectionEvent e) {
173: fPluginViewer.setAllChecked(false);
174: fTab.updateLaunchConfigurationDialog();
175: }
176: });
177: }
178:
179: protected int createPropertySheet(Composite parent) {
180: fToolkit = new FormToolkit(parent.getDisplay());
181: int toolkitBorderStyle = fToolkit.getBorderStyle();
182: int style = toolkitBorderStyle == SWT.BORDER ? SWT.NULL
183: : SWT.BORDER;
184:
185: Composite container = new Composite(parent, style);
186: FillLayout flayout = new FillLayout();
187: flayout.marginWidth = 1;
188: flayout.marginHeight = 1;
189: container.setLayout(flayout);
190: container.setLayoutData(new GridData(GridData.FILL_BOTH));
191:
192: fPageBook = new ScrolledPageBook(container, style
193: | SWT.V_SCROLL | SWT.H_SCROLL);
194: fToolkit.adapt(fPageBook, false, false);
195:
196: if (style == SWT.NULL) {
197: fPageBook.setData(FormToolkit.KEY_DRAW_BORDER,
198: FormToolkit.TREE_BORDER);
199: fToolkit.paintBordersFor(container);
200: }
201: return style == SWT.NULL ? 2 : 0;
202: }
203:
204: public void initializeFrom(ILaunchConfiguration config) {
205: fMasterOptions.clear();
206: disposePropertySources();
207: try {
208: fTracingCheck.setSelection(config.getAttribute(
209: IPDELauncherConstants.TRACING, false));
210: Map options = config.getAttribute(
211: IPDELauncherConstants.TRACING_OPTIONS, (Map) null);
212: if (options == null)
213: options = PDECore.getDefault()
214: .getTracingOptionsManager()
215: .getTracingTemplateCopy();
216: else
217: options = PDECore.getDefault()
218: .getTracingOptionsManager().getTracingOptions(
219: options);
220: fMasterOptions.putAll(options);
221: masterCheckChanged(false);
222: IPluginModelBase model = getLastSelectedPlugin(config);
223: if (model != null) {
224: fPluginViewer.setSelection(new StructuredSelection(
225: model));
226: } else {
227: pluginSelected(null, false);
228: }
229: String checked = config.getAttribute(
230: IPDELauncherConstants.TRACING_CHECKED,
231: (String) null);
232: if (checked == null) {
233: fPluginViewer.setAllChecked(true);
234: } else if (checked
235: .equals(IPDELauncherConstants.TRACING_NONE)) {
236: fPluginViewer.setAllChecked(false);
237: } else {
238: StringTokenizer tokenizer = new StringTokenizer(
239: checked, ","); //$NON-NLS-1$
240: ArrayList list = new ArrayList();
241: while (tokenizer.hasMoreTokens()) {
242: String id = tokenizer.nextToken();
243: model = PluginRegistry.findModel(id);
244: if (model != null) {
245: list.add(model);
246: }
247: }
248: fPluginViewer.setCheckedElements(list.toArray());
249: }
250: } catch (CoreException e) {
251: PDEPlugin.logException(e);
252: }
253: }
254:
255: public void performApply(ILaunchConfigurationWorkingCopy config) {
256: boolean tracingEnabled = fTracingCheck.getSelection();
257: config.setAttribute(IPDELauncherConstants.TRACING,
258: tracingEnabled);
259: if (tracingEnabled) {
260: IPluginModelBase model = getSelectedModel();
261: String id = (model == null) ? null : model.getPluginBase()
262: .getId();
263: config.setAttribute(
264: IPDELauncherConstants.TRACING_SELECTED_PLUGIN, id);
265: boolean changes = false;
266: for (Enumeration elements = fPropertySources.elements(); elements
267: .hasMoreElements();) {
268: TracingPropertySource source = (TracingPropertySource) elements
269: .nextElement();
270: if (source.isModified()) {
271: changes = true;
272: source.save();
273: }
274: }
275: if (changes)
276: config.setAttribute(
277: IPDELauncherConstants.TRACING_OPTIONS,
278: fMasterOptions);
279: } else {
280: config.setAttribute(
281: IPDELauncherConstants.TRACING_SELECTED_PLUGIN,
282: (String) null);
283: }
284: Object[] checked = fPluginViewer.getCheckedElements();
285: if (checked.length == fPluginViewer.getTable().getItemCount()) {
286: config.setAttribute(IPDELauncherConstants.TRACING_CHECKED,
287: (String) null);
288: } else if (checked.length == 0) {
289: config.setAttribute(IPDELauncherConstants.TRACING_CHECKED,
290: IPDELauncherConstants.TRACING_NONE);
291: } else {
292: StringBuffer buffer = new StringBuffer();
293: for (int i = 0; i < checked.length; i++) {
294: IPluginModelBase model = (IPluginModelBase) checked[i];
295: buffer.append(model.getPluginBase().getId());
296: if (i < checked.length - 1)
297: buffer.append(',');
298: }
299: config.setAttribute(IPDELauncherConstants.TRACING_CHECKED,
300: buffer.toString());
301: }
302: }
303:
304: public void setDefaults(
305: ILaunchConfigurationWorkingCopy configuration) {
306: configuration
307: .setAttribute(IPDELauncherConstants.TRACING, false);
308: configuration.setAttribute(
309: IPDELauncherConstants.TRACING_CHECKED,
310: IPDELauncherConstants.TRACING_NONE);
311: }
312:
313: public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
314: fPageBook.getParent().getParent().layout(true);
315: }
316:
317: public void dispose() {
318: if (fToolkit != null)
319: fToolkit.dispose();
320: }
321:
322: public FormToolkit getToolkit() {
323: return fToolkit;
324: }
325:
326: private IPluginModelBase getSelectedModel() {
327: if (fTracingCheck.isEnabled()) {
328: Object item = ((IStructuredSelection) fPluginViewer
329: .getSelection()).getFirstElement();
330: if (item instanceof IPluginModelBase)
331: return ((IPluginModelBase) item);
332: }
333: return null;
334: }
335:
336: private void pluginSelected(IPluginModelBase model, boolean checked) {
337: TracingPropertySource source = getPropertySource(model);
338: if (source == null || checked == false) {
339: fPageBook.showEmptyPage();
340: } else {
341: if (!fPageBook.hasPage(model)) {
342: Composite parent = fPageBook.createPage(model);
343: source.createContents(parent);
344: }
345: fPageBook.showPage(model);
346: }
347: }
348:
349: private IPluginModelBase[] getTraceableModels() {
350: if (fTraceableModels == null) {
351: IPluginModelBase[] models = PluginRegistry
352: .getActiveModels();
353: ArrayList result = new ArrayList();
354: for (int i = 0; i < models.length; i++) {
355: if (TracingOptionsManager.isTraceable(models[i]))
356: result.add(models[i]);
357: }
358: fTraceableModels = (IPluginModelBase[]) result
359: .toArray(new IPluginModelBase[result.size()]);
360: }
361: return fTraceableModels;
362: }
363:
364: private IPluginModelBase getLastSelectedPlugin(
365: ILaunchConfiguration config) throws CoreException {
366: String pluginID = config.getAttribute(
367: IPDELauncherConstants.TRACING_SELECTED_PLUGIN,
368: (String) null);
369: return pluginID == null ? null : PluginRegistry
370: .findModel(pluginID);
371: }
372:
373: private TracingPropertySource getPropertySource(
374: IPluginModelBase model) {
375: if (model == null)
376: return null;
377: TracingPropertySource source = (TracingPropertySource) fPropertySources
378: .get(model);
379: if (source == null) {
380: String id = model.getPluginBase().getId();
381: Hashtable defaults = PDECore.getDefault()
382: .getTracingOptionsManager().getTemplateTable(id);
383: source = new TracingPropertySource(model, fMasterOptions,
384: defaults, this );
385: fPropertySources.put(model, source);
386: }
387: return source;
388: }
389:
390: private void masterCheckChanged(boolean userChange) {
391: boolean enabled = fTracingCheck.getSelection();
392: fPluginViewer.getTable().setEnabled(enabled);
393: Control currentPage = fPageBook.getCurrentPage();
394: if (currentPage != null && enabled == false) {
395: fPageBook.showEmptyPage();
396: }
397: fSelectAllButton.setEnabled(enabled);
398: fDeselectAllButton.setEnabled(enabled);
399: }
400:
401: private void disposePropertySources() {
402: Enumeration elements = fPropertySources.elements();
403: while (elements.hasMoreElements()) {
404: TracingPropertySource source = (TracingPropertySource) elements
405: .nextElement();
406: fPageBook.removePage(source.getModel());
407: }
408: fPropertySources.clear();
409: }
410:
411: }
|