1 11 12 package org.eclipse.jdt.internal.ui.preferences; 13 14 import java.io.BufferedReader ; 15 import java.io.IOException ; 16 import java.io.InputStreamReader ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 20 import org.eclipse.core.runtime.Preferences; 21 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.events.SelectionAdapter; 24 import org.eclipse.swt.events.SelectionEvent; 25 import org.eclipse.swt.events.SelectionListener; 26 import org.eclipse.swt.graphics.Font; 27 import org.eclipse.swt.graphics.FontMetrics; 28 import org.eclipse.swt.graphics.GC; 29 import org.eclipse.swt.graphics.Point; 30 import org.eclipse.swt.graphics.RGB; 31 import org.eclipse.swt.layout.GridData; 32 import org.eclipse.swt.layout.GridLayout; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.Label; 37 import org.eclipse.swt.widgets.Link; 38 import org.eclipse.swt.widgets.ScrollBar; 39 import org.eclipse.swt.widgets.Scrollable; 40 41 import org.eclipse.jface.dialogs.Dialog; 42 import org.eclipse.jface.preference.ColorSelector; 43 import org.eclipse.jface.preference.IPreferenceStore; 44 import org.eclipse.jface.preference.PreferenceConverter; 45 import org.eclipse.jface.resource.JFaceResources; 46 import org.eclipse.jface.util.PropertyChangeEvent; 47 import org.eclipse.jface.viewers.ISelectionChangedListener; 48 import org.eclipse.jface.viewers.IStructuredSelection; 49 import org.eclipse.jface.viewers.ITreeContentProvider; 50 import org.eclipse.jface.viewers.LabelProvider; 51 import org.eclipse.jface.viewers.SelectionChangedEvent; 52 import org.eclipse.jface.viewers.StructuredSelection; 53 import org.eclipse.jface.viewers.StructuredViewer; 54 import org.eclipse.jface.viewers.TreeViewer; 55 import org.eclipse.jface.viewers.Viewer; 56 import org.eclipse.jface.viewers.ViewerComparator; 57 58 import org.eclipse.jface.text.BadLocationException; 59 import org.eclipse.jface.text.Document; 60 import org.eclipse.jface.text.IDocument; 61 62 import org.eclipse.ui.dialogs.PreferencesUtil; 63 import org.eclipse.ui.texteditor.ChainedPreferenceStore; 64 65 import org.eclipse.ui.editors.text.EditorsUI; 66 67 import org.eclipse.jdt.core.JavaCore; 68 69 import org.eclipse.jdt.ui.PreferenceConstants; 70 import org.eclipse.jdt.ui.text.IColorManager; 71 import org.eclipse.jdt.ui.text.IJavaPartitions; 72 73 import org.eclipse.jdt.internal.ui.JavaPlugin; 74 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; 75 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlighting; 76 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightingManager; 77 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightings; 78 import org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightingManager.HighlightedRange; 79 import org.eclipse.jdt.internal.ui.text.JavaColorManager; 80 import org.eclipse.jdt.internal.ui.text.PreferencesAdapter; 81 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration; 82 import org.eclipse.jdt.internal.ui.util.PixelConverter; 83 84 89 class JavaEditorColoringConfigurationBlock extends AbstractConfigurationBlock { 90 91 96 private static class HighlightingColorListItem { 97 98 private String fDisplayName; 99 100 private String fColorKey; 101 102 private String fBoldKey; 103 104 private String fItalicKey; 105 109 private String fStrikethroughKey; 110 113 private String fUnderlineKey; 114 115 124 public HighlightingColorListItem(String displayName, String colorKey, String boldKey, String italicKey, String strikethroughKey, String underlineKey) { 125 fDisplayName= displayName; 126 fColorKey= colorKey; 127 fBoldKey= boldKey; 128 fItalicKey= italicKey; 129 fStrikethroughKey= strikethroughKey; 130 fUnderlineKey= underlineKey; 131 } 132 133 136 public String getBoldKey() { 137 return fBoldKey; 138 } 139 140 143 public String getItalicKey() { 144 return fItalicKey; 145 } 146 147 151 public String getStrikethroughKey() { 152 return fStrikethroughKey; 153 } 154 155 159 public String getUnderlineKey() { 160 return fUnderlineKey; 161 } 162 163 166 public String getColorKey() { 167 return fColorKey; 168 } 169 170 173 public String getDisplayName() { 174 return fDisplayName; 175 } 176 } 177 178 private static class SemanticHighlightingColorListItem extends HighlightingColorListItem { 179 180 181 private final String fEnableKey; 182 183 193 public SemanticHighlightingColorListItem(String displayName, String colorKey, String boldKey, String italicKey, String strikethroughKey, String underlineKey, String enableKey) { 194 super(displayName, colorKey, boldKey, italicKey, strikethroughKey, underlineKey); 195 fEnableKey= enableKey; 196 } 197 198 201 public String getEnableKey() { 202 return fEnableKey; 203 } 204 } 205 206 211 private class ColorListLabelProvider extends LabelProvider { 212 215 public String getText(Object element) { 216 if (element instanceof String ) 217 return (String ) element; 218 return ((HighlightingColorListItem)element).getDisplayName(); 219 } 220 } 221 222 227 private class ColorListContentProvider implements ITreeContentProvider { 228 229 232 public Object [] getElements(Object inputElement) { 233 return new String [] {fJavaCategory, fJavadocCategory, fCommentsCategory}; 234 } 235 236 239 public void dispose() { 240 } 241 242 245 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 246 } 247 248 public Object [] getChildren(Object parentElement) { 249 if (parentElement instanceof String ) { 250 String entry= (String ) parentElement; 251 if (fJavaCategory.equals(entry)) 252 return fListModel.subList(7, fListModel.size()).toArray(); 253 if (fJavadocCategory.equals(entry)) 254 return fListModel.subList(0, 4).toArray(); 255 if (fCommentsCategory.equals(entry)) 256 return fListModel.subList(4, 7).toArray(); 257 } 258 return new Object [0]; 259 } 260 261 public Object getParent(Object element) { 262 if (element instanceof String ) 263 return null; 264 int index= fListModel.indexOf(element); 265 if (index < 4) 266 return fJavadocCategory; 267 if (index >= 7) 268 return fJavaCategory; 269 return fCommentsCategory; 270 } 271 272 public boolean hasChildren(Object element) { 273 return element instanceof String ; 274 } 275 } 276 277 private static final String BOLD= PreferenceConstants.EDITOR_BOLD_SUFFIX; 278 282 private static final String ITALIC= PreferenceConstants.EDITOR_ITALIC_SUFFIX; 283 287 private static final String STRIKETHROUGH= PreferenceConstants.EDITOR_STRIKETHROUGH_SUFFIX; 288 292 private static final String UNDERLINE= PreferenceConstants.EDITOR_UNDERLINE_SUFFIX; 293 294 private static final String COMPILER_TASK_TAGS= JavaCore.COMPILER_TASK_TAGS; 295 298 private final String [][] fSyntaxColorListModel= new String [][] { 299 { PreferencesMessages.JavaEditorPreferencePage_javaDocKeywords, PreferenceConstants.EDITOR_JAVADOC_KEYWORD_COLOR }, 300 { PreferencesMessages.JavaEditorPreferencePage_javaDocHtmlTags, PreferenceConstants.EDITOR_JAVADOC_TAG_COLOR }, 301 { PreferencesMessages.JavaEditorPreferencePage_javaDocLinks, PreferenceConstants.EDITOR_JAVADOC_LINKS_COLOR }, 302 { PreferencesMessages.JavaEditorPreferencePage_javaDocOthers, PreferenceConstants.EDITOR_JAVADOC_DEFAULT_COLOR }, 303 { PreferencesMessages.JavaEditorPreferencePage_multiLineComment, PreferenceConstants.EDITOR_MULTI_LINE_COMMENT_COLOR }, 304 { PreferencesMessages.JavaEditorPreferencePage_singleLineComment, PreferenceConstants.EDITOR_SINGLE_LINE_COMMENT_COLOR }, 305 { PreferencesMessages.JavaEditorPreferencePage_javaCommentTaskTags, PreferenceConstants.EDITOR_TASK_TAG_COLOR }, 306 { PreferencesMessages.JavaEditorPreferencePage_keywords, PreferenceConstants.EDITOR_JAVA_KEYWORD_COLOR }, 307 { PreferencesMessages.JavaEditorPreferencePage_returnKeyword, PreferenceConstants.EDITOR_JAVA_KEYWORD_RETURN_COLOR }, 308 { PreferencesMessages.JavaEditorPreferencePage_operators, PreferenceConstants.EDITOR_JAVA_OPERATOR_COLOR }, 309 { PreferencesMessages.JavaEditorPreferencePage_brackets, PreferenceConstants.EDITOR_JAVA_BRACKET_COLOR }, 310 { PreferencesMessages.JavaEditorPreferencePage_strings, PreferenceConstants.EDITOR_STRING_COLOR }, 311 { PreferencesMessages.JavaEditorPreferencePage_others, PreferenceConstants.EDITOR_JAVA_DEFAULT_COLOR }, 312 }; 313 314 private final String fJavaCategory= PreferencesMessages.JavaEditorPreferencePage_coloring_category_java; 315 private final String fJavadocCategory= PreferencesMessages.JavaEditorPreferencePage_coloring_category_javadoc; 316 private final String fCommentsCategory= PreferencesMessages.JavaEditorPreferencePage_coloring_category_comments; 317 318 private ColorSelector fSyntaxForegroundColorEditor; 319 private Label fColorEditorLabel; 320 private Button fBoldCheckBox; 321 private Button fEnableCheckbox; 322 326 private Button fItalicCheckBox; 327 331 private Button fStrikethroughCheckBox; 332 336 private Button fUnderlineCheckBox; 337 341 private final java.util.List fListModel= new ArrayList (); 342 346 private StructuredViewer fListViewer; 347 351 private SemanticHighlightingManager fSemanticHighlightingManager; 352 356 private JavaSourceViewer fPreviewViewer; 357 361 private IColorManager fColorManager; 362 366 private FontMetrics fFontMetrics; 367 368 public JavaEditorColoringConfigurationBlock(OverlayPreferenceStore store) { 369 super(store); 370 371 fColorManager= new JavaColorManager(false); 372 373 for (int i= 0, n= fSyntaxColorListModel.length; i < n; i++) 374 fListModel.add(new HighlightingColorListItem (fSyntaxColorListModel[i][0], fSyntaxColorListModel[i][1], fSyntaxColorListModel[i][1] + BOLD, fSyntaxColorListModel[i][1] + ITALIC, fSyntaxColorListModel[i][1] + STRIKETHROUGH, fSyntaxColorListModel[i][1] + UNDERLINE)); 375 376 SemanticHighlighting[] semanticHighlightings= SemanticHighlightings.getSemanticHighlightings(); 377 for (int i= 0, n= semanticHighlightings.length; i < n; i++) 378 fListModel.add( 379 new SemanticHighlightingColorListItem( 380 semanticHighlightings[i].getDisplayName(), 381 SemanticHighlightings.getColorPreferenceKey(semanticHighlightings[i]), 382 SemanticHighlightings.getBoldPreferenceKey(semanticHighlightings[i]), 383 SemanticHighlightings.getItalicPreferenceKey(semanticHighlightings[i]), 384 SemanticHighlightings.getStrikethroughPreferenceKey(semanticHighlightings[i]), 385 SemanticHighlightings.getUnderlinePreferenceKey(semanticHighlightings[i]), 386 SemanticHighlightings.getEnabledPreferenceKey(semanticHighlightings[i]) 387 )); 388 389 store.addKeys(createOverlayStoreKeys()); 390 } 391 392 private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() { 393 394 ArrayList overlayKeys= new ArrayList (); 395 396 for (int i= 0, n= fListModel.size(); i < n; i++) { 397 HighlightingColorListItem item= (HighlightingColorListItem) fListModel.get(i); 398 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, item.getColorKey())); 399 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, item.getBoldKey())); 400 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, item.getItalicKey())); 401 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, item.getStrikethroughKey())); 402 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, item.getUnderlineKey())); 403 404 if (item instanceof SemanticHighlightingColorListItem) 405 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ((SemanticHighlightingColorListItem) item).getEnableKey())); 406 } 407 408 OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()]; 409 overlayKeys.toArray(keys); 410 return keys; 411 } 412 413 419 public Control createControl(Composite parent) { 420 initializeDialogUnits(parent); 421 422 ScrolledPageContent scrolled= new ScrolledPageContent(parent, SWT.H_SCROLL | SWT.V_SCROLL); 423 scrolled.setExpandHorizontal(true); 424 scrolled.setExpandVertical(true); 425 426 Control control= createSyntaxPage(scrolled); 427 428 scrolled.setContent(control); 429 final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT); 430 scrolled.setMinSize(size.x, size.y); 431 432 return scrolled; 433 } 434 435 450 private int convertWidthInCharsToPixels(int chars) { 451 if (fFontMetrics == null) 453 return 0; 454 return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars); 455 } 456 457 472 private int convertHeightInCharsToPixels(int chars) { 473 if (fFontMetrics == null) 475 return 0; 476 return Dialog.convertHeightInCharsToPixels(fFontMetrics, chars); 477 } 478 479 public void initialize() { 480 super.initialize(); 481 482 fListViewer.setInput(fListModel); 483 fListViewer.setSelection(new StructuredSelection(fJavaCategory)); 484 } 485 486 public void performDefaults() { 487 super.performDefaults(); 488 489 handleSyntaxColorListSelection(); 490 491 uninstallSemanticHighlighting(); 492 installSemanticHighlighting(); 493 494 fPreviewViewer.invalidateTextPresentation(); 495 } 496 497 500 public void dispose() { 501 uninstallSemanticHighlighting(); 502 fColorManager.dispose(); 503 504 super.dispose(); 505 } 506 507 private void handleSyntaxColorListSelection() { 508 HighlightingColorListItem item= getHighlightingColorListItem(); 509 if (item == null) { 510 fEnableCheckbox.setEnabled(false); 511 fSyntaxForegroundColorEditor.getButton().setEnabled(false); 512 fColorEditorLabel.setEnabled(false); 513 fBoldCheckBox.setEnabled(false); 514 fItalicCheckBox.setEnabled(false); 515 fStrikethroughCheckBox.setEnabled(false); 516 fUnderlineCheckBox.setEnabled(false); 517 return; 518 } 519 RGB rgb= PreferenceConverter.getColor(getPreferenceStore(), item.getColorKey()); 520 fSyntaxForegroundColorEditor.setColorValue(rgb); 521 fBoldCheckBox.setSelection(getPreferenceStore().getBoolean(item.getBoldKey())); 522 fItalicCheckBox.setSelection(getPreferenceStore().getBoolean(item.getItalicKey())); 523 fStrikethroughCheckBox.setSelection(getPreferenceStore().getBoolean(item.getStrikethroughKey())); 524 fUnderlineCheckBox.setSelection(getPreferenceStore().getBoolean(item.getUnderlineKey())); 525 if (item instanceof SemanticHighlightingColorListItem) { 526 fEnableCheckbox.setEnabled(true); 527 boolean enable= getPreferenceStore().getBoolean(((SemanticHighlightingColorListItem) item).getEnableKey()); 528 fEnableCheckbox.setSelection(enable); 529 fSyntaxForegroundColorEditor.getButton().setEnabled(enable); 530 fColorEditorLabel.setEnabled(enable); 531 fBoldCheckBox.setEnabled(enable); 532 fItalicCheckBox.setEnabled(enable); 533 fStrikethroughCheckBox.setEnabled(enable); 534 fUnderlineCheckBox.setEnabled(enable); 535 } else { 536 fSyntaxForegroundColorEditor.getButton().setEnabled(true); 537 fColorEditorLabel.setEnabled(true); 538 fBoldCheckBox.setEnabled(true); 539 fItalicCheckBox.setEnabled(true); 540 fStrikethroughCheckBox.setEnabled(true); 541 fUnderlineCheckBox.setEnabled(true); 542 fEnableCheckbox.setEnabled(false); 543 fEnableCheckbox.setSelection(true); 544 } 545 } 546 547 private Control createSyntaxPage(final Composite parent) { 548 549 Composite colorComposite= new Composite(parent, SWT.NONE); 550 GridLayout layout= new GridLayout(); 551 layout.marginHeight= 0; 552 layout.marginWidth= 0; 553 colorComposite.setLayout(layout); 554 555 Link link= new Link(colorComposite, SWT.NONE); 556 link.setText(PreferencesMessages.JavaEditorColoringConfigurationBlock_link); 557 link.addSelectionListener(new SelectionAdapter() { 558 public void widgetSelected(SelectionEvent e) { 559 PreferencesUtil.createPreferenceDialogOn(parent.getShell(), e.text, null, null); 560 } 561 }); 562 566 GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false); 567 gridData.widthHint= 150; gridData.horizontalSpan= 2; 569 link.setLayoutData(gridData); 570 571 addFiller(colorComposite, 1); 572 573 Label label; 574 label= new Label(colorComposite, SWT.LEFT); 575 label.setText(PreferencesMessages.JavaEditorPreferencePage_coloring_element); 576 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 577 578 Composite editorComposite= new Composite(colorComposite, SWT.NONE); 579 layout= new GridLayout(); 580 layout.numColumns= 2; 581 layout.marginHeight= 0; 582 layout.marginWidth= 0; 583 editorComposite.setLayout(layout); 584 GridData gd= new GridData(SWT.FILL, SWT.BEGINNING, true, false); 585 editorComposite.setLayoutData(gd); 586 587 fListViewer= new TreeViewer(editorComposite, SWT.SINGLE | SWT.BORDER); 588 fListViewer.setLabelProvider(new ColorListLabelProvider()); 589 fListViewer.setContentProvider(new ColorListContentProvider()); 590 fListViewer.setComparator(new ViewerComparator() { 591 public int category(Object element) { 592 if (fJavaCategory.equals(element)) 594 return 0; 595 if (fJavadocCategory.equals(element)) 596 return 1; 597 if (fCommentsCategory.equals(element)) 598 return 2; 599 return 0; 603 } 604 }); 605 gd= new GridData(SWT.BEGINNING, SWT.BEGINNING, false, true); 606 gd.heightHint= convertHeightInCharsToPixels(9); 607 int maxWidth= 0; 608 for (Iterator it= fListModel.iterator(); it.hasNext();) { 609 HighlightingColorListItem item= (HighlightingColorListItem) it.next(); 610 maxWidth= Math.max(maxWidth, convertWidthInCharsToPixels(item.getDisplayName().length())); 611 } 612 ScrollBar vBar= ((Scrollable) fListViewer.getControl()).getVerticalBar(); 613 if (vBar != null) 614 maxWidth += vBar.getSize().x * 3; gd.widthHint= maxWidth; 616 617 fListViewer.getControl().setLayoutData(gd); 618 619 Composite stylesComposite= new Composite(editorComposite, SWT.NONE); 620 layout= new GridLayout(); 621 layout.marginHeight= 0; 622 layout.marginWidth= 0; 623 layout.numColumns= 2; 624 stylesComposite.setLayout(layout); 625 stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); 626 627 fEnableCheckbox= new Button(stylesComposite, SWT.CHECK); 628 fEnableCheckbox.setText(PreferencesMessages.JavaEditorPreferencePage_enable); 629 gd= new GridData(GridData.FILL_HORIZONTAL); 630 gd.horizontalAlignment= GridData.BEGINNING; 631 gd.horizontalSpan= 2; 632 fEnableCheckbox.setLayoutData(gd); 633 634 fColorEditorLabel= new Label(stylesComposite, SWT.LEFT); 635 fColorEditorLabel.setText(PreferencesMessages.JavaEditorPreferencePage_color); 636 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 637 gd.horizontalIndent= 20; 638 fColorEditorLabel.setLayoutData(gd); 639 640 fSyntaxForegroundColorEditor= new ColorSelector(stylesComposite); 641 Button foregroundColorButton= fSyntaxForegroundColorEditor.getButton(); 642 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 643 foregroundColorButton.setLayoutData(gd); 644 645 fBoldCheckBox= new Button(stylesComposite, SWT.CHECK); 646 fBoldCheckBox.setText(PreferencesMessages.JavaEditorPreferencePage_bold); 647 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 648 gd.horizontalIndent= 20; 649 gd.horizontalSpan= 2; 650 fBoldCheckBox.setLayoutData(gd); 651 652 fItalicCheckBox= new Button(stylesComposite, SWT.CHECK); 653 fItalicCheckBox.setText(PreferencesMessages.JavaEditorPreferencePage_italic); 654 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 655 gd.horizontalIndent= 20; 656 gd.horizontalSpan= 2; 657 fItalicCheckBox.setLayoutData(gd); 658 659 fStrikethroughCheckBox= new Button(stylesComposite, SWT.CHECK); 660 fStrikethroughCheckBox.setText(PreferencesMessages.JavaEditorPreferencePage_strikethrough); 661 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 662 gd.horizontalIndent= 20; 663 gd.horizontalSpan= 2; 664 fStrikethroughCheckBox.setLayoutData(gd); 665 666 fUnderlineCheckBox= new Button(stylesComposite, SWT.CHECK); 667 fUnderlineCheckBox.setText(PreferencesMessages.JavaEditorPreferencePage_underline); 668 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 669 gd.horizontalIndent= 20; 670 gd.horizontalSpan= 2; 671 fUnderlineCheckBox.setLayoutData(gd); 672 673 label= new Label(colorComposite, SWT.LEFT); 674 label.setText(PreferencesMessages.JavaEditorPreferencePage_preview); 675 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 676 677 Control previewer= createPreviewer(colorComposite); 678 gd= new GridData(GridData.FILL_BOTH); 679 gd.widthHint= convertWidthInCharsToPixels(20); 680 gd.heightHint= convertHeightInCharsToPixels(5); 681 previewer.setLayoutData(gd); 682 683 fListViewer.addSelectionChangedListener(new ISelectionChangedListener() { 684 public void selectionChanged(SelectionChangedEvent event) { 685 handleSyntaxColorListSelection(); 686 } 687 }); 688 689 foregroundColorButton.addSelectionListener(new SelectionListener() { 690 public void widgetDefaultSelected(SelectionEvent e) { 691 } 693 public void widgetSelected(SelectionEvent e) { 694 HighlightingColorListItem item= getHighlightingColorListItem(); 695 PreferenceConverter.setValue(getPreferenceStore(), item.getColorKey(), fSyntaxForegroundColorEditor.getColorValue()); 696 } 697 }); 698 699 fBoldCheckBox.addSelectionListener(new SelectionListener() { 700 public void widgetDefaultSelected(SelectionEvent e) { 701 } 703 public void widgetSelected(SelectionEvent e) { 704 HighlightingColorListItem item= getHighlightingColorListItem(); 705 getPreferenceStore().setValue(item.getBoldKey(), fBoldCheckBox.getSelection()); 706 } 707 }); 708 709 fItalicCheckBox.addSelectionListener(new SelectionListener() { 710 public void widgetDefaultSelected(SelectionEvent e) { 711 } 713 public void widgetSelected(SelectionEvent e) { 714 HighlightingColorListItem item= getHighlightingColorListItem(); 715 getPreferenceStore().setValue(item.getItalicKey(), fItalicCheckBox.getSelection()); 716 } 717 }); 718 fStrikethroughCheckBox.addSelectionListener(new SelectionListener() { 719 public void widgetDefaultSelected(SelectionEvent e) { 720 } 722 public void widgetSelected(SelectionEvent e) { 723 HighlightingColorListItem item= getHighlightingColorListItem(); 724 getPreferenceStore().setValue(item.getStrikethroughKey(), fStrikethroughCheckBox.getSelection()); 725 } 726 }); 727 728 fUnderlineCheckBox.addSelectionListener(new SelectionListener() { 729 public void widgetDefaultSelected(SelectionEvent e) { 730 } 732 public void widgetSelected(SelectionEvent e) { 733 HighlightingColorListItem item= getHighlightingColorListItem(); 734 getPreferenceStore().setValue(item.getUnderlineKey(), fUnderlineCheckBox.getSelection()); 735 } 736 }); 737 738 fEnableCheckbox.addSelectionListener(new SelectionListener() { 739 public void widgetDefaultSelected(SelectionEvent e) { 740 } 742 public void widgetSelected(SelectionEvent e) { 743 HighlightingColorListItem item= getHighlightingColorListItem(); 744 if (item instanceof SemanticHighlightingColorListItem) { 745 boolean enable= fEnableCheckbox.getSelection(); 746 getPreferenceStore().setValue(((SemanticHighlightingColorListItem) item).getEnableKey(), enable); 747 fEnableCheckbox.setSelection(enable); 748 fSyntaxForegroundColorEditor.getButton().setEnabled(enable); 749 fColorEditorLabel.setEnabled(enable); 750 fBoldCheckBox.setEnabled(enable); 751 fItalicCheckBox.setEnabled(enable); 752 fStrikethroughCheckBox.setEnabled(enable); 753 fUnderlineCheckBox.setEnabled(enable); 754 uninstallSemanticHighlighting(); 755 installSemanticHighlighting(); 756 } 757 } 758 }); 759 760 colorComposite.layout(false); 761 762 return colorComposite; 763 } 764 765 private void addFiller(Composite composite, int horizontalSpan) { 766 PixelConverter pixelConverter= new PixelConverter(composite); 767 Label filler= new Label(composite, SWT.LEFT ); 768 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 769 gd.horizontalSpan= horizontalSpan; 770 gd.heightHint= pixelConverter.convertHeightInCharsToPixels(1) / 2; 771 filler.setLayoutData(gd); 772 } 773 774 private Control createPreviewer(Composite parent) { 775 776 IPreferenceStore generalTextStore= EditorsUI.getPreferenceStore(); 777 IPreferenceStore store= new ChainedPreferenceStore(new IPreferenceStore[] { getPreferenceStore(), new PreferencesAdapter(createTemporaryCorePreferenceStore()), generalTextStore }); 778 fPreviewViewer= new JavaSourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER, store); 779 SimpleJavaSourceViewerConfiguration configuration= new SimpleJavaSourceViewerConfiguration(fColorManager, store, null, IJavaPartitions.JAVA_PARTITIONING, false); 780 fPreviewViewer.configure(configuration); 781 configuration.handlePropertyChangeEvent(new PropertyChangeEvent(this, JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_4, JavaCore.VERSION_1_5)); 783 Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT); 784 fPreviewViewer.getTextWidget().setFont(font); 785 new JavaSourcePreviewerUpdater(fPreviewViewer, configuration, store); 786 fPreviewViewer.setEditable(false); 787 788 String content= loadPreviewContentFromFile("ColorSettingPreviewCode.txt"); IDocument document= new Document(content); 790 JavaPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING); 791 fPreviewViewer.setDocument(document); 792 793 installSemanticHighlighting(); 794 795 return fPreviewViewer.getControl(); 796 } 797 798 799 private Preferences createTemporaryCorePreferenceStore() { 800 Preferences result= new Preferences(); 801 802 result.setValue(COMPILER_TASK_TAGS, "TASK,TODO"); 804 return result; 805 } 806 807 808 private String loadPreviewContentFromFile(String filename) { 809 String line; 810 String separator= System.getProperty("line.separator"); StringBuffer buffer= new StringBuffer (512); 812 BufferedReader reader= null; 813 try { 814 reader= new BufferedReader (new InputStreamReader (getClass().getResourceAsStream(filename))); 815 while ((line= reader.readLine()) != null) { 816 buffer.append(line); 817 buffer.append(separator); 818 } 819 } catch (IOException io) { 820 JavaPlugin.log(io); 821 } finally { 822 if (reader != null) { 823 try { reader.close(); } catch (IOException e) {} 824 } 825 } 826 return buffer.toString(); 827 } 828 829 830 835 private void installSemanticHighlighting() { 836 if (fSemanticHighlightingManager == null) { 837 fSemanticHighlightingManager= new SemanticHighlightingManager(); 838 fSemanticHighlightingManager.install(fPreviewViewer, fColorManager, getPreferenceStore(), createPreviewerRanges()); 839 } 840 } 841 842 843 848 private void uninstallSemanticHighlighting() { 849 if (fSemanticHighlightingManager != null) { 850 fSemanticHighlightingManager.uninstall(); 851 fSemanticHighlightingManager= null; 852 } 853 } 854 855 856 862 private SemanticHighlightingManager.HighlightedRange[][] createPreviewerRanges() { 863 return new SemanticHighlightingManager.HighlightedRange[][] { 864 { createHighlightedRange( 6, 13, 9, SemanticHighlightings.DEPRECATED_MEMBER), createHighlightedRange( 6, 13, 9, SemanticHighlightings.CLASS), }, 865 { createHighlightedRange( 6, 23, 1, SemanticHighlightings.TYPE_VARIABLE), createHighlightedRange( 6, 23, 1, SemanticHighlightings.TYPE_ARGUMENT), }, 866 { createHighlightedRange( 6, 37, 9, SemanticHighlightings.INTERFACE) }, 867 { createHighlightedRange( 6, 47, 6, SemanticHighlightings.TYPE_ARGUMENT), createHighlightedRange( 6, 47, 6, SemanticHighlightings.CLASS) }, 868 { createHighlightedRange( 7, 6, 5, SemanticHighlightings.ENUM), }, 869 { createHighlightedRange( 7, 14, 3, SemanticHighlightings.STATIC_FINAL_FIELD), createHighlightedRange(7, 14, 3, SemanticHighlightings.STATIC_FIELD), createHighlightedRange(7, 14, 3, SemanticHighlightings.FIELD) }, 870 { createHighlightedRange( 7, 19, 5, SemanticHighlightings.STATIC_FINAL_FIELD), createHighlightedRange(7, 19, 5, SemanticHighlightings.STATIC_FIELD), createHighlightedRange(7, 19, 5, SemanticHighlightings.FIELD) }, 871 { createHighlightedRange( 7, 26, 4, SemanticHighlightings.STATIC_FINAL_FIELD), createHighlightedRange(7, 26, 4, SemanticHighlightings.STATIC_FIELD), createHighlightedRange(7, 26, 4, SemanticHighlightings.FIELD) }, 872 { createHighlightedRange( 9, 8, 6, SemanticHighlightings.CLASS), }, 873 { createHighlightedRange( 9, 15, 11, SemanticHighlightings.STATIC_FIELD), createHighlightedRange(9, 15, 11, SemanticHighlightings.FIELD) }, 874 { createHighlightedRange(11, 9, 1, SemanticHighlightings.TYPE_VARIABLE) }, 875 { createHighlightedRange(11, 11, 5, SemanticHighlightings.FIELD) }, 876 { createHighlightedRange(13, 2, 16, SemanticHighlightings.ANNOTATION) }, 877 { createHighlightedRange(13, 19, 5, SemanticHighlightings.ANNOTATION_ELEMENT_REFERENCE) }, 878 { createHighlightedRange(14, 12, 3, SemanticHighlightings.METHOD_DECLARATION), createHighlightedRange(14, 12, 3, SemanticHighlightings.METHOD) }, 879 { createHighlightedRange(14, 24, 9, SemanticHighlightings.PARAMETER_VARIABLE) }, 880 { createHighlightedRange(15, 2, 14, SemanticHighlightings.ABSTRACT_METHOD_INVOCATION), createHighlightedRange(15, 2, 14, SemanticHighlightings.METHOD) }, 881 { createHighlightedRange(16, 6, 5, SemanticHighlightings.LOCAL_VARIABLE_DECLARATION) }, 882 { createHighlightedRange(16, 16, 8, SemanticHighlightings.INHERITED_METHOD_INVOCATION), createHighlightedRange(16, 16, 8, SemanticHighlightings.METHOD) }, 883 { createHighlightedRange(17, 2, 12, SemanticHighlightings.STATIC_METHOD_INVOCATION), createHighlightedRange(17, 2, 12, SemanticHighlightings.METHOD) }, 884 { createHighlightedRange(18, 9, 3, SemanticHighlightings.METHOD) }, 885 { createHighlightedRange(18, 13, 5, SemanticHighlightings.LOCAL_VARIABLE) }, 886 { createHighlightedRange(18, 22, 9, SemanticHighlightings.AUTOBOXING) }, 887 }; 888 } 889 890 891 901 private HighlightedRange createHighlightedRange(int line, int column, int length, String key) { 902 try { 903 IDocument document= fPreviewViewer.getDocument(); 904 int offset= document.getLineOffset(line) + column; 905 return new HighlightedRange(offset, length, key); 906 } catch (BadLocationException x) { 907 JavaPlugin.log(x); 908 } 909 return null; 910 } 911 912 913 919 private HighlightingColorListItem getHighlightingColorListItem() { 920 IStructuredSelection selection= (IStructuredSelection) fListViewer.getSelection(); 921 Object element= selection.getFirstElement(); 922 if (element instanceof String ) 923 return null; 924 return (HighlightingColorListItem) element; 925 } 926 927 938 private void initializeDialogUnits(Control testControl) { 939 GC gc = new GC(testControl); 941 gc.setFont(JFaceResources.getDialogFont()); 942 fFontMetrics = gc.getFontMetrics(); 943 gc.dispose(); 944 } 945 } 946 | Popular Tags |