Example #1
0
  static void updateStyle(JTextComponent comp, SynthContext context, String prefix) {
    SynthStyle style = context.getStyle();

    Color color = comp.getCaretColor();
    if (color == null || color instanceof UIResource) {
      comp.setCaretColor((Color) style.get(context, prefix + ".caretForeground"));
    }

    Color fg = comp.getForeground();
    if (fg == null || fg instanceof UIResource) {
      fg = style.getColorForState(context, ColorType.TEXT_FOREGROUND);
      if (fg != null) {
        comp.setForeground(fg);
      }
    }

    Object ar = style.get(context, prefix + ".caretAspectRatio");
    if (ar instanceof Number) {
      comp.putClientProperty("caretAspectRatio", ar);
    }

    context.setComponentState(SELECTED | FOCUSED);

    Color s = comp.getSelectionColor();
    if (s == null || s instanceof UIResource) {
      comp.setSelectionColor(style.getColor(context, ColorType.TEXT_BACKGROUND));
    }

    Color sfg = comp.getSelectedTextColor();
    if (sfg == null || sfg instanceof UIResource) {
      comp.setSelectedTextColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
    }

    context.setComponentState(DISABLED);

    Color dfg = comp.getDisabledTextColor();
    if (dfg == null || dfg instanceof UIResource) {
      comp.setDisabledTextColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
    }

    Insets margin = comp.getMargin();
    if (margin == null || margin instanceof UIResource) {
      margin = (Insets) style.get(context, prefix + ".margin");

      if (margin == null) {
        // Some places assume margins are non-null.
        margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
      }
      comp.setMargin(margin);
    }

    Caret caret = comp.getCaret();
    if (caret instanceof UIResource) {
      Object o = style.get(context, prefix + ".caretBlinkRate");
      if (o != null && o instanceof Integer) {
        Integer rate = (Integer) o;
        caret.setBlinkRate(rate.intValue());
      }
    }
  }
    /**
     * Paints a highlight.
     *
     * @param g the graphics context
     * @param offs0 the starting model offset >= 0
     * @param offs1 the ending model offset >= offs1
     * @param bounds the bounding box for the highlight
     * @param c the editor
     */
    public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
      Rectangle alloc = bounds.getBounds();
      try {
        // --- determine locations ---
        TextUI mapper = c.getUI();
        Rectangle p0 = mapper.modelToView(c, offs0);
        Rectangle p1 = mapper.modelToView(c, offs1);

        // --- render ---
        Color color = getColor();

        if (color == null) {
          g.setColor(c.getSelectionColor());
        } else {
          g.setColor(color);
        }
        if (p0.y == p1.y) {
          // same line, render a rectangle
          Rectangle r = p0.union(p1);
          g.fillRect(r.x, r.y, r.width, r.height);
        } else {
          // different lines
          int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
          g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
          if ((p0.y + p0.height) != p1.y) {
            g.fillRect(alloc.x, p0.y + p0.height, alloc.width, p1.y - (p0.y + p0.height));
          }
          g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
        }
      } catch (BadLocationException e) {
        // can't render
      }
    }
    /**
     * Paints a portion of a highlight.
     *
     * @param g the graphics context
     * @param offs0 the starting model offset >= 0
     * @param offs1 the ending model offset >= offs1
     * @param bounds the bounding box of the view, which is not necessarily the region to paint.
     * @param c the editor
     * @param view View painting for
     * @return region drawing occurred in
     */
    public Shape paintLayer(
        Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
      Color color = getColor();

      if (color == null) {
        g.setColor(c.getSelectionColor());
      } else {
        g.setColor(color);
      }

      Rectangle r;

      if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset()) {
        // Contained in view, can just use bounds.
        if (bounds instanceof Rectangle) {
          r = (Rectangle) bounds;
        } else {
          r = bounds.getBounds();
        }
      } else {
        // Should only render part of View.
        try {
          // --- determine locations ---
          Shape shape =
              view.modelToView(offs0, Position.Bias.Forward, offs1, Position.Bias.Backward, bounds);
          r = (shape instanceof Rectangle) ? (Rectangle) shape : shape.getBounds();
        } catch (BadLocationException e) {
          // can't render
          r = null;
        }
      }

      if (r != null) {
        // If we are asked to highlight, we should draw something even
        // if the model-to-view projection is of zero width (6340106).
        r.width = Math.max(r.width, 1);

        g.fillRect(r.x, r.y, r.width, r.height);
      }

      return r;
    }
 /** Returns the text editor's highlight color. */
 protected Color getHighlightColor() {
   JTextComponent textComp = (JTextComponent) fContainer;
   return textComp.getSelectionColor();
 }