@Override
  public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
    Rectangle r0 = null, r1 = null;
    try {
      r0 = c.modelToView(p0);
      r1 = c.modelToView(p1);
    } catch (BadLocationException ex) {
      // Nothing
    }

    if (r0 != null && r1 != null) {
      if (c instanceof JTextField) {
        g.setColor(color);
        g.fillRect(r0.x, r0.y, r1.x - r0.x, r0.height);
        g.setColor(Color.black);
        g.drawString(c.getText().substring(p0, p1), r0.x, r0.y + r0.height - 2);
      } else if (c instanceof JTextArea) {
        String str = c.getText().substring(p0, p1);
        int strMeasure = g.getFontMetrics().stringWidth(str);
        if (r0.x + strMeasure > c.getWidth() - 12) {
          int posX = r0.x, posY = r0.y + r0.height - 3;
          double countLine = 1;

          for (char ch : str.toCharArray()) {
            int chMeasure = g.getFontMetrics().charWidth(ch);
            if (posX + chMeasure > c.getWidth() - 12) {

              Double d1 = new Double(posX + "");
              Double d2 = new Double(strMeasure + "");
              Double d3 = new Double(c.getWidth() + "");

              if ((d1 + d2) / d3 > countLine) {
                posX = 6;
                posY += r0.height;
                countLine += 1.0d;
              }
            }
            g.setColor(color);
            g.fillRect(posX, posY - r0.height - 3, chMeasure, r0.height);
            g.setColor(Color.black);
            g.drawString(ch + "", posX, posY);
            posX += chMeasure;
          }
        } else {
          int RX = r1.x - r0.x < 0 ? strMeasure : r1.x - r0.x;
          g.setColor(color);
          g.fillRect(r0.x, r0.y, RX, r0.height);
          g.setColor(Color.black);
          g.drawString(str, r0.x, r0.y + r0.height - 3);
        }
      }
    }
  }
  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  protected Rectangle getCancelButtonBounds() {
    JTextComponent c = getComponent();
    final int x = c.getWidth() - c.getHeight() / 2 - 9;
    final int y = c.getHeight() / 2 - 8;

    return new Rectangle(x, y, 17, 17);
  }
 public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
   try {
     if (c.getSelectionStart() == c.getSelectionEnd()) { // if no
       // selection
       Rectangle r = c.modelToView(c.getCaretPosition());
       g.setColor(col);
       g.fillRect(0, r.y, c.getWidth(), r.height);
     }
   } catch (BadLocationException ignore) {
   }
 }
  protected void paintBackgroundSafely(final Graphics g) {
    final JTextComponent c = getComponent();
    final int width = c.getWidth();
    final int height = c.getHeight();

    // a delegate takes precedence
    if (delegate != null) {
      delegate.paint(c, g, 0, 0, width, height);
      return;
    }

    final boolean isOpaque = c.isOpaque();
    if (!(c.getBorder() instanceof AquaTextFieldBorder)) {
      // developer must have set a custom border
      if (!isOpaque && AquaUtils.hasOpaqueBeenExplicitlySet(c)) return;

      // must fill whole region with background color if opaque
      g.setColor(c.getBackground());
      g.fillRect(0, 0, width, height);
      return;
    }

    // using our own border
    g.setColor(c.getBackground());
    if (isOpaque) {
      g.fillRect(0, 0, width, height);
      return;
    }

    final Insets margin = c.getMargin();
    Insets insets = c.getInsets();

    if (insets == null) insets = new Insets(0, 0, 0, 0);
    if (margin != null) {
      insets.top -= margin.top;
      insets.left -= margin.left;
      insets.bottom -= margin.bottom;
      insets.right -= margin.right;
    }

    // the common case
    final int shrinkage = AquaTextFieldBorder.getShrinkageFor(c, height);
    g.fillRect(
        insets.left - 2,
        insets.top - shrinkage - 1,
        width - insets.right - insets.left + 4,
        height - insets.bottom - insets.top + shrinkage * 2 + 2);
  }
  /**
   * @param sep the pane where to display the LaTeX
   * @param exp the expression
   * @param b the beginning of the expression
   * @param e the end of the expression
   */
  public static int displayExpression(
      JTextComponent tc, int totalHeight, String exp, int b, int e) {
    String latex = exp;
    if (exp.startsWith("$") || exp.startsWith("\"$") || exp.startsWith("'$")) {
      int n = 1;
      // We have $...$ or "$...$"
      if (exp.charAt(0) != '$') {
        n = 2;
      }
      latex = exp.substring(n, exp.length() - n);
    }

    viewer.icon = ScilabSpecialTextUtilities.compilePartialLaTeXExpression(latex, defaultSize);
    if (viewer.icon == null) {
      return 0;
    }

    width = viewer.icon.getIconWidth();
    height = viewer.icon.getIconHeight();
    viewer.setSize(width + 2 * INSET, height + 2 * INSET);
    tc.add(viewer);
    Rectangle begin;
    Rectangle end;
    try {
      begin = tc.modelToView(b);
      end = tc.modelToView(e);
    } catch (BadLocationException ex) {
      begin = NULLRECT;
      end = NULLRECT;
    }

    int abs;
    int ord;
    if (begin.y == end.y) {
      // We're on the same line
      abs = Math.max(1, (end.x + begin.x - width) / 2);
      ord = begin.y + begin.height + 1;
      if (height + ord > totalHeight) {
        ord = begin.y - 1 - height - 2 * INSET;
      }
    } else if (begin.y + begin.height == end.y) {
      // The line is drawn on two lines
      ord = end.y + end.height + 1;
      if (height + ord > totalHeight) {
        ord = begin.y - 1 - height - 2 * INSET;
        abs = begin.x;
      } else {
        abs = end.x;
      }
    } else {
      // Guess
      abs = Math.max(1, (tc.getWidth() - width) / 2);
      ord = end.y + end.height + 1;
      if (height + ord > totalHeight) {
        ord = begin.y - 1 - height - 2 * INSET;
      }
    }

    viewer.setLocation(abs, ord);
    viewer.setVisible(true);
    viewer.repaint();

    return height + 2 * INSET;
  }