1 11 package org.eclipse.ant.internal.ui.preferences; 12 13 14 import java.io.BufferedReader ; 15 import java.io.IOException ; 16 import java.io.InputStreamReader ; 17 import com.ibm.icu.text.MessageFormat; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import org.eclipse.ant.internal.ui.AntUIPlugin; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.jface.dialogs.DialogPage; 28 import org.eclipse.jface.dialogs.IMessageProvider; 29 import org.eclipse.jface.preference.PreferencePage; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.events.ModifyEvent; 32 import org.eclipse.swt.events.ModifyListener; 33 import org.eclipse.swt.events.SelectionEvent; 34 import org.eclipse.swt.events.SelectionListener; 35 import org.eclipse.swt.graphics.Font; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.widgets.Button; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.Label; 41 import org.eclipse.swt.widgets.Text; 42 import org.eclipse.ui.IWorkbench; 43 import org.eclipse.ui.IWorkbenchPreferencePage; 44 45 public abstract class AbstractAntEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { 46 47 private OverlayPreferenceStore fOverlayStore; 48 protected List fStatusList; 49 private boolean fInitialized= false; 50 51 private Map fCheckBoxes= new HashMap (); 52 private SelectionListener fCheckBoxListener= new SelectionListener() { 53 public void widgetDefaultSelected(SelectionEvent e) { 54 } 55 public void widgetSelected(SelectionEvent e) { 56 Button button= (Button) e.widget; 57 fOverlayStore.setValue((String ) fCheckBoxes.get(button), button.getSelection()); 58 } 59 }; 60 61 private Map fTextFields= new HashMap (); 62 private ModifyListener fTextFieldListener= new ModifyListener() { 63 public void modifyText(ModifyEvent e) { 64 if (fInitialized) { 65 Text text= (Text) e.widget; 66 fOverlayStore.setValue((String ) fTextFields.get(text), text.getText()); 67 } 68 } 69 }; 70 71 private Map fNumberFields= new HashMap (); 72 private ModifyListener fNumberFieldListener= new ModifyListener() { 73 public void modifyText(ModifyEvent e) { 74 if (fInitialized) { 75 numberFieldChanged((Text) e.widget); 76 } 77 } 78 }; 79 80 public AbstractAntEditorPreferencePage() { 81 super(); 82 setPreferenceStore(AntUIPlugin.getDefault().getPreferenceStore()); 83 fOverlayStore= createOverlayStore(); 84 } 85 86 protected abstract OverlayPreferenceStore createOverlayStore(); 87 88 91 public void init(IWorkbench workbench) { 92 } 93 94 protected void initializeFields() { 95 Map checkBoxes= getCheckBoxes(); 96 Map textFields= getTextFields(); 97 Iterator e= checkBoxes.keySet().iterator(); 98 while (e.hasNext()) { 99 Button b= (Button) e.next(); 100 String key= (String ) checkBoxes.get(b); 101 b.setSelection(getOverlayStore().getBoolean(key)); 102 } 103 104 e= textFields.keySet().iterator(); 105 while (e.hasNext()) { 106 Text t= (Text) e.next(); 107 String key= (String ) textFields.get(t); 108 t.setText(getOverlayStore().getString(key)); 109 } 110 fInitialized= true; 111 } 112 113 116 public boolean performOk() { 117 getOverlayStore().propagate(); 118 AntUIPlugin.getDefault().savePluginPreferences(); 119 return true; 120 } 121 122 protected OverlayPreferenceStore getOverlayStore() { 123 return fOverlayStore; 124 } 125 126 protected OverlayPreferenceStore setOverlayStore() { 127 return fOverlayStore; 128 } 129 130 protected Map getCheckBoxes() { 131 return fCheckBoxes; 132 } 133 134 protected Map getTextFields() { 135 return fTextFields; 136 } 137 138 protected Map getNumberFields() { 139 return fNumberFields; 140 } 141 142 145 protected void performDefaults() { 146 getOverlayStore().loadDefaults(); 147 initializeFields(); 148 handleDefaults(); 149 super.performDefaults(); 150 } 151 152 protected abstract void handleDefaults(); 153 154 157 public void dispose() { 158 if (getOverlayStore() != null) { 159 getOverlayStore().stop(); 160 fOverlayStore= null; 161 } 162 super.dispose(); 163 } 164 165 protected Button addCheckBox(Composite parent, String labelText, String key, int indentation) { 166 Button checkBox= new Button(parent, SWT.CHECK); 167 checkBox.setText(labelText); 168 checkBox.setFont(parent.getFont()); 169 170 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 171 gd.horizontalIndent= indentation; 172 gd.horizontalSpan= 2; 173 checkBox.setLayoutData(gd); 174 checkBox.addSelectionListener(fCheckBoxListener); 175 176 getCheckBoxes().put(checkBox, key); 177 178 return checkBox; 179 } 180 181 protected Text addTextField(Composite composite, String labelText, String key, int textLimit, int indentation, String [] errorMessages) { 182 Font font= composite.getFont(); 183 184 Label label= new Label(composite, SWT.NONE); 185 label.setText(labelText); 186 label.setFont(font); 187 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 188 gd.horizontalIndent= indentation; 189 label.setLayoutData(gd); 190 191 Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE); 192 textControl.setFont(font); 193 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 194 gd.widthHint= convertWidthInCharsToPixels(textLimit + 1); 195 textControl.setLayoutData(gd); 196 textControl.setTextLimit(textLimit); 197 getTextFields().put(textControl, key); 198 if (errorMessages != null) { 199 getNumberFields().put(textControl, errorMessages); 200 textControl.addModifyListener(fNumberFieldListener); 201 } else { 202 textControl.addModifyListener(fTextFieldListener); 203 } 204 205 return textControl; 206 } 207 208 private void numberFieldChanged(Text textControl) { 209 String number= textControl.getText(); 210 IStatus status= validatePositiveNumber(number, (String [])getNumberFields().get(textControl)); 211 if (!status.matches(IStatus.ERROR)) { 212 getOverlayStore().setValue((String ) getTextFields().get(textControl), number); 213 } 214 updateStatus(status); 215 } 216 217 private IStatus validatePositiveNumber(String number, String [] errorMessages) { 218 StatusInfo status= new StatusInfo(); 219 if (number.length() == 0) { 220 status.setError(errorMessages[0]); 221 } else { 222 try { 223 int value= Integer.parseInt(number); 224 if (value < 0) 225 status.setError(MessageFormat.format(errorMessages[1], new String []{number})); 226 } catch (NumberFormatException e) { 227 status.setError(MessageFormat.format(errorMessages[1], new String []{number})); 228 } 229 } 230 return status; 231 } 232 233 protected void updateStatus(IStatus status) { 234 if (!status.matches(IStatus.ERROR)) { 235 Set keys= getNumberFields().keySet(); 236 for (Iterator iter = keys.iterator(); iter.hasNext();) { 237 Text text = (Text) iter.next(); 238 IStatus s= validatePositiveNumber(text.getText(), (String [])getNumberFields().get(text)); 239 status= s.getSeverity() > status.getSeverity() ? s : status; 240 } 241 } 242 243 List statusList= getStatusList(); 244 if (statusList != null) { 245 List temp= new ArrayList (statusList.size() + 1); 246 temp.add(status); 247 temp.addAll(statusList); 248 status= getMostSevere(temp); 249 } 250 setValid(!status.matches(IStatus.ERROR)); 251 applyToStatusLine(this, status); 252 } 253 254 protected List getStatusList() { 255 return fStatusList; 256 } 257 258 263 private IStatus getMostSevere(List statusList) { 264 IStatus max= null; 265 for (int i= 0; i < statusList.size(); i++) { 266 IStatus curr= (IStatus)statusList.get(i); 267 if (curr.matches(IStatus.ERROR)) { 268 return curr; 269 } 270 if (max == null || curr.getSeverity() > max.getSeverity()) { 271 max= curr; 272 } 273 } 274 return max; 275 } 276 277 280 private void applyToStatusLine(DialogPage page, IStatus status) { 281 String message= status.getMessage(); 282 switch (status.getSeverity()) { 283 case IStatus.OK: 284 page.setMessage(message, IMessageProvider.NONE); 285 page.setErrorMessage(null); 286 break; 287 case IStatus.WARNING: 288 page.setMessage(message, IMessageProvider.WARNING); 289 page.setErrorMessage(null); 290 break; 291 case IStatus.INFO: 292 page.setMessage(message, IMessageProvider.INFORMATION); 293 page.setErrorMessage(null); 294 break; 295 default: 296 if (message.length() == 0) { 297 message= null; 298 } 299 page.setMessage(null); 300 page.setErrorMessage(message); 301 break; 302 } 303 } 304 305 311 protected Control[] addLabelledTextField(Composite composite, String label, String key, int textLimit, int indentation, String [] errorMessages) { 312 Label labelControl= new Label(composite, SWT.NONE); 313 labelControl.setText(label); 314 labelControl.setFont(composite.getFont()); 315 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 316 gd.horizontalIndent= indentation; 317 labelControl.setLayoutData(gd); 318 319 Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE); 320 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 321 if (textLimit > -1) { 322 gd.widthHint= convertWidthInCharsToPixels(textLimit + 1); 323 textControl.setTextLimit(textLimit); 324 } else { 325 gd.widthHint= convertWidthInCharsToPixels(50); 326 } 327 textControl.setLayoutData(gd); 328 textControl.setFont(composite.getFont()); 329 fTextFields.put(textControl, key); 330 if (errorMessages != null) { 331 fNumberFields.put(textControl, errorMessages); 332 textControl.addModifyListener(fNumberFieldListener); 333 } else { 334 textControl.addModifyListener(fTextFieldListener); 335 } 336 337 return new Control[]{labelControl, textControl}; 338 } 339 340 protected String loadPreviewContentFromFile(String filename) { 341 String line; 342 String separator= System.getProperty("line.separator"); StringBuffer buffer= new StringBuffer (512); 344 BufferedReader reader= null; 345 try { 346 reader= new BufferedReader (new InputStreamReader (getClass().getResourceAsStream(filename))); 347 while ((line= reader.readLine()) != null) { 348 buffer.append(line); 349 buffer.append(separator); 350 } 351 } catch (IOException io) { 352 AntUIPlugin.log(io); 353 } finally { 354 if (reader != null) { 355 try { reader.close(); } catch (IOException e) {} 356 } 357 } 358 return buffer.toString(); 359 } 360 361 protected Label getLabelControl(Control[] labelledTextField) { 362 return (Label)labelledTextField[0]; 363 } 364 365 protected Text getTextControl(Control[] labelledTextField) { 366 return (Text)labelledTextField[1]; 367 } 368 } 369 | Popular Tags |