void handleSyntaxColorListSelection() {
    final TokenHighlight item = getHighlight();

    fEnableCheckbox.setEnabled(item != null);
    fSyntaxForegroundColorEditor.getButton().setEnabled(item != null);
    fColorEditorLabel.setEnabled(item != null);
    fBoldCheckBox.setEnabled(item != null);
    fItalicCheckBox.setEnabled(item != null);
    fStrikethroughCheckBox.setEnabled(item != null);
    fUnderlineCheckBox.setEnabled(item != null);

    if (item == null) {
      return;
    }

    final HighlightStyle style = item.getStyle(fOverlayStore);
    fSyntaxForegroundColorEditor.setColorValue(style.getColor());
    fBoldCheckBox.setSelection(style.hasStyle(SWT.BOLD));
    fItalicCheckBox.setSelection(style.hasStyle(SWT.ITALIC));
    fStrikethroughCheckBox.setSelection(style.hasStyle(TextAttribute.STRIKETHROUGH));
    fUnderlineCheckBox.setSelection(style.hasStyle(TextAttribute.UNDERLINE));

    // if (item instanceof SemanticHighlightingColorListItem) {
    // fEnableCheckbox.setEnabled(true);
    // final boolean enable = getPreferenceStore().getBoolean(
    // ((SemanticHighlightingColorListItem) item).getEnableKey());
    // fEnableCheckbox.setSelection(enable);
    // fSyntaxForegroundColorEditor.getButton().setEnabled(enable);
    // fColorEditorLabel.setEnabled(enable);
    // fBoldCheckBox.setEnabled(enable);
    // fItalicCheckBox.setEnabled(enable);
    // fStrikethroughCheckBox.setEnabled(enable);
    // fUnderlineCheckBox.setEnabled(enable);
    // }
  }
 public void storeHighlight(
     final IPreferenceStore store, final TokenHighlight th, HighlightStyle style) {
   if (store != null) {
     store.setValue(th.getColorKey(), StringConverter.asString(style.getColor()));
     store.setValue(th.getStylesKey(), style.getStyles());
   }
 }
 private void loadColors() {
   fColors = new HashMap<TokenHighlight, HighlightStyle>();
   for (final TokenHighlight th : TokenHighlight.values()) {
     final HighlightStyle data = new HighlightStyle();
     data.load(COLORS_QUALIFIER + th.getName(), th.getDefaultData());
     fColors.put(th, data);
   }
 }
