KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 import java.util.Arrays JavaDoc;
13 import java.util.Comparator JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.Properties JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.pde.core.plugin.IPluginModelBase;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Text;
32 import org.eclipse.ui.forms.widgets.TableWrapData;
33 import org.eclipse.ui.forms.widgets.TableWrapLayout;
34
35
36 public class TracingPropertySource {
37     private IPluginModelBase fModel;
38     private Vector JavaDoc fDescriptors;
39     private Hashtable JavaDoc fTemplate;
40     private Hashtable JavaDoc fValues;
41     private Hashtable JavaDoc fDvalues;
42     private static final String JavaDoc[] fBooleanChoices = {"false", "true"}; //$NON-NLS-1$ //$NON-NLS-2$
43
private Properties JavaDoc fMasterOptions;
44     private boolean fModified;
45     private TracingBlock fBlock;
46     private abstract class PropertyEditor {
47         private String JavaDoc key;
48         private String JavaDoc label;
49         public PropertyEditor(String JavaDoc key, String JavaDoc label) {
50             this.key = key;
51             this.label = label;
52         }
53         public String JavaDoc getKey() {
54             return key;
55         }
56         public String JavaDoc getLabel() {
57             return label;
58         }
59         abstract void create(Composite parent);
60         abstract void update();
61         abstract void initialize();
62         protected void valueModified(Object JavaDoc value) {
63             fValues.put(getKey(), value);
64             fModified = true;
65             fBlock.getTab().updateLaunchConfigurationDialog();
66         }
67     }
68     private class BooleanEditor extends PropertyEditor {
69         private Button checkbox;
70         public BooleanEditor(String JavaDoc key, String JavaDoc label) {
71             super(key, label);
72         }
73         public void create(Composite parent) {
74             checkbox = fBlock.getToolkit().createButton(parent, getLabel(),
75                     SWT.CHECK);
76             TableWrapData td = new TableWrapData();
77             td.colspan = 2;
78             checkbox.setLayoutData(td);
79         }
80         public void update() {
81             Integer JavaDoc value = (Integer JavaDoc) fValues.get(getKey());
82             checkbox.setSelection(value.intValue() == 1);
83         }
84         public void initialize() {
85             update();
86             checkbox.addSelectionListener(new SelectionAdapter() {
87                 public void widgetSelected(SelectionEvent e) {
88                     int value = checkbox.getSelection() ? 1 : 0;
89                     valueModified(new Integer JavaDoc(value));
90                 }
91             });
92         }
93     }
94     private class TextEditor extends PropertyEditor {
95         private Text text;
96         public TextEditor(String JavaDoc key, String JavaDoc label) {
97             super(key, label);
98         }
99         public void create(Composite parent) {
100             Label label = fBlock.getToolkit().createLabel(parent, getLabel());
101             TableWrapData td = new TableWrapData();
102             td.valign = TableWrapData.MIDDLE;
103             label.setLayoutData(td);
104             text = fBlock.getToolkit().createText(parent, ""); //$NON-NLS-1$
105
td = new TableWrapData(TableWrapData.FILL_GRAB);
106             //gd.widthHint = 100;
107
text.setLayoutData(td);
108         }
109         public void update() {
110             String JavaDoc value = (String JavaDoc) fValues.get(getKey());
111             text.setText(value);
112         }
113         public void initialize() {
114             update();
115             text.addModifyListener(new ModifyListener() {
116                 public void modifyText(ModifyEvent e) {
117                     valueModified(text.getText());
118                 }
119             });
120         }
121     }
122     public TracingPropertySource(IPluginModelBase model,
123             Properties JavaDoc masterOptions, Hashtable JavaDoc template,
124             TracingBlock block) {
125         fModel = model;
126         fMasterOptions = masterOptions;
127         fTemplate = template;
128         fBlock = block;
129         fValues = new Hashtable JavaDoc();
130         fDvalues = new Hashtable JavaDoc();
131     }
132     public IPluginModelBase getModel() {
133         return fModel;
134     }
135     private Object JavaDoc[] getSortedKeys(int size) {
136         Object JavaDoc[] keyArray = new Object JavaDoc[size];
137         int i = 0;
138         for (Enumeration JavaDoc keys = fTemplate.keys(); keys.hasMoreElements();) {
139             String JavaDoc key = (String JavaDoc) keys.nextElement();
140             keyArray[i++] = key;
141         }
142         Arrays.sort(keyArray, new Comparator JavaDoc() {
143             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
144                 return compareKeys(o1, o2);
145             }
146         });
147         return keyArray;
148     }
149     private int compareKeys(Object JavaDoc o1, Object JavaDoc o2) {
150         String JavaDoc s1 = (String JavaDoc) o1;
151         String JavaDoc s2 = (String JavaDoc) o2;
152         // equal
153
return s1.compareTo(s2);
154     }
155
156     public void createContents(Composite parent) {
157         fDescriptors = new Vector JavaDoc();
158         TableWrapLayout layout = new TableWrapLayout();
159         layout.numColumns = 2;
160         parent.setLayout(layout);
161         boolean bordersNeeded=false;
162         Object JavaDoc[] sortedKeys = getSortedKeys(fTemplate.size());
163         for (int i = 0; i < sortedKeys.length; i++) {
164             String JavaDoc key = (String JavaDoc) sortedKeys[i];
165             IPath path = new Path(key);
166             path = path.removeFirstSegments(1);
167             String JavaDoc shortKey = path.toString();
168             String JavaDoc value = (String JavaDoc) fTemplate.get(key);
169             String JavaDoc lvalue = null;
170             String JavaDoc masterValue = fMasterOptions.getProperty(key);
171             PropertyEditor editor;
172             if (value != null)
173                 lvalue = value.toLowerCase(Locale.ENGLISH);
174             if (lvalue != null
175                     && (lvalue.equals("true") || lvalue.equals("false"))) { //$NON-NLS-1$ //$NON-NLS-2$
176
editor = new BooleanEditor(shortKey, shortKey);
177                 Integer JavaDoc dvalue = new Integer JavaDoc(lvalue.equals("true") ? 1 : 0); //$NON-NLS-1$
178
fDvalues.put(shortKey, dvalue);
179                 if (masterValue != null) {
180                     Integer JavaDoc mvalue = new Integer JavaDoc(masterValue.equals("true") //$NON-NLS-1$
181
? 1
182                             : 0);
183                     fValues.put(shortKey, mvalue);
184                 }
185             } else {
186                 editor = new TextEditor(shortKey, shortKey);
187                 fDvalues.put(shortKey, value != null ? value : ""); //$NON-NLS-1$
188
if (masterValue != null) {
189                     fValues.put(shortKey, masterValue);
190                 }
191                 bordersNeeded=true;
192             }
193             editor.create(parent);
194             editor.initialize();
195             fDescriptors.add(editor);
196             if (bordersNeeded)
197                 fBlock.getToolkit().paintBordersFor(parent);
198         }
199     }
200
201     /**
202      */

203     public void save() {
204         String JavaDoc pid = fModel.getPluginBase().getId();
205         for (Enumeration JavaDoc keys = fValues.keys(); keys.hasMoreElements();) {
206             String JavaDoc shortKey = (String JavaDoc) keys.nextElement();
207             Object JavaDoc value = fValues.get(shortKey);
208             String JavaDoc svalue = value.toString();
209             if (value instanceof Integer JavaDoc)
210                 svalue = fBooleanChoices[((Integer JavaDoc) value).intValue()];
211             IPath path = new Path(pid).append(shortKey);
212             fMasterOptions.setProperty(path.toString(), svalue);
213         }
214         fModified = false;
215     }
216     public void dispose() {
217     }
218     public boolean isModified() {
219         return fModified;
220     }
221 }
222
Popular Tags