|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +import javax.swing.*; |
| 4 | +import javax.swing.event.*; |
| 5 | +import javax.swing.text.*; |
| 6 | + |
| 7 | +/* |
| 8 | + * Track the movement of the Caret by painting a background line at the |
| 9 | + * current caret position. |
| 10 | + */ |
| 11 | +public class LinePainter |
| 12 | + implements Highlighter.HighlightPainter, CaretListener, MouseListener, MouseMotionListener |
| 13 | +{ |
| 14 | + private JTextComponent component; |
| 15 | + |
| 16 | + private Color color; |
| 17 | + |
| 18 | +// private Rectangle lastView; |
| 19 | + private Rectangle lastView = new Rectangle(0, 0, 5, 5); |
| 20 | + |
| 21 | + /* |
| 22 | + * The line color will be calculated automatically by attempting |
| 23 | + * to make the current selection lighter by a factor of 1.2. |
| 24 | + * |
| 25 | + * @param component text component that requires background line painting |
| 26 | + */ |
| 27 | + public LinePainter(JTextComponent component) |
| 28 | + { |
| 29 | + this(component, null); |
| 30 | + setLighter(component.getSelectionColor()); |
| 31 | + } |
| 32 | + |
| 33 | + /* |
| 34 | + * Manually control the line color |
| 35 | + * |
| 36 | + * @param component text component that requires background line painting |
| 37 | + * @param color the color of the background line |
| 38 | + */ |
| 39 | + public LinePainter(JTextComponent component, Color color) |
| 40 | + { |
| 41 | + this.component = component; |
| 42 | + setColor( color ); |
| 43 | + |
| 44 | + // Add listeners so we know when to change highlighting |
| 45 | + |
| 46 | + component.addCaretListener( this ); |
| 47 | + component.addMouseListener( this ); |
| 48 | + component.addMouseMotionListener( this ); |
| 49 | + |
| 50 | + // Turn highlighting on by adding a dummy highlight |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + component.getHighlighter().addHighlight(0, 0, this); |
| 55 | + } |
| 56 | + catch(BadLocationException ble) {} |
| 57 | + } |
| 58 | + |
| 59 | + /* |
| 60 | + * You can reset the line color at any time |
| 61 | + * |
| 62 | + * @param color the color of the background line |
| 63 | + */ |
| 64 | + public void setColor(Color color) |
| 65 | + { |
| 66 | + this.color = color; |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + * Calculate the line color by making the selection color lighter |
| 71 | + * |
| 72 | + * @return the color of the background line |
| 73 | + */ |
| 74 | + public void setLighter(Color color) |
| 75 | + { |
| 76 | + int red = Math.min(255, (int)(color.getRed() * 1.2)); |
| 77 | + int green = Math.min(255, (int)(color.getGreen() * 1.2)); |
| 78 | + int blue = Math.min(255, (int)(color.getBlue() * 1.2)); |
| 79 | + setColor(new Color(red, green, blue)); |
| 80 | + } |
| 81 | + |
| 82 | + // Paint the background highlight |
| 83 | + |
| 84 | + public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + Rectangle r = c.modelToView(c.getCaretPosition()); |
| 89 | + g.setColor( color ); |
| 90 | + g.fillRect(0, r.y, c.getWidth(), r.height); |
| 91 | + |
| 92 | + if (lastView == null) |
| 93 | + lastView = r; |
| 94 | + } |
| 95 | + catch(BadLocationException ble) {System.out.println(ble);} |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * Caret position has changed, remove the highlight |
| 100 | + */ |
| 101 | + private void resetHighlight() |
| 102 | + { |
| 103 | + // Use invokeLater to make sure updates to the Document are completed, |
| 104 | + // otherwise Undo processing causes the modelToView method to loop. |
| 105 | + |
| 106 | + SwingUtilities.invokeLater(new Runnable() |
| 107 | + { |
| 108 | + public void run() |
| 109 | + { |
| 110 | + try |
| 111 | + { |
| 112 | + int offset = component.getCaretPosition(); |
| 113 | + Rectangle currentView = component.modelToView(offset); |
| 114 | + |
| 115 | + // Remove the highlighting from the previously highlighted line |
| 116 | + |
| 117 | + if (lastView.y != currentView.y) |
| 118 | + { |
| 119 | + component.repaint(0, lastView.y, component.getWidth(), lastView.height); |
| 120 | + lastView = currentView; |
| 121 | + } |
| 122 | + } |
| 123 | + catch(BadLocationException ble) {} |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + // Implement CaretListener |
| 129 | + |
| 130 | + public void caretUpdate(CaretEvent e) |
| 131 | + { |
| 132 | + resetHighlight(); |
| 133 | + } |
| 134 | + |
| 135 | + // Implement MouseListener |
| 136 | + |
| 137 | + public void mousePressed(MouseEvent e) |
| 138 | + { |
| 139 | + resetHighlight(); |
| 140 | + } |
| 141 | + |
| 142 | + public void mouseClicked(MouseEvent e) {} |
| 143 | + public void mouseEntered(MouseEvent e) {} |
| 144 | + public void mouseExited(MouseEvent e) {} |
| 145 | + public void mouseReleased(MouseEvent e) {} |
| 146 | + |
| 147 | + // Implement MouseMotionListener |
| 148 | + |
| 149 | + public void mouseDragged(MouseEvent e) |
| 150 | + { |
| 151 | + resetHighlight(); |
| 152 | + } |
| 153 | + |
| 154 | + public void mouseMoved(MouseEvent e) {} |
| 155 | +} |
0 commit comments