1 12 package org.eclipse.ui.internal.editors.text; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.ModifyEvent; 23 import org.eclipse.swt.events.ModifyListener; 24 import org.eclipse.swt.events.SelectionAdapter; 25 import org.eclipse.swt.events.SelectionEvent; 26 import org.eclipse.swt.events.SelectionListener; 27 import org.eclipse.swt.graphics.RGB; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.layout.GridLayout; 30 import org.eclipse.swt.widgets.Button; 31 import org.eclipse.swt.widgets.Combo; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.swt.widgets.Label; 35 import org.eclipse.swt.widgets.Link; 36 import org.eclipse.swt.widgets.List; 37 import org.eclipse.swt.widgets.Spinner; 38 import org.eclipse.swt.widgets.Text; 39 40 import org.eclipse.core.runtime.Assert; 41 import org.eclipse.core.runtime.IStatus; 42 43 import org.eclipse.jface.dialogs.Dialog; 44 import org.eclipse.jface.dialogs.DialogPage; 45 import org.eclipse.jface.dialogs.IMessageProvider; 46 import org.eclipse.jface.preference.ColorSelector; 47 import org.eclipse.jface.preference.PreferenceConverter; 48 import org.eclipse.jface.preference.PreferencePage; 49 50 import org.eclipse.ui.editors.text.ITextEditorHelpContextIds; 51 52 import org.eclipse.ui.IWorkbench; 53 import org.eclipse.ui.IWorkbenchPreferencePage; 54 import org.eclipse.ui.PlatformUI; 55 import org.eclipse.ui.dialogs.PreferencesUtil; 56 import org.eclipse.ui.internal.editors.text.TextEditorDefaultsPreferencePage.EnumeratedDomain.EnumValue; 57 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; 58 import org.eclipse.ui.texteditor.AbstractTextEditor; 59 60 61 68 public class TextEditorDefaultsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { 69 70 private static abstract class Initializer { 71 72 protected final Preference fPreference; 73 74 protected Initializer(Preference preference) { 75 fPreference= preference; 76 } 77 78 public abstract void initialize(); 79 } 80 81 82 public final class InitializerFactory { 83 private class TextInitializer extends Initializer { 84 private final Text fText; 85 86 public TextInitializer(Preference preference, Text control) { 87 super(preference); 88 fText= control; 89 } 90 public void initialize() { 91 String value= fOverlayStore.getString(fPreference.getKey()); 92 fText.setText(value); 93 } 94 } 95 96 private class CheckboxInitializer extends Initializer { 97 private final Button fControl; 98 99 public CheckboxInitializer(Preference preference, Button control) { 100 super(preference); 101 fControl= control; 102 } 103 public void initialize() { 104 boolean value= fOverlayStore.getBoolean(fPreference.getKey()); 105 fControl.setSelection(value); 106 } 107 } 108 109 private class ComboInitializer extends Initializer { 110 private final Combo fControl; 111 private final EnumeratedDomain fDomain; 112 113 public ComboInitializer(Preference preference, Combo control, EnumeratedDomain domain) { 114 super(preference); 115 fControl= control; 116 fDomain= domain; 117 } 118 public void initialize() { 119 int value= fOverlayStore.getInt(fPreference.getKey()); 120 EnumValue enumValue= fDomain.getValueByInteger(value); 121 if (enumValue != null) { 122 int index= fDomain.getIndex(enumValue); 123 if (index >= 0) 124 fControl.select(index); 125 } 126 } 127 } 128 129 private class SpinnerInitializer extends Initializer { 130 private final Spinner fControl; 131 private final EnumeratedDomain fDomain; 132 133 public SpinnerInitializer(Preference preference, Spinner control, EnumeratedDomain domain) { 134 super(preference); 135 fControl= control; 136 fDomain= domain; 137 } 138 public void initialize() { 139 int value= fOverlayStore.getInt(fPreference.getKey()); 140 EnumValue enumValue= fDomain.getValueByInteger(value); 141 if (enumValue != null) { 142 fControl.setSelection(value); 143 } 144 } 145 } 146 147 public Initializer create(Preference preference, Text control) { 148 return new TextInitializer(preference, control); 149 } 150 151 public Initializer create(Preference preference, Button control) { 152 return new CheckboxInitializer(preference, control); 153 } 154 155 public Initializer create(Preference preference, Combo control, EnumeratedDomain domain) { 156 return new ComboInitializer(preference, control, domain); 157 } 158 159 public Initializer create(Preference preference, Spinner control, EnumeratedDomain domain) { 160 return new SpinnerInitializer(preference, control, domain); 161 } 162 } 163 164 165 abstract static class Domain { 166 public abstract IStatus validate(Object value); 167 protected int parseInteger(Object val) throws NumberFormatException { 168 if (val instanceof Integer ) { 169 return ((Integer ) val).intValue(); 170 } 171 if (val instanceof String ) { 172 return Integer.parseInt((String ) val); 173 } 174 throw new NumberFormatException (NLSUtility.format(TextEditorMessages.TextEditorPreferencePage_invalidInput, String.valueOf(val))); 175 } 176 } 177 178 static class IntegerDomain extends Domain { 179 private final int fMax; 180 private final int fMin; 181 public IntegerDomain(int min, int max) { 182 Assert.isLegal(max >= min); 183 fMax= max; 184 fMin= min; 185 } 186 187 public IStatus validate(Object value) { 188 StatusInfo status= new StatusInfo(); 189 if (value instanceof String && ((String )value).length() == 0) { 190 status.setError(TextEditorMessages.TextEditorPreferencePage_emptyInput); 191 return status; 192 } 193 try { 194 int integer= parseInteger(value); 195 if (!rangeCheck(integer)) 196 status.setError(NLSUtility.format(TextEditorMessages.TextEditorPreferencePage_invalidInput, String.valueOf(integer))); 197 } catch (NumberFormatException e) { 198 status.setError(NLSUtility.format(TextEditorMessages.TextEditorPreferencePage_invalidInput, String.valueOf(value))); 199 } 200 return status; 201 } 202 203 protected boolean rangeCheck(int i) { 204 return (i >= fMin && i <= fMax); 205 } 206 207 } 208 209 static class EnumeratedDomain extends Domain { 210 public final static class EnumValue { 211 private final int fValue; 212 private final String fName; 213 public EnumValue(int value) { 214 this(value, null); 215 } 216 public EnumValue(int value, String name) { 217 fValue= value; 218 fName= name; 219 } 220 public String getLabel() { 221 return fName == null ? String.valueOf(fValue) : fName; 222 } 223 public int getIntValue() { 224 return fValue; 225 } 226 public final int hashCode() { 227 return getIntValue(); 228 } 229 public boolean equals(Object obj) { 230 if (obj instanceof EnumValue) { 231 return ((EnumValue) obj).getIntValue() == fValue; 232 } 233 return false; 234 } 235 } 236 237 private final java.util.List fItems= new ArrayList (); 238 private final Set fValueSet= new HashSet (); 239 240 public void addValue(EnumValue val) { 241 if (fValueSet.contains(val)) 242 fItems.remove(val); 243 fItems.add(val); 244 fValueSet.add(val); 245 } 246 247 public int getIndex(EnumValue enumValue) { 248 int i= 0; 249 for (Iterator it= fItems.iterator(); it.hasNext();) { 250 EnumValue ev= (EnumValue) it.next(); 251 if (ev.equals(enumValue)) 252 return i; 253 i++; 254 } 255 return -1; 256 } 257 258 public EnumValue getValueByIndex (int index) { 259 if (index >= 0 && fItems.size() > index) 260 return (EnumValue) fItems.get(index); 261 return null; 262 } 263 264 public EnumValue getValueByInteger(int intValue) { 265 for (Iterator it= fItems.iterator(); it.hasNext();) { 266 EnumValue e= (EnumValue) it.next(); 267 if (e.getIntValue() == intValue) 268 return e; 269 } 270 return null; 271 } 272 273 public void addValue(int val) { 274 addValue(new EnumValue(val)); 275 } 276 277 public void addRange(int from, int to) { 278 while (from <= to) 279 addValue(from++); 280 } 281 282 public IStatus validate(Object value) { 283 StatusInfo status= new StatusInfo(); 284 if (value instanceof String && ((String )value).length() == 0) { 285 status.setError(TextEditorMessages.TextEditorPreferencePage_emptyInput); 286 return status; 287 } 288 try { 289 EnumValue e= parseEnumValue(value); 290 if (!fValueSet.contains(e)) 291 status.setError(NLSUtility.format(TextEditorMessages.TextEditorPreferencePage_invalidRange, new String [] {getValueByIndex(0).getLabel(), getValueByIndex(fItems.size() - 1).getLabel()})); 292 } catch (NumberFormatException e) { 293 status.setError(NLSUtility.format(TextEditorMessages.TextEditorPreferencePage_invalidInput, String.valueOf(value))); 294 } 295 296 return status; 297 } 298 299 private EnumValue parseEnumValue(Object value) { 300 if (value instanceof EnumValue) 301 return (EnumValue) value; 302 int integer= parseInteger(value); 303 return getValueByInteger(integer); 304 } 305 306 public EnumValue getMinimumValue() { 307 return getValueByIndex(0); 308 } 309 310 public EnumValue getMaximumValue() { 311 return getValueByIndex(fItems.size() - 1); 312 } 313 } 314 315 static class BooleanDomain extends Domain { 316 public IStatus validate(Object value) { 317 StatusInfo status= new StatusInfo(); 318 if (value instanceof String && ((String )value).length() == 0) { 319 status.setError(TextEditorMessages.TextEditorPreferencePage_emptyInput); 320 return status; 321 } 322 try { 323 parseBoolean(value); 324 } catch (NumberFormatException e) { 325 status.setError(NLSUtility.format(TextEditorMessages.TextEditorPreferencePage_invalidInput, String.valueOf(value))); 326 } 327 328 return status; 329 } 330 331 private boolean parseBoolean(Object value) throws NumberFormatException { 332 if (value instanceof Boolean ) 333 return ((Boolean ) value).booleanValue(); 334 335 if (value instanceof String ) { 336 if (Boolean.TRUE.toString().equalsIgnoreCase((String ) value)) 337 return true; 338 if (Boolean.FALSE.toString().equalsIgnoreCase((String ) value)) 339 return false; 340 } 341 342 throw new NumberFormatException (NLSUtility.format(TextEditorMessages.TextEditorPreferencePage_invalidInput, String.valueOf(value))); 343 } 344 } 345 346 private static class Preference { 347 private String fKey; 348 private String fName; 349 private String fDescription; 351 public Preference(String key, String name, String description) { 352 Assert.isNotNull(key); 353 Assert.isNotNull(name); 354 fKey= key; 355 fName= name; 356 fDescription= description; 357 } 358 public final String getKey() { 359 return fKey; 360 } 361 public final String getName() { 362 return fName; 363 } 364 public final String getDescription() { 365 return fDescription; 366 } 367 } 368 369 370 private final String [][] fAppearanceColorListModel= new String [][] { 371 {TextEditorMessages.TextEditorPreferencePage_lineNumberForegroundColor, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR, null}, 372 {TextEditorMessages.TextEditorPreferencePage_currentLineHighlighColor, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR, null}, 373 {TextEditorMessages.TextEditorPreferencePage_printMarginColor, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLOR, null}, 374 {TextEditorMessages.TextEditorPreferencePage_findScopeColor, AbstractTextEditor.PREFERENCE_COLOR_FIND_SCOPE, null}, 375 {TextEditorMessages.TextEditorPreferencePage_selectionForegroundColor, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR}, 376 {TextEditorMessages.TextEditorPreferencePage_selectionBackgroundColor, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR}, 377 {TextEditorMessages.TextEditorPreferencePage_backgroundColor, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT}, 378 {TextEditorMessages.TextEditorPreferencePage_foregroundColor, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT}, 379 {TextEditorMessages.HyperlinkColor_label, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_COLOR, null}, 380 }; 381 382 private OverlayPreferenceStore fOverlayStore; 383 384 private List fAppearanceColorList; 385 private ColorSelector fAppearanceColorEditor; 386 private Button fAppearanceColorDefault; 387 388 391 private boolean fFieldsInitialized= false; 392 393 private ArrayList fMasterSlaveListeners= new ArrayList (); 394 395 private java.util.List fInitializers= new ArrayList (); 396 397 private InitializerFactory fInitializerFactory= new InitializerFactory(); 398 399 private Map fDomains= new HashMap (); 400 401 402 public TextEditorDefaultsPreferencePage() { 403 setPreferenceStore(EditorsPlugin.getDefault().getPreferenceStore()); 404 405 fOverlayStore= createOverlayStore(); 406 } 407 408 private OverlayPreferenceStore createOverlayStore() { 409 410 ArrayList overlayKeys= new ArrayList (); 411 412 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractTextEditor.PREFERENCE_COLOR_FIND_SCOPE)); 413 414 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE_COLOR)); 415 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE)); 416 417 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH)); 418 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS)); 419 420 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLOR)); 421 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN)); 422 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN)); 423 424 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_UNDO_HISTORY_SIZE)); 425 426 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER_COLOR)); 427 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER)); 428 429 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR)); 430 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR)); 431 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR)); 432 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR)); 433 434 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND)); 435 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT)); 436 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND)); 437 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)); 438 439 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED)); 440 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_COLOR)); 441 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER)); 442 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINK_KEY_MODIFIER_MASK)); 443 444 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SHOW_WHITESPACE_CHARACTERS)); 445 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.SHOW_RANGE_INDICATOR)); 446 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_WARN_IF_INPUT_DERIVED)); 447 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SMART_HOME_END)); 448 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TEXT_DRAG_AND_DROP_ENABLED)); 449 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE)); 450 451 OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()]; 452 overlayKeys.toArray(keys); 453 return new OverlayPreferenceStore(getPreferenceStore(), keys); 454 } 455 456 459 public void init(IWorkbench workbench) { 460 } 461 462 465 public void createControl(Composite parent) { 466 super.createControl(parent); 467 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), ITextEditorHelpContextIds.TEXT_EDITOR_PREFERENCE_PAGE); 468 } 469 470 private void handleAppearanceColorListSelection() { 471 int i= fAppearanceColorList.getSelectionIndex(); 472 if (i == -1) 473 return; 474 475 String key= fAppearanceColorListModel[i][1]; 476 RGB rgb= PreferenceConverter.getColor(fOverlayStore, key); 477 fAppearanceColorEditor.setColorValue(rgb); 478 updateAppearanceColorWidgets(fAppearanceColorListModel[i][2]); 479 } 480 481 private void updateAppearanceColorWidgets(String systemDefaultKey) { 482 if (systemDefaultKey == null) { 483 fAppearanceColorDefault.setSelection(false); 484 fAppearanceColorDefault.setVisible(false); 485 fAppearanceColorEditor.getButton().setEnabled(true); 486 } else { 487 boolean systemDefault= fOverlayStore.getBoolean(systemDefaultKey); 488 fAppearanceColorDefault.setSelection(systemDefault); 489 fAppearanceColorDefault.setVisible(true); 490 fAppearanceColorEditor.getButton().setEnabled(!systemDefault); 491 } 492 } 493 494 private Control createAppearancePage(Composite parent) { 495 496 Composite appearanceComposite= new Composite(parent, SWT.NONE); 497 GridLayout layout= new GridLayout(); 498 layout.numColumns= 2; 499 layout.marginHeight= 0; 500 layout.marginWidth= 0; 501 502 appearanceComposite.setLayout(layout); 503 504 String label= TextEditorMessages.TextEditorPreferencePage_undoHistorySize; 505 Preference undoHistorySize= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_UNDO_HISTORY_SIZE, label, null); 506 IntegerDomain undoHistorySizeDomain= new IntegerDomain(0, 99999); 507 addTextField(appearanceComposite, undoHistorySize, undoHistorySizeDomain, 15, 0); 508 509 label= TextEditorMessages.TextEditorPreferencePage_displayedTabWidth; 510 Preference tabWidth= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH, label, null); 511 IntegerDomain tabWidthDomain= new IntegerDomain(1, 16); 512 addTextField(appearanceComposite, tabWidth, tabWidthDomain, 15, 0); 513 514 label= TextEditorMessages.TextEditorPreferencePage_convertTabsToSpaces; 515 Preference spacesForTabs= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS, label, null); 516 addCheckBox(appearanceComposite, spacesForTabs, new BooleanDomain(), 0); 517 518 519 label= TextEditorMessages.TextEditorPreferencePage_highlightCurrentLine; 520 Preference highlightCurrentLine= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_CURRENT_LINE, label, null); 521 addCheckBox(appearanceComposite, highlightCurrentLine, new BooleanDomain(), 0); 522 523 label= TextEditorMessages.TextEditorPreferencePage_showPrintMargin; 524 Preference showPrintMargin= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN, label, null); 525 final Button showPrintMarginButton= addCheckBox(appearanceComposite, showPrintMargin, new BooleanDomain(), 0); 526 527 528 label= TextEditorMessages.TextEditorPreferencePage_printMarginColumn; 529 Preference printMarginColumn= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN, label, null); 530 final IntegerDomain printMarginDomain= new IntegerDomain(20, 200); 531 final Control[] printMarginControls= addTextField(appearanceComposite, printMarginColumn, printMarginDomain, 15, 20); 532 createDependency(showPrintMarginButton, showPrintMargin, printMarginControls); 533 534 showPrintMarginButton.addSelectionListener(new SelectionAdapter() { 535 public void widgetSelected(SelectionEvent e) { 536 updateStatus(printMarginDomain); 537 } 538 }); 539 540 label= TextEditorMessages.TextEditorPreferencePage_showLineNumbers; 541 Preference showLineNumbers= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER, label, null); 542 addCheckBox(appearanceComposite, showLineNumbers, new BooleanDomain(), 0); 543 544 label= TextEditorMessages.TextEditorDefaultsPreferencePage_range_indicator; 545 Preference showMagnet= new Preference(AbstractDecoratedTextEditorPreferenceConstants.SHOW_RANGE_INDICATOR, label, null); 546 addCheckBox(appearanceComposite, showMagnet, new BooleanDomain(), 0); 547 548 label= TextEditorMessages.TextEditorDefaultsPreferencePage_showWhitespaceCharacters; 549 Preference showWhitespaceCharacters= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SHOW_WHITESPACE_CHARACTERS, label, null); 550 addCheckBox(appearanceComposite, showWhitespaceCharacters, new BooleanDomain(), 0); 551 552 label= TextEditorMessages.TextEditorDefaultsPreferencePage_textDragAndDrop; 553 Preference textDragAndDrop= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TEXT_DRAG_AND_DROP_ENABLED, label, null); 554 addCheckBox(appearanceComposite, textDragAndDrop, new BooleanDomain(), 0); 555 556 label= TextEditorMessages.TextEditorDefaultsPreferencePage_warn_if_derived; 557 Preference warnIfDerived= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_WARN_IF_INPUT_DERIVED, label, null); 558 addCheckBox(appearanceComposite, warnIfDerived, new BooleanDomain(), 0); 559 560 label= TextEditorMessages.TextEditorDefaultsPreferencePage_smartHomeEnd; 561 Preference smartHomeEnd= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SMART_HOME_END, label, null); 562 addCheckBox(appearanceComposite, smartHomeEnd, new BooleanDomain(), 0); 563 564 label= TextEditorMessages.TextEditorPreferencePage_showAffordance; 565 Preference showAffordance= new Preference(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SHOW_TEXT_HOVER_AFFORDANCE, label, null); 566 addCheckBox(appearanceComposite, showAffordance, new BooleanDomain(), 0); 567 568 Label l= new Label(appearanceComposite, SWT.LEFT ); 569 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 570 gd.horizontalSpan= 2; 571 gd.heightHint= convertHeightInCharsToPixels(1) / 2; 572 l.setLayoutData(gd); 573 574 l= new Label(appearanceComposite, SWT.LEFT); 575 l.setText(TextEditorMessages.TextEditorPreferencePage_appearanceOptions); 576 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 577 gd.horizontalSpan= 2; 578 l.setLayoutData(gd); 579 580 Composite editorComposite= new Composite(appearanceComposite, SWT.NONE); 581 layout= new GridLayout(); 582 layout.numColumns= 2; 583 layout.marginHeight= 0; 584 layout.marginWidth= 0; 585 editorComposite.setLayout(layout); 586 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL); 587 gd.horizontalSpan= 2; 588 editorComposite.setLayoutData(gd); 589 590 fAppearanceColorList= new List (editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER); 591 gd= new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_BOTH); 592 gd.heightHint= fAppearanceColorList.getItemHeight() * 8; 593 fAppearanceColorList.setLayoutData(gd); 594 595 Composite stylesComposite= new Composite(editorComposite, SWT.NONE); 596 layout= new GridLayout(); 597 layout.marginHeight= 0; 598 layout.marginWidth= 0; 599 layout.numColumns= 2; 600 stylesComposite.setLayout(layout); 601 stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); 602 603 l= new Label(stylesComposite, SWT.LEFT); 604 l.setText(TextEditorMessages.TextEditorPreferencePage_color); 605 gd= new GridData(); 606 gd.horizontalAlignment= GridData.BEGINNING; 607 l.setLayoutData(gd); 608 609 fAppearanceColorEditor= new ColorSelector(stylesComposite); 610 Button foregroundColorButton= fAppearanceColorEditor.getButton(); 611 gd= new GridData(GridData.FILL_HORIZONTAL); 612 gd.horizontalAlignment= GridData.BEGINNING; 613 foregroundColorButton.setLayoutData(gd); 614 615 SelectionListener colorDefaultSelectionListener= new SelectionListener() { 616 public void widgetSelected(SelectionEvent e) { 617 boolean systemDefault= fAppearanceColorDefault.getSelection(); 618 fAppearanceColorEditor.getButton().setEnabled(!systemDefault); 619 620 int i= fAppearanceColorList.getSelectionIndex(); 621 if (i == -1) 622 return; 623 624 String key= fAppearanceColorListModel[i][2]; 625 if (key != null) 626 fOverlayStore.setValue(key, systemDefault); 627 } 628 public void widgetDefaultSelected(SelectionEvent e) {} 629 }; 630 631 fAppearanceColorDefault= new Button(stylesComposite, SWT.CHECK); 632 fAppearanceColorDefault.setText(TextEditorMessages.TextEditorPreferencePage_systemDefault); 633 gd= new GridData(GridData.FILL_HORIZONTAL); 634 gd.horizontalAlignment= GridData.BEGINNING; 635 gd.horizontalSpan= 2; 636 fAppearanceColorDefault.setLayoutData(gd); 637 fAppearanceColorDefault.setVisible(false); 638 fAppearanceColorDefault.addSelectionListener(colorDefaultSelectionListener); 639 640 fAppearanceColorList.addSelectionListener(new SelectionListener() { 641 public void widgetDefaultSelected(SelectionEvent e) { 642 } 644 public void widgetSelected(SelectionEvent e) { 645 handleAppearanceColorListSelection(); 646 } 647 }); 648 foregroundColorButton.addSelectionListener(new SelectionListener() { 649 public void widgetDefaultSelected(SelectionEvent e) { 650 } 652 public void widgetSelected(SelectionEvent e) { 653 int i= fAppearanceColorList.getSelectionIndex(); 654 if (i == -1) 655 return; 656 657 String key= fAppearanceColorListModel[i][1]; 658 PreferenceConverter.setValue(fOverlayStore, key, fAppearanceColorEditor.getColorValue()); 659 } 660 }); 661 662 Link link= new Link(appearanceComposite, SWT.NONE); 663 link.setText(TextEditorMessages.TextEditorPreferencePage_colorsAndFonts_link); 664 link.addSelectionListener(new SelectionAdapter() { 665 public void widgetSelected(SelectionEvent e) { 666 PreferencesUtil.createPreferenceDialogOn(getShell(), "org.eclipse.ui.preferencePages.ColorsAndFonts", null, null); } 668 }); 669 link.setToolTipText(TextEditorMessages.TextEditorPreferencePage_colorsAndFonts_link_tooltip); 672 673 GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false); 674 gridData.widthHint= 150; gridData.horizontalSpan= 2; 676 link.setLayoutData(gridData); 677 678 addFiller(appearanceComposite, 2); 679 680 appearanceComposite.layout(); 681 682 return appearanceComposite; 683 } 684 685 688 protected Control createContents(Composite parent) { 689 690 initializeDefaultColors(); 691 692 fOverlayStore.load(); 693 fOverlayStore.start(); 694 695 Control control= createAppearancePage(parent); 696 697 initialize(); 698 Dialog.applyDialogFont(control); 699 return control; 700 } 701 702 private void initialize() { 703 704 initializeFields(); 705 706 for (int i= 0; i < fAppearanceColorListModel.length; i++) 707 fAppearanceColorList.add(fAppearanceColorListModel[i][0]); 708 fAppearanceColorList.getDisplay().asyncExec(new Runnable () { 709 public void run() { 710 if (fAppearanceColorList != null && !fAppearanceColorList.isDisposed()) { 711 fAppearanceColorList.select(0); 712 handleAppearanceColorListSelection(); 713 } 714 } 715 }); 716 } 717 718 private void initializeFields() { 719 for (Iterator it= fInitializers.iterator(); it.hasNext();) { 720 Initializer initializer= (Initializer) it.next(); 721 initializer.initialize(); 722 } 723 724 fFieldsInitialized= true; 725 updateStatus(new StatusInfo()); 726 727 Iterator iter= fMasterSlaveListeners.iterator(); 729 while (iter.hasNext()) { 730 SelectionListener listener= (SelectionListener)iter.next(); 731 listener.widgetSelected(null); 732 } 733 734 } 735 736 private void initializeDefaultColors() { 737 if (!getPreferenceStore().contains(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR)) { 738 RGB rgb= getControl().getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION).getRGB(); 739 PreferenceConverter.setDefault(fOverlayStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, rgb); 740 PreferenceConverter.setDefault(getPreferenceStore(), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, rgb); 741 } 742 if (!getPreferenceStore().contains(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR)) { 743 RGB rgb= getControl().getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT).getRGB(); 744 PreferenceConverter.setDefault(fOverlayStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, rgb); 745 PreferenceConverter.setDefault(getPreferenceStore(), AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, rgb); 746 } 747 if (!getPreferenceStore().contains(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND)) { 748 RGB rgb= getControl().getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB(); 749 PreferenceConverter.setDefault(fOverlayStore, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND, rgb); 750 PreferenceConverter.setDefault(getPreferenceStore(), AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND, rgb); 751 } 752 if (!getPreferenceStore().contains(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND)) { 753 RGB rgb= getControl().getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND).getRGB(); 754 PreferenceConverter.setDefault(fOverlayStore, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND, rgb); 755 PreferenceConverter.setDefault(getPreferenceStore(), AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND, rgb); 756 } 757 } 758 759 762 public boolean performOk() { 763 fOverlayStore.propagate(); 764 EditorsPlugin.getDefault().savePluginPreferences(); 765 return true; 766 } 767 768 771 protected void performDefaults() { 772 773 fOverlayStore.loadDefaults(); 774 775 initializeFields(); 776 777 handleAppearanceColorListSelection(); 778 779 super.performDefaults(); 780 } 781 782 785 public void dispose() { 786 787 if (fOverlayStore != null) { 788 fOverlayStore.stop(); 789 fOverlayStore= null; 790 } 791 792 super.dispose(); 793 } 794 795 private void addFiller(Composite composite, int horizontalSpan) { 796 PixelConverter pixelConverter= new PixelConverter(composite); 797 Label filler= new Label(composite, SWT.LEFT ); 798 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 799 gd.horizontalSpan= horizontalSpan; 800 gd.heightHint= pixelConverter.convertHeightInCharsToPixels(1) / 2; 801 filler.setLayoutData(gd); 802 } 803 804 Button addCheckBox(Composite composite, final Preference preference, final Domain domain, int indentation) { 805 final Button checkBox= new Button(composite, SWT.CHECK); 806 checkBox.setText(preference.getName()); 807 checkBox.setToolTipText(preference.getDescription()); 808 809 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 810 gd.horizontalIndent= indentation; 811 gd.horizontalSpan= 2; 812 checkBox.setLayoutData(gd); 813 checkBox.addSelectionListener(new SelectionAdapter() { 814 public void widgetSelected(SelectionEvent e) { 815 boolean value= checkBox.getSelection(); 816 IStatus status= domain.validate(Boolean.valueOf(value)); 817 if (!status.matches(IStatus.ERROR)) 818 fOverlayStore.setValue(preference.getKey(), value); 819 updateStatus(status); 820 } 821 }); 822 823 fInitializers.add(fInitializerFactory.create(preference, checkBox)); 824 825 return checkBox; 826 } 827 828 Control[] addCombo(Composite composite, final Preference preference, final EnumeratedDomain domain, int indentation) { 829 Label labelControl= new Label(composite, SWT.NONE); 830 labelControl.setText(preference.getName()); 831 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 832 gd.horizontalIndent= indentation; 833 labelControl.setLayoutData(gd); 834 835 final Combo combo= new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY); 836 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 837 combo.setLayoutData(gd); 838 combo.setToolTipText(preference.getDescription()); 839 for (Iterator it= domain.fItems.iterator(); it.hasNext();) { 840 EnumValue value= (EnumValue) it.next(); 841 combo.add(value.getLabel()); 842 } 843 844 combo.addSelectionListener(new SelectionAdapter() { 845 public void widgetSelected(SelectionEvent e) { 846 int index= combo.getSelectionIndex(); 847 EnumValue value= domain.getValueByIndex(index); 848 IStatus status= domain.validate(value); 849 if (!status.matches(IStatus.ERROR)) 850 fOverlayStore.setValue(preference.getKey(), value.getIntValue()); 851 updateStatus(status); 852 } 853 }); 854 855 fInitializers.add(fInitializerFactory.create(preference, combo, domain)); 856 857 return new Control[] {labelControl, combo}; 858 } 859 860 873 Control[] addSpinner(Composite composite, final Preference preference, final EnumeratedDomain domain, int indentation) { 874 Label labelControl= new Label(composite, SWT.NONE); 875 labelControl.setText(preference.getName()); 876 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 877 gd.horizontalIndent= indentation; 878 labelControl.setLayoutData(gd); 879 880 final Spinner spinner= new Spinner(composite, SWT.READ_ONLY | SWT.BORDER); 881 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 882 spinner.setLayoutData(gd); 883 spinner.setToolTipText(preference.getDescription()); 884 spinner.setMinimum(domain.getMinimumValue().getIntValue()); 885 spinner.setMaximum(domain.getMaximumValue().getIntValue()); 886 spinner.setIncrement(1); 887 spinner.setPageIncrement(4); 888 889 spinner.addSelectionListener(new SelectionAdapter() { 890 public void widgetSelected(SelectionEvent e) { 891 int index= spinner.getSelection(); 892 EnumValue value= domain.getValueByInteger(index); 893 IStatus status= domain.validate(value); 894 if (!status.matches(IStatus.ERROR)) 895 fOverlayStore.setValue(preference.getKey(), value.getIntValue()); 896 updateStatus(status); 897 } 898 }); 899 900 fInitializers.add(fInitializerFactory.create(preference, spinner, domain)); 901 902 return new Control[] {labelControl, spinner}; 903 } 904 905 private Control[] addTextField(Composite composite, final Preference preference, final Domain domain, int textLimit, int indentation) { 906 Label labelControl= new Label(composite, SWT.NONE); 907 labelControl.setText(preference.getName()); 908 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 909 gd.horizontalIndent= indentation; 910 labelControl.setLayoutData(gd); 911 912 final Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE); 913 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 914 gd.widthHint= convertWidthInCharsToPixels(textLimit + 1); 915 textControl.setLayoutData(gd); 916 textControl.setTextLimit(textLimit); 917 textControl.setToolTipText(preference.getDescription()); 918 919 if (domain != null) { 920 textControl.addModifyListener(new ModifyListener() { 921 public void modifyText(ModifyEvent e) { 922 String value= textControl.getText(); 923 IStatus status= domain.validate(value); 924 if (!status.matches(IStatus.ERROR)) 925 fOverlayStore.setValue(preference.getKey(), value); 926 updateStatus(domain); 927 } 928 }); 929 } 930 931 fInitializers.add(fInitializerFactory.create(preference, textControl)); 932 933 fDomains.put(domain, textControl); 934 935 return new Control[] {labelControl, textControl}; 936 } 937 938 private void createDependency(final Button master, Preference preference, final Control[] slaves) { 939 indent(slaves[0]); 940 941 boolean masterState= fOverlayStore.getBoolean(preference.getKey()); 942 for (int i= 0; i < slaves.length; i++) { 943 slaves[i].setEnabled(masterState); 944 } 945 946 SelectionListener listener= new SelectionListener() { 947 public void widgetSelected(SelectionEvent e) { 948 boolean state= master.getSelection(); 949 for (int i= 0; i < slaves.length; i++) { 950 slaves[i].setEnabled(state); 951 } 952 } 953 954 public void widgetDefaultSelected(SelectionEvent e) {} 955 }; 956 master.addSelectionListener(listener); 957 fMasterSlaveListeners.add(listener); 958 } 959 960 private static void indent(Control control) { 961 GridData gridData= new GridData(); 962 gridData.horizontalIndent= 20; 963 control.setLayoutData(gridData); 964 } 965 966 void updateStatus(IStatus status) { 967 if (!fFieldsInitialized) 968 return; 969 setValid(!status.matches(IStatus.ERROR)); 970 applyToStatusLine(this, status); 971 } 972 973 void updateStatus(Domain checkedDomain) { 974 if (!fFieldsInitialized) 975 return; 976 977 if (updateStatusOnError(checkedDomain)) 978 return; 979 980 Iterator iter= fDomains.keySet().iterator(); 981 while (iter.hasNext()) { 982 Domain domain= (Domain)iter.next(); 983 if (domain.equals(checkedDomain)) 984 continue; 985 if (updateStatusOnError(domain)) 986 return; 987 } 988 updateStatus(new StatusInfo()); 989 } 990 991 private boolean updateStatusOnError(Domain domain) { 992 Text textWidget= (Text)fDomains.get(domain); 993 if (textWidget.isEnabled()) { 994 IStatus status= domain.validate(textWidget.getText()); 995 if (status.matches(IStatus.ERROR)) { 996 updateStatus(status); 997 return true; 998 } 999 } 1000 return false; 1001 } 1002 1003 1009 public void applyToStatusLine(DialogPage page, IStatus status) { 1010 String message= status.getMessage(); 1011 switch (status.getSeverity()) { 1012 case IStatus.OK: 1013 page.setMessage(message, IMessageProvider.NONE); 1014 page.setErrorMessage(null); 1015 break; 1016 case IStatus.WARNING: 1017 page.setMessage(message, IMessageProvider.WARNING); 1018 page.setErrorMessage(null); 1019 break; 1020 case IStatus.INFO: 1021 page.setMessage(message, IMessageProvider.INFORMATION); 1022 page.setErrorMessage(null); 1023 break; 1024 default: 1025 if (message.length() == 0) { 1026 message= null; 1027 } 1028 page.setMessage(null); 1029 page.setErrorMessage(message); 1030 break; 1031 } 1032 } 1033 1034} 1035 | Popular Tags |