001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.Arrays;
013: import java.util.Comparator;
014: import java.util.Enumeration;
015: import java.util.Hashtable;
016: import java.util.Locale;
017: import java.util.Properties;
018: import java.util.Vector;
019:
020: import org.eclipse.core.runtime.IPath;
021: import org.eclipse.core.runtime.Path;
022: import org.eclipse.pde.core.plugin.IPluginModelBase;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.events.ModifyEvent;
025: import org.eclipse.swt.events.ModifyListener;
026: import org.eclipse.swt.events.SelectionAdapter;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.widgets.Button;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Label;
031: import org.eclipse.swt.widgets.Text;
032: import org.eclipse.ui.forms.widgets.TableWrapData;
033: import org.eclipse.ui.forms.widgets.TableWrapLayout;
034:
035: public class TracingPropertySource {
036: private IPluginModelBase fModel;
037: private Vector fDescriptors;
038: private Hashtable fTemplate;
039: private Hashtable fValues;
040: private Hashtable fDvalues;
041: private static final String[] fBooleanChoices = { "false", "true" }; //$NON-NLS-1$ //$NON-NLS-2$
042: private Properties fMasterOptions;
043: private boolean fModified;
044: private TracingBlock fBlock;
045:
046: private abstract class PropertyEditor {
047: private String key;
048: private String label;
049:
050: public PropertyEditor(String key, String label) {
051: this .key = key;
052: this .label = label;
053: }
054:
055: public String getKey() {
056: return key;
057: }
058:
059: public String getLabel() {
060: return label;
061: }
062:
063: abstract void create(Composite parent);
064:
065: abstract void update();
066:
067: abstract void initialize();
068:
069: protected void valueModified(Object value) {
070: fValues.put(getKey(), value);
071: fModified = true;
072: fBlock.getTab().updateLaunchConfigurationDialog();
073: }
074: }
075:
076: private class BooleanEditor extends PropertyEditor {
077: private Button checkbox;
078:
079: public BooleanEditor(String key, String label) {
080: super (key, label);
081: }
082:
083: public void create(Composite parent) {
084: checkbox = fBlock.getToolkit().createButton(parent,
085: getLabel(), SWT.CHECK);
086: TableWrapData td = new TableWrapData();
087: td.colspan = 2;
088: checkbox.setLayoutData(td);
089: }
090:
091: public void update() {
092: Integer value = (Integer) fValues.get(getKey());
093: checkbox.setSelection(value.intValue() == 1);
094: }
095:
096: public void initialize() {
097: update();
098: checkbox.addSelectionListener(new SelectionAdapter() {
099: public void widgetSelected(SelectionEvent e) {
100: int value = checkbox.getSelection() ? 1 : 0;
101: valueModified(new Integer(value));
102: }
103: });
104: }
105: }
106:
107: private class TextEditor extends PropertyEditor {
108: private Text text;
109:
110: public TextEditor(String key, String label) {
111: super (key, label);
112: }
113:
114: public void create(Composite parent) {
115: Label label = fBlock.getToolkit().createLabel(parent,
116: getLabel());
117: TableWrapData td = new TableWrapData();
118: td.valign = TableWrapData.MIDDLE;
119: label.setLayoutData(td);
120: text = fBlock.getToolkit().createText(parent, ""); //$NON-NLS-1$
121: td = new TableWrapData(TableWrapData.FILL_GRAB);
122: //gd.widthHint = 100;
123: text.setLayoutData(td);
124: }
125:
126: public void update() {
127: String value = (String) fValues.get(getKey());
128: text.setText(value);
129: }
130:
131: public void initialize() {
132: update();
133: text.addModifyListener(new ModifyListener() {
134: public void modifyText(ModifyEvent e) {
135: valueModified(text.getText());
136: }
137: });
138: }
139: }
140:
141: public TracingPropertySource(IPluginModelBase model,
142: Properties masterOptions, Hashtable template,
143: TracingBlock block) {
144: fModel = model;
145: fMasterOptions = masterOptions;
146: fTemplate = template;
147: fBlock = block;
148: fValues = new Hashtable();
149: fDvalues = new Hashtable();
150: }
151:
152: public IPluginModelBase getModel() {
153: return fModel;
154: }
155:
156: private Object[] getSortedKeys(int size) {
157: Object[] keyArray = new Object[size];
158: int i = 0;
159: for (Enumeration keys = fTemplate.keys(); keys
160: .hasMoreElements();) {
161: String key = (String) keys.nextElement();
162: keyArray[i++] = key;
163: }
164: Arrays.sort(keyArray, new Comparator() {
165: public int compare(Object o1, Object o2) {
166: return compareKeys(o1, o2);
167: }
168: });
169: return keyArray;
170: }
171:
172: private int compareKeys(Object o1, Object o2) {
173: String s1 = (String) o1;
174: String s2 = (String) o2;
175: // equal
176: return s1.compareTo(s2);
177: }
178:
179: public void createContents(Composite parent) {
180: fDescriptors = new Vector();
181: TableWrapLayout layout = new TableWrapLayout();
182: layout.numColumns = 2;
183: parent.setLayout(layout);
184: boolean bordersNeeded = false;
185: Object[] sortedKeys = getSortedKeys(fTemplate.size());
186: for (int i = 0; i < sortedKeys.length; i++) {
187: String key = (String) sortedKeys[i];
188: IPath path = new Path(key);
189: path = path.removeFirstSegments(1);
190: String shortKey = path.toString();
191: String value = (String) fTemplate.get(key);
192: String lvalue = null;
193: String masterValue = fMasterOptions.getProperty(key);
194: PropertyEditor editor;
195: if (value != null)
196: lvalue = value.toLowerCase(Locale.ENGLISH);
197: if (lvalue != null
198: && (lvalue.equals("true") || lvalue.equals("false"))) { //$NON-NLS-1$ //$NON-NLS-2$
199: editor = new BooleanEditor(shortKey, shortKey);
200: Integer dvalue = new Integer(
201: lvalue.equals("true") ? 1 : 0); //$NON-NLS-1$
202: fDvalues.put(shortKey, dvalue);
203: if (masterValue != null) {
204: Integer mvalue = new Integer(masterValue
205: .equals("true") //$NON-NLS-1$
206: ? 1
207: : 0);
208: fValues.put(shortKey, mvalue);
209: }
210: } else {
211: editor = new TextEditor(shortKey, shortKey);
212: fDvalues.put(shortKey, value != null ? value : ""); //$NON-NLS-1$
213: if (masterValue != null) {
214: fValues.put(shortKey, masterValue);
215: }
216: bordersNeeded = true;
217: }
218: editor.create(parent);
219: editor.initialize();
220: fDescriptors.add(editor);
221: if (bordersNeeded)
222: fBlock.getToolkit().paintBordersFor(parent);
223: }
224: }
225:
226: /**
227: */
228: public void save() {
229: String pid = fModel.getPluginBase().getId();
230: for (Enumeration keys = fValues.keys(); keys.hasMoreElements();) {
231: String shortKey = (String) keys.nextElement();
232: Object value = fValues.get(shortKey);
233: String svalue = value.toString();
234: if (value instanceof Integer)
235: svalue = fBooleanChoices[((Integer) value).intValue()];
236: IPath path = new Path(pid).append(shortKey);
237: fMasterOptions.setProperty(path.toString(), svalue);
238: }
239: fModified = false;
240: }
241:
242: public void dispose() {
243: }
244:
245: public boolean isModified() {
246: return fModified;
247: }
248: }
|