1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52: import ;
53:
54:
55: public class DefaultCaret extends Rectangle
56: implements Caret, FocusListener, MouseListener, MouseMotionListener
57: {
58: private static final long serialVersionUID = 228155774675466193L;
59:
60: protected ChangeEvent changeEvent = new ChangeEvent(this);
61: protected EventListenerList listenerList = new EventListenerList();
62:
63: private JTextComponent textComponent;
64:
65: private boolean selectionVisible = true;
66: private int blinkRate = 500;
67: private int dot = 0;
68: private int mark = 0;
69: private Point magicCaretPosition = null;
70: private boolean visible = true;
71: private Object highlightEntry;
72:
73: public void mouseDragged(MouseEvent event)
74: {
75: }
76:
77: public void mouseMoved(MouseEvent event)
78: {
79: }
80:
81: public void mouseClicked(MouseEvent event)
82: {
83: }
84:
85: public void mouseEntered(MouseEvent event)
86: {
87: }
88:
89: public void mouseExited(MouseEvent event)
90: {
91: }
92:
93: public void mousePressed(MouseEvent event)
94: {
95: }
96:
97: public void mouseReleased(MouseEvent event)
98: {
99: }
100:
101: public void focusGained(FocusEvent event)
102: {
103: }
104:
105: public void focusLost(FocusEvent event)
106: {
107: }
108:
109: protected void moveCaret(MouseEvent event)
110: {
111: }
112:
113: protected void positionCaret(MouseEvent event)
114: {
115: }
116:
117: public void deinstall(JTextComponent c)
118: {
119: textComponent.removeFocusListener(this);
120: textComponent.removeMouseListener(this);
121: textComponent.removeMouseMotionListener(this);
122: textComponent = null;
123: }
124:
125: public void install(JTextComponent c)
126: {
127: textComponent = c;
128: textComponent.addFocusListener(this);
129: textComponent.addMouseListener(this);
130: textComponent.addMouseMotionListener(this);
131: repaint();
132: }
133:
134: public void setMagicCaretPosition(Point p)
135: {
136: magicCaretPosition = p;
137: }
138:
139: public Point getMagicCaretPosition()
140: {
141: return magicCaretPosition;
142: }
143:
144: public int getMark()
145: {
146: return mark;
147: }
148:
149: private void handleHighlight()
150: {
151: Highlighter highlighter = textComponent.getHighlighter();
152:
153: if (highlighter == null)
154: return;
155:
156: int p0 = Math.min(dot, mark);
157: int p1 = Math.max(dot, mark);
158:
159: if (selectionVisible && p0 != p1)
160: {
161: try
162: {
163: if (highlightEntry == null)
164: highlightEntry = highlighter.addHighlight(p0, p1, getSelectionPainter());
165: else
166: highlighter.changeHighlight(highlightEntry, p0, p1);
167: }
168: catch (BadLocationException e)
169: {
170:
171: throw new InternalError();
172: }
173: }
174: else
175: {
176: if (highlightEntry != null)
177: {
178: highlighter.removeHighlight(highlightEntry);
179: highlightEntry = null;
180: }
181: }
182: }
183:
184: public void setSelectionVisible(boolean v)
185: {
186: if (selectionVisible == v)
187: return;
188:
189: selectionVisible = v;
190: handleHighlight();
191: repaint();
192: }
193:
194: public boolean isSelectionVisible()
195: {
196: return selectionVisible;
197: }
198:
199: protected final void repaint()
200: {
201: if (textComponent != null)
202: textComponent.repaint();
203: }
204:
205: public void paint(Graphics g)
206: {
207: if (textComponent == null)
208: return;
209:
210: int dot = getDot();
211: Rectangle rect = null;
212:
213: try
214: {
215: rect = textComponent.modelToView(dot);
216: }
217: catch (BadLocationException e)
218: {
219:
220: return;
221: }
222:
223: if (rect == null)
224: return;
225:
226:
227:
228:
229:
230: if (visible)
231: {
232: g.setColor(textComponent.getCaretColor());
233: g.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height);
234: }
235: }
236:
237: public EventListener[] getListeners(Class listenerType)
238: {
239: return listenerList.getListeners(listenerType);
240: }
241:
242: public void addChangeListener(ChangeListener listener)
243: {
244: listenerList.add(ChangeListener.class, listener);
245: }
246:
247: public void removeChangeListener(ChangeListener listener)
248: {
249: listenerList.remove(ChangeListener.class, listener);
250: }
251:
252: public ChangeListener[] getChangeListeners()
253: {
254: return (ChangeListener[]) getListeners(ChangeListener.class);
255: }
256:
257: protected void fireStateChanged()
258: {
259: ChangeListener[] listeners = getChangeListeners();
260:
261: for (int index = 0; index < listeners.length; ++index)
262: listeners[index].stateChanged(changeEvent);
263: }
264:
265: protected final JTextComponent getComponent()
266: {
267: return textComponent;
268: }
269:
270: public int getBlinkRate()
271: {
272: return blinkRate;
273: }
274:
275: public void setBlinkRate(int rate)
276: {
277: blinkRate = rate;
278: }
279:
280: public int getDot()
281: {
282: return dot;
283: }
284:
285: public void moveDot(int dot)
286: {
287: this.dot = dot;
288: handleHighlight();
289: repaint();
290: }
291:
292: public void setDot(int dot)
293: {
294: this.dot = dot;
295: this.mark = dot;
296: handleHighlight();
297: repaint();
298: }
299:
300: public boolean isVisible()
301: {
302: return visible;
303: }
304:
305: public void setVisible(boolean v)
306: {
307: visible = v;
308: repaint();
309: }
310:
311: protected Highlighter.HighlightPainter getSelectionPainter()
312: {
313: return DefaultHighlighter.DefaultPainter;
314: }
315: }