001: /*
002: * LineNumberPanel2.java
003: *
004: * Created on pirmdiena, 2005, 12 decembris, 15:49
005: * Created by Andrejs Grave (agrave@inbox.lv)
006: *
007: */
008:
009: package org.dbbrowser.ui.widget;
010:
011: import java.awt.*;
012: import java.awt.event.*;
013: import javax.swing.*;
014: import javax.swing.event.*;
015: import javax.swing.text.*;
016:
017: /**
018: *
019: * @author andrgrav
020: */
021: public class LineNumberPanel extends JPanel implements DocumentListener {
022: private final static Color DEFAULT_BACKGROUND = new Color(224, 224,
023: 224);
024: private final static int MARGIN = 5;
025: private final static int INITIAL_LINE_COUNT = 15;
026:
027: private JTextComponent theTextComponent;
028: private FontMetrics theFontMetrics;
029: private int currentRowWidth;
030:
031: public LineNumberPanel(JTextComponent aTextComponent) {
032: theTextComponent = aTextComponent;
033: theTextComponent.getDocument().addDocumentListener(this );
034: setOpaque(true);
035: setBackground(DEFAULT_BACKGROUND);
036: setFont(theTextComponent.getFont());
037: theFontMetrics = getFontMetrics(getFont());
038: setForeground(theTextComponent.getForeground());
039: currentRowWidth = getDesiredRowWidth();
040: }
041:
042: private void update() {
043: int desiredRowWidth = getDesiredRowWidth();
044: if (desiredRowWidth != currentRowWidth) {
045: currentRowWidth = desiredRowWidth;
046: revalidate();
047: }
048: repaint();
049: }
050:
051: private int getRowHeight() {
052: return (theFontMetrics.getHeight() + theFontMetrics
053: .getDescent());
054: }
055:
056: private int getDesiredRowWidth() {
057: Document doc = theTextComponent.getDocument();
058: int length = doc.getLength();
059: Element map = doc.getDefaultRootElement();
060: int nbLines = map.getElementIndex(length) + INITIAL_LINE_COUNT;
061: return theFontMetrics.stringWidth(Integer.toString(nbLines));
062: }
063:
064: public void paintComponent(Graphics g) {
065: super .paintComponent(g);
066: try {
067: Document doc = theTextComponent.getDocument();
068: int length = doc.getLength();
069: Element map = doc.getDefaultRootElement();
070: int startLine = map.getElementIndex(0);
071: int endline = map.getElementIndex(length);
072: endline = (endline > INITIAL_LINE_COUNT) ? endline
073: : INITIAL_LINE_COUNT;
074: int y = theTextComponent.modelToView(map.getElement(
075: startLine).getStartOffset()).y
076: + theFontMetrics.getHeight()
077: - theFontMetrics.getDescent();
078:
079: for (int line = startLine; line <= endline; line++) {
080: String s = Integer.toString(line + 1);
081: int width = theFontMetrics.stringWidth(s);
082: g.drawString(s, MARGIN + currentRowWidth - width, y);
083: y += theFontMetrics.getHeight();
084: }
085: } catch (Exception e) {
086: e.printStackTrace();
087: }
088: }
089:
090: public Dimension getPreferredSize() {
091: return new Dimension(2 * MARGIN + currentRowWidth,
092: theTextComponent.getHeight());
093: }
094:
095: public void insertUpdate(DocumentEvent e) {
096: update();
097: }
098:
099: public void removeUpdate(DocumentEvent e) {
100: update();
101: }
102:
103: public void changedUpdate(DocumentEvent e) {
104: update();
105: }
106: }
|