コード例 #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());
      }
    }
  }
コード例 #2
0
ファイル: OverwritePane.java プロジェクト: mhcrnl/test
  private void initializePane() {
    mode = INSERT;
    addKeyListener(
        new KeyAdapter() {

          public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_INSERT) {
              toggleTypingMode();
            }
          }
        });

    // all editors display another caret when in overwrite mode
    // replace it
    setFont(new Font("Monospaced", Font.PLAIN, 12));
    Caret c =
        new DefaultCaret() {

          public void paint(Graphics g) {
            if (mode == INSERT) {
              super.paint(g);
              return;
            }
            JTextComponent comp = getComponent();

            int dot = getDot();
            Rectangle r = null;
            char c;
            try {
              r = comp.modelToView(dot);
              if (r == null) return;
              c = comp.getText(dot, 1).charAt(0);
            } catch (BadLocationException e) {
              return;
            }

            // erase provious caret
            if ((x != r.x) || (y != r.y)) {
              repaint();
              x = r.x;
              y = r.y;
              height = r.height;
            }

            g.setColor(comp.getCaretColor());
            g.setXORMode(comp.getBackground());

            width = g.getFontMetrics().charWidth(c);
            if (c == '\t' || c == '\n') width = g.getFontMetrics().charWidth(' ');
            if (isVisible()) {
              g.fillRect(r.x, r.y, width, r.height);
            }
          }
        };
    c.setBlinkRate(500); // default rate
    setCaret(c);
  }