author | prr |
Sat, 19 Sep 2015 15:45:59 -0700 | |
changeset 32865 | f9cb6e427f9e |
parent 32678 | fa523710b4f5 |
child 42216 | 621af0ebf6c4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30462
507bcb03c954
8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents:
26037
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing.text; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.event.*; |
|
29 |
import java.awt.datatransfer.*; |
|
30 |
import java.beans.*; |
|
31 |
import java.awt.event.ActionEvent; |
|
32 |
import java.awt.event.ActionListener; |
|
33 |
import java.io.*; |
|
34 |
import javax.swing.*; |
|
35 |
import javax.swing.event.*; |
|
36 |
import javax.swing.plaf.*; |
|
37 |
import java.util.EventListener; |
|
38 |
import sun.swing.SwingUtilities2; |
|
39 |
||
40 |
/** |
|
41 |
* A default implementation of Caret. The caret is rendered as |
|
42 |
* a vertical line in the color specified by the CaretColor property |
|
43 |
* of the associated JTextComponent. It can blink at the rate specified |
|
44 |
* by the BlinkRate property. |
|
45 |
* <p> |
|
46 |
* This implementation expects two sources of asynchronous notification. |
|
47 |
* The timer thread fires asynchronously, and causes the caret to simply |
|
48 |
* repaint the most recent bounding box. The caret also tracks change |
|
49 |
* as the document is modified. Typically this will happen on the |
|
50 |
* event dispatch thread as a result of some mouse or keyboard event. |
|
51 |
* The caret behavior on both synchronous and asynchronous documents updates |
|
52 |
* is controlled by <code>UpdatePolicy</code> property. The repaint of the |
|
53 |
* new caret location will occur on the event thread in any case, as calls to |
|
54 |
* <code>modelToView</code> are only safe on the event thread. |
|
55 |
* <p> |
|
56 |
* The caret acts as a mouse and focus listener on the text component |
|
57 |
* it has been installed in, and defines the caret semantics based upon |
|
58 |
* those events. The listener methods can be reimplemented to change the |
|
59 |
* semantics. |
|
60 |
* By default, the first mouse button will be used to set focus and caret |
|
61 |
* position. Dragging the mouse pointer with the first mouse button will |
|
62 |
* sweep out a selection that is contiguous in the model. If the associated |
|
63 |
* text component is editable, the caret will become visible when focus |
|
64 |
* is gained, and invisible when focus is lost. |
|
65 |
* <p> |
|
66 |
* The Highlighter bound to the associated text component is used to |
|
67 |
* render the selection by default. |
|
68 |
* Selection appearance can be customized by supplying a |
|
69 |
* painter to use for the highlights. By default a painter is used that |
|
70 |
* will render a solid color as specified in the associated text component |
|
71 |
* in the <code>SelectionColor</code> property. This can easily be changed |
|
72 |
* by reimplementing the |
|
7959 | 73 |
* {@link #getSelectionPainter getSelectionPainter} |
2 | 74 |
* method. |
75 |
* <p> |
|
76 |
* A customized caret appearance can be achieved by reimplementing |
|
77 |
* the paint method. If the paint method is changed, the damage method |
|
78 |
* should also be reimplemented to cause a repaint for the area needed |
|
79 |
* to render the caret. The caret extends the Rectangle class which |
|
80 |
* is used to hold the bounding box for where the caret was last rendered. |
|
81 |
* This enables the caret to repaint in a thread-safe manner when the |
|
82 |
* caret moves without making a call to modelToView which is unstable |
|
83 |
* between model updates and view repair (i.e. the order of delivery |
|
84 |
* to DocumentListeners is not guaranteed). |
|
85 |
* <p> |
|
86 |
* The magic caret position is set to null when the caret position changes. |
|
87 |
* A timer is used to determine the new location (after the caret change). |
|
88 |
* When the timer fires, if the magic caret position is still null it is |
|
89 |
* reset to the current caret position. Any actions that change |
|
90 |
* the caret position and want the magic caret position to remain the |
|
91 |
* same, must remember the magic caret position, change the cursor, and |
|
92 |
* then set the magic caret position to its original value. This has the |
|
93 |
* benefit that only actions that want the magic caret position to persist |
|
94 |
* (such as open/down) need to know about it. |
|
95 |
* <p> |
|
96 |
* <strong>Warning:</strong> |
|
97 |
* Serialized objects of this class will not be compatible with |
|
98 |
* future Swing releases. The current serialization support is |
|
99 |
* appropriate for short term storage or RMI between applications running |
|
100 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 101 |
* of all JavaBeans™ |
2 | 102 |
* has been added to the <code>java.beans</code> package. |
103 |
* Please see {@link java.beans.XMLEncoder}. |
|
104 |
* |
|
105 |
* @author Timothy Prinzing |
|
106 |
* @see Caret |
|
107 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
108 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 109 |
public class DefaultCaret extends Rectangle implements Caret, FocusListener, MouseListener, MouseMotionListener { |
110 |
||
111 |
/** |
|
112 |
* Indicates that the caret position is to be updated only when |
|
113 |
* document changes are performed on the Event Dispatching Thread. |
|
114 |
* @see #setUpdatePolicy |
|
115 |
* @see #getUpdatePolicy |
|
116 |
* @since 1.5 |
|
117 |
*/ |
|
118 |
public static final int UPDATE_WHEN_ON_EDT = 0; |
|
119 |
||
120 |
/** |
|
121 |
* Indicates that the caret should remain at the same |
|
122 |
* absolute position in the document regardless of any document |
|
123 |
* updates, except when the document length becomes less than |
|
124 |
* the current caret position due to removal. In that case the caret |
|
125 |
* position is adjusted to the end of the document. |
|
126 |
* |
|
127 |
* @see #setUpdatePolicy |
|
128 |
* @see #getUpdatePolicy |
|
129 |
* @since 1.5 |
|
130 |
*/ |
|
131 |
public static final int NEVER_UPDATE = 1; |
|
132 |
||
133 |
/** |
|
134 |
* Indicates that the caret position is to be <b>always</b> |
|
135 |
* updated accordingly to the document changes regardless whether |
|
136 |
* the document updates are performed on the Event Dispatching Thread |
|
137 |
* or not. |
|
138 |
* |
|
139 |
* @see #setUpdatePolicy |
|
140 |
* @see #getUpdatePolicy |
|
141 |
* @since 1.5 |
|
142 |
*/ |
|
143 |
public static final int ALWAYS_UPDATE = 2; |
|
144 |
||
145 |
/** |
|
146 |
* Constructs a default caret. |
|
147 |
*/ |
|
148 |
public DefaultCaret() { |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Sets the caret movement policy on the document updates. Normally |
|
153 |
* the caret updates its absolute position within the document on |
|
154 |
* insertions occurred before or at the caret position and |
|
155 |
* on removals before the caret position. 'Absolute position' |
|
156 |
* means here the position relative to the start of the document. |
|
157 |
* For example if |
|
158 |
* a character is typed within editable text component it is inserted |
|
159 |
* at the caret position and the caret moves to the next absolute |
|
160 |
* position within the document due to insertion and if |
|
161 |
* <code>BACKSPACE</code> is typed then caret decreases its absolute |
|
162 |
* position due to removal of a character before it. Sometimes |
|
163 |
* it may be useful to turn off the caret position updates so that |
|
164 |
* the caret stays at the same absolute position within the |
|
165 |
* document position regardless of any document updates. |
|
166 |
* <p> |
|
167 |
* The following update policies are allowed: |
|
168 |
* <ul> |
|
169 |
* <li><code>NEVER_UPDATE</code>: the caret stays at the same |
|
170 |
* absolute position in the document regardless of any document |
|
171 |
* updates, except when document length becomes less than |
|
172 |
* the current caret position due to removal. In that case caret |
|
173 |
* position is adjusted to the end of the document. |
|
174 |
* The caret doesn't try to keep itself visible by scrolling |
|
175 |
* the associated view when using this policy. </li> |
|
176 |
* <li><code>ALWAYS_UPDATE</code>: the caret always tracks document |
|
177 |
* changes. For regular changes it increases its position |
|
178 |
* if an insertion occurs before or at its current position, |
|
179 |
* and decreases position if a removal occurs before |
|
180 |
* its current position. For undo/redo updates it is always |
|
181 |
* moved to the position where update occurred. The caret |
|
182 |
* also tries to keep itself visible by calling |
|
183 |
* <code>adjustVisibility</code> method.</li> |
|
184 |
* <li><code>UPDATE_WHEN_ON_EDT</code>: acts like <code>ALWAYS_UPDATE</code> |
|
185 |
* if the document updates are performed on the Event Dispatching Thread |
|
186 |
* and like <code>NEVER_UPDATE</code> if updates are performed on |
|
187 |
* other thread. </li> |
|
188 |
* </ul> <p> |
|
189 |
* The default property value is <code>UPDATE_WHEN_ON_EDT</code>. |
|
190 |
* |
|
191 |
* @param policy one of the following values : <code>UPDATE_WHEN_ON_EDT</code>, |
|
192 |
* <code>NEVER_UPDATE</code>, <code>ALWAYS_UPDATE</code> |
|
193 |
* @throws IllegalArgumentException if invalid value is passed |
|
194 |
* |
|
195 |
* @see #getUpdatePolicy |
|
196 |
* @see #adjustVisibility |
|
197 |
* @see #UPDATE_WHEN_ON_EDT |
|
198 |
* @see #NEVER_UPDATE |
|
199 |
* @see #ALWAYS_UPDATE |
|
200 |
* |
|
201 |
* @since 1.5 |
|
202 |
*/ |
|
203 |
public void setUpdatePolicy(int policy) { |
|
204 |
updatePolicy = policy; |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Gets the caret movement policy on document updates. |
|
209 |
* |
|
210 |
* @return one of the following values : <code>UPDATE_WHEN_ON_EDT</code>, |
|
211 |
* <code>NEVER_UPDATE</code>, <code>ALWAYS_UPDATE</code> |
|
212 |
* |
|
213 |
* @see #setUpdatePolicy |
|
214 |
* @see #UPDATE_WHEN_ON_EDT |
|
215 |
* @see #NEVER_UPDATE |
|
216 |
* @see #ALWAYS_UPDATE |
|
217 |
* |
|
218 |
* @since 1.5 |
|
219 |
*/ |
|
220 |
public int getUpdatePolicy() { |
|
221 |
return updatePolicy; |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Gets the text editor component that this caret is |
|
226 |
* is bound to. |
|
227 |
* |
|
228 |
* @return the component |
|
229 |
*/ |
|
230 |
protected final JTextComponent getComponent() { |
|
231 |
return component; |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Cause the caret to be painted. The repaint |
|
236 |
* area is the bounding box of the caret (i.e. |
|
237 |
* the caret rectangle or <em>this</em>). |
|
238 |
* <p> |
|
239 |
* This method is thread safe, although most Swing methods |
|
240 |
* are not. Please see |
|
20455
f6f9a0c2796b
8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents:
20170
diff
changeset
|
241 |
* <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency |
10316
8d1ca7d93fb2
7075563: Broken link in "javax.swing.SwingWorker"
rupashka
parents:
9035
diff
changeset
|
242 |
* in Swing</A> for more information. |
2 | 243 |
*/ |
244 |
protected final synchronized void repaint() { |
|
245 |
if (component != null) { |
|
246 |
component.repaint(x, y, width, height); |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Damages the area surrounding the caret to cause |
|
252 |
* it to be repainted in a new location. If paint() |
|
253 |
* is reimplemented, this method should also be |
|
254 |
* reimplemented. This method should update the |
|
255 |
* caret bounds (x, y, width, and height). |
|
256 |
* |
|
257 |
* @param r the current location of the caret |
|
258 |
* @see #paint |
|
259 |
*/ |
|
260 |
protected synchronized void damage(Rectangle r) { |
|
261 |
if (r != null) { |
|
262 |
int damageWidth = getCaretWidth(r.height); |
|
263 |
x = r.x - 4 - (damageWidth >> 1); |
|
264 |
y = r.y; |
|
265 |
width = 9 + damageWidth; |
|
266 |
height = r.height; |
|
267 |
repaint(); |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
/** |
|
272 |
* Scrolls the associated view (if necessary) to make |
|
273 |
* the caret visible. Since how this should be done |
|
274 |
* is somewhat of a policy, this method can be |
|
275 |
* reimplemented to change the behavior. By default |
|
276 |
* the scrollRectToVisible method is called on the |
|
277 |
* associated component. |
|
278 |
* |
|
279 |
* @param nloc the new position to scroll to |
|
280 |
*/ |
|
281 |
protected void adjustVisibility(Rectangle nloc) { |
|
282 |
if(component == null) { |
|
283 |
return; |
|
284 |
} |
|
285 |
if (SwingUtilities.isEventDispatchThread()) { |
|
286 |
component.scrollRectToVisible(nloc); |
|
287 |
} else { |
|
288 |
SwingUtilities.invokeLater(new SafeScroller(nloc)); |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Gets the painter for the Highlighter. |
|
294 |
* |
|
295 |
* @return the painter |
|
296 |
*/ |
|
297 |
protected Highlighter.HighlightPainter getSelectionPainter() { |
|
298 |
return DefaultHighlighter.DefaultPainter; |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Tries to set the position of the caret from |
|
303 |
* the coordinates of a mouse event, using viewToModel(). |
|
304 |
* |
|
305 |
* @param e the mouse event |
|
306 |
*/ |
|
307 |
protected void positionCaret(MouseEvent e) { |
|
308 |
Point pt = new Point(e.getX(), e.getY()); |
|
309 |
Position.Bias[] biasRet = new Position.Bias[1]; |
|
310 |
int pos = component.getUI().viewToModel(component, pt, biasRet); |
|
311 |
if(biasRet[0] == null) |
|
312 |
biasRet[0] = Position.Bias.Forward; |
|
313 |
if (pos >= 0) { |
|
314 |
setDot(pos, biasRet[0]); |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* Tries to move the position of the caret from |
|
320 |
* the coordinates of a mouse event, using viewToModel(). |
|
321 |
* This will cause a selection if the dot and mark |
|
322 |
* are different. |
|
323 |
* |
|
324 |
* @param e the mouse event |
|
325 |
*/ |
|
326 |
protected void moveCaret(MouseEvent e) { |
|
327 |
Point pt = new Point(e.getX(), e.getY()); |
|
328 |
Position.Bias[] biasRet = new Position.Bias[1]; |
|
329 |
int pos = component.getUI().viewToModel(component, pt, biasRet); |
|
330 |
if(biasRet[0] == null) |
|
331 |
biasRet[0] = Position.Bias.Forward; |
|
332 |
if (pos >= 0) { |
|
333 |
moveDot(pos, biasRet[0]); |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
// --- FocusListener methods -------------------------- |
|
338 |
||
339 |
/** |
|
340 |
* Called when the component containing the caret gains |
|
341 |
* focus. This is implemented to set the caret to visible |
|
342 |
* if the component is editable. |
|
343 |
* |
|
344 |
* @param e the focus event |
|
345 |
* @see FocusListener#focusGained |
|
346 |
*/ |
|
347 |
public void focusGained(FocusEvent e) { |
|
348 |
if (component.isEnabled()) { |
|
349 |
if (component.isEditable()) { |
|
350 |
setVisible(true); |
|
351 |
} |
|
352 |
setSelectionVisible(true); |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Called when the component containing the caret loses |
|
358 |
* focus. This is implemented to set the caret to visibility |
|
359 |
* to false. |
|
360 |
* |
|
361 |
* @param e the focus event |
|
362 |
* @see FocusListener#focusLost |
|
363 |
*/ |
|
364 |
public void focusLost(FocusEvent e) { |
|
365 |
setVisible(false); |
|
366 |
setSelectionVisible(ownsSelection || e.isTemporary()); |
|
367 |
} |
|
368 |
||
369 |
||
370 |
/** |
|
371 |
* Selects word based on the MouseEvent |
|
372 |
*/ |
|
373 |
private void selectWord(MouseEvent e) { |
|
374 |
if (selectedWordEvent != null |
|
375 |
&& selectedWordEvent.getX() == e.getX() |
|
376 |
&& selectedWordEvent.getY() == e.getY()) { |
|
377 |
//we already done selection for this |
|
378 |
return; |
|
379 |
} |
|
380 |
Action a = null; |
|
381 |
ActionMap map = getComponent().getActionMap(); |
|
382 |
if (map != null) { |
|
383 |
a = map.get(DefaultEditorKit.selectWordAction); |
|
384 |
} |
|
385 |
if (a == null) { |
|
386 |
if (selectWord == null) { |
|
387 |
selectWord = new DefaultEditorKit.SelectWordAction(); |
|
388 |
} |
|
389 |
a = selectWord; |
|
390 |
} |
|
391 |
a.actionPerformed(new ActionEvent(getComponent(), |
|
392 |
ActionEvent.ACTION_PERFORMED, null, e.getWhen(), e.getModifiers())); |
|
393 |
selectedWordEvent = e; |
|
394 |
} |
|
395 |
||
396 |
// --- MouseListener methods ----------------------------------- |
|
397 |
||
398 |
/** |
|
399 |
* Called when the mouse is clicked. If the click was generated |
|
400 |
* from button1, a double click selects a word, |
|
401 |
* and a triple click the current line. |
|
402 |
* |
|
403 |
* @param e the mouse event |
|
404 |
* @see MouseListener#mouseClicked |
|
405 |
*/ |
|
406 |
public void mouseClicked(MouseEvent e) { |
|
11084
27e89dc55e63
6938583: Unexpected NullPointerException by InputContext.endComposition()
rupashka
parents:
10875
diff
changeset
|
407 |
if (getComponent() == null) { |
27e89dc55e63
6938583: Unexpected NullPointerException by InputContext.endComposition()
rupashka
parents:
10875
diff
changeset
|
408 |
return; |
27e89dc55e63
6938583: Unexpected NullPointerException by InputContext.endComposition()
rupashka
parents:
10875
diff
changeset
|
409 |
} |
27e89dc55e63
6938583: Unexpected NullPointerException by InputContext.endComposition()
rupashka
parents:
10875
diff
changeset
|
410 |
|
2 | 411 |
int nclicks = SwingUtilities2.getAdjustedClickCount(getComponent(), e); |
412 |
||
413 |
if (! e.isConsumed()) { |
|
414 |
if (SwingUtilities.isLeftMouseButton(e)) { |
|
415 |
// mouse 1 behavior |
|
416 |
if(nclicks == 1) { |
|
417 |
selectedWordEvent = null; |
|
418 |
} else if(nclicks == 2 |
|
419 |
&& SwingUtilities2.canEventAccessSystemClipboard(e)) { |
|
420 |
selectWord(e); |
|
421 |
selectedWordEvent = null; |
|
422 |
} else if(nclicks == 3 |
|
423 |
&& SwingUtilities2.canEventAccessSystemClipboard(e)) { |
|
424 |
Action a = null; |
|
425 |
ActionMap map = getComponent().getActionMap(); |
|
426 |
if (map != null) { |
|
427 |
a = map.get(DefaultEditorKit.selectLineAction); |
|
428 |
} |
|
429 |
if (a == null) { |
|
430 |
if (selectLine == null) { |
|
431 |
selectLine = new DefaultEditorKit.SelectLineAction(); |
|
432 |
} |
|
433 |
a = selectLine; |
|
434 |
} |
|
435 |
a.actionPerformed(new ActionEvent(getComponent(), |
|
436 |
ActionEvent.ACTION_PERFORMED, null, e.getWhen(), e.getModifiers())); |
|
437 |
} |
|
438 |
} else if (SwingUtilities.isMiddleMouseButton(e)) { |
|
439 |
// mouse 2 behavior |
|
440 |
if (nclicks == 1 && component.isEditable() && component.isEnabled() |
|
441 |
&& SwingUtilities2.canEventAccessSystemClipboard(e)) { |
|
442 |
// paste system selection, if it exists |
|
443 |
JTextComponent c = (JTextComponent) e.getSource(); |
|
444 |
if (c != null) { |
|
445 |
try { |
|
446 |
Toolkit tk = c.getToolkit(); |
|
447 |
Clipboard buffer = tk.getSystemSelection(); |
|
448 |
if (buffer != null) { |
|
449 |
// platform supports system selections, update it. |
|
450 |
adjustCaret(e); |
|
451 |
TransferHandler th = c.getTransferHandler(); |
|
452 |
if (th != null) { |
|
453 |
Transferable trans = null; |
|
454 |
||
455 |
try { |
|
456 |
trans = buffer.getContents(null); |
|
457 |
} catch (IllegalStateException ise) { |
|
458 |
// clipboard was unavailable |
|
459 |
UIManager.getLookAndFeel().provideErrorFeedback(c); |
|
460 |
} |
|
461 |
||
462 |
if (trans != null) { |
|
463 |
th.importData(c, trans); |
|
464 |
} |
|
465 |
} |
|
466 |
adjustFocus(true); |
|
467 |
} |
|
468 |
} catch (HeadlessException he) { |
|
469 |
// do nothing... there is no system clipboard |
|
470 |
} |
|
471 |
} |
|
472 |
} |
|
473 |
} |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* If button 1 is pressed, this is implemented to |
|
479 |
* request focus on the associated text component, |
|
480 |
* and to set the caret position. If the shift key is held down, |
|
481 |
* the caret will be moved, potentially resulting in a selection, |
|
482 |
* otherwise the |
|
483 |
* caret position will be set to the new location. If the component |
|
484 |
* is not enabled, there will be no request for focus. |
|
485 |
* |
|
486 |
* @param e the mouse event |
|
487 |
* @see MouseListener#mousePressed |
|
488 |
*/ |
|
489 |
public void mousePressed(MouseEvent e) { |
|
490 |
int nclicks = SwingUtilities2.getAdjustedClickCount(getComponent(), e); |
|
491 |
||
492 |
if (SwingUtilities.isLeftMouseButton(e)) { |
|
493 |
if (e.isConsumed()) { |
|
494 |
shouldHandleRelease = true; |
|
495 |
} else { |
|
496 |
shouldHandleRelease = false; |
|
497 |
adjustCaretAndFocus(e); |
|
498 |
if (nclicks == 2 |
|
499 |
&& SwingUtilities2.canEventAccessSystemClipboard(e)) { |
|
500 |
selectWord(e); |
|
501 |
} |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
void adjustCaretAndFocus(MouseEvent e) { |
|
507 |
adjustCaret(e); |
|
508 |
adjustFocus(false); |
|
509 |
} |
|
510 |
||
511 |
/** |
|
512 |
* Adjusts the caret location based on the MouseEvent. |
|
513 |
*/ |
|
514 |
private void adjustCaret(MouseEvent e) { |
|
515 |
if ((e.getModifiers() & ActionEvent.SHIFT_MASK) != 0 && |
|
516 |
getDot() != -1) { |
|
517 |
moveCaret(e); |
|
3977
0da8e3baf0b5
4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents:
1639
diff
changeset
|
518 |
} else if (!e.isPopupTrigger()) { |
2 | 519 |
positionCaret(e); |
520 |
} |
|
521 |
} |
|
522 |
||
523 |
/** |
|
524 |
* Adjusts the focus, if necessary. |
|
525 |
* |
|
526 |
* @param inWindow if true indicates requestFocusInWindow should be used |
|
527 |
*/ |
|
528 |
private void adjustFocus(boolean inWindow) { |
|
529 |
if ((component != null) && component.isEnabled() && |
|
530 |
component.isRequestFocusEnabled()) { |
|
531 |
if (inWindow) { |
|
532 |
component.requestFocusInWindow(); |
|
533 |
} |
|
534 |
else { |
|
535 |
component.requestFocus(); |
|
536 |
} |
|
537 |
} |
|
538 |
} |
|
539 |
||
540 |
/** |
|
541 |
* Called when the mouse is released. |
|
542 |
* |
|
543 |
* @param e the mouse event |
|
544 |
* @see MouseListener#mouseReleased |
|
545 |
*/ |
|
546 |
public void mouseReleased(MouseEvent e) { |
|
547 |
if (!e.isConsumed() |
|
548 |
&& shouldHandleRelease |
|
549 |
&& SwingUtilities.isLeftMouseButton(e)) { |
|
550 |
||
551 |
adjustCaretAndFocus(e); |
|
552 |
} |
|
553 |
} |
|
554 |
||
555 |
/** |
|
556 |
* Called when the mouse enters a region. |
|
557 |
* |
|
558 |
* @param e the mouse event |
|
559 |
* @see MouseListener#mouseEntered |
|
560 |
*/ |
|
561 |
public void mouseEntered(MouseEvent e) { |
|
562 |
} |
|
563 |
||
564 |
/** |
|
565 |
* Called when the mouse exits a region. |
|
566 |
* |
|
567 |
* @param e the mouse event |
|
568 |
* @see MouseListener#mouseExited |
|
569 |
*/ |
|
570 |
public void mouseExited(MouseEvent e) { |
|
571 |
} |
|
572 |
||
573 |
// --- MouseMotionListener methods ------------------------- |
|
574 |
||
575 |
/** |
|
576 |
* Moves the caret position |
|
577 |
* according to the mouse pointer's current |
|
578 |
* location. This effectively extends the |
|
579 |
* selection. By default, this is only done |
|
580 |
* for mouse button 1. |
|
581 |
* |
|
582 |
* @param e the mouse event |
|
583 |
* @see MouseMotionListener#mouseDragged |
|
584 |
*/ |
|
585 |
public void mouseDragged(MouseEvent e) { |
|
586 |
if ((! e.isConsumed()) && SwingUtilities.isLeftMouseButton(e)) { |
|
587 |
moveCaret(e); |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
/** |
|
592 |
* Called when the mouse is moved. |
|
593 |
* |
|
594 |
* @param e the mouse event |
|
595 |
* @see MouseMotionListener#mouseMoved |
|
596 |
*/ |
|
597 |
public void mouseMoved(MouseEvent e) { |
|
598 |
} |
|
599 |
||
600 |
// ---- Caret methods --------------------------------- |
|
601 |
||
602 |
/** |
|
603 |
* Renders the caret as a vertical line. If this is reimplemented |
|
604 |
* the damage method should also be reimplemented as it assumes the |
|
605 |
* shape of the caret is a vertical line. Sets the caret color to |
|
606 |
* the value returned by getCaretColor(). |
|
607 |
* <p> |
|
608 |
* If there are multiple text directions present in the associated |
|
609 |
* document, a flag indicating the caret bias will be rendered. |
|
610 |
* This will occur only if the associated document is a subclass |
|
611 |
* of AbstractDocument and there are multiple bidi levels present |
|
612 |
* in the bidi element structure (i.e. the text has multiple |
|
613 |
* directions associated with it). |
|
614 |
* |
|
615 |
* @param g the graphics context |
|
616 |
* @see #damage |
|
617 |
*/ |
|
618 |
public void paint(Graphics g) { |
|
619 |
if(isVisible()) { |
|
620 |
try { |
|
621 |
TextUI mapper = component.getUI(); |
|
622 |
Rectangle r = mapper.modelToView(component, dot, dotBias); |
|
623 |
||
624 |
if ((r == null) || ((r.width == 0) && (r.height == 0))) { |
|
625 |
return; |
|
626 |
} |
|
627 |
if (width > 0 && height > 0 && |
|
628 |
!this._contains(r.x, r.y, r.width, r.height)) { |
|
629 |
// We seem to have gotten out of sync and no longer |
|
630 |
// contain the right location, adjust accordingly. |
|
631 |
Rectangle clip = g.getClipBounds(); |
|
632 |
||
633 |
if (clip != null && !clip.contains(this)) { |
|
634 |
// Clip doesn't contain the old location, force it |
|
635 |
// to be repainted lest we leave a caret around. |
|
636 |
repaint(); |
|
637 |
} |
|
638 |
// This will potentially cause a repaint of something |
|
639 |
// we're already repainting, but without changing the |
|
640 |
// semantics of damage we can't really get around this. |
|
641 |
damage(r); |
|
642 |
} |
|
643 |
g.setColor(component.getCaretColor()); |
|
644 |
int paintWidth = getCaretWidth(r.height); |
|
645 |
r.x -= paintWidth >> 1; |
|
646 |
g.fillRect(r.x, r.y, paintWidth, r.height); |
|
647 |
||
648 |
// see if we should paint a flag to indicate the bias |
|
649 |
// of the caret. |
|
650 |
// PENDING(prinz) this should be done through |
|
651 |
// protected methods so that alternative LAF |
|
652 |
// will show bidi information. |
|
653 |
Document doc = component.getDocument(); |
|
654 |
if (doc instanceof AbstractDocument) { |
|
655 |
Element bidi = ((AbstractDocument)doc).getBidiRootElement(); |
|
656 |
if ((bidi != null) && (bidi.getElementCount() > 1)) { |
|
657 |
// there are multiple directions present. |
|
658 |
flagXPoints[0] = r.x + ((dotLTR) ? paintWidth : 0); |
|
659 |
flagYPoints[0] = r.y; |
|
660 |
flagXPoints[1] = flagXPoints[0]; |
|
661 |
flagYPoints[1] = flagYPoints[0] + 4; |
|
662 |
flagXPoints[2] = flagXPoints[0] + ((dotLTR) ? 4 : -4); |
|
663 |
flagYPoints[2] = flagYPoints[0]; |
|
664 |
g.fillPolygon(flagXPoints, flagYPoints, 3); |
|
665 |
} |
|
666 |
} |
|
667 |
} catch (BadLocationException e) { |
|
668 |
// can't render I guess |
|
669 |
//System.err.println("Can't render cursor"); |
|
670 |
} |
|
671 |
} |
|
672 |
} |
|
673 |
||
674 |
/** |
|
675 |
* Called when the UI is being installed into the |
|
676 |
* interface of a JTextComponent. This can be used |
|
677 |
* to gain access to the model that is being navigated |
|
678 |
* by the implementation of this interface. Sets the dot |
|
679 |
* and mark to 0, and establishes document, property change, |
|
680 |
* focus, mouse, and mouse motion listeners. |
|
681 |
* |
|
682 |
* @param c the component |
|
683 |
* @see Caret#install |
|
684 |
*/ |
|
685 |
public void install(JTextComponent c) { |
|
686 |
component = c; |
|
687 |
Document doc = c.getDocument(); |
|
688 |
dot = mark = 0; |
|
689 |
dotLTR = markLTR = true; |
|
690 |
dotBias = markBias = Position.Bias.Forward; |
|
691 |
if (doc != null) { |
|
692 |
doc.addDocumentListener(handler); |
|
693 |
} |
|
694 |
c.addPropertyChangeListener(handler); |
|
695 |
c.addFocusListener(this); |
|
696 |
c.addMouseListener(this); |
|
697 |
c.addMouseMotionListener(this); |
|
698 |
||
699 |
// if the component already has focus, it won't |
|
700 |
// be notified. |
|
701 |
if (component.hasFocus()) { |
|
702 |
focusGained(null); |
|
703 |
} |
|
704 |
||
705 |
Number ratio = (Number) c.getClientProperty("caretAspectRatio"); |
|
706 |
if (ratio != null) { |
|
707 |
aspectRatio = ratio.floatValue(); |
|
708 |
} else { |
|
709 |
aspectRatio = -1; |
|
710 |
} |
|
711 |
||
712 |
Integer width = (Integer) c.getClientProperty("caretWidth"); |
|
713 |
if (width != null) { |
|
714 |
caretWidth = width.intValue(); |
|
715 |
} else { |
|
716 |
caretWidth = -1; |
|
717 |
} |
|
718 |
} |
|
719 |
||
720 |
/** |
|
721 |
* Called when the UI is being removed from the |
|
722 |
* interface of a JTextComponent. This is used to |
|
723 |
* unregister any listeners that were attached. |
|
724 |
* |
|
725 |
* @param c the component |
|
726 |
* @see Caret#deinstall |
|
727 |
*/ |
|
728 |
public void deinstall(JTextComponent c) { |
|
729 |
c.removeMouseListener(this); |
|
730 |
c.removeMouseMotionListener(this); |
|
731 |
c.removeFocusListener(this); |
|
732 |
c.removePropertyChangeListener(handler); |
|
733 |
Document doc = c.getDocument(); |
|
734 |
if (doc != null) { |
|
735 |
doc.removeDocumentListener(handler); |
|
736 |
} |
|
737 |
synchronized(this) { |
|
738 |
component = null; |
|
739 |
} |
|
740 |
if (flasher != null) { |
|
741 |
flasher.stop(); |
|
742 |
} |
|
743 |
||
744 |
||
745 |
} |
|
746 |
||
747 |
/** |
|
748 |
* Adds a listener to track whenever the caret position has |
|
749 |
* been changed. |
|
750 |
* |
|
751 |
* @param l the listener |
|
752 |
* @see Caret#addChangeListener |
|
753 |
*/ |
|
754 |
public void addChangeListener(ChangeListener l) { |
|
755 |
listenerList.add(ChangeListener.class, l); |
|
756 |
} |
|
757 |
||
758 |
/** |
|
759 |
* Removes a listener that was tracking caret position changes. |
|
760 |
* |
|
761 |
* @param l the listener |
|
762 |
* @see Caret#removeChangeListener |
|
763 |
*/ |
|
764 |
public void removeChangeListener(ChangeListener l) { |
|
765 |
listenerList.remove(ChangeListener.class, l); |
|
766 |
} |
|
767 |
||
768 |
/** |
|
769 |
* Returns an array of all the change listeners |
|
770 |
* registered on this caret. |
|
771 |
* |
|
772 |
* @return all of this caret's <code>ChangeListener</code>s |
|
773 |
* or an empty |
|
774 |
* array if no change listeners are currently registered |
|
775 |
* |
|
776 |
* @see #addChangeListener |
|
777 |
* @see #removeChangeListener |
|
778 |
* |
|
779 |
* @since 1.4 |
|
780 |
*/ |
|
781 |
public ChangeListener[] getChangeListeners() { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
782 |
return listenerList.getListeners(ChangeListener.class); |
2 | 783 |
} |
784 |
||
785 |
/** |
|
786 |
* Notifies all listeners that have registered interest for |
|
787 |
* notification on this event type. The event instance |
|
788 |
* is lazily created using the parameters passed into |
|
789 |
* the fire method. The listener list is processed last to first. |
|
790 |
* |
|
791 |
* @see EventListenerList |
|
792 |
*/ |
|
793 |
protected void fireStateChanged() { |
|
794 |
// Guaranteed to return a non-null array |
|
795 |
Object[] listeners = listenerList.getListenerList(); |
|
796 |
// Process the listeners last to first, notifying |
|
797 |
// those that are interested in this event |
|
798 |
for (int i = listeners.length-2; i>=0; i-=2) { |
|
799 |
if (listeners[i]==ChangeListener.class) { |
|
800 |
// Lazily create the event: |
|
801 |
if (changeEvent == null) |
|
802 |
changeEvent = new ChangeEvent(this); |
|
803 |
((ChangeListener)listeners[i+1]).stateChanged(changeEvent); |
|
804 |
} |
|
805 |
} |
|
806 |
} |
|
807 |
||
808 |
/** |
|
809 |
* Returns an array of all the objects currently registered |
|
810 |
* as <code><em>Foo</em>Listener</code>s |
|
811 |
* upon this caret. |
|
812 |
* <code><em>Foo</em>Listener</code>s are registered using the |
|
813 |
* <code>add<em>Foo</em>Listener</code> method. |
|
814 |
* |
|
815 |
* <p> |
|
816 |
* |
|
817 |
* You can specify the <code>listenerType</code> argument |
|
818 |
* with a class literal, |
|
819 |
* such as |
|
820 |
* <code><em>Foo</em>Listener.class</code>. |
|
821 |
* For example, you can query a |
|
822 |
* <code>DefaultCaret</code> <code>c</code> |
|
823 |
* for its change listeners with the following code: |
|
824 |
* |
|
825 |
* <pre>ChangeListener[] cls = (ChangeListener[])(c.getListeners(ChangeListener.class));</pre> |
|
826 |
* |
|
827 |
* If no such listeners exist, this method returns an empty array. |
|
30462
507bcb03c954
8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents:
26037
diff
changeset
|
828 |
* @param <T> the listener type |
507bcb03c954
8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents:
26037
diff
changeset
|
829 |
* @param listenerType the type of listeners requested |
2 | 830 |
* @return an array of all objects registered as |
831 |
* <code><em>Foo</em>Listener</code>s on this component, |
|
832 |
* or an empty array if no such |
|
833 |
* listeners have been added |
|
834 |
* @exception ClassCastException if <code>listenerType</code> |
|
835 |
* doesn't specify a class or interface that implements |
|
836 |
* <code>java.util.EventListener</code> |
|
837 |
* |
|
838 |
* @see #getChangeListeners |
|
839 |
* |
|
840 |
* @since 1.3 |
|
841 |
*/ |
|
842 |
public <T extends EventListener> T[] getListeners(Class<T> listenerType) { |
|
843 |
return listenerList.getListeners(listenerType); |
|
844 |
} |
|
845 |
||
846 |
/** |
|
847 |
* Changes the selection visibility. |
|
848 |
* |
|
849 |
* @param vis the new visibility |
|
850 |
*/ |
|
851 |
public void setSelectionVisible(boolean vis) { |
|
852 |
if (vis != selectionVisible) { |
|
853 |
selectionVisible = vis; |
|
854 |
if (selectionVisible) { |
|
855 |
// show |
|
856 |
Highlighter h = component.getHighlighter(); |
|
857 |
if ((dot != mark) && (h != null) && (selectionTag == null)) { |
|
858 |
int p0 = Math.min(dot, mark); |
|
859 |
int p1 = Math.max(dot, mark); |
|
860 |
Highlighter.HighlightPainter p = getSelectionPainter(); |
|
861 |
try { |
|
862 |
selectionTag = h.addHighlight(p0, p1, p); |
|
32678
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
863 |
updateOwnsSelection(); |
2 | 864 |
} catch (BadLocationException bl) { |
865 |
selectionTag = null; |
|
866 |
} |
|
867 |
} |
|
868 |
} else { |
|
869 |
// hide |
|
870 |
if (selectionTag != null) { |
|
871 |
Highlighter h = component.getHighlighter(); |
|
872 |
h.removeHighlight(selectionTag); |
|
873 |
selectionTag = null; |
|
32678
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
874 |
updateOwnsSelection(); |
2 | 875 |
} |
876 |
} |
|
877 |
} |
|
878 |
} |
|
879 |
||
880 |
/** |
|
881 |
* Checks whether the current selection is visible. |
|
882 |
* |
|
883 |
* @return true if the selection is visible |
|
884 |
*/ |
|
885 |
public boolean isSelectionVisible() { |
|
886 |
return selectionVisible; |
|
887 |
} |
|
888 |
||
889 |
/** |
|
890 |
* Determines if the caret is currently active. |
|
891 |
* <p> |
|
892 |
* This method returns whether or not the <code>Caret</code> |
|
893 |
* is currently in a blinking state. It does not provide |
|
894 |
* information as to whether it is currently blinked on or off. |
|
895 |
* To determine if the caret is currently painted use the |
|
896 |
* <code>isVisible</code> method. |
|
897 |
* |
|
898 |
* @return <code>true</code> if active else <code>false</code> |
|
899 |
* @see #isVisible |
|
900 |
* |
|
901 |
* @since 1.5 |
|
902 |
*/ |
|
903 |
public boolean isActive() { |
|
904 |
return active; |
|
905 |
} |
|
906 |
||
907 |
/** |
|
908 |
* Indicates whether or not the caret is currently visible. As the |
|
909 |
* caret flashes on and off the return value of this will change |
|
910 |
* between true, when the caret is painted, and false, when the |
|
911 |
* caret is not painted. <code>isActive</code> indicates whether |
|
912 |
* or not the caret is in a blinking state, such that it <b>can</b> |
|
913 |
* be visible, and <code>isVisible</code> indicates whether or not |
|
914 |
* the caret <b>is</b> actually visible. |
|
915 |
* <p> |
|
916 |
* Subclasses that wish to render a different flashing caret |
|
917 |
* should override paint and only paint the caret if this method |
|
918 |
* returns true. |
|
919 |
* |
|
920 |
* @return true if visible else false |
|
921 |
* @see Caret#isVisible |
|
922 |
* @see #isActive |
|
923 |
*/ |
|
924 |
public boolean isVisible() { |
|
925 |
return visible; |
|
926 |
} |
|
927 |
||
928 |
/** |
|
929 |
* Sets the caret visibility, and repaints the caret. |
|
930 |
* It is important to understand the relationship between this method, |
|
931 |
* <code>isVisible</code> and <code>isActive</code>. |
|
932 |
* Calling this method with a value of <code>true</code> activates the |
|
933 |
* caret blinking. Setting it to <code>false</code> turns it completely off. |
|
934 |
* To determine whether the blinking is active, you should call |
|
935 |
* <code>isActive</code>. In effect, <code>isActive</code> is an |
|
936 |
* appropriate corresponding "getter" method for this one. |
|
937 |
* <code>isVisible</code> can be used to fetch the current |
|
938 |
* visibility status of the caret, meaning whether or not it is currently |
|
939 |
* painted. This status will change as the caret blinks on and off. |
|
940 |
* <p> |
|
941 |
* Here's a list showing the potential return values of both |
|
942 |
* <code>isActive</code> and <code>isVisible</code> |
|
943 |
* after calling this method: |
|
944 |
* <p> |
|
945 |
* <b><code>setVisible(true)</code></b>: |
|
946 |
* <ul> |
|
947 |
* <li>isActive(): true</li> |
|
948 |
* <li>isVisible(): true or false depending on whether |
|
949 |
* or not the caret is blinked on or off</li> |
|
950 |
* </ul> |
|
951 |
* <p> |
|
952 |
* <b><code>setVisible(false)</code></b>: |
|
953 |
* <ul> |
|
954 |
* <li>isActive(): false</li> |
|
955 |
* <li>isVisible(): false</li> |
|
956 |
* </ul> |
|
957 |
* |
|
958 |
* @param e the visibility specifier |
|
959 |
* @see #isActive |
|
960 |
* @see Caret#setVisible |
|
961 |
*/ |
|
962 |
public void setVisible(boolean e) { |
|
963 |
// focus lost notification can come in later after the |
|
964 |
// caret has been deinstalled, in which case the component |
|
965 |
// will be null. |
|
20170
8a39772d6062
7083457: Incomplete specification for javax/swing/text/DefaultCaret.html#setVisible(boolean)
alexsch
parents:
20103
diff
changeset
|
966 |
active = e; |
2 | 967 |
if (component != null) { |
968 |
TextUI mapper = component.getUI(); |
|
969 |
if (visible != e) { |
|
970 |
visible = e; |
|
971 |
// repaint the caret |
|
972 |
try { |
|
973 |
Rectangle loc = mapper.modelToView(component, dot,dotBias); |
|
974 |
damage(loc); |
|
975 |
} catch (BadLocationException badloc) { |
|
976 |
// hmm... not legally positioned |
|
977 |
} |
|
978 |
} |
|
979 |
} |
|
980 |
if (flasher != null) { |
|
981 |
if (visible) { |
|
982 |
flasher.start(); |
|
983 |
} else { |
|
984 |
flasher.stop(); |
|
985 |
} |
|
986 |
} |
|
987 |
} |
|
988 |
||
989 |
/** |
|
990 |
* Sets the caret blink rate. |
|
991 |
* |
|
992 |
* @param rate the rate in milliseconds, 0 to stop blinking |
|
993 |
* @see Caret#setBlinkRate |
|
994 |
*/ |
|
995 |
public void setBlinkRate(int rate) { |
|
996 |
if (rate != 0) { |
|
997 |
if (flasher == null) { |
|
998 |
flasher = new Timer(rate, handler); |
|
999 |
} |
|
1000 |
flasher.setDelay(rate); |
|
1001 |
} else { |
|
1002 |
if (flasher != null) { |
|
1003 |
flasher.stop(); |
|
1004 |
flasher.removeActionListener(handler); |
|
1005 |
flasher = null; |
|
1006 |
} |
|
1007 |
} |
|
1008 |
} |
|
1009 |
||
1010 |
/** |
|
1011 |
* Gets the caret blink rate. |
|
1012 |
* |
|
1013 |
* @return the delay in milliseconds. If this is |
|
1014 |
* zero the caret will not blink. |
|
1015 |
* @see Caret#getBlinkRate |
|
1016 |
*/ |
|
1017 |
public int getBlinkRate() { |
|
1018 |
return (flasher == null) ? 0 : flasher.getDelay(); |
|
1019 |
} |
|
1020 |
||
1021 |
/** |
|
1022 |
* Fetches the current position of the caret. |
|
1023 |
* |
|
1024 |
* @return the position >= 0 |
|
1025 |
* @see Caret#getDot |
|
1026 |
*/ |
|
1027 |
public int getDot() { |
|
1028 |
return dot; |
|
1029 |
} |
|
1030 |
||
1031 |
/** |
|
1032 |
* Fetches the current position of the mark. If there is a selection, |
|
1033 |
* the dot and mark will not be the same. |
|
1034 |
* |
|
1035 |
* @return the position >= 0 |
|
1036 |
* @see Caret#getMark |
|
1037 |
*/ |
|
1038 |
public int getMark() { |
|
1039 |
return mark; |
|
1040 |
} |
|
1041 |
||
1042 |
/** |
|
1043 |
* Sets the caret position and mark to the specified position, |
|
1044 |
* with a forward bias. This implicitly sets the |
|
1045 |
* selection range to zero. |
|
1046 |
* |
|
1047 |
* @param dot the position >= 0 |
|
1048 |
* @see #setDot(int, Position.Bias) |
|
1049 |
* @see Caret#setDot |
|
1050 |
*/ |
|
1051 |
public void setDot(int dot) { |
|
1052 |
setDot(dot, Position.Bias.Forward); |
|
1053 |
} |
|
1054 |
||
1055 |
/** |
|
1056 |
* Moves the caret position to the specified position, |
|
1057 |
* with a forward bias. |
|
1058 |
* |
|
1059 |
* @param dot the position >= 0 |
|
1060 |
* @see #moveDot(int, javax.swing.text.Position.Bias) |
|
1061 |
* @see Caret#moveDot |
|
1062 |
*/ |
|
1063 |
public void moveDot(int dot) { |
|
1064 |
moveDot(dot, Position.Bias.Forward); |
|
1065 |
} |
|
1066 |
||
1067 |
// ---- Bidi methods (we could put these in a subclass) |
|
1068 |
||
1069 |
/** |
|
1070 |
* Moves the caret position to the specified position, with the |
|
1071 |
* specified bias. |
|
1072 |
* |
|
1073 |
* @param dot the position >= 0 |
|
1074 |
* @param dotBias the bias for this position, not <code>null</code> |
|
1075 |
* @throws IllegalArgumentException if the bias is <code>null</code> |
|
1076 |
* @see Caret#moveDot |
|
1077 |
* @since 1.6 |
|
1078 |
*/ |
|
1079 |
public void moveDot(int dot, Position.Bias dotBias) { |
|
1080 |
if (dotBias == null) { |
|
1081 |
throw new IllegalArgumentException("null bias"); |
|
1082 |
} |
|
1083 |
||
1084 |
if (! component.isEnabled()) { |
|
1085 |
// don't allow selection on disabled components. |
|
1086 |
setDot(dot, dotBias); |
|
1087 |
return; |
|
1088 |
} |
|
1089 |
if (dot != this.dot) { |
|
1090 |
NavigationFilter filter = component.getNavigationFilter(); |
|
1091 |
||
1092 |
if (filter != null) { |
|
1093 |
filter.moveDot(getFilterBypass(), dot, dotBias); |
|
1094 |
} |
|
1095 |
else { |
|
1096 |
handleMoveDot(dot, dotBias); |
|
1097 |
} |
|
1098 |
} |
|
1099 |
} |
|
1100 |
||
1101 |
void handleMoveDot(int dot, Position.Bias dotBias) { |
|
1102 |
changeCaretPosition(dot, dotBias); |
|
1103 |
||
1104 |
if (selectionVisible) { |
|
1105 |
Highlighter h = component.getHighlighter(); |
|
1106 |
if (h != null) { |
|
1107 |
int p0 = Math.min(dot, mark); |
|
1108 |
int p1 = Math.max(dot, mark); |
|
1109 |
||
1110 |
// if p0 == p1 then there should be no highlight, remove it if necessary |
|
1111 |
if (p0 == p1) { |
|
1112 |
if (selectionTag != null) { |
|
1113 |
h.removeHighlight(selectionTag); |
|
1114 |
selectionTag = null; |
|
32678
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1115 |
updateOwnsSelection(); |
2 | 1116 |
} |
1117 |
// otherwise, change or add the highlight |
|
1118 |
} else { |
|
1119 |
try { |
|
1120 |
if (selectionTag != null) { |
|
1121 |
h.changeHighlight(selectionTag, p0, p1); |
|
1122 |
} else { |
|
1123 |
Highlighter.HighlightPainter p = getSelectionPainter(); |
|
1124 |
selectionTag = h.addHighlight(p0, p1, p); |
|
1125 |
} |
|
32678
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1126 |
updateOwnsSelection(); |
2 | 1127 |
} catch (BadLocationException e) { |
1128 |
throw new StateInvariantError("Bad caret position"); |
|
1129 |
} |
|
1130 |
} |
|
1131 |
} |
|
1132 |
} |
|
1133 |
} |
|
1134 |
||
1135 |
/** |
|
1136 |
* Sets the caret position and mark to the specified position, with the |
|
1137 |
* specified bias. This implicitly sets the selection range |
|
1138 |
* to zero. |
|
1139 |
* |
|
1140 |
* @param dot the position >= 0 |
|
1141 |
* @param dotBias the bias for this position, not <code>null</code> |
|
1142 |
* @throws IllegalArgumentException if the bias is <code>null</code> |
|
1143 |
* @see Caret#setDot |
|
1144 |
* @since 1.6 |
|
1145 |
*/ |
|
1146 |
public void setDot(int dot, Position.Bias dotBias) { |
|
1147 |
if (dotBias == null) { |
|
1148 |
throw new IllegalArgumentException("null bias"); |
|
1149 |
} |
|
1150 |
||
1151 |
NavigationFilter filter = component.getNavigationFilter(); |
|
1152 |
||
1153 |
if (filter != null) { |
|
1154 |
filter.setDot(getFilterBypass(), dot, dotBias); |
|
1155 |
} |
|
1156 |
else { |
|
1157 |
handleSetDot(dot, dotBias); |
|
1158 |
} |
|
1159 |
} |
|
1160 |
||
1161 |
void handleSetDot(int dot, Position.Bias dotBias) { |
|
1162 |
// move dot, if it changed |
|
1163 |
Document doc = component.getDocument(); |
|
1164 |
if (doc != null) { |
|
1165 |
dot = Math.min(dot, doc.getLength()); |
|
1166 |
} |
|
1167 |
dot = Math.max(dot, 0); |
|
1168 |
||
1169 |
// The position (0,Backward) is out of range so disallow it. |
|
1170 |
if( dot == 0 ) |
|
1171 |
dotBias = Position.Bias.Forward; |
|
1172 |
||
1173 |
mark = dot; |
|
1174 |
if (this.dot != dot || this.dotBias != dotBias || |
|
1175 |
selectionTag != null || forceCaretPositionChange) { |
|
1176 |
changeCaretPosition(dot, dotBias); |
|
32678
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1177 |
updateOwnsSelection(); |
2 | 1178 |
} |
1179 |
this.markBias = this.dotBias; |
|
1180 |
this.markLTR = dotLTR; |
|
1181 |
Highlighter h = component.getHighlighter(); |
|
1182 |
if ((h != null) && (selectionTag != null)) { |
|
1183 |
h.removeHighlight(selectionTag); |
|
1184 |
selectionTag = null; |
|
32678
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1185 |
updateOwnsSelection(); |
2 | 1186 |
} |
1187 |
} |
|
1188 |
||
1189 |
/** |
|
1190 |
* Returns the bias of the caret position. |
|
1191 |
* |
|
1192 |
* @return the bias of the caret position |
|
1193 |
* @since 1.6 |
|
1194 |
*/ |
|
1195 |
public Position.Bias getDotBias() { |
|
1196 |
return dotBias; |
|
1197 |
} |
|
1198 |
||
1199 |
/** |
|
1200 |
* Returns the bias of the mark. |
|
1201 |
* |
|
1202 |
* @return the bias of the mark |
|
1203 |
* @since 1.6 |
|
1204 |
*/ |
|
1205 |
public Position.Bias getMarkBias() { |
|
1206 |
return markBias; |
|
1207 |
} |
|
1208 |
||
1209 |
boolean isDotLeftToRight() { |
|
1210 |
return dotLTR; |
|
1211 |
} |
|
1212 |
||
1213 |
boolean isMarkLeftToRight() { |
|
1214 |
return markLTR; |
|
1215 |
} |
|
1216 |
||
1217 |
boolean isPositionLTR(int position, Position.Bias bias) { |
|
1218 |
Document doc = component.getDocument(); |
|
20103
f640c22218a3
6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents:
13779
diff
changeset
|
1219 |
if(bias == Position.Bias.Backward && --position < 0) |
f640c22218a3
6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents:
13779
diff
changeset
|
1220 |
position = 0; |
f640c22218a3
6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents:
13779
diff
changeset
|
1221 |
return AbstractDocument.isLeftToRight(doc, position, position); |
2 | 1222 |
} |
1223 |
||
1224 |
Position.Bias guessBiasForOffset(int offset, Position.Bias lastBias, |
|
1225 |
boolean lastLTR) { |
|
1226 |
// There is an abiguous case here. That if your model looks like: |
|
1227 |
// abAB with the cursor at abB]A (visual representation of |
|
1228 |
// 3 forward) deleting could either become abB] or |
|
1229 |
// ab[B. I'ld actually prefer abB]. But, if I implement that |
|
1230 |
// a delete at abBA] would result in aBA] vs a[BA which I |
|
1231 |
// think is totally wrong. To get this right we need to know what |
|
1232 |
// was deleted. And we could get this from the bidi structure |
|
1233 |
// in the change event. So: |
|
1234 |
// PENDING: base this off what was deleted. |
|
1235 |
if(lastLTR != isPositionLTR(offset, lastBias)) { |
|
1236 |
lastBias = Position.Bias.Backward; |
|
1237 |
} |
|
1238 |
else if(lastBias != Position.Bias.Backward && |
|
1239 |
lastLTR != isPositionLTR(offset, Position.Bias.Backward)) { |
|
1240 |
lastBias = Position.Bias.Backward; |
|
1241 |
} |
|
1242 |
if (lastBias == Position.Bias.Backward && offset > 0) { |
|
1243 |
try { |
|
1244 |
Segment s = new Segment(); |
|
1245 |
component.getDocument().getText(offset - 1, 1, s); |
|
1246 |
if (s.count > 0 && s.array[s.offset] == '\n') { |
|
1247 |
lastBias = Position.Bias.Forward; |
|
1248 |
} |
|
1249 |
} |
|
1250 |
catch (BadLocationException ble) {} |
|
1251 |
} |
|
1252 |
return lastBias; |
|
1253 |
} |
|
1254 |
||
1255 |
// ---- local methods -------------------------------------------- |
|
1256 |
||
1257 |
/** |
|
1258 |
* Sets the caret position (dot) to a new location. This |
|
1259 |
* causes the old and new location to be repainted. It |
|
1260 |
* also makes sure that the caret is within the visible |
|
1261 |
* region of the view, if the view is scrollable. |
|
1262 |
*/ |
|
1263 |
void changeCaretPosition(int dot, Position.Bias dotBias) { |
|
1264 |
// repaint the old position and set the new value of |
|
1265 |
// the dot. |
|
1266 |
repaint(); |
|
1267 |
||
1268 |
||
1269 |
// Make sure the caret is visible if this window has the focus. |
|
1270 |
if (flasher != null && flasher.isRunning()) { |
|
1271 |
visible = true; |
|
1272 |
flasher.restart(); |
|
1273 |
} |
|
1274 |
||
1275 |
// notify listeners at the caret moved |
|
1276 |
this.dot = dot; |
|
1277 |
this.dotBias = dotBias; |
|
1278 |
dotLTR = isPositionLTR(dot, dotBias); |
|
1279 |
fireStateChanged(); |
|
1280 |
||
1281 |
updateSystemSelection(); |
|
1282 |
||
1283 |
setMagicCaretPosition(null); |
|
1284 |
||
1285 |
// We try to repaint the caret later, since things |
|
1286 |
// may be unstable at the time this is called |
|
1287 |
// (i.e. we don't want to depend upon notification |
|
1288 |
// order or the fact that this might happen on |
|
1289 |
// an unsafe thread). |
|
1290 |
Runnable callRepaintNewCaret = new Runnable() { |
|
1291 |
public void run() { |
|
1292 |
repaintNewCaret(); |
|
1293 |
} |
|
1294 |
}; |
|
1295 |
SwingUtilities.invokeLater(callRepaintNewCaret); |
|
1296 |
} |
|
1297 |
||
1298 |
/** |
|
1299 |
* Repaints the new caret position, with the |
|
1300 |
* assumption that this is happening on the |
|
1301 |
* event thread so that calling <code>modelToView</code> |
|
1302 |
* is safe. |
|
1303 |
*/ |
|
1304 |
void repaintNewCaret() { |
|
1305 |
if (component != null) { |
|
1306 |
TextUI mapper = component.getUI(); |
|
1307 |
Document doc = component.getDocument(); |
|
1308 |
if ((mapper != null) && (doc != null)) { |
|
1309 |
// determine the new location and scroll if |
|
1310 |
// not visible. |
|
1311 |
Rectangle newLoc; |
|
1312 |
try { |
|
1313 |
newLoc = mapper.modelToView(component, this.dot, this.dotBias); |
|
1314 |
} catch (BadLocationException e) { |
|
1315 |
newLoc = null; |
|
1316 |
} |
|
1317 |
if (newLoc != null) { |
|
1318 |
adjustVisibility(newLoc); |
|
1319 |
// If there is no magic caret position, make one |
|
1320 |
if (getMagicCaretPosition() == null) { |
|
1321 |
setMagicCaretPosition(new Point(newLoc.x, newLoc.y)); |
|
1322 |
} |
|
1323 |
} |
|
1324 |
||
1325 |
// repaint the new position |
|
1326 |
damage(newLoc); |
|
1327 |
} |
|
1328 |
} |
|
1329 |
} |
|
1330 |
||
1331 |
private void updateSystemSelection() { |
|
1332 |
if ( ! SwingUtilities2.canCurrentEventAccessSystemClipboard() ) { |
|
1333 |
return; |
|
1334 |
} |
|
10875
4e4f58a00bf3
7049024: DnD fails with JTextArea and JTextField
rupashka
parents:
10316
diff
changeset
|
1335 |
if (this.dot != this.mark && component != null && component.hasFocus()) { |
2 | 1336 |
Clipboard clip = getSystemSelection(); |
1337 |
if (clip != null) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
2
diff
changeset
|
1338 |
String selectedText; |
2 | 1339 |
if (component instanceof JPasswordField |
1340 |
&& component.getClientProperty("JPasswordField.cutCopyAllowed") != |
|
1341 |
Boolean.TRUE) { |
|
1342 |
//fix for 4793761 |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
1343 |
StringBuilder txt = null; |
2 | 1344 |
char echoChar = ((JPasswordField)component).getEchoChar(); |
1345 |
int p0 = Math.min(getDot(), getMark()); |
|
1346 |
int p1 = Math.max(getDot(), getMark()); |
|
1347 |
for (int i = p0; i < p1; i++) { |
|
1348 |
if (txt == null) { |
|
7014
eb4fcf73ee99
6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents:
5506
diff
changeset
|
1349 |
txt = new StringBuilder(); |
2 | 1350 |
} |
1351 |
txt.append(echoChar); |
|
1352 |
} |
|
1353 |
selectedText = (txt != null) ? txt.toString() : null; |
|
1354 |
} else { |
|
1355 |
selectedText = component.getSelectedText(); |
|
1356 |
} |
|
1357 |
try { |
|
1358 |
clip.setContents( |
|
1359 |
new StringSelection(selectedText), getClipboardOwner()); |
|
1360 |
||
1361 |
ownsSelection = true; |
|
1362 |
} catch (IllegalStateException ise) { |
|
1363 |
// clipboard was unavailable |
|
1364 |
// no need to provide error feedback to user since updating |
|
1365 |
// the system selection is not a user invoked action |
|
1366 |
} |
|
1367 |
} |
|
1368 |
} |
|
1369 |
} |
|
1370 |
||
1371 |
private Clipboard getSystemSelection() { |
|
1372 |
try { |
|
1373 |
return component.getToolkit().getSystemSelection(); |
|
1374 |
} catch (HeadlessException he) { |
|
1375 |
// do nothing... there is no system clipboard |
|
1376 |
} catch (SecurityException se) { |
|
1377 |
// do nothing... there is no allowed system clipboard |
|
1378 |
} |
|
1379 |
return null; |
|
1380 |
} |
|
1381 |
||
1382 |
private ClipboardOwner getClipboardOwner() { |
|
1383 |
return handler; |
|
1384 |
} |
|
1385 |
||
1386 |
/** |
|
1387 |
* This is invoked after the document changes to verify the current |
|
1388 |
* dot/mark is valid. We do this in case the <code>NavigationFilter</code> |
|
1389 |
* changed where to position the dot, that resulted in the current location |
|
1390 |
* being bogus. |
|
1391 |
*/ |
|
1392 |
private void ensureValidPosition() { |
|
1393 |
int length = component.getDocument().getLength(); |
|
1394 |
if (dot > length || mark > length) { |
|
1395 |
// Current location is bogus and filter likely vetoed the |
|
1396 |
// change, force the reset without giving the filter a |
|
1397 |
// chance at changing it. |
|
1398 |
handleSetDot(length, Position.Bias.Forward); |
|
1399 |
} |
|
1400 |
} |
|
1401 |
||
1402 |
||
1403 |
/** |
|
1404 |
* Saves the current caret position. This is used when |
|
1405 |
* caret up/down actions occur, moving between lines |
|
1406 |
* that have uneven end positions. |
|
1407 |
* |
|
1408 |
* @param p the position |
|
1409 |
* @see #getMagicCaretPosition |
|
1410 |
*/ |
|
1411 |
public void setMagicCaretPosition(Point p) { |
|
1412 |
magicCaretPosition = p; |
|
1413 |
} |
|
1414 |
||
1415 |
/** |
|
1416 |
* Gets the saved caret position. |
|
1417 |
* |
|
1418 |
* @return the position |
|
1419 |
* see #setMagicCaretPosition |
|
1420 |
*/ |
|
1421 |
public Point getMagicCaretPosition() { |
|
1422 |
return magicCaretPosition; |
|
1423 |
} |
|
1424 |
||
1425 |
/** |
|
1426 |
* Compares this object to the specified object. |
|
1427 |
* The superclass behavior of comparing rectangles |
|
1428 |
* is not desired, so this is changed to the Object |
|
1429 |
* behavior. |
|
1430 |
* |
|
1431 |
* @param obj the object to compare this font with |
|
1432 |
* @return <code>true</code> if the objects are equal; |
|
1433 |
* <code>false</code> otherwise |
|
1434 |
*/ |
|
1435 |
public boolean equals(Object obj) { |
|
1436 |
return (this == obj); |
|
1437 |
} |
|
1438 |
||
1439 |
public String toString() { |
|
1440 |
String s = "Dot=(" + dot + ", " + dotBias + ")"; |
|
1441 |
s += " Mark=(" + mark + ", " + markBias + ")"; |
|
1442 |
return s; |
|
1443 |
} |
|
1444 |
||
1445 |
private NavigationFilter.FilterBypass getFilterBypass() { |
|
1446 |
if (filterBypass == null) { |
|
1447 |
filterBypass = new DefaultFilterBypass(); |
|
1448 |
} |
|
1449 |
return filterBypass; |
|
1450 |
} |
|
1451 |
||
1452 |
// Rectangle.contains returns false if passed a rect with a w or h == 0, |
|
1453 |
// this won't (assuming X,Y are contained with this rectangle). |
|
1454 |
private boolean _contains(int X, int Y, int W, int H) { |
|
1455 |
int w = this.width; |
|
1456 |
int h = this.height; |
|
1457 |
if ((w | h | W | H) < 0) { |
|
1458 |
// At least one of the dimensions is negative... |
|
1459 |
return false; |
|
1460 |
} |
|
1461 |
// Note: if any dimension is zero, tests below must return false... |
|
1462 |
int x = this.x; |
|
1463 |
int y = this.y; |
|
1464 |
if (X < x || Y < y) { |
|
1465 |
return false; |
|
1466 |
} |
|
1467 |
if (W > 0) { |
|
1468 |
w += x; |
|
1469 |
W += X; |
|
1470 |
if (W <= X) { |
|
1471 |
// X+W overflowed or W was zero, return false if... |
|
1472 |
// either original w or W was zero or |
|
1473 |
// x+w did not overflow or |
|
1474 |
// the overflowed x+w is smaller than the overflowed X+W |
|
1475 |
if (w >= x || W > w) return false; |
|
1476 |
} else { |
|
1477 |
// X+W did not overflow and W was not zero, return false if... |
|
1478 |
// original w was zero or |
|
1479 |
// x+w did not overflow and x+w is smaller than X+W |
|
1480 |
if (w >= x && W > w) return false; |
|
1481 |
} |
|
1482 |
} |
|
1483 |
else if ((x + w) < X) { |
|
1484 |
return false; |
|
1485 |
} |
|
1486 |
if (H > 0) { |
|
1487 |
h += y; |
|
1488 |
H += Y; |
|
1489 |
if (H <= Y) { |
|
1490 |
if (h >= y || H > h) return false; |
|
1491 |
} else { |
|
1492 |
if (h >= y && H > h) return false; |
|
1493 |
} |
|
1494 |
} |
|
1495 |
else if ((y + h) < Y) { |
|
1496 |
return false; |
|
1497 |
} |
|
1498 |
return true; |
|
1499 |
} |
|
1500 |
||
1501 |
int getCaretWidth(int height) { |
|
1502 |
if (aspectRatio > -1) { |
|
1503 |
return (int) (aspectRatio * height) + 1; |
|
1504 |
} |
|
1505 |
||
1506 |
if (caretWidth > -1) { |
|
1507 |
return caretWidth; |
|
13779
011727a60840
6994562: Swing classes (both JTextArea and JTextField) don't support caret width tuning
VKARNAUK
parents:
11084
diff
changeset
|
1508 |
} else { |
011727a60840
6994562: Swing classes (both JTextArea and JTextField) don't support caret width tuning
VKARNAUK
parents:
11084
diff
changeset
|
1509 |
Object property = UIManager.get("Caret.width"); |
011727a60840
6994562: Swing classes (both JTextArea and JTextField) don't support caret width tuning
VKARNAUK
parents:
11084
diff
changeset
|
1510 |
if (property instanceof Integer) { |
011727a60840
6994562: Swing classes (both JTextArea and JTextField) don't support caret width tuning
VKARNAUK
parents:
11084
diff
changeset
|
1511 |
return ((Integer) property).intValue(); |
011727a60840
6994562: Swing classes (both JTextArea and JTextField) don't support caret width tuning
VKARNAUK
parents:
11084
diff
changeset
|
1512 |
} else { |
011727a60840
6994562: Swing classes (both JTextArea and JTextField) don't support caret width tuning
VKARNAUK
parents:
11084
diff
changeset
|
1513 |
return 1; |
011727a60840
6994562: Swing classes (both JTextArea and JTextField) don't support caret width tuning
VKARNAUK
parents:
11084
diff
changeset
|
1514 |
} |
2 | 1515 |
} |
1516 |
} |
|
1517 |
||
1518 |
// --- serialization --------------------------------------------- |
|
1519 |
||
1520 |
private void readObject(ObjectInputStream s) |
|
1521 |
throws ClassNotFoundException, IOException |
|
1522 |
{ |
|
26001
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1523 |
ObjectInputStream.GetField f = s.readFields(); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1524 |
|
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1525 |
EventListenerList newListenerList = (EventListenerList) f.get("listenerList", null); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1526 |
if (newListenerList == null) { |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1527 |
throw new InvalidObjectException("Null listenerList"); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1528 |
} |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1529 |
listenerList = newListenerList; |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1530 |
component = (JTextComponent) f.get("component", null); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1531 |
updatePolicy = f.get("updatePolicy", 0); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1532 |
visible = f.get("visible", false); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1533 |
active = f.get("active", false); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1534 |
dot = f.get("dot", 0); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1535 |
mark = f.get("mark", 0); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1536 |
selectionTag = f.get("selectionTag", null); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1537 |
selectionVisible = f.get("selectionVisible", false); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1538 |
flasher = (Timer) f.get("flasher", null); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1539 |
magicCaretPosition = (Point) f.get("magicCaretPosition", null); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1540 |
dotLTR = f.get("dotLTR", false); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1541 |
markLTR = f.get("markLTR", false); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1542 |
ownsSelection = f.get("ownsSelection", false); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1543 |
forceCaretPositionChange = f.get("forceCaretPositionChange", false); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1544 |
caretWidth = f.get("caretWidth", 0); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1545 |
aspectRatio = f.get("aspectRatio", 0.0f); |
991e1be0b235
8038937: Validate fields on Swing classes deserialization
alexsch
parents:
22574
diff
changeset
|
1546 |
|
2 | 1547 |
handler = new Handler(); |
1548 |
if (!s.readBoolean()) { |
|
1549 |
dotBias = Position.Bias.Forward; |
|
1550 |
} |
|
1551 |
else { |
|
1552 |
dotBias = Position.Bias.Backward; |
|
1553 |
} |
|
1554 |
if (!s.readBoolean()) { |
|
1555 |
markBias = Position.Bias.Forward; |
|
1556 |
} |
|
1557 |
else { |
|
1558 |
markBias = Position.Bias.Backward; |
|
1559 |
} |
|
1560 |
} |
|
1561 |
||
1562 |
private void writeObject(ObjectOutputStream s) throws IOException { |
|
1563 |
s.defaultWriteObject(); |
|
1564 |
s.writeBoolean((dotBias == Position.Bias.Backward)); |
|
1565 |
s.writeBoolean((markBias == Position.Bias.Backward)); |
|
1566 |
} |
|
1567 |
||
1568 |
// ---- member variables ------------------------------------------ |
|
1569 |
||
1570 |
/** |
|
1571 |
* The event listener list. |
|
1572 |
*/ |
|
1573 |
protected EventListenerList listenerList = new EventListenerList(); |
|
1574 |
||
1575 |
/** |
|
1576 |
* The change event for the model. |
|
1577 |
* Only one ChangeEvent is needed per model instance since the |
|
1578 |
* event's only (read-only) state is the source property. The source |
|
1579 |
* of events generated here is always "this". |
|
1580 |
*/ |
|
1581 |
protected transient ChangeEvent changeEvent = null; |
|
1582 |
||
1583 |
// package-private to avoid inner classes private member |
|
1584 |
// access bug |
|
1585 |
JTextComponent component; |
|
1586 |
||
1587 |
int updatePolicy = UPDATE_WHEN_ON_EDT; |
|
1588 |
boolean visible; |
|
1589 |
boolean active; |
|
1590 |
int dot; |
|
1591 |
int mark; |
|
1592 |
Object selectionTag; |
|
1593 |
boolean selectionVisible; |
|
1594 |
Timer flasher; |
|
1595 |
Point magicCaretPosition; |
|
1596 |
transient Position.Bias dotBias; |
|
1597 |
transient Position.Bias markBias; |
|
1598 |
boolean dotLTR; |
|
1599 |
boolean markLTR; |
|
1600 |
transient Handler handler = new Handler(); |
|
32865
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
32678
diff
changeset
|
1601 |
private transient int[] flagXPoints = new int[3]; |
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
32678
diff
changeset
|
1602 |
private transient int[] flagYPoints = new int[3]; |
2 | 1603 |
private transient NavigationFilter.FilterBypass filterBypass; |
32865
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
32678
diff
changeset
|
1604 |
private static transient Action selectWord = null; |
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
32678
diff
changeset
|
1605 |
private static transient Action selectLine = null; |
2 | 1606 |
/** |
1607 |
* This is used to indicate if the caret currently owns the selection. |
|
1608 |
* This is always false if the system does not support the system |
|
1609 |
* clipboard. |
|
1610 |
*/ |
|
1611 |
private boolean ownsSelection; |
|
1612 |
||
1613 |
/** |
|
1614 |
* If this is true, the location of the dot is updated regardless of |
|
1615 |
* the current location. This is set in the DocumentListener |
|
1616 |
* such that even if the model location of dot hasn't changed (perhaps do |
|
1617 |
* to a forward delete) the visual location is updated. |
|
1618 |
*/ |
|
1619 |
private boolean forceCaretPositionChange; |
|
1620 |
||
1621 |
/** |
|
1622 |
* Whether or not mouseReleased should adjust the caret and focus. |
|
1623 |
* This flag is set by mousePressed if it wanted to adjust the caret |
|
1624 |
* and focus but couldn't because of a possible DnD operation. |
|
1625 |
*/ |
|
1626 |
private transient boolean shouldHandleRelease; |
|
1627 |
||
1628 |
||
1629 |
/** |
|
1630 |
* holds last MouseEvent which caused the word selection |
|
1631 |
*/ |
|
1632 |
private transient MouseEvent selectedWordEvent = null; |
|
1633 |
||
1634 |
/** |
|
1635 |
* The width of the caret in pixels. |
|
1636 |
*/ |
|
1637 |
private int caretWidth = -1; |
|
1638 |
private float aspectRatio = -1; |
|
1639 |
||
1640 |
class SafeScroller implements Runnable { |
|
1641 |
||
1642 |
SafeScroller(Rectangle r) { |
|
1643 |
this.r = r; |
|
1644 |
} |
|
1645 |
||
1646 |
public void run() { |
|
1647 |
if (component != null) { |
|
1648 |
component.scrollRectToVisible(r); |
|
1649 |
} |
|
1650 |
} |
|
1651 |
||
1652 |
Rectangle r; |
|
1653 |
} |
|
1654 |
||
1655 |
||
1656 |
class Handler implements PropertyChangeListener, DocumentListener, ActionListener, ClipboardOwner { |
|
1657 |
||
1658 |
// --- ActionListener methods ---------------------------------- |
|
1659 |
||
1660 |
/** |
|
1661 |
* Invoked when the blink timer fires. This is called |
|
1662 |
* asynchronously. The simply changes the visibility |
|
1663 |
* and repaints the rectangle that last bounded the caret. |
|
1664 |
* |
|
1665 |
* @param e the action event |
|
1666 |
*/ |
|
1667 |
public void actionPerformed(ActionEvent e) { |
|
1668 |
if (width == 0 || height == 0) { |
|
1669 |
// setVisible(true) will cause a scroll, only do this if the |
|
1670 |
// new location is really valid. |
|
1671 |
if (component != null) { |
|
1672 |
TextUI mapper = component.getUI(); |
|
1673 |
try { |
|
1674 |
Rectangle r = mapper.modelToView(component, dot, |
|
1675 |
dotBias); |
|
1676 |
if (r != null && r.width != 0 && r.height != 0) { |
|
1677 |
damage(r); |
|
1678 |
} |
|
1679 |
} catch (BadLocationException ble) { |
|
1680 |
} |
|
1681 |
} |
|
1682 |
} |
|
1683 |
visible = !visible; |
|
1684 |
repaint(); |
|
1685 |
} |
|
1686 |
||
1687 |
// --- DocumentListener methods -------------------------------- |
|
1688 |
||
1689 |
/** |
|
1690 |
* Updates the dot and mark if they were changed by |
|
1691 |
* the insertion. |
|
1692 |
* |
|
1693 |
* @param e the document event |
|
1694 |
* @see DocumentListener#insertUpdate |
|
1695 |
*/ |
|
1696 |
public void insertUpdate(DocumentEvent e) { |
|
1697 |
if (getUpdatePolicy() == NEVER_UPDATE || |
|
1698 |
(getUpdatePolicy() == UPDATE_WHEN_ON_EDT && |
|
1699 |
!SwingUtilities.isEventDispatchThread())) { |
|
1700 |
||
1701 |
if ((e.getOffset() <= dot || e.getOffset() <= mark) |
|
1702 |
&& selectionTag != null) { |
|
1703 |
try { |
|
1704 |
component.getHighlighter().changeHighlight(selectionTag, |
|
1705 |
Math.min(dot, mark), Math.max(dot, mark)); |
|
1706 |
} catch (BadLocationException e1) { |
|
1707 |
e1.printStackTrace(); |
|
1708 |
} |
|
1709 |
} |
|
1710 |
return; |
|
1711 |
} |
|
1712 |
int offset = e.getOffset(); |
|
1713 |
int length = e.getLength(); |
|
1714 |
int newDot = dot; |
|
1715 |
short changed = 0; |
|
1716 |
||
1717 |
if (e instanceof AbstractDocument.UndoRedoDocumentEvent) { |
|
1718 |
setDot(offset + length); |
|
1719 |
return; |
|
1720 |
} |
|
1721 |
if (newDot >= offset) { |
|
1722 |
newDot += length; |
|
1723 |
changed |= 1; |
|
1724 |
} |
|
1725 |
int newMark = mark; |
|
1726 |
if (newMark >= offset) { |
|
1727 |
newMark += length; |
|
1728 |
changed |= 2; |
|
1729 |
} |
|
1730 |
||
1731 |
if (changed != 0) { |
|
1732 |
Position.Bias dotBias = DefaultCaret.this.dotBias; |
|
1733 |
if (dot == offset) { |
|
1734 |
Document doc = component.getDocument(); |
|
1735 |
boolean isNewline; |
|
1736 |
try { |
|
1737 |
Segment s = new Segment(); |
|
1738 |
doc.getText(newDot - 1, 1, s); |
|
1739 |
isNewline = (s.count > 0 && |
|
1740 |
s.array[s.offset] == '\n'); |
|
1741 |
} catch (BadLocationException ble) { |
|
1742 |
isNewline = false; |
|
1743 |
} |
|
1744 |
if (isNewline) { |
|
1745 |
dotBias = Position.Bias.Forward; |
|
1746 |
} else { |
|
1747 |
dotBias = Position.Bias.Backward; |
|
1748 |
} |
|
1749 |
} |
|
1750 |
if (newMark == newDot) { |
|
1751 |
setDot(newDot, dotBias); |
|
1752 |
ensureValidPosition(); |
|
1753 |
} |
|
1754 |
else { |
|
1755 |
setDot(newMark, markBias); |
|
1756 |
if (getDot() == newMark) { |
|
1757 |
// Due this test in case the filter vetoed the |
|
1758 |
// change in which case this probably won't be |
|
1759 |
// valid either. |
|
1760 |
moveDot(newDot, dotBias); |
|
1761 |
} |
|
1762 |
ensureValidPosition(); |
|
1763 |
} |
|
1764 |
} |
|
1765 |
} |
|
1766 |
||
1767 |
/** |
|
1768 |
* Updates the dot and mark if they were changed |
|
1769 |
* by the removal. |
|
1770 |
* |
|
1771 |
* @param e the document event |
|
1772 |
* @see DocumentListener#removeUpdate |
|
1773 |
*/ |
|
1774 |
public void removeUpdate(DocumentEvent e) { |
|
1775 |
if (getUpdatePolicy() == NEVER_UPDATE || |
|
1776 |
(getUpdatePolicy() == UPDATE_WHEN_ON_EDT && |
|
1777 |
!SwingUtilities.isEventDispatchThread())) { |
|
1778 |
||
1779 |
int length = component.getDocument().getLength(); |
|
1780 |
dot = Math.min(dot, length); |
|
1781 |
mark = Math.min(mark, length); |
|
1782 |
if ((e.getOffset() < dot || e.getOffset() < mark) |
|
1783 |
&& selectionTag != null) { |
|
1784 |
try { |
|
1785 |
component.getHighlighter().changeHighlight(selectionTag, |
|
1786 |
Math.min(dot, mark), Math.max(dot, mark)); |
|
1787 |
} catch (BadLocationException e1) { |
|
1788 |
e1.printStackTrace(); |
|
1789 |
} |
|
1790 |
} |
|
1791 |
return; |
|
1792 |
} |
|
1793 |
int offs0 = e.getOffset(); |
|
1794 |
int offs1 = offs0 + e.getLength(); |
|
1795 |
int newDot = dot; |
|
1796 |
boolean adjustDotBias = false; |
|
1797 |
int newMark = mark; |
|
1798 |
boolean adjustMarkBias = false; |
|
1799 |
||
1800 |
if(e instanceof AbstractDocument.UndoRedoDocumentEvent) { |
|
1801 |
setDot(offs0); |
|
1802 |
return; |
|
1803 |
} |
|
1804 |
if (newDot >= offs1) { |
|
1805 |
newDot -= (offs1 - offs0); |
|
1806 |
if(newDot == offs1) { |
|
1807 |
adjustDotBias = true; |
|
1808 |
} |
|
1809 |
} else if (newDot >= offs0) { |
|
1810 |
newDot = offs0; |
|
1811 |
adjustDotBias = true; |
|
1812 |
} |
|
1813 |
if (newMark >= offs1) { |
|
1814 |
newMark -= (offs1 - offs0); |
|
1815 |
if(newMark == offs1) { |
|
1816 |
adjustMarkBias = true; |
|
1817 |
} |
|
1818 |
} else if (newMark >= offs0) { |
|
1819 |
newMark = offs0; |
|
1820 |
adjustMarkBias = true; |
|
1821 |
} |
|
1822 |
if (newMark == newDot) { |
|
1823 |
forceCaretPositionChange = true; |
|
1824 |
try { |
|
1825 |
setDot(newDot, guessBiasForOffset(newDot, dotBias, |
|
1826 |
dotLTR)); |
|
1827 |
} finally { |
|
1828 |
forceCaretPositionChange = false; |
|
1829 |
} |
|
1830 |
ensureValidPosition(); |
|
1831 |
} else { |
|
1832 |
Position.Bias dotBias = DefaultCaret.this.dotBias; |
|
1833 |
Position.Bias markBias = DefaultCaret.this.markBias; |
|
1834 |
if(adjustDotBias) { |
|
1835 |
dotBias = guessBiasForOffset(newDot, dotBias, dotLTR); |
|
1836 |
} |
|
1837 |
if(adjustMarkBias) { |
|
1838 |
markBias = guessBiasForOffset(mark, markBias, markLTR); |
|
1839 |
} |
|
1840 |
setDot(newMark, markBias); |
|
1841 |
if (getDot() == newMark) { |
|
1842 |
// Due this test in case the filter vetoed the change |
|
1843 |
// in which case this probably won't be valid either. |
|
1844 |
moveDot(newDot, dotBias); |
|
1845 |
} |
|
1846 |
ensureValidPosition(); |
|
1847 |
} |
|
1848 |
} |
|
1849 |
||
1850 |
/** |
|
1851 |
* Gives notification that an attribute or set of attributes changed. |
|
1852 |
* |
|
1853 |
* @param e the document event |
|
1854 |
* @see DocumentListener#changedUpdate |
|
1855 |
*/ |
|
1856 |
public void changedUpdate(DocumentEvent e) { |
|
1857 |
if (getUpdatePolicy() == NEVER_UPDATE || |
|
1858 |
(getUpdatePolicy() == UPDATE_WHEN_ON_EDT && |
|
1859 |
!SwingUtilities.isEventDispatchThread())) { |
|
1860 |
return; |
|
1861 |
} |
|
1862 |
if(e instanceof AbstractDocument.UndoRedoDocumentEvent) { |
|
1863 |
setDot(e.getOffset() + e.getLength()); |
|
1864 |
} |
|
1865 |
} |
|
1866 |
||
1867 |
// --- PropertyChangeListener methods ----------------------- |
|
1868 |
||
1869 |
/** |
|
1870 |
* This method gets called when a bound property is changed. |
|
1871 |
* We are looking for document changes on the editor. |
|
1872 |
*/ |
|
1873 |
public void propertyChange(PropertyChangeEvent evt) { |
|
1874 |
Object oldValue = evt.getOldValue(); |
|
1875 |
Object newValue = evt.getNewValue(); |
|
1876 |
if ((oldValue instanceof Document) || (newValue instanceof Document)) { |
|
1877 |
setDot(0); |
|
1878 |
if (oldValue != null) { |
|
1879 |
((Document)oldValue).removeDocumentListener(this); |
|
1880 |
} |
|
1881 |
if (newValue != null) { |
|
1882 |
((Document)newValue).addDocumentListener(this); |
|
1883 |
} |
|
1884 |
} else if("enabled".equals(evt.getPropertyName())) { |
|
1885 |
Boolean enabled = (Boolean) evt.getNewValue(); |
|
1886 |
if(component.isFocusOwner()) { |
|
1887 |
if(enabled == Boolean.TRUE) { |
|
1888 |
if(component.isEditable()) { |
|
1889 |
setVisible(true); |
|
1890 |
} |
|
1891 |
setSelectionVisible(true); |
|
1892 |
} else { |
|
1893 |
setVisible(false); |
|
1894 |
setSelectionVisible(false); |
|
1895 |
} |
|
1896 |
} |
|
1897 |
} else if("caretWidth".equals(evt.getPropertyName())) { |
|
1898 |
Integer newWidth = (Integer) evt.getNewValue(); |
|
1899 |
if (newWidth != null) { |
|
1900 |
caretWidth = newWidth.intValue(); |
|
1901 |
} else { |
|
1902 |
caretWidth = -1; |
|
1903 |
} |
|
1904 |
repaint(); |
|
1905 |
} else if("caretAspectRatio".equals(evt.getPropertyName())) { |
|
1906 |
Number newRatio = (Number) evt.getNewValue(); |
|
1907 |
if (newRatio != null) { |
|
1908 |
aspectRatio = newRatio.floatValue(); |
|
1909 |
} else { |
|
1910 |
aspectRatio = -1; |
|
1911 |
} |
|
1912 |
repaint(); |
|
1913 |
} |
|
1914 |
} |
|
1915 |
||
1916 |
||
1917 |
// |
|
1918 |
// ClipboardOwner |
|
1919 |
// |
|
1920 |
/** |
|
1921 |
* Toggles the visibility of the selection when ownership is lost. |
|
1922 |
*/ |
|
1923 |
public void lostOwnership(Clipboard clipboard, |
|
1924 |
Transferable contents) { |
|
1925 |
if (ownsSelection) { |
|
1926 |
ownsSelection = false; |
|
1927 |
if (component != null && !component.hasFocus()) { |
|
1928 |
setSelectionVisible(false); |
|
1929 |
} |
|
1930 |
} |
|
1931 |
} |
|
1932 |
} |
|
1933 |
||
32678
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1934 |
/** |
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1935 |
* Updates ownsSelection based on text selection in the caret. |
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1936 |
*/ |
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1937 |
private void updateOwnsSelection() { |
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1938 |
ownsSelection = (selectionTag != null) |
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1939 |
&& SwingUtilities2.canAccessSystemClipboard(); |
fa523710b4f5
8025082: The behaviour of the highlight will be lost after clicking the set button
alexsch
parents:
30462
diff
changeset
|
1940 |
} |
2 | 1941 |
|
1942 |
private class DefaultFilterBypass extends NavigationFilter.FilterBypass { |
|
1943 |
public Caret getCaret() { |
|
1944 |
return DefaultCaret.this; |
|
1945 |
} |
|
1946 |
||
1947 |
public void setDot(int dot, Position.Bias bias) { |
|
1948 |
handleSetDot(dot, bias); |
|
1949 |
} |
|
1950 |
||
1951 |
public void moveDot(int dot, Position.Bias bias) { |
|
1952 |
handleMoveDot(dot, bias); |
|
1953 |
} |
|
1954 |
} |
|
1955 |
} |