1 11 package org.eclipse.pde.internal.ui.launcher; 12 import java.util.Arrays ; 13 import java.util.Comparator ; 14 import java.util.Enumeration ; 15 import java.util.Hashtable ; 16 import java.util.Locale ; 17 import java.util.Properties ; 18 import java.util.Vector ; 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 fDescriptors; 39 private Hashtable fTemplate; 40 private Hashtable fValues; 41 private Hashtable fDvalues; 42 private static final String [] fBooleanChoices = {"false", "true"}; private Properties fMasterOptions; 44 private boolean fModified; 45 private TracingBlock fBlock; 46 private abstract class PropertyEditor { 47 private String key; 48 private String label; 49 public PropertyEditor(String key, String label) { 50 this.key = key; 51 this.label = label; 52 } 53 public String getKey() { 54 return key; 55 } 56 public String getLabel() { 57 return label; 58 } 59 abstract void create(Composite parent); 60 abstract void update(); 61 abstract void initialize(); 62 protected void valueModified(Object 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 key, String 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 value = (Integer ) 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 (value)); 90 } 91 }); 92 } 93 } 94 private class TextEditor extends PropertyEditor { 95 private Text text; 96 public TextEditor(String key, String 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, ""); td = new TableWrapData(TableWrapData.FILL_GRAB); 106 text.setLayoutData(td); 108 } 109 public void update() { 110 String value = (String ) 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 masterOptions, Hashtable template, 124 TracingBlock block) { 125 fModel = model; 126 fMasterOptions = masterOptions; 127 fTemplate = template; 128 fBlock = block; 129 fValues = new Hashtable (); 130 fDvalues = new Hashtable (); 131 } 132 public IPluginModelBase getModel() { 133 return fModel; 134 } 135 private Object [] getSortedKeys(int size) { 136 Object [] keyArray = new Object [size]; 137 int i = 0; 138 for (Enumeration keys = fTemplate.keys(); keys.hasMoreElements();) { 139 String key = (String ) keys.nextElement(); 140 keyArray[i++] = key; 141 } 142 Arrays.sort(keyArray, new Comparator () { 143 public int compare(Object o1, Object o2) { 144 return compareKeys(o1, o2); 145 } 146 }); 147 return keyArray; 148 } 149 private int compareKeys(Object o1, Object o2) { 150 String s1 = (String ) o1; 151 String s2 = (String ) o2; 152 return s1.compareTo(s2); 154 } 155 156 public void createContents(Composite parent) { 157 fDescriptors = new Vector (); 158 TableWrapLayout layout = new TableWrapLayout(); 159 layout.numColumns = 2; 160 parent.setLayout(layout); 161 boolean bordersNeeded=false; 162 Object [] sortedKeys = getSortedKeys(fTemplate.size()); 163 for (int i = 0; i < sortedKeys.length; i++) { 164 String key = (String ) sortedKeys[i]; 165 IPath path = new Path(key); 166 path = path.removeFirstSegments(1); 167 String shortKey = path.toString(); 168 String value = (String ) fTemplate.get(key); 169 String lvalue = null; 170 String 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"))) { editor = new BooleanEditor(shortKey, shortKey); 177 Integer dvalue = new Integer (lvalue.equals("true") ? 1 : 0); fDvalues.put(shortKey, dvalue); 179 if (masterValue != null) { 180 Integer mvalue = new Integer (masterValue.equals("true") ? 1 182 : 0); 183 fValues.put(shortKey, mvalue); 184 } 185 } else { 186 editor = new TextEditor(shortKey, shortKey); 187 fDvalues.put(shortKey, value != null ? value : ""); 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 203 public void save() { 204 String pid = fModel.getPluginBase().getId(); 205 for (Enumeration keys = fValues.keys(); keys.hasMoreElements();) { 206 String shortKey = (String ) keys.nextElement(); 207 Object value = fValues.get(shortKey); 208 String svalue = value.toString(); 209 if (value instanceof Integer ) 210 svalue = fBooleanChoices[((Integer ) 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 |