45 |
import java.awt.event.MouseEvent; |
import java.awt.event.MouseEvent; |
46 |
import java.awt.event.MouseListener; |
import java.awt.event.MouseListener; |
47 |
import java.awt.event.MouseMotionListener; |
import java.awt.event.MouseMotionListener; |
48 |
|
import java.beans.PropertyChangeEvent; |
49 |
|
import java.beans.PropertyChangeListener; |
50 |
import java.util.EventListener; |
import java.util.EventListener; |
51 |
|
|
52 |
import javax.swing.event.ChangeEvent; |
import javax.swing.event.ChangeEvent; |
53 |
import javax.swing.event.ChangeListener; |
import javax.swing.event.ChangeListener; |
54 |
|
import javax.swing.event.DocumentEvent; |
55 |
|
import javax.swing.event.DocumentListener; |
56 |
import javax.swing.event.EventListenerList; |
import javax.swing.event.EventListenerList; |
57 |
|
|
58 |
/** |
/** |
64 |
public class DefaultCaret extends Rectangle |
public class DefaultCaret extends Rectangle |
65 |
implements Caret, FocusListener, MouseListener, MouseMotionListener |
implements Caret, FocusListener, MouseListener, MouseMotionListener |
66 |
{ |
{ |
67 |
|
/** |
68 |
|
* Listens for changes in the text component's document and updates the |
69 |
|
* caret accordingly. |
70 |
|
* |
71 |
|
* @author Roman Kennke (kennke@aicas.com) |
72 |
|
*/ |
73 |
|
private class DocumentHandler implements DocumentListener |
74 |
|
{ |
75 |
|
/** |
76 |
|
* Receives notification that some text attributes have changed. No action |
77 |
|
* is taken here. |
78 |
|
* |
79 |
|
* @param event the document event |
80 |
|
*/ |
81 |
|
public void changedUpdate(DocumentEvent event) |
82 |
|
{ |
83 |
|
// Nothing to do here. |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
|
* Receives notification that some text has been removed from the text |
88 |
|
* component. The caret is moved backwards accordingly. |
89 |
|
* |
90 |
|
* @param event the document event |
91 |
|
*/ |
92 |
|
public void insertUpdate(DocumentEvent event) |
93 |
|
{ |
94 |
|
int dot = getDot(); |
95 |
|
setDot(dot + event.getLength()); |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
|
* Receives notification that some text has been inserte into the text |
100 |
|
* component. The caret is moved forwards accordingly. |
101 |
|
* |
102 |
|
* @param event the document event |
103 |
|
*/ |
104 |
|
public void removeUpdate(DocumentEvent event) |
105 |
|
{ |
106 |
|
int dot = getDot(); |
107 |
|
setDot(dot - event.getLength()); |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
|
* Listens for property changes on the text document. This is used to add and |
113 |
|
* remove our document listener, if the document of the text component has |
114 |
|
* changed. |
115 |
|
* |
116 |
|
* @author Roman Kennke (kennke@aicas.com) |
117 |
|
*/ |
118 |
|
private class PropertyChangeHandler implements PropertyChangeListener |
119 |
|
{ |
120 |
|
|
121 |
|
/** |
122 |
|
* Receives notification when a property has changed on the text component. |
123 |
|
* This adds/removes our document listener from the text component's |
124 |
|
* document when the document changes. |
125 |
|
* |
126 |
|
* @param e the property change event |
127 |
|
*/ |
128 |
|
public void propertyChange(PropertyChangeEvent e) |
129 |
|
{ |
130 |
|
if (e.getPropertyName().equals("document")) |
131 |
|
{ |
132 |
|
Document oldDoc = (Document) e.getOldValue(); |
133 |
|
oldDoc.removeDocumentListener(documentListener); |
134 |
|
Document newDoc = (Document) e.getNewValue(); |
135 |
|
newDoc.addDocumentListener(documentListener); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
|
} |
140 |
|
|
141 |
/** The serialization UID (compatible with JDK1.5). */ |
/** The serialization UID (compatible with JDK1.5). */ |
142 |
private static final long serialVersionUID = 4325555698756477346L; |
private static final long serialVersionUID = 4325555698756477346L; |
143 |
|
|
152 |
protected EventListenerList listenerList = new EventListenerList(); |
protected EventListenerList listenerList = new EventListenerList(); |
153 |
|
|
154 |
/** |
/** |
155 |
|
* Our document listener. |
156 |
|
*/ |
157 |
|
DocumentListener documentListener; |
158 |
|
|
159 |
|
/** |
160 |
|
* Our property listener. |
161 |
|
*/ |
162 |
|
PropertyChangeListener propertyChangeListener; |
163 |
|
|
164 |
|
/** |
165 |
* The text component in which this caret is installed. |
* The text component in which this caret is installed. |
166 |
*/ |
*/ |
167 |
private JTextComponent textComponent; |
private JTextComponent textComponent; |
339 |
textComponent.removeFocusListener(this); |
textComponent.removeFocusListener(this); |
340 |
textComponent.removeMouseListener(this); |
textComponent.removeMouseListener(this); |
341 |
textComponent.removeMouseMotionListener(this); |
textComponent.removeMouseMotionListener(this); |
342 |
|
textComponent.getDocument().removeDocumentListener(documentListener); |
343 |
|
documentListener = null; |
344 |
|
textComponent.removePropertyChangeListener(propertyChangeListener); |
345 |
|
propertyChangeListener = null; |
346 |
textComponent = null; |
textComponent = null; |
347 |
} |
} |
348 |
|
|
359 |
textComponent.addFocusListener(this); |
textComponent.addFocusListener(this); |
360 |
textComponent.addMouseListener(this); |
textComponent.addMouseListener(this); |
361 |
textComponent.addMouseMotionListener(this); |
textComponent.addMouseMotionListener(this); |
362 |
|
propertyChangeListener = new PropertyChangeHandler(); |
363 |
|
textComponent.addPropertyChangeListener(propertyChangeListener); |
364 |
|
documentListener = new DocumentHandler(); |
365 |
|
textComponent.getDocument().addDocumentListener(documentListener); |
366 |
repaint(); |
repaint(); |
367 |
} |
} |
368 |
|
|