public SyntaxHighlighterPane()

in src/syntaxhighlight/SyntaxHighlighterPane.java [105:280]


  public SyntaxHighlighterPane() {
    super();

    setEditable(false);
    //<editor-fold defaultstate="collapsed" desc="editor kit">
    setEditorKit(new StyledEditorKit() {

      private static final long serialVersionUID = 1L;

      @Override
      public ViewFactory getViewFactory() {
        return new ViewFactory() {

          @Override
          public View create(Element elem) {
            String kind = elem.getName();
            if (kind != null) {
              if (kind.equals(AbstractDocument.ContentElementName)) {
                return new LabelView(elem) {

                  @Override
                  public int getBreakWeight(int axis, float pos, float len) {
                    return 0;
                  }
                };
              } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                return new ParagraphView(elem) {

                  @Override
                  public int getBreakWeight(int axis, float pos, float len) {
                    return 0;
                  }
                };
              } else if (kind.equals(AbstractDocument.SectionElementName)) {
                return new BoxView(elem, View.Y_AXIS);
              } else if (kind.equals(StyleConstants.ComponentElementName)) {
                return new ComponentView(elem) {

                  @Override
                  public int getBreakWeight(int axis, float pos, float len) {
                    return 0;
                  }
                };
              } else if (kind.equals(StyleConstants.IconElementName)) {
                return new IconView(elem);
              }
            }
            return new LabelView(elem) {

              @Override
              public int getBreakWeight(int axis, float pos, float len) {
                return 0;
              }
            };
          }
        };
      }
    });
    //</editor-fold>

    lineNumberOffset = 0;

    //<editor-fold defaultstate="collapsed" desc="highlighter painter">
    highlightedBackground = Color.black;
    highlightWhenMouseOver = true;
    highlightedLineList = new ArrayList<Integer>();

    highlightPainter = new Highlighter.HighlightPainter() {

      @Override
      public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
        if (c.getParent() == null) {
          return;
        }

        // get the Y-axis value of the visible area of the text component
        int startY = Math.abs(c.getY());
        int endY = startY + c.getParent().getHeight();

        FontMetrics textPaneFontMetrics = g.getFontMetrics(getFont());
        int textPaneFontHeight = textPaneFontMetrics.getHeight();

        int largerestLineNumber = c.getDocument().getDefaultRootElement().getElementCount();

        g.setColor(highlightedBackground);

        // draw the highlight background to the highlighted line
        synchronized (highlightedLineList) {
          for (Integer lineNumber : highlightedLineList) {
            if (lineNumber > largerestLineNumber + lineNumberOffset) {
              // skip those line number that out of range
              continue;
            }
            // get the Y-axis value of this highlighted line
            int _y = Math.max(0, textPaneFontHeight * (lineNumber - lineNumberOffset - 1));
            if (_y > endY || _y + textPaneFontHeight < startY) {
              // this line is out of visible area, skip it
              continue;
            }
            // draw the highlighted background
            g.fillRect(0, _y, c.getWidth(), textPaneFontHeight);
          }
        }

        // draw the mouse-over-highlight effect
        if (mouseOnLine != -1) {
          if (mouseOnLine <= largerestLineNumber + lineNumberOffset) {
            int _y = Math.max(0, textPaneFontHeight * (mouseOnLine - lineNumberOffset - 1));
            if (_y < endY && _y + textPaneFontHeight > startY) {
              // the line is within the range of visible area
              g.fillRect(0, _y, c.getWidth(), textPaneFontHeight);
            }
          }
        }
      }
    };
    try {
      getHighlighter().addHighlight(0, 0, highlightPainter);
    } catch (BadLocationException ex) {
      LOG.log(Level.SEVERE, null, ex);
    }
    //</editor-fold>

    mouseOnLine = -1;

    //<editor-fold defaultstate="collapsed" desc="mouse listener">
    addMouseListener(new MouseAdapter() {

      @Override
      public void mouseExited(MouseEvent e) {
        if (!highlightWhenMouseOver) {
          return;
        }
        mouseOnLine = -1;
        repaint();
      }
    });
    addMouseMotionListener(new MouseMotionListener() {

      @Override
      public void mouseDragged(MouseEvent e) {
      }

      @Override
      public void mouseMoved(MouseEvent e) {
        if (!highlightWhenMouseOver) {
          return;
        }

        Element defaultRootElement = getDocument().getDefaultRootElement();
        // get the position of the document the mouse cursor is pointing
        int documentOffsetStart = viewToModel(e.getPoint());

        // the line number that the mouse cursor is currently pointing
        int lineNumber = documentOffsetStart == -1 ? -1 : defaultRootElement.getElementIndex(documentOffsetStart) + 1 + lineNumberOffset;
        if (lineNumber == defaultRootElement.getElementCount()) {
          // if the line number got is the last line, check if the cursor is actually on the line or already below the line
          try {
            Rectangle rectangle = modelToView(documentOffsetStart);
            if (e.getY() > rectangle.y + rectangle.height) {
              lineNumber = -1;
            }
          } catch (BadLocationException ex) {
            LOG.log(Level.SEVERE, null, ex);
          }
        }

        // only repaint when the line number changed
        if (mouseOnLine != lineNumber) {
          mouseOnLine = lineNumber;
          repaint();
        }
      }
    });
    //</editor-fold>
  }