KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Properties JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
23 import org.eclipse.jface.viewers.ArrayContentProvider;
24 import org.eclipse.jface.viewers.CheckStateChangedEvent;
25 import org.eclipse.jface.viewers.CheckboxTableViewer;
26 import org.eclipse.jface.viewers.ICheckStateListener;
27 import org.eclipse.jface.viewers.ISelectionChangedListener;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.pde.core.plugin.IPluginModelBase;
32 import org.eclipse.pde.core.plugin.PluginRegistry;
33 import org.eclipse.pde.internal.core.PDECore;
34 import org.eclipse.pde.internal.core.TracingOptionsManager;
35 import org.eclipse.pde.internal.ui.PDEPlugin;
36 import org.eclipse.pde.internal.ui.PDEUIMessages;
37 import org.eclipse.pde.internal.ui.util.SWTUtil;
38 import org.eclipse.pde.internal.ui.wizards.ListUtil;
39 import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
40 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
41 import org.eclipse.pde.ui.launcher.TracingTab;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.custom.SashForm;
44 import org.eclipse.swt.events.SelectionAdapter;
45 import org.eclipse.swt.events.SelectionEvent;
46 import org.eclipse.swt.layout.FillLayout;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Button;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Control;
52 import org.eclipse.ui.forms.widgets.FormToolkit;
53 import org.eclipse.ui.forms.widgets.ScrolledPageBook;
54
55 public class TracingBlock {
56
57     private TracingTab fTab;
58     private Button fTracingCheck;
59     private CheckboxTableViewer fPluginViewer;
60     private IPluginModelBase[] fTraceableModels;
61     private Properties JavaDoc fMasterOptions = new Properties JavaDoc();
62     private Button fSelectAllButton;
63     private Button fDeselectAllButton;
64     private Hashtable JavaDoc fPropertySources = new Hashtable JavaDoc();
65     private FormToolkit fToolkit;
66     private ScrolledPageBook fPageBook;
67
68     public TracingBlock(TracingTab tab) {
69         fTab = tab;
70     }
71     
72     public AbstractLauncherTab getTab() {
73         return fTab;
74     }
75     
76     public void createControl(Composite parent) {
77         fTracingCheck = new Button(parent, SWT.CHECK);
78         fTracingCheck.setText(PDEUIMessages.TracingLauncherTab_tracing);
79         fTracingCheck.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
80         fTracingCheck.addSelectionListener(new SelectionAdapter() {
81             public void widgetSelected(SelectionEvent e) {
82                 masterCheckChanged(true);
83                 fTab.updateLaunchConfigurationDialog();
84             }
85         });
86
87         createSashSection(parent);
88         createButtonSection(parent);
89     }
90     
91     private void createSashSection(Composite container) {
92         SashForm sashForm = new SashForm(container, SWT.HORIZONTAL);
93         sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
94         createPluginViewer(sashForm);
95         createPropertySheetClient(sashForm);
96     }
97     
98     private void createPluginViewer(Composite sashForm) {
99         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, SWT.BORDER);
105         fPluginViewer.setContentProvider(new ArrayContentProvider());
106         fPluginViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
107         fPluginViewer.setComparator(new ListUtil.PluginComparator());
108         fPluginViewer.addSelectionChangedListener(new ISelectionChangedListener() {
109             public void selectionChanged(SelectionChangedEvent e) {
110                 CheckboxTableViewer tableViewer = (CheckboxTableViewer) e.getSource();
111                 boolean selected = tableViewer.getChecked(getSelectedModel());
112                 pluginSelected(getSelectedModel(), selected);
113             }
114         });
115         fPluginViewer.addCheckStateListener(new ICheckStateListener() {
116             public void checkStateChanged(CheckStateChangedEvent event) {
117                 CheckboxTableViewer tableViewer = (CheckboxTableViewer) event.getSource();
118                 tableViewer.setSelection(new StructuredSelection(event.getElement()));
119                 pluginSelected(getSelectedModel(), event.getChecked());
120                 fTab.updateLaunchConfigurationDialog();
121             }
122         });
123         GridData gd = new GridData(GridData.FILL_BOTH);
124         gd.widthHint = 125;
125         gd.heightHint = 100;
126         fPluginViewer.getTable().setLayoutData(gd);
127         fPluginViewer.setInput(getTraceableModels());
128     }
129     
130     private void createPropertySheetClient(Composite sashForm) {
131         Composite tableChild = new Composite(sashForm, SWT.NULL);
132         GridLayout layout = new GridLayout();
133         layout.marginHeight = layout.marginWidth = 0;
134         tableChild.setLayout(layout);
135         int margin = createPropertySheet(tableChild);
136         layout.marginWidth = layout.marginHeight = margin;
137     }
138     
139     private void createButtonSection(Composite parent) {
140         Composite container = new Composite(parent, SWT.NONE);
141         GridLayout layout = new GridLayout();
142         layout.numColumns = 2;
143         container.setLayout(layout);
144         
145         fSelectAllButton = new Button(container, SWT.PUSH);
146         fSelectAllButton.setText(PDEUIMessages.TracingLauncherTab_selectAll);
147         fSelectAllButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
148         SWTUtil.setButtonDimensionHint(fSelectAllButton);
149         fSelectAllButton.addSelectionListener(new SelectionAdapter() {
150             public void widgetSelected(SelectionEvent e) {
151                 fPluginViewer.setAllChecked(true);
152                 fTab.updateLaunchConfigurationDialog();
153             }
154         });
155         
156         fDeselectAllButton = new Button(container, SWT.PUSH);
157         fDeselectAllButton.setText(PDEUIMessages.TracinglauncherTab_deselectAll);
158         fDeselectAllButton.setLayoutData(new GridData(
159                 GridData.HORIZONTAL_ALIGN_BEGINNING));
160         SWTUtil.setButtonDimensionHint(fDeselectAllButton);
161         fDeselectAllButton.addSelectionListener(new SelectionAdapter() {
162             public void widgetSelected(SelectionEvent e) {
163                 fPluginViewer.setAllChecked(false);
164                 fTab.updateLaunchConfigurationDialog();
165             }
166         });
167     }
168     
169     protected int createPropertySheet(Composite parent) {
170         fToolkit = new FormToolkit(parent.getDisplay());
171         int toolkitBorderStyle = fToolkit.getBorderStyle();
172         int style = toolkitBorderStyle == SWT.BORDER ? SWT.NULL : SWT.BORDER;
173         
174         Composite container = new Composite(parent, style);
175         FillLayout flayout = new FillLayout();
176         flayout.marginWidth = 1;
177         flayout.marginHeight = 1;
178         container.setLayout(flayout);
179         container.setLayoutData(new GridData(GridData.FILL_BOTH));
180
181         fPageBook = new ScrolledPageBook(container, style | SWT.V_SCROLL | SWT.H_SCROLL);
182         fToolkit.adapt(fPageBook, false, false);
183         
184         if (style == SWT.NULL) {
185             fPageBook.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER);
186             fToolkit.paintBordersFor(container);
187         }
188         return style == SWT.NULL ? 2 : 0;
189     }
190     
191     public void initializeFrom(ILaunchConfiguration config) {
192         fMasterOptions.clear();
193         disposePropertySources();
194         try {
195             fTracingCheck.setSelection(config.getAttribute(IPDELauncherConstants.TRACING, false));
196             Map JavaDoc options = config.getAttribute(IPDELauncherConstants.TRACING_OPTIONS, (Map JavaDoc) null);
197             if (options == null)
198                 options = PDECore.getDefault().getTracingOptionsManager().getTracingTemplateCopy();
199             else
200                 options = PDECore.getDefault().getTracingOptionsManager().getTracingOptions(options);
201             fMasterOptions.putAll(options);
202             masterCheckChanged(false);
203             IPluginModelBase model = getLastSelectedPlugin(config);
204             if (model != null) {
205                 fPluginViewer.setSelection(new StructuredSelection(model));
206             } else {
207                 pluginSelected(null, false);
208             }
209             String JavaDoc checked = config.getAttribute(IPDELauncherConstants.TRACING_CHECKED, (String JavaDoc) null);
210             if (checked == null) {
211                 fPluginViewer.setAllChecked(true);
212             } else if (checked.equals(IPDELauncherConstants.TRACING_NONE)) {
213                 fPluginViewer.setAllChecked(false);
214             } else {
215                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(checked, ","); //$NON-NLS-1$
216
ArrayList JavaDoc list = new ArrayList JavaDoc();
217                 while (tokenizer.hasMoreTokens()) {
218                     String JavaDoc id = tokenizer.nextToken();
219                     model = PluginRegistry.findModel(id);
220                     if (model != null) {
221                         list.add(model);
222                     }
223                 }
224                 fPluginViewer.setCheckedElements(list.toArray());
225             }
226         } catch (CoreException e) {
227             PDEPlugin.logException(e);
228         }
229     }
230     
231     public void performApply(ILaunchConfigurationWorkingCopy config) {
232         boolean tracingEnabled = fTracingCheck.getSelection();
233         config.setAttribute(IPDELauncherConstants.TRACING, tracingEnabled);
234         if (tracingEnabled) {
235             IPluginModelBase model = getSelectedModel();
236             String JavaDoc id = (model == null) ? null : model.getPluginBase().getId();
237             config.setAttribute(IPDELauncherConstants.TRACING_SELECTED_PLUGIN, id);
238             boolean changes = false;
239             for (Enumeration JavaDoc elements = fPropertySources.elements(); elements
240                     .hasMoreElements();) {
241                 TracingPropertySource source = (TracingPropertySource) elements
242                         .nextElement();
243                 if (source.isModified()) {
244                     changes = true;
245                     source.save();
246                 }
247             }
248             if (changes)
249                 config.setAttribute(IPDELauncherConstants.TRACING_OPTIONS, fMasterOptions);
250         } else {
251             config.setAttribute(IPDELauncherConstants.TRACING_SELECTED_PLUGIN, (String JavaDoc) null);
252         }
253         Object JavaDoc[] checked = fPluginViewer.getCheckedElements();
254         if (checked.length == fPluginViewer.getTable().getItemCount()) {
255             config.setAttribute(IPDELauncherConstants.TRACING_CHECKED, (String JavaDoc) null);
256         } else if (checked.length == 0) {
257             config.setAttribute(IPDELauncherConstants.TRACING_CHECKED, IPDELauncherConstants.TRACING_NONE);
258         } else {
259             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
260             for (int i = 0; i < checked.length; i++) {
261                 IPluginModelBase model = (IPluginModelBase) checked[i];
262                 buffer.append(model.getPluginBase().getId());
263                 if (i < checked.length - 1)
264                     buffer.append(',');
265             }
266             config.setAttribute(IPDELauncherConstants.TRACING_CHECKED, buffer.toString());
267         }
268     }
269     
270     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
271         configuration.setAttribute(IPDELauncherConstants.TRACING, false);
272         configuration.setAttribute(IPDELauncherConstants.TRACING_CHECKED, IPDELauncherConstants.TRACING_NONE);
273     }
274     
275     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
276         fPageBook.getParent().getParent().layout(true);
277     }
278     
279     public void dispose() {
280         if (fToolkit != null)
281             fToolkit.dispose();
282     }
283     
284     public FormToolkit getToolkit() {
285         return fToolkit;
286     }
287     
288     private IPluginModelBase getSelectedModel() {
289         if (fTracingCheck.isEnabled()) {
290             Object JavaDoc item = ((IStructuredSelection) fPluginViewer.getSelection()).getFirstElement();
291             if (item instanceof IPluginModelBase)
292                 return ((IPluginModelBase) item);
293         }
294         return null;
295     }
296     
297     private void pluginSelected(IPluginModelBase model, boolean checked) {
298         TracingPropertySource source = getPropertySource(model);
299         if (source == null || checked == false) {
300             fPageBook.showEmptyPage();
301         } else {
302             if (!fPageBook.hasPage(model)) {
303                 Composite parent = fPageBook.createPage(model);
304                 source.createContents(parent);
305             }
306             fPageBook.showPage(model);
307         }
308     }
309
310     private IPluginModelBase[] getTraceableModels() {
311         if (fTraceableModels == null) {
312             IPluginModelBase[] models = PluginRegistry.getActiveModels();
313             ArrayList JavaDoc result = new ArrayList JavaDoc();
314             for (int i = 0; i < models.length; i++) {
315                 if (TracingOptionsManager.isTraceable(models[i]))
316                     result.add(models[i]);
317             }
318             fTraceableModels = (IPluginModelBase[]) result.toArray(new IPluginModelBase[result.size()]);
319         }
320         return fTraceableModels;
321     }
322     
323     private IPluginModelBase getLastSelectedPlugin(ILaunchConfiguration config)
324             throws CoreException {
325         String JavaDoc pluginID = config.getAttribute(IPDELauncherConstants.TRACING_SELECTED_PLUGIN, (String JavaDoc) null);
326         return pluginID == null ? null : PluginRegistry.findModel(pluginID);
327     }
328
329     private TracingPropertySource getPropertySource(IPluginModelBase model) {
330         if (model == null)
331             return null;
332         TracingPropertySource source = (TracingPropertySource) fPropertySources.get(model);
333         if (source == null) {
334             String JavaDoc id = model.getPluginBase().getId();
335             Hashtable JavaDoc defaults = PDECore.getDefault().getTracingOptionsManager().getTemplateTable(id);
336             source = new TracingPropertySource(model, fMasterOptions, defaults, this);
337             fPropertySources.put(model, source);
338         }
339         return source;
340     }
341     
342     private void masterCheckChanged(boolean userChange) {
343         boolean enabled = fTracingCheck.getSelection();
344         fPluginViewer.getTable().setEnabled(enabled);
345         Control currentPage = fPageBook.getCurrentPage();
346         if (currentPage != null && enabled == false) {
347             fPageBook.showEmptyPage();
348         }
349         fSelectAllButton.setEnabled(enabled);
350         fDeselectAllButton.setEnabled(enabled);
351     }
352
353     private void disposePropertySources() {
354         Enumeration JavaDoc elements = fPropertySources.elements();
355         while (elements.hasMoreElements()) {
356             TracingPropertySource source = (TracingPropertySource) elements.nextElement();
357             fPageBook.removePage(source.getModel());
358         }
359         fPropertySources.clear();
360     }
361
362 }
363
Popular Tags