예제 #1
0
  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;
  }
  /**
   * Creates a source viewer information control with the given shell as parent. The given shell
   * styles are applied to the created shell. The given styles are applied to the created styled
   * text widget. The text widget will be initialized with the given font. The status field will
   * contain the given text or be hidden.
   *
   * @param parent the parent shell
   * @param isResizable <code>true</code> if resizable
   * @param symbolicFontName the symbolic font name
   * @param statusFieldText the text to be used in the optional status field or <code>null</code> if
   *     the status field should be hidden
   */
  public SourceViewerInformationControl(
      Shell parent, boolean isResizable, String symbolicFontName, String statusFieldText) {
    GridLayout layout;
    GridData gd;

    int shellStyle = SWT.TOOL | SWT.ON_TOP | (isResizable ? SWT.RESIZE : 0);
    int textStyle = isResizable ? SWT.V_SCROLL | SWT.H_SCROLL : SWT.NONE;

    fShell = new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | shellStyle);
    Display display = fShell.getDisplay();

    Composite composite = fShell;
    layout = new GridLayout(1, false);
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    composite.setLayout(layout);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    composite.setLayoutData(gd);

    if (statusFieldText != null) {
      composite = new Composite(composite, SWT.NONE);
      layout = new GridLayout(1, false);
      layout.marginHeight = 0;
      layout.marginWidth = 0;
      composite.setLayout(layout);
      gd = new GridData(GridData.FILL_BOTH);
      composite.setLayoutData(gd);
      composite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
      composite.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    }

    // Source viewer
    fViewer = new SourceViewer(composite, null, textStyle);
    fViewer.configure(new SourceViewerConfiguration());
    fViewer.setEditable(false);

    fText = fViewer.getTextWidget();
    gd = new GridData(GridData.BEGINNING | GridData.FILL_BOTH);
    fText.setLayoutData(gd);
    fText.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
    fText.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    fSymbolicFontName = symbolicFontName;
    fTextFont = JFaceResources.getFont(symbolicFontName);
    fText.setFont(fTextFont);

    fText.addKeyListener(
        new KeyListener() {

          public void keyPressed(KeyEvent e) {
            if (e.character == 0x1B) // ESC
            fShell.dispose();
          }

          public void keyReleased(KeyEvent e) {}
        });

    // Status field
    if (statusFieldText != null) {

      // Horizontal separator line
      fSeparator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT);
      fSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      // Status field label
      fStatusField = new Label(composite, SWT.RIGHT);
      fStatusField.setText(statusFieldText);
      Font font = fStatusField.getFont();
      FontData[] fontDatas = font.getFontData();
      for (int i = 0; i < fontDatas.length; i++)
        fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10);
      fStatusTextFont = new Font(fStatusField.getDisplay(), fontDatas);
      fStatusField.setFont(fStatusTextFont);
      GridData gd2 =
          new GridData(
              GridData.FILL_VERTICAL
                  | GridData.FILL_HORIZONTAL
                  | GridData.HORIZONTAL_ALIGN_BEGINNING
                  | GridData.VERTICAL_ALIGN_BEGINNING);
      fStatusField.setLayoutData(gd2);

      fStatusTextForegroundColor =
          new Color(
              fStatusField.getDisplay(),
              blend(
                  display.getSystemColor(SWT.COLOR_INFO_BACKGROUND).getRGB(),
                  display.getSystemColor(SWT.COLOR_INFO_FOREGROUND).getRGB(),
                  0.56f));
      fStatusField.setForeground(fStatusTextForegroundColor);

      fStatusField.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    }

    addDisposeListener(this);
  }