Beispiel #4
0
  private void loadDefaultEditorColors() {
    final IPreferenceStore rootStore = getPreferenceStore();

    for (final TokenHighlight th : TokenHighlight.values()) {
      final HighlightStyle data = th.getDefaultStyle();
      rootStore.setDefault(th.getColorKey(), StringConverter.asString(data.getColor()));
      rootStore.setDefault(th.getStylesKey(), data.getStyles());
    }
  }
 private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
   final List<OverlayPreferenceStore.OverlayKey> overlayKeys = Lists.newArrayList();
   for (final TokenHighlight item : TokenHighlight.values()) {
     overlayKeys.add(
         new OverlayPreferenceStore.OverlayKey(
             OverlayPreferenceStore.TypeDescriptor.STRING, item.getColorKey()));
     overlayKeys.add(
         new OverlayPreferenceStore.OverlayKey(
             OverlayPreferenceStore.TypeDescriptor.INT, item.getStylesKey()));
   }
   final OverlayPreferenceStore.OverlayKey[] keys =
       new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
   overlayKeys.toArray(keys);
   return keys;
 }
  public static SourceViewer createErlangPreviewer(
      final Composite parent,
      IColorManager colorManager,
      Map<TokenHighlight, HighlightStyle> colors,
      final String content) {
    // TODO we should move this method, to a utility class (or maybe create
    // an ErlangPreviewSourceViewer class)
    if (colorManager == null) {
      colorManager = new ColorManager();
    }
    if (colors == null) {
      colors = new HashMap<TokenHighlight, HighlightStyle>();
      for (final TokenHighlight th : TokenHighlight.values()) {
        colors.put(th, th.getDefaultData());
      }
    }

    final IPreferenceStore generalTextStore = EditorsUI.getPreferenceStore();
    final IPreferenceStore store =
        new ChainedPreferenceStore(
            new IPreferenceStore[] {
              ErlideUIPlugin.getDefault().getPreferenceStore(), generalTextStore
            });

    final SourceViewer viewer =
        new SourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
    final IDocument document = new Document(content);
    viewer.setDocument(document);

    final ErlangDocumentSetupParticipant setupParticipant = new ErlangDocumentSetupParticipant();
    setupParticipant.setup(document);

    final TextSourceViewerConfiguration configuration =
        new SyntaxColorPreviewEditorConfiguration(store, colorManager, colors);
    viewer.configure(configuration);

    final Font font = JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
    viewer.getTextWidget().setFont(font);
    new ErlangSourceViewerUpdater(viewer, configuration, store);
    viewer.setEditable(false);

    final Cursor arrowCursor =
        viewer.getTextWidget().getDisplay().getSystemCursor(SWT.CURSOR_ARROW);
    viewer.getTextWidget().setCursor(arrowCursor);

    return viewer;
  }
  @Override
  public boolean performOk() {
    for (final TokenHighlight th : fColors.keySet()) {
      final IEclipsePreferences node = new InstanceScope().getNode(COLORS_QUALIFIER + th.getName());
      final HighlightStyle data = fColors.get(th);

      // TODO only if different than default!
      data.store(node);
    }
    return super.performOk();
  }
  /** @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench) */
  @Override
  public void init(final IWorkbench workbench) {
    fColorManager = new ColorManager();
    fOverlayStore =
        new OverlayPreferenceStore(
            ErlideUIPlugin.getDefault().getPreferenceStore(),
            new OverlayPreferenceStore.OverlayKey[] {});
    fOverlayStore.addKeys(createOverlayStoreKeys());
    fOverlayStore.load();
    fOverlayStore.start();

    for (final TokenHighlight th : TokenHighlight.values()) {
      fColors.put(th, null);
    }
  }
  private Control createSyntaxPage(final Composite parent) {
    final Composite colorComposite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    colorComposite.setLayout(layout);

    final Link link = new Link(colorComposite, SWT.NONE);
    link.setText(PreferencesMessages.ErlEditorColoringConfigurationBlock_link);
    link.addSelectionListener(
        new SelectionAdapter() {

          @Override
          public void widgetSelected(final SelectionEvent e) {
            PreferencesUtil.createPreferenceDialogOn(parent.getShell(), e.text, null, null);
          }
        });

    final GridData gridData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    gridData.widthHint = 150;
    gridData.horizontalSpan = 2;
    link.setLayoutData(gridData);

    addFiller(colorComposite, 1);

    Label label;
    label = new Label(colorComposite, SWT.LEFT);
    label.setText(PreferencesMessages.ErlEditorPreferencePage_coloring_element);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    final Composite editorComposite = new Composite(colorComposite, SWT.NONE);
    layout = new GridLayout();
    layout.numColumns = 2;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    editorComposite.setLayout(layout);
    GridData gd = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    editorComposite.setLayoutData(gd);

    fListViewer = new TreeViewer(editorComposite, SWT.SINGLE | SWT.BORDER);
    final Tree tree = fListViewer.getTree();
    final GridData gd_tree = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
    gd_tree.widthHint = 100;
    tree.setLayoutData(gd_tree);
    fListViewer.setLabelProvider(new ColorListLabelProvider());
    fListViewer.setContentProvider(new ColorListContentProvider());
    fListViewer.setInput(fColors);
    fListViewer.setSelection(new StructuredSelection(fErlangCategory));
    fListViewer.setSorter(
        new ViewerSorter() {

          @Override
          public int category(final Object element) {
            // don't sort the top level categories
            if (fErlangCategory.equals(element)) {
              return 0;
            }
            // if (fEdocCategory.equals(element)) {
            // return 1;
            // }
            // to sort semantic settings after partition based ones:
            // if (element instanceof SemanticHighlightingColorListItem)
            // return 1;
            return 0;
          }
        });
    gd = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, true);
    gd.heightHint = convertHeightInCharsToPixels(9);
    int maxWidth = 0;
    for (final TokenHighlight item : fColors.keySet()) {
      maxWidth = Math.max(maxWidth, convertWidthInCharsToPixels(item.getName().length()));
    }
    final ScrollBar vBar = ((Scrollable) fListViewer.getControl()).getVerticalBar();
    if (vBar != null) {
      maxWidth += vBar.getSize().x * 3; // scrollbars and tree
    }
    // indentation guess
    gd.widthHint = maxWidth;

    fListViewer.getControl().setLayoutData(gd);
    fListViewer.expandAll();

    final Composite stylesComposite = new Composite(editorComposite, SWT.NONE);
    layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.numColumns = 2;
    stylesComposite.setLayout(layout);
    stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

    fEnableCheckbox = new Button(stylesComposite, SWT.CHECK);
    fEnableCheckbox.setText(PreferencesMessages.ErlEditorPreferencePage_enable);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalAlignment = GridData.BEGINNING;
    gd.horizontalSpan = 2;
    fEnableCheckbox.setLayoutData(gd);
    // TODO hide this until reworking the dialog
    fEnableCheckbox.setVisible(false);

    fColorEditorLabel = new Label(stylesComposite, SWT.LEFT);
    fColorEditorLabel.setText(PreferencesMessages.ErlEditorPreferencePage_color);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    gd.horizontalIndent = 20;
    fColorEditorLabel.setLayoutData(gd);

    fSyntaxForegroundColorEditor = new ColorSelector(stylesComposite);
    final Button foregroundColorButton = fSyntaxForegroundColorEditor.getButton();
    gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    foregroundColorButton.setLayoutData(gd);
    new Label(stylesComposite, SWT.NONE);

    fBoldCheckBox = new Button(stylesComposite, SWT.CHECK);
    fBoldCheckBox.setText(PreferencesMessages.ErlEditorPreferencePage_bold);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    gd.horizontalIndent = 20;
    gd.horizontalSpan = 2;
    fBoldCheckBox.setLayoutData(gd);

    fItalicCheckBox = new Button(stylesComposite, SWT.CHECK);
    fItalicCheckBox.setText(PreferencesMessages.ErlEditorPreferencePage_italic);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    gd.horizontalIndent = 20;
    gd.horizontalSpan = 2;
    fItalicCheckBox.setLayoutData(gd);

    fStrikethroughCheckBox = new Button(stylesComposite, SWT.CHECK);
    fStrikethroughCheckBox.setText(PreferencesMessages.ErlEditorPreferencePage_strikeout);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    gd.horizontalIndent = 20;
    gd.horizontalSpan = 2;
    fStrikethroughCheckBox.setLayoutData(gd);

    fUnderlineCheckBox = new Button(stylesComposite, SWT.CHECK);
    fUnderlineCheckBox.setText(PreferencesMessages.ErlEditorPreferencePage_underline);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
    gd.horizontalIndent = 20;
    gd.horizontalSpan = 2;
    fUnderlineCheckBox.setLayoutData(gd);

    label = new Label(colorComposite, SWT.LEFT);
    label.setText(
        PreferencesMessages.ErlEditorPreferencePage_preview
            + "\n  - Currently the preview doesn't work, but this dialog does."
            + "\n  - Open files need to be reopened to refresh the coloring.");
    label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

    final String content =
        loadPreviewContentFromFile(getClass(), "ColorSettingPreviewCode.txt"); // $NON-NLS-1$
    fPreviewViewer = createErlangPreviewer(colorComposite, fColorManager, fColors, content);
    final Control previewer = fPreviewViewer.getControl();
    gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = convertWidthInCharsToPixels(20);
    gd.heightHint = convertHeightInCharsToPixels(5);
    gd.grabExcessHorizontalSpace = true;
    previewer.setLayoutData(gd);

    fListViewer.addSelectionChangedListener(
        new ISelectionChangedListener() {

          @Override
          public void selectionChanged(final SelectionChangedEvent event) {
            handleSyntaxColorListSelection();
          }
        });

    foregroundColorButton.addSelectionListener(
        new SelectionListener() {
          @Override
          public void widgetDefaultSelected(final SelectionEvent e) {
            // do nothing
          }

          @Override
          public void widgetSelected(final SelectionEvent e) {
            final TokenHighlight item = getHighlight();
            final HighlightStyle data = fColors.get(item);
            if (data == null) {
              return;
            }
            data.setColor(fSyntaxForegroundColorEditor.getColorValue());
            fPreviewViewer.invalidateTextPresentation();
          }
        });

    fBoldCheckBox.addSelectionListener(
        new SelectionListener() {
          @Override
          public void widgetDefaultSelected(final SelectionEvent e) {
            // do nothing
          }

          @Override
          public void widgetSelected(final SelectionEvent e) {
            final TokenHighlight item = getHighlight();
            final HighlightStyle data = fColors.get(item);
            if (data == null) {
              return;
            }
            data.setStyle(SWT.BOLD, fBoldCheckBox.getSelection());
            fPreviewViewer.invalidateTextPresentation();
          }
        });

    fItalicCheckBox.addSelectionListener(
        new SelectionListener() {
          @Override
          public void widgetDefaultSelected(final SelectionEvent e) {
            // do nothing
          }

          @Override
          public void widgetSelected(final SelectionEvent e) {
            final TokenHighlight item = getHighlight();
            final HighlightStyle data = fColors.get(item);
            if (data == null) {
              return;
            }
            data.setStyle(SWT.ITALIC, fItalicCheckBox.getSelection());
            fPreviewViewer.invalidateTextPresentation();
          }
        });
    fStrikethroughCheckBox.addSelectionListener(
        new SelectionListener() {
          @Override
          public void widgetDefaultSelected(final SelectionEvent e) {
            // do nothing
          }

          @Override
          public void widgetSelected(final SelectionEvent e) {
            final TokenHighlight item = getHighlight();
            final HighlightStyle data = fColors.get(item);
            if (data == null) {
              return;
            }
            data.setStyle(TextAttribute.STRIKETHROUGH, fStrikethroughCheckBox.getSelection());
            fPreviewViewer.invalidateTextPresentation();
          }
        });

    fUnderlineCheckBox.addSelectionListener(
        new SelectionListener() {
          @Override
          public void widgetDefaultSelected(final SelectionEvent e) {
            // do nothing
          }

          @Override
          public void widgetSelected(final SelectionEvent e) {
            final TokenHighlight item = getHighlight();
            final HighlightStyle data = fColors.get(item);
            if (data == null) {
              return;
            }
            data.setStyle(TextAttribute.UNDERLINE, fUnderlineCheckBox.getSelection());
            fPreviewViewer.invalidateTextPresentation();
          }
        });

    fEnableCheckbox.addSelectionListener(
        new SelectionListener() {
          @Override
          public void widgetDefaultSelected(final SelectionEvent e) {
            // do nothing
          }

          @Override
          public void widgetSelected(final SelectionEvent e) {
            fEnableCheckbox.setSelection(true);
            // final TokenHighlight item = getHighlight();
            // if (item instanceof SemanticHighlightingColorListItem) {
            // final boolean enable = fEnableCheckbox.getSelection();
            // getPreferenceStore().setValue(
            // ((SemanticHighlightingColorListItem) item)
            // .getEnableKey(), enable);
            // fEnableCheckbox.setSelection(enable);
            // fSyntaxForegroundColorEditor.getButton().setEnabled(enable);
            // fColorEditorLabel.setEnabled(enable);
            // fBoldCheckBox.setEnabled(enable);
            // fItalicCheckBox.setEnabled(enable);
            // fStrikethroughCheckBox.setEnabled(enable);
            // fUnderlineCheckBox.setEnabled(enable);
            // }
          }
        });

    colorComposite.layout(false);

    return colorComposite;
  }