2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package javax.swing.text;
|
|
26 |
|
|
27 |
import java.lang.reflect.Method;
|
|
28 |
|
|
29 |
import java.security.AccessController;
|
|
30 |
import java.security.PrivilegedAction;
|
|
31 |
|
|
32 |
import java.util.Collections;
|
|
33 |
import java.util.HashMap;
|
|
34 |
import java.util.Hashtable;
|
|
35 |
import java.util.Enumeration;
|
|
36 |
import java.util.Vector;
|
|
37 |
import java.util.Iterator;
|
|
38 |
import java.util.Map;
|
|
39 |
import java.util.Map.Entry;
|
|
40 |
import java.util.Set;
|
|
41 |
|
|
42 |
import java.util.concurrent.*;
|
|
43 |
|
|
44 |
import java.io.*;
|
|
45 |
|
|
46 |
import java.awt.*;
|
|
47 |
import java.awt.event.*;
|
|
48 |
import java.awt.print.*;
|
|
49 |
import java.awt.datatransfer.*;
|
|
50 |
import java.awt.im.InputContext;
|
|
51 |
import java.awt.im.InputMethodRequests;
|
|
52 |
import java.awt.font.TextHitInfo;
|
|
53 |
import java.awt.font.TextAttribute;
|
|
54 |
|
|
55 |
import java.awt.print.Printable;
|
|
56 |
import java.awt.print.PrinterException;
|
|
57 |
|
|
58 |
import javax.print.PrintService;
|
|
59 |
import javax.print.attribute.PrintRequestAttributeSet;
|
|
60 |
|
|
61 |
import java.text.*;
|
|
62 |
import java.text.AttributedCharacterIterator.Attribute;
|
|
63 |
|
|
64 |
import javax.swing.*;
|
|
65 |
import javax.swing.event.*;
|
|
66 |
import javax.swing.plaf.*;
|
|
67 |
|
|
68 |
import javax.accessibility.*;
|
|
69 |
|
|
70 |
import javax.print.attribute.*;
|
|
71 |
|
|
72 |
import sun.awt.AppContext;
|
|
73 |
|
|
74 |
|
|
75 |
import sun.swing.PrintingStatus;
|
|
76 |
import sun.swing.SwingUtilities2;
|
|
77 |
import sun.swing.text.TextComponentPrintable;
|
|
78 |
|
|
79 |
/**
|
|
80 |
* <code>JTextComponent</code> is the base class for swing text
|
|
81 |
* components. It tries to be compatible with the
|
|
82 |
* <code>java.awt.TextComponent</code> class
|
|
83 |
* where it can reasonably do so. Also provided are other services
|
|
84 |
* for additional flexibility (beyond the pluggable UI and bean
|
|
85 |
* support).
|
|
86 |
* You can find information on how to use the functionality
|
|
87 |
* this class provides in
|
|
88 |
* <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html">General Rules for Using Text Components</a>,
|
|
89 |
* a section in <em>The Java Tutorial.</em>
|
|
90 |
*
|
|
91 |
* <p>
|
|
92 |
* <dl>
|
|
93 |
* <dt><b><font size=+1>Caret Changes</font></b>
|
|
94 |
* <dd>
|
|
95 |
* The caret is a pluggable object in swing text components.
|
|
96 |
* Notification of changes to the caret position and the selection
|
|
97 |
* are sent to implementations of the <code>CaretListener</code>
|
|
98 |
* interface that have been registered with the text component.
|
|
99 |
* The UI will install a default caret unless a customized caret
|
|
100 |
* has been set. <br>
|
|
101 |
* By default the caret tracks all the document changes
|
|
102 |
* performed on the Event Dispatching Thread and updates it's position
|
|
103 |
* accordingly if an insertion occurs before or at the caret position
|
|
104 |
* or a removal occurs before the caret position. <code>DefaultCaret</code>
|
|
105 |
* tries to make itself visible which may lead to scrolling
|
|
106 |
* of a text component within <code>JScrollPane</code>. The default caret
|
|
107 |
* behavior can be changed by the {@link DefaultCaret#setUpdatePolicy} method.
|
|
108 |
* <br>
|
|
109 |
* <b>Note</b>: Non-editable text components also have a caret though
|
|
110 |
* it may not be painted.
|
|
111 |
*
|
|
112 |
* <p>
|
|
113 |
* <dt><b><font size=+1>Commands</font></b>
|
|
114 |
* <dd>
|
|
115 |
* Text components provide a number of commands that can be used
|
|
116 |
* to manipulate the component. This is essentially the way that
|
|
117 |
* the component expresses its capabilities. These are expressed
|
|
118 |
* in terms of the swing <code>Action</code> interface,
|
|
119 |
* using the <code>TextAction</code> implementation.
|
|
120 |
* The set of commands supported by the text component can be
|
|
121 |
* found with the {@link #getActions} method. These actions
|
|
122 |
* can be bound to key events, fired from buttons, etc.
|
|
123 |
*
|
|
124 |
* <p>
|
|
125 |
* <dt><b><font size=+1>Text Input</font></b>
|
|
126 |
* <dd>
|
|
127 |
* The text components support flexible and internationalized text input, using
|
|
128 |
* keymaps and the input method framework, while maintaining compatibility with
|
|
129 |
* the AWT listener model.
|
|
130 |
* <p>
|
|
131 |
* A {@link javax.swing.text.Keymap} lets an application bind key
|
|
132 |
* strokes to actions.
|
|
133 |
* In order to allow keymaps to be shared across multiple text components, they
|
|
134 |
* can use actions that extend <code>TextAction</code>.
|
|
135 |
* <code>TextAction</code> can determine which <code>JTextComponent</code>
|
|
136 |
* most recently has or had focus and therefore is the subject of
|
|
137 |
* the action (In the case that the <code>ActionEvent</code>
|
|
138 |
* sent to the action doesn't contain the target text component as its source).
|
|
139 |
* <p>
|
|
140 |
* The <a href="../../../../technotes/guides/imf/spec.html">input method framework</a>
|
|
141 |
* lets text components interact with input methods, separate software
|
|
142 |
* components that preprocess events to let users enter thousands of
|
|
143 |
* different characters using keyboards with far fewer keys.
|
|
144 |
* <code>JTextComponent</code> is an <em>active client</em> of
|
|
145 |
* the framework, so it implements the preferred user interface for interacting
|
|
146 |
* with input methods. As a consequence, some key events do not reach the text
|
|
147 |
* component because they are handled by an input method, and some text input
|
|
148 |
* reaches the text component as committed text within an {@link
|
|
149 |
* java.awt.event.InputMethodEvent} instead of as a key event.
|
|
150 |
* The complete text input is the combination of the characters in
|
|
151 |
* <code>keyTyped</code> key events and committed text in input method events.
|
|
152 |
* <p>
|
|
153 |
* The AWT listener model lets applications attach event listeners to
|
|
154 |
* components in order to bind events to actions. Swing encourages the
|
|
155 |
* use of keymaps instead of listeners, but maintains compatibility
|
|
156 |
* with listeners by giving the listeners a chance to steal an event
|
|
157 |
* by consuming it.
|
|
158 |
* <p>
|
|
159 |
* Keyboard event and input method events are handled in the following stages,
|
|
160 |
* with each stage capable of consuming the event:
|
|
161 |
*
|
|
162 |
* <table border=1 summary="Stages of keyboard and input method event handling">
|
|
163 |
* <tr>
|
|
164 |
* <th id="stage"><p align="left">Stage</p></th>
|
|
165 |
* <th id="ke"><p align="left">KeyEvent</p></th>
|
|
166 |
* <th id="ime"><p align="left">InputMethodEvent</p></th></tr>
|
|
167 |
* <tr><td headers="stage">1. </td>
|
|
168 |
* <td headers="ke">input methods </td>
|
|
169 |
* <td headers="ime">(generated here)</td></tr>
|
|
170 |
* <tr><td headers="stage">2. </td>
|
|
171 |
* <td headers="ke">focus manager </td>
|
|
172 |
* <td headers="ime"></td>
|
|
173 |
* </tr>
|
|
174 |
* <tr>
|
|
175 |
* <td headers="stage">3. </td>
|
|
176 |
* <td headers="ke">registered key listeners</td>
|
|
177 |
* <td headers="ime">registered input method listeners</tr>
|
|
178 |
* <tr>
|
|
179 |
* <td headers="stage">4. </td>
|
|
180 |
* <td headers="ke"></td>
|
|
181 |
* <td headers="ime">input method handling in JTextComponent</tr>
|
|
182 |
* <tr>
|
|
183 |
* <td headers="stage">5. </td><td headers="ke ime" colspan=2>keymap handling using the current keymap</td></tr>
|
|
184 |
* <tr><td headers="stage">6. </td><td headers="ke">keyboard handling in JComponent (e.g. accelerators, component navigation, etc.)</td>
|
|
185 |
* <td headers="ime"></td></tr>
|
|
186 |
* </table>
|
|
187 |
*
|
|
188 |
* <p>
|
|
189 |
* To maintain compatibility with applications that listen to key
|
|
190 |
* events but are not aware of input method events, the input
|
|
191 |
* method handling in stage 4 provides a compatibility mode for
|
|
192 |
* components that do not process input method events. For these
|
|
193 |
* components, the committed text is converted to keyTyped key events
|
|
194 |
* and processed in the key event pipeline starting at stage 3
|
|
195 |
* instead of in the input method event pipeline.
|
|
196 |
* <p>
|
|
197 |
* By default the component will create a keymap (named <b>DEFAULT_KEYMAP</b>)
|
|
198 |
* that is shared by all JTextComponent instances as the default keymap.
|
|
199 |
* Typically a look-and-feel implementation will install a different keymap
|
|
200 |
* that resolves to the default keymap for those bindings not found in the
|
|
201 |
* different keymap. The minimal bindings include:
|
|
202 |
* <ul>
|
|
203 |
* <li>inserting content into the editor for the
|
|
204 |
* printable keys.
|
|
205 |
* <li>removing content with the backspace and del
|
|
206 |
* keys.
|
|
207 |
* <li>caret movement forward and backward
|
|
208 |
* </ul>
|
|
209 |
*
|
|
210 |
* <p>
|
|
211 |
* <dt><b><font size=+1>Model/View Split</font></b>
|
|
212 |
* <dd>
|
|
213 |
* The text components have a model-view split. A text component pulls
|
|
214 |
* together the objects used to represent the model, view, and controller.
|
|
215 |
* The text document model may be shared by other views which act as observers
|
|
216 |
* of the model (e.g. a document may be shared by multiple components).
|
|
217 |
*
|
|
218 |
* <p align=center><img src="doc-files/editor.gif" alt="Diagram showing interaction between Controller, Document, events, and ViewFactory"
|
|
219 |
* HEIGHT=358 WIDTH=587></p>
|
|
220 |
*
|
|
221 |
* <p>
|
|
222 |
* The model is defined by the {@link Document} interface.
|
|
223 |
* This is intended to provide a flexible text storage mechanism
|
|
224 |
* that tracks change during edits and can be extended to more sophisticated
|
|
225 |
* models. The model interfaces are meant to capture the capabilities of
|
|
226 |
* expression given by SGML, a system used to express a wide variety of
|
|
227 |
* content.
|
|
228 |
* Each modification to the document causes notification of the
|
|
229 |
* details of the change to be sent to all observers in the form of a
|
|
230 |
* {@link DocumentEvent} which allows the views to stay up to date with the model.
|
|
231 |
* This event is sent to observers that have implemented the
|
|
232 |
* {@link DocumentListener}
|
|
233 |
* interface and registered interest with the model being observed.
|
|
234 |
*
|
|
235 |
* <p>
|
|
236 |
* <dt><b><font size=+1>Location Information</font></b>
|
|
237 |
* <dd>
|
|
238 |
* The capability of determining the location of text in
|
|
239 |
* the view is provided. There are two methods, {@link #modelToView}
|
|
240 |
* and {@link #viewToModel} for determining this information.
|
|
241 |
*
|
|
242 |
* <p>
|
|
243 |
* <dt><b><font size=+1>Undo/Redo support</font></b>
|
|
244 |
* <dd>
|
|
245 |
* Support for an edit history mechanism is provided to allow
|
|
246 |
* undo/redo operations. The text component does not itself
|
|
247 |
* provide the history buffer by default, but does provide
|
|
248 |
* the <code>UndoableEdit</code> records that can be used in conjunction
|
|
249 |
* with a history buffer to provide the undo/redo support.
|
|
250 |
* The support is provided by the Document model, which allows
|
|
251 |
* one to attach UndoableEditListener implementations.
|
|
252 |
*
|
|
253 |
* <p>
|
|
254 |
* <dt><b><font size=+1>Thread Safety</font></b>
|
|
255 |
* <dd>
|
|
256 |
* The swing text components provide some support of thread
|
|
257 |
* safe operations. Because of the high level of configurability
|
|
258 |
* of the text components, it is possible to circumvent the
|
|
259 |
* protection provided. The protection primarily comes from
|
|
260 |
* the model, so the documentation of <code>AbstractDocument</code>
|
|
261 |
* describes the assumptions of the protection provided.
|
|
262 |
* The methods that are safe to call asynchronously are marked
|
|
263 |
* with comments.
|
|
264 |
*
|
|
265 |
* <p>
|
|
266 |
* <dt><b><font size=+1>Newlines</font></b>
|
|
267 |
* <dd>
|
|
268 |
* For a discussion on how newlines are handled, see
|
|
269 |
* <a href="DefaultEditorKit.html">DefaultEditorKit</a>.
|
|
270 |
*
|
|
271 |
* <p>
|
|
272 |
* <dt><b><font size=+1>Printing support</font></b>
|
|
273 |
* <dd>
|
|
274 |
* Several {@link #print print} methods are provided for basic
|
|
275 |
* document printing. If more advanced printing is needed, use the
|
|
276 |
* {@link #getPrintable} method.
|
|
277 |
* </dl>
|
|
278 |
*
|
|
279 |
* <p>
|
|
280 |
* <strong>Warning:</strong>
|
|
281 |
* Serialized objects of this class will not be compatible with
|
|
282 |
* future Swing releases. The current serialization support is
|
|
283 |
* appropriate for short term storage or RMI between applications running
|
|
284 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
285 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
286 |
* has been added to the <code>java.beans</code> package.
|
|
287 |
* Please see {@link java.beans.XMLEncoder}.
|
|
288 |
*
|
|
289 |
* @beaninfo
|
|
290 |
* attribute: isContainer false
|
|
291 |
*
|
|
292 |
* @author Timothy Prinzing
|
|
293 |
* @author Igor Kushnirskiy (printing support)
|
|
294 |
* @see Document
|
|
295 |
* @see DocumentEvent
|
|
296 |
* @see DocumentListener
|
|
297 |
* @see Caret
|
|
298 |
* @see CaretEvent
|
|
299 |
* @see CaretListener
|
|
300 |
* @see TextUI
|
|
301 |
* @see View
|
|
302 |
* @see ViewFactory
|
|
303 |
*/
|
|
304 |
public abstract class JTextComponent extends JComponent implements Scrollable, Accessible
|
|
305 |
{
|
|
306 |
/**
|
|
307 |
* Creates a new <code>JTextComponent</code>.
|
|
308 |
* Listeners for caret events are established, and the pluggable
|
|
309 |
* UI installed. The component is marked as editable. No layout manager
|
|
310 |
* is used, because layout is managed by the view subsystem of text.
|
|
311 |
* The document model is set to <code>null</code>.
|
|
312 |
*/
|
|
313 |
public JTextComponent() {
|
|
314 |
super();
|
|
315 |
// enable InputMethodEvent for on-the-spot pre-editing
|
|
316 |
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.INPUT_METHOD_EVENT_MASK);
|
|
317 |
caretEvent = new MutableCaretEvent(this);
|
|
318 |
addMouseListener(caretEvent);
|
|
319 |
addFocusListener(caretEvent);
|
|
320 |
setEditable(true);
|
|
321 |
setDragEnabled(false);
|
|
322 |
setLayout(null); // layout is managed by View hierarchy
|
|
323 |
updateUI();
|
|
324 |
}
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Fetches the user-interface factory for this text-oriented editor.
|
|
328 |
*
|
|
329 |
* @return the factory
|
|
330 |
*/
|
|
331 |
public TextUI getUI() { return (TextUI)ui; }
|
|
332 |
|
|
333 |
/**
|
|
334 |
* Sets the user-interface factory for this text-oriented editor.
|
|
335 |
*
|
|
336 |
* @param ui the factory
|
|
337 |
*/
|
|
338 |
public void setUI(TextUI ui) {
|
|
339 |
super.setUI(ui);
|
|
340 |
}
|
|
341 |
|
|
342 |
/**
|
|
343 |
* Reloads the pluggable UI. The key used to fetch the
|
|
344 |
* new interface is <code>getUIClassID()</code>. The type of
|
|
345 |
* the UI is <code>TextUI</code>. <code>invalidate</code>
|
|
346 |
* is called after setting the UI.
|
|
347 |
*/
|
|
348 |
public void updateUI() {
|
|
349 |
setUI((TextUI)UIManager.getUI(this));
|
|
350 |
invalidate();
|
|
351 |
}
|
|
352 |
|
|
353 |
/**
|
|
354 |
* Adds a caret listener for notification of any changes
|
|
355 |
* to the caret.
|
|
356 |
*
|
|
357 |
* @param listener the listener to be added
|
|
358 |
* @see javax.swing.event.CaretEvent
|
|
359 |
*/
|
|
360 |
public void addCaretListener(CaretListener listener) {
|
|
361 |
listenerList.add(CaretListener.class, listener);
|
|
362 |
}
|
|
363 |
|
|
364 |
/**
|
|
365 |
* Removes a caret listener.
|
|
366 |
*
|
|
367 |
* @param listener the listener to be removed
|
|
368 |
* @see javax.swing.event.CaretEvent
|
|
369 |
*/
|
|
370 |
public void removeCaretListener(CaretListener listener) {
|
|
371 |
listenerList.remove(CaretListener.class, listener);
|
|
372 |
}
|
|
373 |
|
|
374 |
/**
|
|
375 |
* Returns an array of all the caret listeners
|
|
376 |
* registered on this text component.
|
|
377 |
*
|
|
378 |
* @return all of this component's <code>CaretListener</code>s
|
|
379 |
* or an empty
|
|
380 |
* array if no caret listeners are currently registered
|
|
381 |
*
|
|
382 |
* @see #addCaretListener
|
|
383 |
* @see #removeCaretListener
|
|
384 |
*
|
|
385 |
* @since 1.4
|
|
386 |
*/
|
|
387 |
public CaretListener[] getCaretListeners() {
|
|
388 |
return (CaretListener[])listenerList.getListeners(CaretListener.class);
|
|
389 |
}
|
|
390 |
|
|
391 |
/**
|
|
392 |
* Notifies all listeners that have registered interest for
|
|
393 |
* notification on this event type. The event instance
|
|
394 |
* is lazily created using the parameters passed into
|
|
395 |
* the fire method. The listener list is processed in a
|
|
396 |
* last-to-first manner.
|
|
397 |
*
|
|
398 |
* @param e the event
|
|
399 |
* @see EventListenerList
|
|
400 |
*/
|
|
401 |
protected void fireCaretUpdate(CaretEvent e) {
|
|
402 |
// Guaranteed to return a non-null array
|
|
403 |
Object[] listeners = listenerList.getListenerList();
|
|
404 |
// Process the listeners last to first, notifying
|
|
405 |
// those that are interested in this event
|
|
406 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
407 |
if (listeners[i]==CaretListener.class) {
|
|
408 |
((CaretListener)listeners[i+1]).caretUpdate(e);
|
|
409 |
}
|
|
410 |
}
|
|
411 |
}
|
|
412 |
|
|
413 |
/**
|
|
414 |
* Associates the editor with a text document.
|
|
415 |
* The currently registered factory is used to build a view for
|
|
416 |
* the document, which gets displayed by the editor after revalidation.
|
|
417 |
* A PropertyChange event ("document") is propagated to each listener.
|
|
418 |
*
|
|
419 |
* @param doc the document to display/edit
|
|
420 |
* @see #getDocument
|
|
421 |
* @beaninfo
|
|
422 |
* description: the text document model
|
|
423 |
* bound: true
|
|
424 |
* expert: true
|
|
425 |
*/
|
|
426 |
public void setDocument(Document doc) {
|
|
427 |
Document old = model;
|
|
428 |
|
|
429 |
/*
|
|
430 |
* aquire a read lock on the old model to prevent notification of
|
|
431 |
* mutations while we disconnecting the old model.
|
|
432 |
*/
|
|
433 |
try {
|
|
434 |
if (old instanceof AbstractDocument) {
|
|
435 |
((AbstractDocument)old).readLock();
|
|
436 |
}
|
|
437 |
if (accessibleContext != null) {
|
|
438 |
model.removeDocumentListener(
|
|
439 |
((AccessibleJTextComponent)accessibleContext));
|
|
440 |
}
|
|
441 |
if (inputMethodRequestsHandler != null) {
|
|
442 |
model.removeDocumentListener((DocumentListener)inputMethodRequestsHandler);
|
|
443 |
}
|
|
444 |
model = doc;
|
|
445 |
|
|
446 |
// Set the document's run direction property to match the
|
|
447 |
// component's ComponentOrientation property.
|
|
448 |
Boolean runDir = getComponentOrientation().isLeftToRight()
|
|
449 |
? TextAttribute.RUN_DIRECTION_LTR
|
|
450 |
: TextAttribute.RUN_DIRECTION_RTL;
|
|
451 |
if (runDir != doc.getProperty(TextAttribute.RUN_DIRECTION)) {
|
|
452 |
doc.putProperty(TextAttribute.RUN_DIRECTION, runDir );
|
|
453 |
}
|
|
454 |
firePropertyChange("document", old, doc);
|
|
455 |
} finally {
|
|
456 |
if (old instanceof AbstractDocument) {
|
|
457 |
((AbstractDocument)old).readUnlock();
|
|
458 |
}
|
|
459 |
}
|
|
460 |
|
|
461 |
revalidate();
|
|
462 |
repaint();
|
|
463 |
if (accessibleContext != null) {
|
|
464 |
model.addDocumentListener(
|
|
465 |
((AccessibleJTextComponent)accessibleContext));
|
|
466 |
}
|
|
467 |
if (inputMethodRequestsHandler != null) {
|
|
468 |
model.addDocumentListener((DocumentListener)inputMethodRequestsHandler);
|
|
469 |
}
|
|
470 |
}
|
|
471 |
|
|
472 |
/**
|
|
473 |
* Fetches the model associated with the editor. This is
|
|
474 |
* primarily for the UI to get at the minimal amount of
|
|
475 |
* state required to be a text editor. Subclasses will
|
|
476 |
* return the actual type of the model which will typically
|
|
477 |
* be something that extends Document.
|
|
478 |
*
|
|
479 |
* @return the model
|
|
480 |
*/
|
|
481 |
public Document getDocument() {
|
|
482 |
return model;
|
|
483 |
}
|
|
484 |
|
|
485 |
// Override of Component.setComponentOrientation
|
|
486 |
public void setComponentOrientation( ComponentOrientation o ) {
|
|
487 |
// Set the document's run direction property to match the
|
|
488 |
// ComponentOrientation property.
|
|
489 |
Document doc = getDocument();
|
|
490 |
if( doc != null ) {
|
|
491 |
Boolean runDir = o.isLeftToRight()
|
|
492 |
? TextAttribute.RUN_DIRECTION_LTR
|
|
493 |
: TextAttribute.RUN_DIRECTION_RTL;
|
|
494 |
doc.putProperty( TextAttribute.RUN_DIRECTION, runDir );
|
|
495 |
}
|
|
496 |
super.setComponentOrientation( o );
|
|
497 |
}
|
|
498 |
|
|
499 |
/**
|
|
500 |
* Fetches the command list for the editor. This is
|
|
501 |
* the list of commands supported by the plugged-in UI
|
|
502 |
* augmented by the collection of commands that the
|
|
503 |
* editor itself supports. These are useful for binding
|
|
504 |
* to events, such as in a keymap.
|
|
505 |
*
|
|
506 |
* @return the command list
|
|
507 |
*/
|
|
508 |
public Action[] getActions() {
|
|
509 |
return getUI().getEditorKit(this).getActions();
|
|
510 |
}
|
|
511 |
|
|
512 |
/**
|
|
513 |
* Sets margin space between the text component's border
|
|
514 |
* and its text. The text component's default <code>Border</code>
|
|
515 |
* object will use this value to create the proper margin.
|
|
516 |
* However, if a non-default border is set on the text component,
|
|
517 |
* it is that <code>Border</code> object's responsibility to create the
|
|
518 |
* appropriate margin space (else this property will effectively
|
|
519 |
* be ignored). This causes a redraw of the component.
|
|
520 |
* A PropertyChange event ("margin") is sent to all listeners.
|
|
521 |
*
|
|
522 |
* @param m the space between the border and the text
|
|
523 |
* @beaninfo
|
|
524 |
* description: desired space between the border and text area
|
|
525 |
* bound: true
|
|
526 |
*/
|
|
527 |
public void setMargin(Insets m) {
|
|
528 |
Insets old = margin;
|
|
529 |
margin = m;
|
|
530 |
firePropertyChange("margin", old, m);
|
|
531 |
invalidate();
|
|
532 |
}
|
|
533 |
|
|
534 |
/**
|
|
535 |
* Returns the margin between the text component's border and
|
|
536 |
* its text.
|
|
537 |
*
|
|
538 |
* @return the margin
|
|
539 |
*/
|
|
540 |
public Insets getMargin() {
|
|
541 |
return margin;
|
|
542 |
}
|
|
543 |
|
|
544 |
/**
|
|
545 |
* Sets the <code>NavigationFilter</code>. <code>NavigationFilter</code>
|
|
546 |
* is used by <code>DefaultCaret</code> and the default cursor movement
|
|
547 |
* actions as a way to restrict the cursor movement.
|
|
548 |
*
|
|
549 |
* @since 1.4
|
|
550 |
*/
|
|
551 |
public void setNavigationFilter(NavigationFilter filter) {
|
|
552 |
navigationFilter = filter;
|
|
553 |
}
|
|
554 |
|
|
555 |
/**
|
|
556 |
* Returns the <code>NavigationFilter</code>. <code>NavigationFilter</code>
|
|
557 |
* is used by <code>DefaultCaret</code> and the default cursor movement
|
|
558 |
* actions as a way to restrict the cursor movement. A null return value
|
|
559 |
* implies the cursor movement and selection should not be restricted.
|
|
560 |
*
|
|
561 |
* @since 1.4
|
|
562 |
* @return the NavigationFilter
|
|
563 |
*/
|
|
564 |
public NavigationFilter getNavigationFilter() {
|
|
565 |
return navigationFilter;
|
|
566 |
}
|
|
567 |
|
|
568 |
/**
|
|
569 |
* Fetches the caret that allows text-oriented navigation over
|
|
570 |
* the view.
|
|
571 |
*
|
|
572 |
* @return the caret
|
|
573 |
*/
|
|
574 |
public Caret getCaret() {
|
|
575 |
return caret;
|
|
576 |
}
|
|
577 |
|
|
578 |
/**
|
|
579 |
* Sets the caret to be used. By default this will be set
|
|
580 |
* by the UI that gets installed. This can be changed to
|
|
581 |
* a custom caret if desired. Setting the caret results in a
|
|
582 |
* PropertyChange event ("caret") being fired.
|
|
583 |
*
|
|
584 |
* @param c the caret
|
|
585 |
* @see #getCaret
|
|
586 |
* @beaninfo
|
|
587 |
* description: the caret used to select/navigate
|
|
588 |
* bound: true
|
|
589 |
* expert: true
|
|
590 |
*/
|
|
591 |
public void setCaret(Caret c) {
|
|
592 |
if (caret != null) {
|
|
593 |
caret.removeChangeListener(caretEvent);
|
|
594 |
caret.deinstall(this);
|
|
595 |
}
|
|
596 |
Caret old = caret;
|
|
597 |
caret = c;
|
|
598 |
if (caret != null) {
|
|
599 |
caret.install(this);
|
|
600 |
caret.addChangeListener(caretEvent);
|
|
601 |
}
|
|
602 |
firePropertyChange("caret", old, caret);
|
|
603 |
}
|
|
604 |
|
|
605 |
/**
|
|
606 |
* Fetches the object responsible for making highlights.
|
|
607 |
*
|
|
608 |
* @return the highlighter
|
|
609 |
*/
|
|
610 |
public Highlighter getHighlighter() {
|
|
611 |
return highlighter;
|
|
612 |
}
|
|
613 |
|
|
614 |
/**
|
|
615 |
* Sets the highlighter to be used. By default this will be set
|
|
616 |
* by the UI that gets installed. This can be changed to
|
|
617 |
* a custom highlighter if desired. The highlighter can be set to
|
|
618 |
* <code>null</code> to disable it.
|
|
619 |
* A PropertyChange event ("highlighter") is fired
|
|
620 |
* when a new highlighter is installed.
|
|
621 |
*
|
|
622 |
* @param h the highlighter
|
|
623 |
* @see #getHighlighter
|
|
624 |
* @beaninfo
|
|
625 |
* description: object responsible for background highlights
|
|
626 |
* bound: true
|
|
627 |
* expert: true
|
|
628 |
*/
|
|
629 |
public void setHighlighter(Highlighter h) {
|
|
630 |
if (highlighter != null) {
|
|
631 |
highlighter.deinstall(this);
|
|
632 |
}
|
|
633 |
Highlighter old = highlighter;
|
|
634 |
highlighter = h;
|
|
635 |
if (highlighter != null) {
|
|
636 |
highlighter.install(this);
|
|
637 |
}
|
|
638 |
firePropertyChange("highlighter", old, h);
|
|
639 |
}
|
|
640 |
|
|
641 |
/**
|
|
642 |
* Sets the keymap to use for binding events to
|
|
643 |
* actions. Setting to <code>null</code> effectively disables
|
|
644 |
* keyboard input.
|
|
645 |
* A PropertyChange event ("keymap") is fired when a new keymap
|
|
646 |
* is installed.
|
|
647 |
*
|
|
648 |
* @param map the keymap
|
|
649 |
* @see #getKeymap
|
|
650 |
* @beaninfo
|
|
651 |
* description: set of key event to action bindings to use
|
|
652 |
* bound: true
|
|
653 |
*/
|
|
654 |
public void setKeymap(Keymap map) {
|
|
655 |
Keymap old = keymap;
|
|
656 |
keymap = map;
|
|
657 |
firePropertyChange("keymap", old, keymap);
|
|
658 |
updateInputMap(old, map);
|
|
659 |
}
|
|
660 |
|
|
661 |
/**
|
|
662 |
* Turns on or off automatic drag handling. In order to enable automatic
|
|
663 |
* drag handling, this property should be set to {@code true}, and the
|
|
664 |
* component's {@code TransferHandler} needs to be {@code non-null}.
|
|
665 |
* The default value of the {@code dragEnabled} property is {@code false}.
|
|
666 |
* <p>
|
|
667 |
* The job of honoring this property, and recognizing a user drag gesture,
|
|
668 |
* lies with the look and feel implementation, and in particular, the component's
|
|
669 |
* {@code TextUI}. When automatic drag handling is enabled, most look and
|
|
670 |
* feels (including those that subclass {@code BasicLookAndFeel}) begin a
|
|
671 |
* drag and drop operation whenever the user presses the mouse button over
|
|
672 |
* a selection and then moves the mouse a few pixels. Setting this property to
|
|
673 |
* {@code true} can therefore have a subtle effect on how selections behave.
|
|
674 |
* <p>
|
|
675 |
* If a look and feel is used that ignores this property, you can still
|
|
676 |
* begin a drag and drop operation by calling {@code exportAsDrag} on the
|
|
677 |
* component's {@code TransferHandler}.
|
|
678 |
*
|
|
679 |
* @param b whether or not to enable automatic drag handling
|
|
680 |
* @exception HeadlessException if
|
|
681 |
* <code>b</code> is <code>true</code> and
|
|
682 |
* <code>GraphicsEnvironment.isHeadless()</code>
|
|
683 |
* returns <code>true</code>
|
|
684 |
* @see java.awt.GraphicsEnvironment#isHeadless
|
|
685 |
* @see #getDragEnabled
|
|
686 |
* @see #setTransferHandler
|
|
687 |
* @see TransferHandler
|
|
688 |
* @since 1.4
|
|
689 |
*
|
|
690 |
* @beaninfo
|
|
691 |
* description: determines whether automatic drag handling is enabled
|
|
692 |
* bound: false
|
|
693 |
*/
|
|
694 |
public void setDragEnabled(boolean b) {
|
|
695 |
if (b && GraphicsEnvironment.isHeadless()) {
|
|
696 |
throw new HeadlessException();
|
|
697 |
}
|
|
698 |
dragEnabled = b;
|
|
699 |
}
|
|
700 |
|
|
701 |
/**
|
|
702 |
* Returns whether or not automatic drag handling is enabled.
|
|
703 |
*
|
|
704 |
* @return the value of the {@code dragEnabled} property
|
|
705 |
* @see #setDragEnabled
|
|
706 |
* @since 1.4
|
|
707 |
*/
|
|
708 |
public boolean getDragEnabled() {
|
|
709 |
return dragEnabled;
|
|
710 |
}
|
|
711 |
|
|
712 |
/**
|
|
713 |
* Sets the drop mode for this component. For backward compatibility,
|
|
714 |
* the default for this property is <code>DropMode.USE_SELECTION</code>.
|
|
715 |
* Usage of <code>DropMode.INSERT</code> is recommended, however,
|
|
716 |
* for an improved user experience. It offers similar behavior of dropping
|
|
717 |
* between text locations, but does so without affecting the actual text
|
|
718 |
* selection and caret location.
|
|
719 |
* <p>
|
|
720 |
* <code>JTextComponents</code> support the following drop modes:
|
|
721 |
* <ul>
|
|
722 |
* <li><code>DropMode.USE_SELECTION</code></li>
|
|
723 |
* <li><code>DropMode.INSERT</code></li>
|
|
724 |
* </ul>
|
|
725 |
* <p>
|
|
726 |
* The drop mode is only meaningful if this component has a
|
|
727 |
* <code>TransferHandler</code> that accepts drops.
|
|
728 |
*
|
|
729 |
* @param dropMode the drop mode to use
|
|
730 |
* @throws IllegalArgumentException if the drop mode is unsupported
|
|
731 |
* or <code>null</code>
|
|
732 |
* @see #getDropMode
|
|
733 |
* @see #getDropLocation
|
|
734 |
* @see #setTransferHandler
|
|
735 |
* @see javax.swing.TransferHandler
|
|
736 |
* @since 1.6
|
|
737 |
*/
|
|
738 |
public final void setDropMode(DropMode dropMode) {
|
|
739 |
if (dropMode != null) {
|
|
740 |
switch (dropMode) {
|
|
741 |
case USE_SELECTION:
|
|
742 |
case INSERT:
|
|
743 |
this.dropMode = dropMode;
|
|
744 |
return;
|
|
745 |
}
|
|
746 |
}
|
|
747 |
|
|
748 |
throw new IllegalArgumentException(dropMode + ": Unsupported drop mode for text");
|
|
749 |
}
|
|
750 |
|
|
751 |
/**
|
|
752 |
* Returns the drop mode for this component.
|
|
753 |
*
|
|
754 |
* @return the drop mode for this component
|
|
755 |
* @see #setDropMode
|
|
756 |
* @since 1.6
|
|
757 |
*/
|
|
758 |
public final DropMode getDropMode() {
|
|
759 |
return dropMode;
|
|
760 |
}
|
|
761 |
|
|
762 |
|
|
763 |
/**
|
|
764 |
* Calculates a drop location in this component, representing where a
|
|
765 |
* drop at the given point should insert data.
|
|
766 |
* <p>
|
|
767 |
* Note: This method is meant to override
|
|
768 |
* <code>JComponent.dropLocationForPoint()</code>, which is package-private
|
|
769 |
* in javax.swing. <code>TransferHandler</code> will detect text components
|
|
770 |
* and call this method instead via reflection. It's name should therefore
|
|
771 |
* not be changed.
|
|
772 |
*
|
|
773 |
* @param p the point to calculate a drop location for
|
|
774 |
* @return the drop location, or <code>null</code>
|
|
775 |
*/
|
|
776 |
DropLocation dropLocationForPoint(Point p) {
|
|
777 |
Position.Bias[] bias = new Position.Bias[1];
|
|
778 |
int index = getUI().viewToModel(this, p, bias);
|
|
779 |
|
|
780 |
// viewToModel currently returns null for some HTML content
|
|
781 |
// when the point is within the component's top inset
|
|
782 |
if (bias[0] == null) {
|
|
783 |
bias[0] = Position.Bias.Forward;
|
|
784 |
}
|
|
785 |
|
|
786 |
return new DropLocation(p, index, bias[0]);
|
|
787 |
}
|
|
788 |
|
|
789 |
/**
|
|
790 |
* Called to set or clear the drop location during a DnD operation.
|
|
791 |
* In some cases, the component may need to use it's internal selection
|
|
792 |
* temporarily to indicate the drop location. To help facilitate this,
|
|
793 |
* this method returns and accepts as a parameter a state object.
|
|
794 |
* This state object can be used to store, and later restore, the selection
|
|
795 |
* state. Whatever this method returns will be passed back to it in
|
|
796 |
* future calls, as the state parameter. If it wants the DnD system to
|
|
797 |
* continue storing the same state, it must pass it back every time.
|
|
798 |
* Here's how this is used:
|
|
799 |
* <p>
|
|
800 |
* Let's say that on the first call to this method the component decides
|
|
801 |
* to save some state (because it is about to use the selection to show
|
|
802 |
* a drop index). It can return a state object to the caller encapsulating
|
|
803 |
* any saved selection state. On a second call, let's say the drop location
|
|
804 |
* is being changed to something else. The component doesn't need to
|
|
805 |
* restore anything yet, so it simply passes back the same state object
|
|
806 |
* to have the DnD system continue storing it. Finally, let's say this
|
|
807 |
* method is messaged with <code>null</code>. This means DnD
|
|
808 |
* is finished with this component for now, meaning it should restore
|
|
809 |
* state. At this point, it can use the state parameter to restore
|
|
810 |
* said state, and of course return <code>null</code> since there's
|
|
811 |
* no longer anything to store.
|
|
812 |
* <p>
|
|
813 |
* Note: This method is meant to override
|
|
814 |
* <code>JComponent.setDropLocation()</code>, which is package-private
|
|
815 |
* in javax.swing. <code>TransferHandler</code> will detect text components
|
|
816 |
* and call this method instead via reflection. It's name should therefore
|
|
817 |
* not be changed.
|
|
818 |
*
|
|
819 |
* @param location the drop location (as calculated by
|
|
820 |
* <code>dropLocationForPoint</code>) or <code>null</code>
|
|
821 |
* if there's no longer a valid drop location
|
|
822 |
* @param state the state object saved earlier for this component,
|
|
823 |
* or <code>null</code>
|
|
824 |
* @param forDrop whether or not the method is being called because an
|
|
825 |
* actual drop occurred
|
|
826 |
* @return any saved state for this component, or <code>null</code> if none
|
|
827 |
*/
|
|
828 |
Object setDropLocation(TransferHandler.DropLocation location,
|
|
829 |
Object state,
|
|
830 |
boolean forDrop) {
|
|
831 |
|
|
832 |
Object retVal = null;
|
|
833 |
DropLocation textLocation = (DropLocation)location;
|
|
834 |
|
|
835 |
if (dropMode == DropMode.USE_SELECTION) {
|
|
836 |
if (textLocation == null) {
|
|
837 |
if (state != null) {
|
|
838 |
/*
|
|
839 |
* This object represents the state saved earlier.
|
|
840 |
* If the caret is a DefaultCaret it will be
|
|
841 |
* an Object array containing, in order:
|
|
842 |
* - the saved caret mark (Integer)
|
|
843 |
* - the saved caret dot (Integer)
|
|
844 |
* - the saved caret visibility (Boolean)
|
|
845 |
* - the saved mark bias (Position.Bias)
|
|
846 |
* - the saved dot bias (Position.Bias)
|
|
847 |
* If the caret is not a DefaultCaret it will
|
|
848 |
* be similar, but will not contain the dot
|
|
849 |
* or mark bias.
|
|
850 |
*/
|
|
851 |
Object[] vals = (Object[])state;
|
|
852 |
|
|
853 |
if (!forDrop) {
|
|
854 |
if (caret instanceof DefaultCaret) {
|
|
855 |
((DefaultCaret)caret).setDot(((Integer)vals[0]).intValue(),
|
|
856 |
(Position.Bias)vals[3]);
|
|
857 |
((DefaultCaret)caret).moveDot(((Integer)vals[1]).intValue(),
|
|
858 |
(Position.Bias)vals[4]);
|
|
859 |
} else {
|
|
860 |
caret.setDot(((Integer)vals[0]).intValue());
|
|
861 |
caret.moveDot(((Integer)vals[1]).intValue());
|
|
862 |
}
|
|
863 |
}
|
|
864 |
|
|
865 |
caret.setVisible(((Boolean)vals[2]).booleanValue());
|
|
866 |
}
|
|
867 |
} else {
|
|
868 |
if (dropLocation == null) {
|
|
869 |
boolean visible;
|
|
870 |
|
|
871 |
if (caret instanceof DefaultCaret) {
|
|
872 |
DefaultCaret dc = (DefaultCaret)caret;
|
|
873 |
visible = dc.isActive();
|
|
874 |
retVal = new Object[] {Integer.valueOf(dc.getMark()),
|
|
875 |
Integer.valueOf(dc.getDot()),
|
|
876 |
Boolean.valueOf(visible),
|
|
877 |
dc.getMarkBias(),
|
|
878 |
dc.getDotBias()};
|
|
879 |
} else {
|
|
880 |
visible = caret.isVisible();
|
|
881 |
retVal = new Object[] {Integer.valueOf(caret.getMark()),
|
|
882 |
Integer.valueOf(caret.getDot()),
|
|
883 |
Boolean.valueOf(visible)};
|
|
884 |
}
|
|
885 |
|
|
886 |
caret.setVisible(true);
|
|
887 |
} else {
|
|
888 |
retVal = state;
|
|
889 |
}
|
|
890 |
|
|
891 |
if (caret instanceof DefaultCaret) {
|
|
892 |
((DefaultCaret)caret).setDot(textLocation.getIndex(), textLocation.getBias());
|
|
893 |
} else {
|
|
894 |
caret.setDot(textLocation.getIndex());
|
|
895 |
}
|
|
896 |
}
|
|
897 |
} else {
|
|
898 |
if (textLocation == null) {
|
|
899 |
if (state != null) {
|
|
900 |
caret.setVisible(((Boolean)state).booleanValue());
|
|
901 |
}
|
|
902 |
} else {
|
|
903 |
if (dropLocation == null) {
|
|
904 |
boolean visible = caret instanceof DefaultCaret
|
|
905 |
? ((DefaultCaret)caret).isActive()
|
|
906 |
: caret.isVisible();
|
|
907 |
retVal = Boolean.valueOf(visible);
|
|
908 |
caret.setVisible(false);
|
|
909 |
} else {
|
|
910 |
retVal = state;
|
|
911 |
}
|
|
912 |
}
|
|
913 |
}
|
|
914 |
|
|
915 |
DropLocation old = dropLocation;
|
|
916 |
dropLocation = textLocation;
|
|
917 |
firePropertyChange("dropLocation", old, dropLocation);
|
|
918 |
|
|
919 |
return retVal;
|
|
920 |
}
|
|
921 |
|
|
922 |
/**
|
|
923 |
* Returns the location that this component should visually indicate
|
|
924 |
* as the drop location during a DnD operation over the component,
|
|
925 |
* or {@code null} if no location is to currently be shown.
|
|
926 |
* <p>
|
|
927 |
* This method is not meant for querying the drop location
|
|
928 |
* from a {@code TransferHandler}, as the drop location is only
|
|
929 |
* set after the {@code TransferHandler}'s <code>canImport</code>
|
|
930 |
* has returned and has allowed for the location to be shown.
|
|
931 |
* <p>
|
|
932 |
* When this property changes, a property change event with
|
|
933 |
* name "dropLocation" is fired by the component.
|
|
934 |
*
|
|
935 |
* @return the drop location
|
|
936 |
* @see #setDropMode
|
|
937 |
* @see TransferHandler#canImport(TransferHandler.TransferSupport)
|
|
938 |
* @since 1.6
|
|
939 |
*/
|
|
940 |
public final DropLocation getDropLocation() {
|
|
941 |
return dropLocation;
|
|
942 |
}
|
|
943 |
|
|
944 |
|
|
945 |
/**
|
|
946 |
* Updates the <code>InputMap</code>s in response to a
|
|
947 |
* <code>Keymap</code> change.
|
|
948 |
* @param oldKm the old <code>Keymap</code>
|
|
949 |
* @param newKm the new <code>Keymap</code>
|
|
950 |
*/
|
|
951 |
void updateInputMap(Keymap oldKm, Keymap newKm) {
|
|
952 |
// Locate the current KeymapWrapper.
|
|
953 |
InputMap km = getInputMap(JComponent.WHEN_FOCUSED);
|
|
954 |
InputMap last = km;
|
|
955 |
while (km != null && !(km instanceof KeymapWrapper)) {
|
|
956 |
last = km;
|
|
957 |
km = km.getParent();
|
|
958 |
}
|
|
959 |
if (km != null) {
|
|
960 |
// Found it, tweak the InputMap that points to it, as well
|
|
961 |
// as anything it points to.
|
|
962 |
if (newKm == null) {
|
|
963 |
if (last != km) {
|
|
964 |
last.setParent(km.getParent());
|
|
965 |
}
|
|
966 |
else {
|
|
967 |
last.setParent(null);
|
|
968 |
}
|
|
969 |
}
|
|
970 |
else {
|
|
971 |
InputMap newKM = new KeymapWrapper(newKm);
|
|
972 |
last.setParent(newKM);
|
|
973 |
if (last != km) {
|
|
974 |
newKM.setParent(km.getParent());
|
|
975 |
}
|
|
976 |
}
|
|
977 |
}
|
|
978 |
else if (newKm != null) {
|
|
979 |
km = getInputMap(JComponent.WHEN_FOCUSED);
|
|
980 |
if (km != null) {
|
|
981 |
// Couldn't find it.
|
|
982 |
// Set the parent of WHEN_FOCUSED InputMap to be the new one.
|
|
983 |
InputMap newKM = new KeymapWrapper(newKm);
|
|
984 |
newKM.setParent(km.getParent());
|
|
985 |
km.setParent(newKM);
|
|
986 |
}
|
|
987 |
}
|
|
988 |
|
|
989 |
// Do the same thing with the ActionMap
|
|
990 |
ActionMap am = getActionMap();
|
|
991 |
ActionMap lastAM = am;
|
|
992 |
while (am != null && !(am instanceof KeymapActionMap)) {
|
|
993 |
lastAM = am;
|
|
994 |
am = am.getParent();
|
|
995 |
}
|
|
996 |
if (am != null) {
|
|
997 |
// Found it, tweak the Actionap that points to it, as well
|
|
998 |
// as anything it points to.
|
|
999 |
if (newKm == null) {
|
|
1000 |
if (lastAM != am) {
|
|
1001 |
lastAM.setParent(am.getParent());
|
|
1002 |
}
|
|
1003 |
else {
|
|
1004 |
lastAM.setParent(null);
|
|
1005 |
}
|
|
1006 |
}
|
|
1007 |
else {
|
|
1008 |
ActionMap newAM = new KeymapActionMap(newKm);
|
|
1009 |
lastAM.setParent(newAM);
|
|
1010 |
if (lastAM != am) {
|
|
1011 |
newAM.setParent(am.getParent());
|
|
1012 |
}
|
|
1013 |
}
|
|
1014 |
}
|
|
1015 |
else if (newKm != null) {
|
|
1016 |
am = getActionMap();
|
|
1017 |
if (am != null) {
|
|
1018 |
// Couldn't find it.
|
|
1019 |
// Set the parent of ActionMap to be the new one.
|
|
1020 |
ActionMap newAM = new KeymapActionMap(newKm);
|
|
1021 |
newAM.setParent(am.getParent());
|
|
1022 |
am.setParent(newAM);
|
|
1023 |
}
|
|
1024 |
}
|
|
1025 |
}
|
|
1026 |
|
|
1027 |
/**
|
|
1028 |
* Fetches the keymap currently active in this text
|
|
1029 |
* component.
|
|
1030 |
*
|
|
1031 |
* @return the keymap
|
|
1032 |
*/
|
|
1033 |
public Keymap getKeymap() {
|
|
1034 |
return keymap;
|
|
1035 |
}
|
|
1036 |
|
|
1037 |
/**
|
|
1038 |
* Adds a new keymap into the keymap hierarchy. Keymap bindings
|
|
1039 |
* resolve from bottom up so an attribute specified in a child
|
|
1040 |
* will override an attribute specified in the parent.
|
|
1041 |
*
|
|
1042 |
* @param nm the name of the keymap (must be unique within the
|
|
1043 |
* collection of named keymaps in the document); the name may
|
|
1044 |
* be <code>null</code> if the keymap is unnamed,
|
|
1045 |
* but the caller is responsible for managing the reference
|
|
1046 |
* returned as an unnamed keymap can't
|
|
1047 |
* be fetched by name
|
|
1048 |
* @param parent the parent keymap; this may be <code>null</code> if
|
|
1049 |
* unspecified bindings need not be resolved in some other keymap
|
|
1050 |
* @return the keymap
|
|
1051 |
*/
|
|
1052 |
public static Keymap addKeymap(String nm, Keymap parent) {
|
|
1053 |
Keymap map = new DefaultKeymap(nm, parent);
|
|
1054 |
if (nm != null) {
|
|
1055 |
// add a named keymap, a class of bindings
|
|
1056 |
getKeymapTable().put(nm, map);
|
|
1057 |
}
|
|
1058 |
return map;
|
|
1059 |
}
|
|
1060 |
|
|
1061 |
/**
|
|
1062 |
* Removes a named keymap previously added to the document. Keymaps
|
|
1063 |
* with <code>null</code> names may not be removed in this way.
|
|
1064 |
*
|
|
1065 |
* @param nm the name of the keymap to remove
|
|
1066 |
* @return the keymap that was removed
|
|
1067 |
*/
|
|
1068 |
public static Keymap removeKeymap(String nm) {
|
|
1069 |
return getKeymapTable().remove(nm);
|
|
1070 |
}
|
|
1071 |
|
|
1072 |
/**
|
|
1073 |
* Fetches a named keymap previously added to the document.
|
|
1074 |
* This does not work with <code>null</code>-named keymaps.
|
|
1075 |
*
|
|
1076 |
* @param nm the name of the keymap
|
|
1077 |
* @return the keymap
|
|
1078 |
*/
|
|
1079 |
public static Keymap getKeymap(String nm) {
|
|
1080 |
return getKeymapTable().get(nm);
|
|
1081 |
}
|
|
1082 |
|
|
1083 |
private static HashMap<String,Keymap> getKeymapTable() {
|
|
1084 |
synchronized (KEYMAP_TABLE) {
|
|
1085 |
AppContext appContext = AppContext.getAppContext();
|
|
1086 |
HashMap<String,Keymap> keymapTable =
|
|
1087 |
(HashMap<String,Keymap>)appContext.get(KEYMAP_TABLE);
|
|
1088 |
if (keymapTable == null) {
|
|
1089 |
keymapTable = new HashMap<String,Keymap>(17);
|
|
1090 |
appContext.put(KEYMAP_TABLE, keymapTable);
|
|
1091 |
//initialize default keymap
|
|
1092 |
Keymap binding = addKeymap(DEFAULT_KEYMAP, null);
|
|
1093 |
binding.setDefaultAction(new
|
|
1094 |
DefaultEditorKit.DefaultKeyTypedAction());
|
|
1095 |
}
|
|
1096 |
return keymapTable;
|
|
1097 |
}
|
|
1098 |
}
|
|
1099 |
|
|
1100 |
/**
|
|
1101 |
* Binding record for creating key bindings.
|
|
1102 |
* <p>
|
|
1103 |
* <strong>Warning:</strong>
|
|
1104 |
* Serialized objects of this class will not be compatible with
|
|
1105 |
* future Swing releases. The current serialization support is
|
|
1106 |
* appropriate for short term storage or RMI between applications running
|
|
1107 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
1108 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
1109 |
* has been added to the <code>java.beans</code> package.
|
|
1110 |
* Please see {@link java.beans.XMLEncoder}.
|
|
1111 |
*/
|
|
1112 |
public static class KeyBinding {
|
|
1113 |
|
|
1114 |
/**
|
|
1115 |
* The key.
|
|
1116 |
*/
|
|
1117 |
public KeyStroke key;
|
|
1118 |
|
|
1119 |
/**
|
|
1120 |
* The name of the action for the key.
|
|
1121 |
*/
|
|
1122 |
public String actionName;
|
|
1123 |
|
|
1124 |
/**
|
|
1125 |
* Creates a new key binding.
|
|
1126 |
*
|
|
1127 |
* @param key the key
|
|
1128 |
* @param actionName the name of the action for the key
|
|
1129 |
*/
|
|
1130 |
public KeyBinding(KeyStroke key, String actionName) {
|
|
1131 |
this.key = key;
|
|
1132 |
this.actionName = actionName;
|
|
1133 |
}
|
|
1134 |
}
|
|
1135 |
|
|
1136 |
/**
|
|
1137 |
* <p>
|
|
1138 |
* Loads a keymap with a bunch of
|
|
1139 |
* bindings. This can be used to take a static table of
|
|
1140 |
* definitions and load them into some keymap. The following
|
|
1141 |
* example illustrates an example of binding some keys to
|
|
1142 |
* the cut, copy, and paste actions associated with a
|
|
1143 |
* JTextComponent. A code fragment to accomplish
|
|
1144 |
* this might look as follows:
|
|
1145 |
* <pre><code>
|
|
1146 |
*
|
|
1147 |
* static final JTextComponent.KeyBinding[] defaultBindings = {
|
|
1148 |
* new JTextComponent.KeyBinding(
|
|
1149 |
* KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),
|
|
1150 |
* DefaultEditorKit.copyAction),
|
|
1151 |
* new JTextComponent.KeyBinding(
|
|
1152 |
* KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK),
|
|
1153 |
* DefaultEditorKit.pasteAction),
|
|
1154 |
* new JTextComponent.KeyBinding(
|
|
1155 |
* KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK),
|
|
1156 |
* DefaultEditorKit.cutAction),
|
|
1157 |
* };
|
|
1158 |
*
|
|
1159 |
* JTextComponent c = new JTextPane();
|
|
1160 |
* Keymap k = c.getKeymap();
|
|
1161 |
* JTextComponent.loadKeymap(k, defaultBindings, c.getActions());
|
|
1162 |
*
|
|
1163 |
* </code></pre>
|
|
1164 |
* The sets of bindings and actions may be empty but must be
|
|
1165 |
* non-<code>null</code>.
|
|
1166 |
*
|
|
1167 |
* @param map the keymap
|
|
1168 |
* @param bindings the bindings
|
|
1169 |
* @param actions the set of actions
|
|
1170 |
*/
|
|
1171 |
public static void loadKeymap(Keymap map, KeyBinding[] bindings, Action[] actions) {
|
|
1172 |
Hashtable h = new Hashtable();
|
|
1173 |
for (int i = 0; i < actions.length; i++) {
|
|
1174 |
Action a = actions[i];
|
|
1175 |
String value = (String)a.getValue(Action.NAME);
|
|
1176 |
h.put((value!=null ? value:""), a);
|
|
1177 |
}
|
|
1178 |
for (int i = 0; i < bindings.length; i++) {
|
|
1179 |
Action a = (Action) h.get(bindings[i].actionName);
|
|
1180 |
if (a != null) {
|
|
1181 |
map.addActionForKeyStroke(bindings[i].key, a);
|
|
1182 |
}
|
|
1183 |
}
|
|
1184 |
}
|
|
1185 |
|
|
1186 |
/**
|
|
1187 |
* Returns true if <code>klass</code> is NOT a JTextComponent and it or
|
|
1188 |
* one of its superclasses (stoping at JTextComponent) overrides
|
|
1189 |
* <code>processInputMethodEvent</code>. It is assumed this will be
|
|
1190 |
* invoked from within a <code>doPrivileged</code>, and it is also
|
|
1191 |
* assumed <code>klass</code> extends <code>JTextComponent</code>.
|
|
1192 |
*/
|
|
1193 |
private static Boolean isProcessInputMethodEventOverridden(Class klass) {
|
|
1194 |
if (klass == JTextComponent.class) {
|
|
1195 |
return Boolean.FALSE;
|
|
1196 |
}
|
|
1197 |
Boolean retValue = (Boolean)overrideMap.get(klass.getName());
|
|
1198 |
|
|
1199 |
if (retValue != null) {
|
|
1200 |
return retValue;
|
|
1201 |
}
|
|
1202 |
Boolean sOverriden = isProcessInputMethodEventOverridden(
|
|
1203 |
klass.getSuperclass());
|
|
1204 |
|
|
1205 |
if (sOverriden.booleanValue()) {
|
|
1206 |
// If our superclass has overriden it, then by definition klass
|
|
1207 |
// overrides it.
|
|
1208 |
overrideMap.put(klass.getName(), sOverriden);
|
|
1209 |
return sOverriden;
|
|
1210 |
}
|
|
1211 |
// klass's superclass didn't override it, check for an override in
|
|
1212 |
// klass.
|
|
1213 |
try {
|
|
1214 |
Class[] classes = new Class[1];
|
|
1215 |
classes[0] = InputMethodEvent.class;
|
|
1216 |
|
|
1217 |
Method m = klass.getDeclaredMethod("processInputMethodEvent",
|
|
1218 |
classes);
|
|
1219 |
retValue = Boolean.TRUE;
|
|
1220 |
} catch (NoSuchMethodException nsme) {
|
|
1221 |
retValue = Boolean.FALSE;
|
|
1222 |
}
|
|
1223 |
overrideMap.put(klass.getName(), retValue);
|
|
1224 |
return retValue;
|
|
1225 |
}
|
|
1226 |
|
|
1227 |
/**
|
|
1228 |
* Fetches the current color used to render the
|
|
1229 |
* caret.
|
|
1230 |
*
|
|
1231 |
* @return the color
|
|
1232 |
*/
|
|
1233 |
public Color getCaretColor() {
|
|
1234 |
return caretColor;
|
|
1235 |
}
|
|
1236 |
|
|
1237 |
/**
|
|
1238 |
* Sets the current color used to render the caret.
|
|
1239 |
* Setting to <code>null</code> effectively restores the default color.
|
|
1240 |
* Setting the color results in a PropertyChange event ("caretColor")
|
|
1241 |
* being fired.
|
|
1242 |
*
|
|
1243 |
* @param c the color
|
|
1244 |
* @see #getCaretColor
|
|
1245 |
* @beaninfo
|
|
1246 |
* description: the color used to render the caret
|
|
1247 |
* bound: true
|
|
1248 |
* preferred: true
|
|
1249 |
*/
|
|
1250 |
public void setCaretColor(Color c) {
|
|
1251 |
Color old = caretColor;
|
|
1252 |
caretColor = c;
|
|
1253 |
firePropertyChange("caretColor", old, caretColor);
|
|
1254 |
}
|
|
1255 |
|
|
1256 |
/**
|
|
1257 |
* Fetches the current color used to render the
|
|
1258 |
* selection.
|
|
1259 |
*
|
|
1260 |
* @return the color
|
|
1261 |
*/
|
|
1262 |
public Color getSelectionColor() {
|
|
1263 |
return selectionColor;
|
|
1264 |
}
|
|
1265 |
|
|
1266 |
/**
|
|
1267 |
* Sets the current color used to render the selection.
|
|
1268 |
* Setting the color to <code>null</code> is the same as setting
|
|
1269 |
* <code>Color.white</code>. Setting the color results in a
|
|
1270 |
* PropertyChange event ("selectionColor").
|
|
1271 |
*
|
|
1272 |
* @param c the color
|
|
1273 |
* @see #getSelectionColor
|
|
1274 |
* @beaninfo
|
|
1275 |
* description: color used to render selection background
|
|
1276 |
* bound: true
|
|
1277 |
* preferred: true
|
|
1278 |
*/
|
|
1279 |
public void setSelectionColor(Color c) {
|
|
1280 |
Color old = selectionColor;
|
|
1281 |
selectionColor = c;
|
|
1282 |
firePropertyChange("selectionColor", old, selectionColor);
|
|
1283 |
}
|
|
1284 |
|
|
1285 |
/**
|
|
1286 |
* Fetches the current color used to render the
|
|
1287 |
* selected text.
|
|
1288 |
*
|
|
1289 |
* @return the color
|
|
1290 |
*/
|
|
1291 |
public Color getSelectedTextColor() {
|
|
1292 |
return selectedTextColor;
|
|
1293 |
}
|
|
1294 |
|
|
1295 |
/**
|
|
1296 |
* Sets the current color used to render the selected text.
|
|
1297 |
* Setting the color to <code>null</code> is the same as
|
|
1298 |
* <code>Color.black</code>. Setting the color results in a
|
|
1299 |
* PropertyChange event ("selectedTextColor") being fired.
|
|
1300 |
*
|
|
1301 |
* @param c the color
|
|
1302 |
* @see #getSelectedTextColor
|
|
1303 |
* @beaninfo
|
|
1304 |
* description: color used to render selected text
|
|
1305 |
* bound: true
|
|
1306 |
* preferred: true
|
|
1307 |
*/
|
|
1308 |
public void setSelectedTextColor(Color c) {
|
|
1309 |
Color old = selectedTextColor;
|
|
1310 |
selectedTextColor = c;
|
|
1311 |
firePropertyChange("selectedTextColor", old, selectedTextColor);
|
|
1312 |
}
|
|
1313 |
|
|
1314 |
/**
|
|
1315 |
* Fetches the current color used to render the
|
|
1316 |
* disabled text.
|
|
1317 |
*
|
|
1318 |
* @return the color
|
|
1319 |
*/
|
|
1320 |
public Color getDisabledTextColor() {
|
|
1321 |
return disabledTextColor;
|
|
1322 |
}
|
|
1323 |
|
|
1324 |
/**
|
|
1325 |
* Sets the current color used to render the
|
|
1326 |
* disabled text. Setting the color fires off a
|
|
1327 |
* PropertyChange event ("disabledTextColor").
|
|
1328 |
*
|
|
1329 |
* @param c the color
|
|
1330 |
* @see #getDisabledTextColor
|
|
1331 |
* @beaninfo
|
|
1332 |
* description: color used to render disabled text
|
|
1333 |
* bound: true
|
|
1334 |
* preferred: true
|
|
1335 |
*/
|
|
1336 |
public void setDisabledTextColor(Color c) {
|
|
1337 |
Color old = disabledTextColor;
|
|
1338 |
disabledTextColor = c;
|
|
1339 |
firePropertyChange("disabledTextColor", old, disabledTextColor);
|
|
1340 |
}
|
|
1341 |
|
|
1342 |
/**
|
|
1343 |
* Replaces the currently selected content with new content
|
|
1344 |
* represented by the given string. If there is no selection
|
|
1345 |
* this amounts to an insert of the given text. If there
|
|
1346 |
* is no replacement text this amounts to a removal of the
|
|
1347 |
* current selection.
|
|
1348 |
* <p>
|
|
1349 |
* This is the method that is used by the default implementation
|
|
1350 |
* of the action for inserting content that gets bound to the
|
|
1351 |
* keymap actions.
|
|
1352 |
* <p>
|
|
1353 |
* This method is thread safe, although most Swing methods
|
|
1354 |
* are not. Please see
|
|
1355 |
* <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
|
|
1356 |
* to Use Threads</A> for more information.
|
|
1357 |
*
|
|
1358 |
* @param content the content to replace the selection with
|
|
1359 |
*/
|
|
1360 |
public void replaceSelection(String content) {
|
|
1361 |
Document doc = getDocument();
|
|
1362 |
if (doc != null) {
|
|
1363 |
try {
|
|
1364 |
boolean composedTextSaved = saveComposedText(caret.getDot());
|
|
1365 |
int p0 = Math.min(caret.getDot(), caret.getMark());
|
|
1366 |
int p1 = Math.max(caret.getDot(), caret.getMark());
|
|
1367 |
if (doc instanceof AbstractDocument) {
|
|
1368 |
((AbstractDocument)doc).replace(p0, p1 - p0, content,null);
|
|
1369 |
}
|
|
1370 |
else {
|
|
1371 |
if (p0 != p1) {
|
|
1372 |
doc.remove(p0, p1 - p0);
|
|
1373 |
}
|
|
1374 |
if (content != null && content.length() > 0) {
|
|
1375 |
doc.insertString(p0, content, null);
|
|
1376 |
}
|
|
1377 |
}
|
|
1378 |
if (composedTextSaved) {
|
|
1379 |
restoreComposedText();
|
|
1380 |
}
|
|
1381 |
} catch (BadLocationException e) {
|
|
1382 |
UIManager.getLookAndFeel().provideErrorFeedback(JTextComponent.this);
|
|
1383 |
}
|
|
1384 |
}
|
|
1385 |
}
|
|
1386 |
|
|
1387 |
/**
|
|
1388 |
* Fetches a portion of the text represented by the
|
|
1389 |
* component. Returns an empty string if length is 0.
|
|
1390 |
*
|
|
1391 |
* @param offs the offset >= 0
|
|
1392 |
* @param len the length >= 0
|
|
1393 |
* @return the text
|
|
1394 |
* @exception BadLocationException if the offset or length are invalid
|
|
1395 |
*/
|
|
1396 |
public String getText(int offs, int len) throws BadLocationException {
|
|
1397 |
return getDocument().getText(offs, len);
|
|
1398 |
}
|
|
1399 |
|
|
1400 |
/**
|
|
1401 |
* Converts the given location in the model to a place in
|
|
1402 |
* the view coordinate system.
|
|
1403 |
* The component must have a positive size for
|
|
1404 |
* this translation to be computed (i.e. layout cannot
|
|
1405 |
* be computed until the component has been sized). The
|
|
1406 |
* component does not have to be visible or painted.
|
|
1407 |
*
|
|
1408 |
* @param pos the position >= 0
|
|
1409 |
* @return the coordinates as a rectangle, with (r.x, r.y) as the location
|
|
1410 |
* in the coordinate system, or null if the component does
|
|
1411 |
* not yet have a positive size.
|
|
1412 |
* @exception BadLocationException if the given position does not
|
|
1413 |
* represent a valid location in the associated document
|
|
1414 |
* @see TextUI#modelToView
|
|
1415 |
*/
|
|
1416 |
public Rectangle modelToView(int pos) throws BadLocationException {
|
|
1417 |
return getUI().modelToView(this, pos);
|
|
1418 |
}
|
|
1419 |
|
|
1420 |
/**
|
|
1421 |
* Converts the given place in the view coordinate system
|
|
1422 |
* to the nearest representative location in the model.
|
|
1423 |
* The component must have a positive size for
|
|
1424 |
* this translation to be computed (i.e. layout cannot
|
|
1425 |
* be computed until the component has been sized). The
|
|
1426 |
* component does not have to be visible or painted.
|
|
1427 |
*
|
|
1428 |
* @param pt the location in the view to translate
|
|
1429 |
* @return the offset >= 0 from the start of the document,
|
|
1430 |
* or -1 if the component does not yet have a positive
|
|
1431 |
* size.
|
|
1432 |
* @see TextUI#viewToModel
|
|
1433 |
*/
|
|
1434 |
public int viewToModel(Point pt) {
|
|
1435 |
return getUI().viewToModel(this, pt);
|
|
1436 |
}
|
|
1437 |
|
|
1438 |
/**
|
|
1439 |
* Transfers the currently selected range in the associated
|
|
1440 |
* text model to the system clipboard, removing the contents
|
|
1441 |
* from the model. The current selection is reset. Does nothing
|
|
1442 |
* for <code>null</code> selections.
|
|
1443 |
*
|
|
1444 |
* @see java.awt.Toolkit#getSystemClipboard
|
|
1445 |
* @see java.awt.datatransfer.Clipboard
|
|
1446 |
*/
|
|
1447 |
public void cut() {
|
|
1448 |
if (isEditable() && isEnabled()) {
|
|
1449 |
invokeAction("cut", TransferHandler.getCutAction());
|
|
1450 |
}
|
|
1451 |
}
|
|
1452 |
|
|
1453 |
/**
|
|
1454 |
* Transfers the currently selected range in the associated
|
|
1455 |
* text model to the system clipboard, leaving the contents
|
|
1456 |
* in the text model. The current selection remains intact.
|
|
1457 |
* Does nothing for <code>null</code> selections.
|
|
1458 |
*
|
|
1459 |
* @see java.awt.Toolkit#getSystemClipboard
|
|
1460 |
* @see java.awt.datatransfer.Clipboard
|
|
1461 |
*/
|
|
1462 |
public void copy() {
|
|
1463 |
invokeAction("copy", TransferHandler.getCopyAction());
|
|
1464 |
}
|
|
1465 |
|
|
1466 |
/**
|
|
1467 |
* Transfers the contents of the system clipboard into the
|
|
1468 |
* associated text model. If there is a selection in the
|
|
1469 |
* associated view, it is replaced with the contents of the
|
|
1470 |
* clipboard. If there is no selection, the clipboard contents
|
|
1471 |
* are inserted in front of the current insert position in
|
|
1472 |
* the associated view. If the clipboard is empty, does nothing.
|
|
1473 |
*
|
|
1474 |
* @see #replaceSelection
|
|
1475 |
* @see java.awt.Toolkit#getSystemClipboard
|
|
1476 |
* @see java.awt.datatransfer.Clipboard
|
|
1477 |
*/
|
|
1478 |
public void paste() {
|
|
1479 |
if (isEditable() && isEnabled()) {
|
|
1480 |
invokeAction("paste", TransferHandler.getPasteAction());
|
|
1481 |
}
|
|
1482 |
}
|
|
1483 |
|
|
1484 |
/**
|
|
1485 |
* This is a conveniance method that is only useful for
|
|
1486 |
* <code>cut</code>, <code>copy</code> and <code>paste</code>. If
|
|
1487 |
* an <code>Action</code> with the name <code>name</code> does not
|
|
1488 |
* exist in the <code>ActionMap</code>, this will attemp to install a
|
|
1489 |
* <code>TransferHandler</code> and then use <code>altAction</code>.
|
|
1490 |
*/
|
|
1491 |
private void invokeAction(String name, Action altAction) {
|
|
1492 |
ActionMap map = getActionMap();
|
|
1493 |
Action action = null;
|
|
1494 |
|
|
1495 |
if (map != null) {
|
|
1496 |
action = map.get(name);
|
|
1497 |
}
|
|
1498 |
if (action == null) {
|
|
1499 |
installDefaultTransferHandlerIfNecessary();
|
|
1500 |
action = altAction;
|
|
1501 |
}
|
|
1502 |
action.actionPerformed(new ActionEvent(this,
|
|
1503 |
ActionEvent.ACTION_PERFORMED, (String)action.
|
|
1504 |
getValue(Action.NAME),
|
|
1505 |
EventQueue.getMostRecentEventTime(),
|
|
1506 |
getCurrentEventModifiers()));
|
|
1507 |
}
|
|
1508 |
|
|
1509 |
/**
|
|
1510 |
* If the current <code>TransferHandler</code> is null, this will
|
|
1511 |
* install a new one.
|
|
1512 |
*/
|
|
1513 |
private void installDefaultTransferHandlerIfNecessary() {
|
|
1514 |
if (getTransferHandler() == null) {
|
|
1515 |
if (defaultTransferHandler == null) {
|
|
1516 |
defaultTransferHandler = new DefaultTransferHandler();
|
|
1517 |
}
|
|
1518 |
setTransferHandler(defaultTransferHandler);
|
|
1519 |
}
|
|
1520 |
}
|
|
1521 |
|
|
1522 |
/**
|
|
1523 |
* Moves the caret to a new position, leaving behind a mark
|
|
1524 |
* defined by the last time <code>setCaretPosition</code> was
|
|
1525 |
* called. This forms a selection.
|
|
1526 |
* If the document is <code>null</code>, does nothing. The position
|
|
1527 |
* must be between 0 and the length of the component's text or else
|
|
1528 |
* an exception is thrown.
|
|
1529 |
*
|
|
1530 |
* @param pos the position
|
|
1531 |
* @exception IllegalArgumentException if the value supplied
|
|
1532 |
* for <code>position</code> is less than zero or greater
|
|
1533 |
* than the component's text length
|
|
1534 |
* @see #setCaretPosition
|
|
1535 |
*/
|
|
1536 |
public void moveCaretPosition(int pos) {
|
|
1537 |
Document doc = getDocument();
|
|
1538 |
if (doc != null) {
|
|
1539 |
if (pos > doc.getLength() || pos < 0) {
|
|
1540 |
throw new IllegalArgumentException("bad position: " + pos);
|
|
1541 |
}
|
|
1542 |
caret.moveDot(pos);
|
|
1543 |
}
|
|
1544 |
}
|
|
1545 |
|
|
1546 |
/**
|
|
1547 |
* The bound property name for the focus accelerator.
|
|
1548 |
*/
|
|
1549 |
public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
|
|
1550 |
|
|
1551 |
/**
|
|
1552 |
* Sets the key accelerator that will cause the receiving text
|
|
1553 |
* component to get the focus. The accelerator will be the
|
|
1554 |
* key combination of the <em>alt</em> key and the character
|
|
1555 |
* given (converted to upper case). By default, there is no focus
|
|
1556 |
* accelerator key. Any previous key accelerator setting will be
|
|
1557 |
* superseded. A '\0' key setting will be registered, and has the
|
|
1558 |
* effect of turning off the focus accelerator. When the new key
|
|
1559 |
* is set, a PropertyChange event (FOCUS_ACCELERATOR_KEY) will be fired.
|
|
1560 |
*
|
|
1561 |
* @param aKey the key
|
|
1562 |
* @see #getFocusAccelerator
|
|
1563 |
* @beaninfo
|
|
1564 |
* description: accelerator character used to grab focus
|
|
1565 |
* bound: true
|
|
1566 |
*/
|
|
1567 |
public void setFocusAccelerator(char aKey) {
|
|
1568 |
aKey = Character.toUpperCase(aKey);
|
|
1569 |
char old = focusAccelerator;
|
|
1570 |
focusAccelerator = aKey;
|
|
1571 |
// Fix for 4341002: value of FOCUS_ACCELERATOR_KEY is wrong.
|
|
1572 |
// So we fire both FOCUS_ACCELERATOR_KEY, for compatibility,
|
|
1573 |
// and the correct event here.
|
|
1574 |
firePropertyChange(FOCUS_ACCELERATOR_KEY, old, focusAccelerator);
|
|
1575 |
firePropertyChange("focusAccelerator", old, focusAccelerator);
|
|
1576 |
}
|
|
1577 |
|
|
1578 |
/**
|
|
1579 |
* Returns the key accelerator that will cause the receiving
|
|
1580 |
* text component to get the focus. Return '\0' if no focus
|
|
1581 |
* accelerator has been set.
|
|
1582 |
*
|
|
1583 |
* @return the key
|
|
1584 |
*/
|
|
1585 |
public char getFocusAccelerator() {
|
|
1586 |
return focusAccelerator;
|
|
1587 |
}
|
|
1588 |
|
|
1589 |
/**
|
|
1590 |
* Initializes from a stream. This creates a
|
|
1591 |
* model of the type appropriate for the component
|
|
1592 |
* and initializes the model from the stream.
|
|
1593 |
* By default this will load the model as plain
|
|
1594 |
* text. Previous contents of the model are discarded.
|
|
1595 |
*
|
|
1596 |
* @param in the stream to read from
|
|
1597 |
* @param desc an object describing the stream; this
|
|
1598 |
* might be a string, a File, a URL, etc. Some kinds
|
|
1599 |
* of documents (such as html for example) might be
|
|
1600 |
* able to make use of this information; if non-<code>null</code>,
|
|
1601 |
* it is added as a property of the document
|
|
1602 |
* @exception IOException as thrown by the stream being
|
|
1603 |
* used to initialize
|
|
1604 |
* @see EditorKit#createDefaultDocument
|
|
1605 |
* @see #setDocument
|
|
1606 |
* @see PlainDocument
|
|
1607 |
*/
|
|
1608 |
public void read(Reader in, Object desc) throws IOException {
|
|
1609 |
EditorKit kit = getUI().getEditorKit(this);
|
|
1610 |
Document doc = kit.createDefaultDocument();
|
|
1611 |
if (desc != null) {
|
|
1612 |
doc.putProperty(Document.StreamDescriptionProperty, desc);
|
|
1613 |
}
|
|
1614 |
try {
|
|
1615 |
kit.read(in, doc, 0);
|
|
1616 |
setDocument(doc);
|
|
1617 |
} catch (BadLocationException e) {
|
|
1618 |
throw new IOException(e.getMessage());
|
|
1619 |
}
|
|
1620 |
}
|
|
1621 |
|
|
1622 |
/**
|
|
1623 |
* Stores the contents of the model into the given
|
|
1624 |
* stream. By default this will store the model as plain
|
|
1625 |
* text.
|
|
1626 |
*
|
|
1627 |
* @param out the output stream
|
|
1628 |
* @exception IOException on any I/O error
|
|
1629 |
*/
|
|
1630 |
public void write(Writer out) throws IOException {
|
|
1631 |
Document doc = getDocument();
|
|
1632 |
try {
|
|
1633 |
getUI().getEditorKit(this).write(out, doc, 0, doc.getLength());
|
|
1634 |
} catch (BadLocationException e) {
|
|
1635 |
throw new IOException(e.getMessage());
|
|
1636 |
}
|
|
1637 |
}
|
|
1638 |
|
|
1639 |
public void removeNotify() {
|
|
1640 |
super.removeNotify();
|
|
1641 |
if (getFocusedComponent() == this) {
|
|
1642 |
AppContext.getAppContext().remove(FOCUSED_COMPONENT);
|
|
1643 |
}
|
|
1644 |
}
|
|
1645 |
|
|
1646 |
// --- java.awt.TextComponent methods ------------------------
|
|
1647 |
|
|
1648 |
/**
|
|
1649 |
* Sets the position of the text insertion caret for the
|
|
1650 |
* <code>TextComponent</code>. Note that the caret tracks change,
|
|
1651 |
* so this may move if the underlying text of the component is changed.
|
|
1652 |
* If the document is <code>null</code>, does nothing. The position
|
|
1653 |
* must be between 0 and the length of the component's text or else
|
|
1654 |
* an exception is thrown.
|
|
1655 |
*
|
|
1656 |
* @param position the position
|
|
1657 |
* @exception IllegalArgumentException if the value supplied
|
|
1658 |
* for <code>position</code> is less than zero or greater
|
|
1659 |
* than the component's text length
|
|
1660 |
* @beaninfo
|
|
1661 |
* description: the caret position
|
|
1662 |
*/
|
|
1663 |
public void setCaretPosition(int position) {
|
|
1664 |
Document doc = getDocument();
|
|
1665 |
if (doc != null) {
|
|
1666 |
if (position > doc.getLength() || position < 0) {
|
|
1667 |
throw new IllegalArgumentException("bad position: " + position);
|
|
1668 |
}
|
|
1669 |
caret.setDot(position);
|
|
1670 |
}
|
|
1671 |
}
|
|
1672 |
|
|
1673 |
/**
|
|
1674 |
* Returns the position of the text insertion caret for the
|
|
1675 |
* text component.
|
|
1676 |
*
|
|
1677 |
* @return the position of the text insertion caret for the
|
|
1678 |
* text component >= 0
|
|
1679 |
*/
|
|
1680 |
public int getCaretPosition() {
|
|
1681 |
return caret.getDot();
|
|
1682 |
}
|
|
1683 |
|
|
1684 |
/**
|
|
1685 |
* Sets the text of this <code>TextComponent</code>
|
|
1686 |
* to the specified text. If the text is <code>null</code>
|
|
1687 |
* or empty, has the effect of simply deleting the old text.
|
|
1688 |
* When text has been inserted, the resulting caret location
|
|
1689 |
* is determined by the implementation of the caret class.
|
|
1690 |
* <p>
|
|
1691 |
* This method is thread safe, although most Swing methods
|
|
1692 |
* are not. Please see
|
|
1693 |
* <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
|
|
1694 |
* to Use Threads</A> for more information.
|
|
1695 |
*
|
|
1696 |
* Note that text is not a bound property, so no <code>PropertyChangeEvent
|
|
1697 |
* </code> is fired when it changes. To listen for changes to the text,
|
|
1698 |
* use <code>DocumentListener</code>.
|
|
1699 |
*
|
|
1700 |
* @param t the new text to be set
|
|
1701 |
* @see #getText
|
|
1702 |
* @see DefaultCaret
|
|
1703 |
* @beaninfo
|
|
1704 |
* description: the text of this component
|
|
1705 |
*/
|
|
1706 |
public void setText(String t) {
|
|
1707 |
try {
|
|
1708 |
Document doc = getDocument();
|
|
1709 |
if (doc instanceof AbstractDocument) {
|
|
1710 |
((AbstractDocument)doc).replace(0, doc.getLength(), t,null);
|
|
1711 |
}
|
|
1712 |
else {
|
|
1713 |
doc.remove(0, doc.getLength());
|
|
1714 |
doc.insertString(0, t, null);
|
|
1715 |
}
|
|
1716 |
} catch (BadLocationException e) {
|
|
1717 |
UIManager.getLookAndFeel().provideErrorFeedback(JTextComponent.this);
|
|
1718 |
}
|
|
1719 |
}
|
|
1720 |
|
|
1721 |
/**
|
|
1722 |
* Returns the text contained in this <code>TextComponent</code>.
|
|
1723 |
* If the underlying document is <code>null</code>,
|
|
1724 |
* will give a <code>NullPointerException</code>.
|
|
1725 |
*
|
|
1726 |
* Note that text is not a bound property, so no <code>PropertyChangeEvent
|
|
1727 |
* </code> is fired when it changes. To listen for changes to the text,
|
|
1728 |
* use <code>DocumentListener</code>.
|
|
1729 |
*
|
|
1730 |
* @return the text
|
|
1731 |
* @exception NullPointerException if the document is <code>null</code>
|
|
1732 |
* @see #setText
|
|
1733 |
*/
|
|
1734 |
public String getText() {
|
|
1735 |
Document doc = getDocument();
|
|
1736 |
String txt;
|
|
1737 |
try {
|
|
1738 |
txt = doc.getText(0, doc.getLength());
|
|
1739 |
} catch (BadLocationException e) {
|
|
1740 |
txt = null;
|
|
1741 |
}
|
|
1742 |
return txt;
|
|
1743 |
}
|
|
1744 |
|
|
1745 |
/**
|
|
1746 |
* Returns the selected text contained in this
|
|
1747 |
* <code>TextComponent</code>. If the selection is
|
|
1748 |
* <code>null</code> or the document empty, returns <code>null</code>.
|
|
1749 |
*
|
|
1750 |
* @return the text
|
|
1751 |
* @exception IllegalArgumentException if the selection doesn't
|
|
1752 |
* have a valid mapping into the document for some reason
|
|
1753 |
* @see #setText
|
|
1754 |
*/
|
|
1755 |
public String getSelectedText() {
|
|
1756 |
String txt = null;
|
|
1757 |
int p0 = Math.min(caret.getDot(), caret.getMark());
|
|
1758 |
int p1 = Math.max(caret.getDot(), caret.getMark());
|
|
1759 |
if (p0 != p1) {
|
|
1760 |
try {
|
|
1761 |
Document doc = getDocument();
|
|
1762 |
txt = doc.getText(p0, p1 - p0);
|
|
1763 |
} catch (BadLocationException e) {
|
|
1764 |
throw new IllegalArgumentException(e.getMessage());
|
|
1765 |
}
|
|
1766 |
}
|
|
1767 |
return txt;
|
|
1768 |
}
|
|
1769 |
|
|
1770 |
/**
|
|
1771 |
* Returns the boolean indicating whether this
|
|
1772 |
* <code>TextComponent</code> is editable or not.
|
|
1773 |
*
|
|
1774 |
* @return the boolean value
|
|
1775 |
* @see #setEditable
|
|
1776 |
*/
|
|
1777 |
public boolean isEditable() {
|
|
1778 |
return editable;
|
|
1779 |
}
|
|
1780 |
|
|
1781 |
/**
|
|
1782 |
* Sets the specified boolean to indicate whether or not this
|
|
1783 |
* <code>TextComponent</code> should be editable.
|
|
1784 |
* A PropertyChange event ("editable") is fired when the
|
|
1785 |
* state is changed.
|
|
1786 |
*
|
|
1787 |
* @param b the boolean to be set
|
|
1788 |
* @see #isEditable
|
|
1789 |
* @beaninfo
|
|
1790 |
* description: specifies if the text can be edited
|
|
1791 |
* bound: true
|
|
1792 |
*/
|
|
1793 |
public void setEditable(boolean b) {
|
|
1794 |
if (b != editable) {
|
|
1795 |
boolean oldVal = editable;
|
|
1796 |
editable = b;
|
|
1797 |
enableInputMethods(editable);
|
|
1798 |
firePropertyChange("editable", Boolean.valueOf(oldVal), Boolean.valueOf(editable));
|
|
1799 |
repaint();
|
|
1800 |
}
|
|
1801 |
}
|
|
1802 |
|
|
1803 |
/**
|
|
1804 |
* Returns the selected text's start position. Return 0 for an
|
|
1805 |
* empty document, or the value of dot if no selection.
|
|
1806 |
*
|
|
1807 |
* @return the start position >= 0
|
|
1808 |
*/
|
|
1809 |
public int getSelectionStart() {
|
|
1810 |
int start = Math.min(caret.getDot(), caret.getMark());
|
|
1811 |
return start;
|
|
1812 |
}
|
|
1813 |
|
|
1814 |
/**
|
|
1815 |
* Sets the selection start to the specified position. The new
|
|
1816 |
* starting point is constrained to be before or at the current
|
|
1817 |
* selection end.
|
|
1818 |
* <p>
|
|
1819 |
* This is available for backward compatibility to code
|
|
1820 |
* that called this method on <code>java.awt.TextComponent</code>.
|
|
1821 |
* This is implemented to forward to the <code>Caret</code>
|
|
1822 |
* implementation which is where the actual selection is maintained.
|
|
1823 |
*
|
|
1824 |
* @param selectionStart the start position of the text >= 0
|
|
1825 |
* @beaninfo
|
|
1826 |
* description: starting location of the selection.
|
|
1827 |
*/
|
|
1828 |
public void setSelectionStart(int selectionStart) {
|
|
1829 |
/* Route through select method to enforce consistent policy
|
|
1830 |
* between selectionStart and selectionEnd.
|
|
1831 |
*/
|
|
1832 |
select(selectionStart, getSelectionEnd());
|
|
1833 |
}
|
|
1834 |
|
|
1835 |
/**
|
|
1836 |
* Returns the selected text's end position. Return 0 if the document
|
|
1837 |
* is empty, or the value of dot if there is no selection.
|
|
1838 |
*
|
|
1839 |
* @return the end position >= 0
|
|
1840 |
*/
|
|
1841 |
public int getSelectionEnd() {
|
|
1842 |
int end = Math.max(caret.getDot(), caret.getMark());
|
|
1843 |
return end;
|
|
1844 |
}
|
|
1845 |
|
|
1846 |
/**
|
|
1847 |
* Sets the selection end to the specified position. The new
|
|
1848 |
* end point is constrained to be at or after the current
|
|
1849 |
* selection start.
|
|
1850 |
* <p>
|
|
1851 |
* This is available for backward compatibility to code
|
|
1852 |
* that called this method on <code>java.awt.TextComponent</code>.
|
|
1853 |
* This is implemented to forward to the <code>Caret</code>
|
|
1854 |
* implementation which is where the actual selection is maintained.
|
|
1855 |
*
|
|
1856 |
* @param selectionEnd the end position of the text >= 0
|
|
1857 |
* @beaninfo
|
|
1858 |
* description: ending location of the selection.
|
|
1859 |
*/
|
|
1860 |
public void setSelectionEnd(int selectionEnd) {
|
|
1861 |
/* Route through select method to enforce consistent policy
|
|
1862 |
* between selectionStart and selectionEnd.
|
|
1863 |
*/
|
|
1864 |
select(getSelectionStart(), selectionEnd);
|
|
1865 |
}
|
|
1866 |
|
|
1867 |
/**
|
|
1868 |
* Selects the text between the specified start and end positions.
|
|
1869 |
* <p>
|
|
1870 |
* This method sets the start and end positions of the
|
|
1871 |
* selected text, enforcing the restriction that the start position
|
|
1872 |
* must be greater than or equal to zero. The end position must be
|
|
1873 |
* greater than or equal to the start position, and less than or
|
|
1874 |
* equal to the length of the text component's text.
|
|
1875 |
* <p>
|
|
1876 |
* If the caller supplies values that are inconsistent or out of
|
|
1877 |
* bounds, the method enforces these constraints silently, and
|
|
1878 |
* without failure. Specifically, if the start position or end
|
|
1879 |
* position is greater than the length of the text, it is reset to
|
|
1880 |
* equal the text length. If the start position is less than zero,
|
|
1881 |
* it is reset to zero, and if the end position is less than the
|
|
1882 |
* start position, it is reset to the start position.
|
|
1883 |
* <p>
|
|
1884 |
* This call is provided for backward compatibility.
|
|
1885 |
* It is routed to a call to <code>setCaretPosition</code>
|
|
1886 |
* followed by a call to <code>moveCaretPosition</code>.
|
|
1887 |
* The preferred way to manage selection is by calling
|
|
1888 |
* those methods directly.
|
|
1889 |
*
|
|
1890 |
* @param selectionStart the start position of the text
|
|
1891 |
* @param selectionEnd the end position of the text
|
|
1892 |
* @see #setCaretPosition
|
|
1893 |
* @see #moveCaretPosition
|
|
1894 |
*/
|
|
1895 |
public void select(int selectionStart, int selectionEnd) {
|
|
1896 |
// argument adjustment done by java.awt.TextComponent
|
|
1897 |
int docLength = getDocument().getLength();
|
|
1898 |
|
|
1899 |
if (selectionStart < 0) {
|
|
1900 |
selectionStart = 0;
|
|
1901 |
}
|
|
1902 |
if (selectionStart > docLength) {
|
|
1903 |
selectionStart = docLength;
|
|
1904 |
}
|
|
1905 |
if (selectionEnd > docLength) {
|
|
1906 |
selectionEnd = docLength;
|
|
1907 |
}
|
|
1908 |
if (selectionEnd < selectionStart) {
|
|
1909 |
selectionEnd = selectionStart;
|
|
1910 |
}
|
|
1911 |
|
|
1912 |
setCaretPosition(selectionStart);
|
|
1913 |
moveCaretPosition(selectionEnd);
|
|
1914 |
}
|
|
1915 |
|
|
1916 |
/**
|
|
1917 |
* Selects all the text in the <code>TextComponent</code>.
|
|
1918 |
* Does nothing on a <code>null</code> or empty document.
|
|
1919 |
*/
|
|
1920 |
public void selectAll() {
|
|
1921 |
Document doc = getDocument();
|
|
1922 |
if (doc != null) {
|
|
1923 |
setCaretPosition(0);
|
|
1924 |
moveCaretPosition(doc.getLength());
|
|
1925 |
}
|
|
1926 |
}
|
|
1927 |
|
|
1928 |
// --- Tooltip Methods ---------------------------------------------
|
|
1929 |
|
|
1930 |
/**
|
|
1931 |
* Returns the string to be used as the tooltip for <code>event</code>.
|
|
1932 |
* This will return one of:
|
|
1933 |
* <ol>
|
|
1934 |
* <li>If <code>setToolTipText</code> has been invoked with a
|
|
1935 |
* non-<code>null</code>
|
|
1936 |
* value, it will be returned, otherwise
|
|
1937 |
* <li>The value from invoking <code>getToolTipText</code> on
|
|
1938 |
* the UI will be returned.
|
|
1939 |
* </ol>
|
|
1940 |
* By default <code>JTextComponent</code> does not register
|
|
1941 |
* itself with the <code>ToolTipManager</code>.
|
|
1942 |
* This means that tooltips will NOT be shown from the
|
|
1943 |
* <code>TextUI</code> unless <code>registerComponent</code> has
|
|
1944 |
* been invoked on the <code>ToolTipManager</code>.
|
|
1945 |
*
|
|
1946 |
* @param event the event in question
|
|
1947 |
* @return the string to be used as the tooltip for <code>event</code>
|
|
1948 |
* @see javax.swing.JComponent#setToolTipText
|
|
1949 |
* @see javax.swing.plaf.TextUI#getToolTipText
|
|
1950 |
* @see javax.swing.ToolTipManager#registerComponent
|
|
1951 |
*/
|
|
1952 |
public String getToolTipText(MouseEvent event) {
|
|
1953 |
String retValue = super.getToolTipText(event);
|
|
1954 |
|
|
1955 |
if (retValue == null) {
|
|
1956 |
TextUI ui = getUI();
|
|
1957 |
if (ui != null) {
|
|
1958 |
retValue = ui.getToolTipText(this, new Point(event.getX(),
|
|
1959 |
event.getY()));
|
|
1960 |
}
|
|
1961 |
}
|
|
1962 |
return retValue;
|
|
1963 |
}
|
|
1964 |
|
|
1965 |
// --- Scrollable methods ---------------------------------------------
|
|
1966 |
|
|
1967 |
/**
|
|
1968 |
* Returns the preferred size of the viewport for a view component.
|
|
1969 |
* This is implemented to do the default behavior of returning
|
|
1970 |
* the preferred size of the component.
|
|
1971 |
*
|
|
1972 |
* @return the <code>preferredSize</code> of a <code>JViewport</code>
|
|
1973 |
* whose view is this <code>Scrollable</code>
|
|
1974 |
*/
|
|
1975 |
public Dimension getPreferredScrollableViewportSize() {
|
|
1976 |
return getPreferredSize();
|
|
1977 |
}
|
|
1978 |
|
|
1979 |
|
|
1980 |
/**
|
|
1981 |
* Components that display logical rows or columns should compute
|
|
1982 |
* the scroll increment that will completely expose one new row
|
|
1983 |
* or column, depending on the value of orientation. Ideally,
|
|
1984 |
* components should handle a partially exposed row or column by
|
|
1985 |
* returning the distance required to completely expose the item.
|
|
1986 |
* <p>
|
|
1987 |
* The default implementation of this is to simply return 10% of
|
|
1988 |
* the visible area. Subclasses are likely to be able to provide
|
|
1989 |
* a much more reasonable value.
|
|
1990 |
*
|
|
1991 |
* @param visibleRect the view area visible within the viewport
|
|
1992 |
* @param orientation either <code>SwingConstants.VERTICAL</code> or
|
|
1993 |
* <code>SwingConstants.HORIZONTAL</code>
|
|
1994 |
* @param direction less than zero to scroll up/left, greater than
|
|
1995 |
* zero for down/right
|
|
1996 |
* @return the "unit" increment for scrolling in the specified direction
|
|
1997 |
* @exception IllegalArgumentException for an invalid orientation
|
|
1998 |
* @see JScrollBar#setUnitIncrement
|
|
1999 |
*/
|
|
2000 |
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
|
|
2001 |
switch(orientation) {
|
|
2002 |
case SwingConstants.VERTICAL:
|
|
2003 |
return visibleRect.height / 10;
|
|
2004 |
case SwingConstants.HORIZONTAL:
|
|
2005 |
return visibleRect.width / 10;
|
|
2006 |
default:
|
|
2007 |
throw new IllegalArgumentException("Invalid orientation: " + orientation);
|
|
2008 |
}
|
|
2009 |
}
|
|
2010 |
|
|
2011 |
|
|
2012 |
/**
|
|
2013 |
* Components that display logical rows or columns should compute
|
|
2014 |
* the scroll increment that will completely expose one block
|
|
2015 |
* of rows or columns, depending on the value of orientation.
|
|
2016 |
* <p>
|
|
2017 |
* The default implementation of this is to simply return the visible
|
|
2018 |
* area. Subclasses will likely be able to provide a much more
|
|
2019 |
* reasonable value.
|
|
2020 |
*
|
|
2021 |
* @param visibleRect the view area visible within the viewport
|
|
2022 |
* @param orientation either <code>SwingConstants.VERTICAL</code> or
|
|
2023 |
* <code>SwingConstants.HORIZONTAL</code>
|
|
2024 |
* @param direction less than zero to scroll up/left, greater than zero
|
|
2025 |
* for down/right
|
|
2026 |
* @return the "block" increment for scrolling in the specified direction
|
|
2027 |
* @exception IllegalArgumentException for an invalid orientation
|
|
2028 |
* @see JScrollBar#setBlockIncrement
|
|
2029 |
*/
|
|
2030 |
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
|
|
2031 |
switch(orientation) {
|
|
2032 |
case SwingConstants.VERTICAL:
|
|
2033 |
return visibleRect.height;
|
|
2034 |
case SwingConstants.HORIZONTAL:
|
|
2035 |
return visibleRect.width;
|
|
2036 |
default:
|
|
2037 |
throw new IllegalArgumentException("Invalid orientation: " + orientation);
|
|
2038 |
}
|
|
2039 |
}
|
|
2040 |
|
|
2041 |
|
|
2042 |
/**
|
|
2043 |
* Returns true if a viewport should always force the width of this
|
|
2044 |
* <code>Scrollable</code> to match the width of the viewport.
|
|
2045 |
* For example a normal text view that supported line wrapping
|
|
2046 |
* would return true here, since it would be undesirable for
|
|
2047 |
* wrapped lines to disappear beyond the right
|
|
2048 |
* edge of the viewport. Note that returning true for a
|
|
2049 |
* <code>Scrollable</code> whose ancestor is a <code>JScrollPane</code>
|
|
2050 |
* effectively disables horizontal scrolling.
|
|
2051 |
* <p>
|
|
2052 |
* Scrolling containers, like <code>JViewport</code>,
|
|
2053 |
* will use this method each time they are validated.
|
|
2054 |
*
|
|
2055 |
* @return true if a viewport should force the <code>Scrollable</code>s
|
|
2056 |
* width to match its own
|
|
2057 |
*/
|
|
2058 |
public boolean getScrollableTracksViewportWidth() {
|
|
2059 |
if (getParent() instanceof JViewport) {
|
|
2060 |
return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
|
|
2061 |
}
|
|
2062 |
return false;
|
|
2063 |
}
|
|
2064 |
|
|
2065 |
/**
|
|
2066 |
* Returns true if a viewport should always force the height of this
|
|
2067 |
* <code>Scrollable</code> to match the height of the viewport.
|
|
2068 |
* For example a columnar text view that flowed text in left to
|
|
2069 |
* right columns could effectively disable vertical scrolling by
|
|
2070 |
* returning true here.
|
|
2071 |
* <p>
|
|
2072 |
* Scrolling containers, like <code>JViewport</code>,
|
|
2073 |
* will use this method each time they are validated.
|
|
2074 |
*
|
|
2075 |
* @return true if a viewport should force the Scrollables height
|
|
2076 |
* to match its own
|
|
2077 |
*/
|
|
2078 |
public boolean getScrollableTracksViewportHeight() {
|
|
2079 |
if (getParent() instanceof JViewport) {
|
|
2080 |
return (((JViewport)getParent()).getHeight() > getPreferredSize().height);
|
|
2081 |
}
|
|
2082 |
return false;
|
|
2083 |
}
|
|
2084 |
|
|
2085 |
|
|
2086 |
//////////////////
|
|
2087 |
// Printing Support
|
|
2088 |
//////////////////
|
|
2089 |
|
|
2090 |
/**
|
|
2091 |
* A convenience print method that displays a print dialog, and then
|
|
2092 |
* prints this {@code JTextComponent} in <i>interactive</i> mode with no
|
|
2093 |
* header or footer text. Note: this method
|
|
2094 |
* blocks until printing is done.
|
|
2095 |
* <p>
|
|
2096 |
* Note: In <i>headless</i> mode, no dialogs will be shown.
|
|
2097 |
*
|
|
2098 |
* <p> This method calls the full featured
|
|
2099 |
* {@link #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean)
|
|
2100 |
* print} method to perform printing.
|
|
2101 |
* @return {@code true}, unless printing is canceled by the user
|
|
2102 |
* @throws PrinterException if an error in the print system causes the job
|
|
2103 |
* to be aborted
|
|
2104 |
* @throws SecurityException if this thread is not allowed to
|
|
2105 |
* initiate a print job request
|
|
2106 |
*
|
|
2107 |
* @see #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean)
|
|
2108 |
*
|
|
2109 |
* @since 1.6
|
|
2110 |
*/
|
|
2111 |
|
|
2112 |
public boolean print() throws PrinterException {
|
|
2113 |
return print(null, null, true, null, null, true);
|
|
2114 |
}
|
|
2115 |
|
|
2116 |
/**
|
|
2117 |
* A convenience print method that displays a print dialog, and then
|
|
2118 |
* prints this {@code JTextComponent} in <i>interactive</i> mode with
|
|
2119 |
* the specified header and footer text. Note: this method
|
|
2120 |
* blocks until printing is done.
|
|
2121 |
* <p>
|
|
2122 |
* Note: In <i>headless</i> mode, no dialogs will be shown.
|
|
2123 |
*
|
|
2124 |
* <p> This method calls the full featured
|
|
2125 |
* {@link #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean)
|
|
2126 |
* print} method to perform printing.
|
|
2127 |
* @param headerFormat the text, in {@code MessageFormat}, to be
|
|
2128 |
* used as the header, or {@code null} for no header
|
|
2129 |
* @param footerFormat the text, in {@code MessageFormat}, to be
|
|
2130 |
* used as the footer, or {@code null} for no footer
|
|
2131 |
* @return {@code true}, unless printing is canceled by the user
|
|
2132 |
* @throws PrinterException if an error in the print system causes the job
|
|
2133 |
* to be aborted
|
|
2134 |
* @throws SecurityException if this thread is not allowed to
|
|
2135 |
* initiate a print job request
|
|
2136 |
*
|
|
2137 |
* @see #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean)
|
|
2138 |
* @see java.text.MessageFormat
|
|
2139 |
* @since 1.6
|
|
2140 |
*/
|
|
2141 |
public boolean print(final MessageFormat headerFormat,
|
|
2142 |
final MessageFormat footerFormat) throws PrinterException {
|
|
2143 |
return print(headerFormat, footerFormat, true, null, null, true);
|
|
2144 |
}
|
|
2145 |
|
|
2146 |
/**
|
|
2147 |
* Prints the content of this {@code JTextComponent}. Note: this method
|
|
2148 |
* blocks until printing is done.
|
|
2149 |
*
|
|
2150 |
* <p>
|
|
2151 |
* Page header and footer text can be added to the output by providing
|
|
2152 |
* {@code MessageFormat} arguments. The printing code requests
|
|
2153 |
* {@code Strings} from the formats, providing a single item which may be
|
|
2154 |
* included in the formatted string: an {@code Integer} representing the
|
|
2155 |
* current page number.
|
|
2156 |
*
|
|
2157 |
* <p>
|
|
2158 |
* {@code showPrintDialog boolean} parameter allows you to specify whether
|
|
2159 |
* a print dialog is displayed to the user. When it is, the user
|
|
2160 |
* may use the dialog to change printing attributes or even cancel the
|
|
2161 |
* print.
|
|
2162 |
*
|
|
2163 |
* <p>
|
|
2164 |
* {@code service} allows you to provide the initial
|
|
2165 |
* {@code PrintService} for the print dialog, or to specify
|
|
2166 |
* {@code PrintService} to print to when the dialog is not shown.
|
|
2167 |
*
|
|
2168 |
* <p>
|
|
2169 |
* {@code attributes} can be used to provide the
|
|
2170 |
* initial values for the print dialog, or to supply any needed
|
|
2171 |
* attributes when the dialog is not shown. {@code attributes} can
|
|
2172 |
* be used to control how the job will print, for example
|
|
2173 |
* <i>duplex</i> or <i>single-sided</i>.
|
|
2174 |
*
|
|
2175 |
* <p>
|
|
2176 |
* {@code interactive boolean} parameter allows you to specify
|
|
2177 |
* whether to perform printing in <i>interactive</i>
|
|
2178 |
* mode. If {@code true}, a progress dialog, with an abort option,
|
|
2179 |
* is displayed for the duration of printing. This dialog is
|
|
2180 |
* <i>modal</i> when {@code print} is invoked on the <i>Event Dispatch
|
|
2181 |
* Thread</i> and <i>non-modal</i> otherwise. <b>Warning</b>:
|
|
2182 |
* calling this method on the <i>Event Dispatch Thread</i> with {@code
|
|
2183 |
* interactive false} blocks <i>all</i> events, including repaints, from
|
|
2184 |
* being processed until printing is complete. It is only
|
|
2185 |
* recommended when printing from an application with no
|
|
2186 |
* visible GUI.
|
|
2187 |
*
|
|
2188 |
* <p>
|
|
2189 |
* Note: In <i>headless</i> mode, {@code showPrintDialog} and
|
|
2190 |
* {@code interactive} parameters are ignored and no dialogs are
|
|
2191 |
* shown.
|
|
2192 |
*
|
|
2193 |
* <p>
|
|
2194 |
* This method ensures the {@code document} is not mutated during printing.
|
|
2195 |
* To indicate it visually, {@code setEnabled(false)} is set for the
|
|
2196 |
* duration of printing.
|
|
2197 |
*
|
|
2198 |
* <p>
|
|
2199 |
* This method uses {@link #getPrintable} to render document content.
|
|
2200 |
*
|
|
2201 |
* <p>
|
|
2202 |
* This method is thread-safe, although most Swing methods are not. Please
|
|
2203 |
* see <A
|
|
2204 |
* HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">
|
|
2205 |
* How to Use Threads</A> for more information.
|
|
2206 |
*
|
|
2207 |
* <p>
|
|
2208 |
* <b>Sample Usage</b>. This code snippet shows a cross-platform print
|
|
2209 |
* dialog and then prints the {@code JTextComponent} in <i>interactive</i> mode
|
|
2210 |
* unless the user cancels the dialog:
|
|
2211 |
*
|
|
2212 |
* <pre>
|
|
2213 |
* textComponent.print(new MessageFormat("My text component header"),
|
|
2214 |
* new MessageFormat("Footer. Page - {0}"), true, null, null, true);
|
|
2215 |
* </pre>
|
|
2216 |
* <p>
|
|
2217 |
* Executing this code off the <i>Event Dispatch Thread</i>
|
|
2218 |
* performs printing on the <i>background</i>.
|
|
2219 |
* The following pattern might be used for <i>background</i>
|
|
2220 |
* printing:
|
|
2221 |
* <pre>
|
|
2222 |
* FutureTask<Boolean> future =
|
|
2223 |
* new FutureTask<Boolean>(
|
|
2224 |
* new Callable<Boolean>() {
|
|
2225 |
* public Boolean call() {
|
|
2226 |
* return textComponent.print(.....);
|
|
2227 |
* }
|
|
2228 |
* });
|
|
2229 |
* executor.execute(future);
|
|
2230 |
* </pre>
|
|
2231 |
*
|
|
2232 |
* @param headerFormat the text, in {@code MessageFormat}, to be
|
|
2233 |
* used as the header, or {@code null} for no header
|
|
2234 |
* @param footerFormat the text, in {@code MessageFormat}, to be
|
|
2235 |
* used as the footer, or {@code null} for no footer
|
|
2236 |
* @param showPrintDialog {@code true} to display a print dialog,
|
|
2237 |
* {@code false} otherwise
|
|
2238 |
* @param service initial {@code PrintService}, or {@code null} for the
|
|
2239 |
* default
|
|
2240 |
* @param attributes the job attributes to be applied to the print job, or
|
|
2241 |
* {@code null} for none
|
|
2242 |
* @param interactive whether to print in an interactive mode
|
|
2243 |
* @return {@code true}, unless printing is canceled by the user
|
|
2244 |
* @throws PrinterException if an error in the print system causes the job
|
|
2245 |
* to be aborted
|
|
2246 |
* @throws SecurityException if this thread is not allowed to
|
|
2247 |
* initiate a print job request
|
|
2248 |
*
|
|
2249 |
* @see #getPrintable
|
|
2250 |
* @see java.text.MessageFormat
|
|
2251 |
* @see java.awt.GraphicsEnvironment#isHeadless
|
|
2252 |
* @see java.util.concurrent.FutureTask
|
|
2253 |
*
|
|
2254 |
* @since 1.6
|
|
2255 |
*/
|
|
2256 |
public boolean print(final MessageFormat headerFormat,
|
|
2257 |
final MessageFormat footerFormat,
|
|
2258 |
final boolean showPrintDialog,
|
|
2259 |
final PrintService service,
|
|
2260 |
final PrintRequestAttributeSet attributes,
|
|
2261 |
final boolean interactive)
|
|
2262 |
throws PrinterException {
|
|
2263 |
|
|
2264 |
final PrinterJob job = PrinterJob.getPrinterJob();
|
|
2265 |
final Printable printable;
|
|
2266 |
final PrintingStatus printingStatus;
|
|
2267 |
final boolean isHeadless = GraphicsEnvironment.isHeadless();
|
|
2268 |
final boolean isEventDispatchThread =
|
|
2269 |
SwingUtilities.isEventDispatchThread();
|
|
2270 |
final Printable textPrintable = getPrintable(headerFormat, footerFormat);
|
|
2271 |
if (interactive && ! isHeadless) {
|
|
2272 |
printingStatus =
|
|
2273 |
PrintingStatus.createPrintingStatus(this, job);
|
|
2274 |
printable =
|
|
2275 |
printingStatus.createNotificationPrintable(textPrintable);
|
|
2276 |
} else {
|
|
2277 |
printingStatus = null;
|
|
2278 |
printable = textPrintable;
|
|
2279 |
}
|
|
2280 |
|
|
2281 |
if (service != null) {
|
|
2282 |
job.setPrintService(service);
|
|
2283 |
}
|
|
2284 |
|
|
2285 |
job.setPrintable(printable);
|
|
2286 |
|
|
2287 |
final PrintRequestAttributeSet attr = (attributes == null)
|
|
2288 |
? new HashPrintRequestAttributeSet()
|
|
2289 |
: attributes;
|
|
2290 |
|
|
2291 |
if (showPrintDialog && ! isHeadless && ! job.printDialog(attr)) {
|
|
2292 |
return false;
|
|
2293 |
}
|
|
2294 |
|
|
2295 |
/*
|
|
2296 |
* there are three cases for printing:
|
|
2297 |
* 1. print non interactively (! interactive || isHeadless)
|
|
2298 |
* 2. print interactively off EDT
|
|
2299 |
* 3. print interactively on EDT
|
|
2300 |
*
|
|
2301 |
* 1 and 2 prints on the current thread (3 prints on another thread)
|
|
2302 |
* 2 and 3 deal with PrintingStatusDialog
|
|
2303 |
*/
|
|
2304 |
final Callable<Object> doPrint =
|
|
2305 |
new Callable<Object>() {
|
|
2306 |
public Object call() throws Exception {
|
|
2307 |
try {
|
|
2308 |
job.print(attr);
|
|
2309 |
} finally {
|
|
2310 |
if (printingStatus != null) {
|
|
2311 |
printingStatus.dispose();
|
|
2312 |
}
|
|
2313 |
}
|
|
2314 |
return null;
|
|
2315 |
}
|
|
2316 |
};
|
|
2317 |
|
|
2318 |
final FutureTask<Object> futurePrinting =
|
|
2319 |
new FutureTask<Object>(doPrint);
|
|
2320 |
|
|
2321 |
final Runnable runnablePrinting =
|
|
2322 |
new Runnable() {
|
|
2323 |
public void run() {
|
|
2324 |
//disable component
|
|
2325 |
boolean wasEnabled = false;
|
|
2326 |
if (isEventDispatchThread) {
|
|
2327 |
if (isEnabled()) {
|
|
2328 |
wasEnabled = true;
|
|
2329 |
setEnabled(false);
|
|
2330 |
}
|
|
2331 |
} else {
|
|
2332 |
try {
|
|
2333 |
wasEnabled = SwingUtilities2.submit(
|
|
2334 |
new Callable<Boolean>() {
|
|
2335 |
public Boolean call() throws Exception {
|
|
2336 |
boolean rv = isEnabled();
|
|
2337 |
if (rv) {
|
|
2338 |
setEnabled(false);
|
|
2339 |
}
|
|
2340 |
return rv;
|
|
2341 |
}
|
|
2342 |
}).get();
|
|
2343 |
} catch (InterruptedException e) {
|
|
2344 |
throw new RuntimeException(e);
|
|
2345 |
} catch (ExecutionException e) {
|
|
2346 |
Throwable cause = e.getCause();
|
|
2347 |
if (cause instanceof Error) {
|
|
2348 |
throw (Error) cause;
|
|
2349 |
}
|
|
2350 |
if (cause instanceof RuntimeException) {
|
|
2351 |
throw (RuntimeException) cause;
|
|
2352 |
}
|
|
2353 |
throw new AssertionError(cause);
|
|
2354 |
}
|
|
2355 |
}
|
|
2356 |
|
|
2357 |
getDocument().render(futurePrinting);
|
|
2358 |
|
|
2359 |
//enable component
|
|
2360 |
if (wasEnabled) {
|
|
2361 |
if (isEventDispatchThread) {
|
|
2362 |
setEnabled(true);
|
|
2363 |
} else {
|
|
2364 |
try {
|
|
2365 |
SwingUtilities2.submit(
|
|
2366 |
new Runnable() {
|
|
2367 |
public void run() {
|
|
2368 |
setEnabled(true);
|
|
2369 |
}
|
|
2370 |
}, null).get();
|
|
2371 |
} catch (InterruptedException e) {
|
|
2372 |
throw new RuntimeException(e);
|
|
2373 |
} catch (ExecutionException e) {
|
|
2374 |
Throwable cause = e.getCause();
|
|
2375 |
if (cause instanceof Error) {
|
|
2376 |
throw (Error) cause;
|
|
2377 |
}
|
|
2378 |
if (cause instanceof RuntimeException) {
|
|
2379 |
throw (RuntimeException) cause;
|
|
2380 |
}
|
|
2381 |
throw new AssertionError(cause);
|
|
2382 |
}
|
|
2383 |
}
|
|
2384 |
}
|
|
2385 |
}
|
|
2386 |
};
|
|
2387 |
|
|
2388 |
if (! interactive || isHeadless) {
|
|
2389 |
runnablePrinting.run();
|
|
2390 |
} else {
|
|
2391 |
if (isEventDispatchThread) {
|
|
2392 |
(new Thread(runnablePrinting)).start();
|
|
2393 |
printingStatus.showModal(true);
|
|
2394 |
} else {
|
|
2395 |
printingStatus.showModal(false);
|
|
2396 |
runnablePrinting.run();
|
|
2397 |
}
|
|
2398 |
}
|
|
2399 |
|
|
2400 |
//the printing is done successfully or otherwise.
|
|
2401 |
//dialog is hidden if needed.
|
|
2402 |
try {
|
|
2403 |
futurePrinting.get();
|
|
2404 |
} catch (InterruptedException e) {
|
|
2405 |
throw new RuntimeException(e);
|
|
2406 |
} catch (ExecutionException e) {
|
|
2407 |
Throwable cause = e.getCause();
|
|
2408 |
if (cause instanceof PrinterAbortException) {
|
|
2409 |
if (printingStatus != null
|
|
2410 |
&& printingStatus.isAborted()) {
|
|
2411 |
return false;
|
|
2412 |
} else {
|
|
2413 |
throw (PrinterAbortException) cause;
|
|
2414 |
}
|
|
2415 |
} else if (cause instanceof PrinterException) {
|
|
2416 |
throw (PrinterException) cause;
|
|
2417 |
} else if (cause instanceof RuntimeException) {
|
|
2418 |
throw (RuntimeException) cause;
|
|
2419 |
} else if (cause instanceof Error) {
|
|
2420 |
throw (Error) cause;
|
|
2421 |
} else {
|
|
2422 |
throw new AssertionError(cause);
|
|
2423 |
}
|
|
2424 |
}
|
|
2425 |
return true;
|
|
2426 |
}
|
|
2427 |
|
|
2428 |
|
|
2429 |
/**
|
|
2430 |
* Returns a {@code Printable} to use for printing the content of this
|
|
2431 |
* {@code JTextComponent}. The returned {@code Printable} prints
|
|
2432 |
* the document as it looks on the screen except being reformatted
|
|
2433 |
* to fit the paper.
|
|
2434 |
* The returned {@code Printable} can be wrapped inside another
|
|
2435 |
* {@code Printable} in order to create complex reports and
|
|
2436 |
* documents.
|
|
2437 |
*
|
|
2438 |
*
|
|
2439 |
* <p>
|
|
2440 |
* The returned {@code Printable} shares the {@code document} with this
|
|
2441 |
* {@code JTextComponent}. It is the responsibility of the developer to
|
|
2442 |
* ensure that the {@code document} is not mutated while this {@code Printable}
|
|
2443 |
* is used. Printing behavior is undefined when the {@code document} is
|
|
2444 |
* mutated during printing.
|
|
2445 |
*
|
|
2446 |
* <p>
|
|
2447 |
* Page header and footer text can be added to the output by providing
|
|
2448 |
* {@code MessageFormat} arguments. The printing code requests
|
|
2449 |
* {@code Strings} from the formats, providing a single item which may be
|
|
2450 |
* included in the formatted string: an {@code Integer} representing the
|
|
2451 |
* current page number.
|
|
2452 |
*
|
|
2453 |
* <p>
|
|
2454 |
* The returned {@code Printable} when printed, formats the
|
|
2455 |
* document content appropriately for the page size. For correct
|
|
2456 |
* line wrapping the {@code imageable width} of all pages must be the
|
|
2457 |
* same. See {@link java.awt.print.PageFormat#getImageableWidth}.
|
|
2458 |
*
|
|
2459 |
* <p>
|
|
2460 |
* This method is thread-safe, although most Swing methods are not. Please
|
|
2461 |
* see <A
|
|
2462 |
* HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">
|
|
2463 |
* How to Use Threads</A> for more information.
|
|
2464 |
*
|
|
2465 |
* <p>
|
|
2466 |
* The returned {@code Printable} can be printed on any thread.
|
|
2467 |
*
|
|
2468 |
* <p>
|
|
2469 |
* This implementation returned {@code Printable} performs all painting on
|
|
2470 |
* the <i>Event Dispatch Thread</i>, regardless of what thread it is
|
|
2471 |
* used on.
|
|
2472 |
*
|
|
2473 |
* @param headerFormat the text, in {@code MessageFormat}, to be
|
|
2474 |
* used as the header, or {@code null} for no header
|
|
2475 |
* @param footerFormat the text, in {@code MessageFormat}, to be
|
|
2476 |
* used as the footer, or {@code null} for no footer
|
|
2477 |
* @return a {@code Printable} for use in printing content of this
|
|
2478 |
* {@code JTextComponent}
|
|
2479 |
*
|
|
2480 |
*
|
|
2481 |
* @see java.awt.print.Printable
|
|
2482 |
* @see java.awt.print.PageFormat
|
|
2483 |
* @see javax.swing.text.Document#render(java.lang.Runnable)
|
|
2484 |
*
|
|
2485 |
* @since 1.6
|
|
2486 |
*/
|
|
2487 |
public Printable getPrintable(final MessageFormat headerFormat,
|
|
2488 |
final MessageFormat footerFormat) {
|
|
2489 |
return TextComponentPrintable.getPrintable(
|
|
2490 |
this, headerFormat, footerFormat);
|
|
2491 |
}
|
|
2492 |
|
|
2493 |
|
|
2494 |
/////////////////
|
|
2495 |
// Accessibility support
|
|
2496 |
////////////////
|
|
2497 |
|
|
2498 |
|
|
2499 |
/**
|
|
2500 |
* Gets the <code>AccessibleContext</code> associated with this
|
|
2501 |
* <code>JTextComponent</code>. For text components,
|
|
2502 |
* the <code>AccessibleContext</code> takes the form of an
|
|
2503 |
* <code>AccessibleJTextComponent</code>.
|
|
2504 |
* A new <code>AccessibleJTextComponent</code> instance
|
|
2505 |
* is created if necessary.
|
|
2506 |
*
|
|
2507 |
* @return an <code>AccessibleJTextComponent</code> that serves as the
|
|
2508 |
* <code>AccessibleContext</code> of this
|
|
2509 |
* <code>JTextComponent</code>
|
|
2510 |
*/
|
|
2511 |
public AccessibleContext getAccessibleContext() {
|
|
2512 |
if (accessibleContext == null) {
|
|
2513 |
accessibleContext = new AccessibleJTextComponent();
|
|
2514 |
}
|
|
2515 |
return accessibleContext;
|
|
2516 |
}
|
|
2517 |
|
|
2518 |
/**
|
|
2519 |
* This class implements accessibility support for the
|
|
2520 |
* <code>JTextComponent</code> class. It provides an implementation of
|
|
2521 |
* the Java Accessibility API appropriate to menu user-interface elements.
|
|
2522 |
* <p>
|
|
2523 |
* <strong>Warning:</strong>
|
|
2524 |
* Serialized objects of this class will not be compatible with
|
|
2525 |
* future Swing releases. The current serialization support is
|
|
2526 |
* appropriate for short term storage or RMI between applications running
|
|
2527 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
2528 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
2529 |
* has been added to the <code>java.beans</code> package.
|
|
2530 |
* Please see {@link java.beans.XMLEncoder}.
|
|
2531 |
*/
|
|
2532 |
public class AccessibleJTextComponent extends AccessibleJComponent
|
|
2533 |
implements AccessibleText, CaretListener, DocumentListener,
|
|
2534 |
AccessibleAction, AccessibleEditableText,
|
|
2535 |
AccessibleExtendedText {
|
|
2536 |
|
|
2537 |
int caretPos;
|
|
2538 |
Point oldLocationOnScreen;
|
|
2539 |
|
|
2540 |
/**
|
|
2541 |
* Constructs an AccessibleJTextComponent. Adds a listener to track
|
|
2542 |
* caret change.
|
|
2543 |
*/
|
|
2544 |
public AccessibleJTextComponent() {
|
|
2545 |
Document doc = JTextComponent.this.getDocument();
|
|
2546 |
if (doc != null) {
|
|
2547 |
doc.addDocumentListener(this);
|
|
2548 |
}
|
|
2549 |
JTextComponent.this.addCaretListener(this);
|
|
2550 |
caretPos = getCaretPosition();
|
|
2551 |
|
|
2552 |
try {
|
|
2553 |
oldLocationOnScreen = getLocationOnScreen();
|
|
2554 |
} catch (IllegalComponentStateException iae) {
|
|
2555 |
}
|
|
2556 |
|
|
2557 |
// Fire a ACCESSIBLE_VISIBLE_DATA_PROPERTY PropertyChangeEvent
|
|
2558 |
// when the text component moves (e.g., when scrolling).
|
|
2559 |
// Using an anonymous class since making AccessibleJTextComponent
|
|
2560 |
// implement ComponentListener would be an API change.
|
|
2561 |
JTextComponent.this.addComponentListener(new ComponentAdapter() {
|
|
2562 |
|
|
2563 |
public void componentMoved(ComponentEvent e) {
|
|
2564 |
try {
|
|
2565 |
Point newLocationOnScreen = getLocationOnScreen();
|
|
2566 |
firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY,
|
|
2567 |
oldLocationOnScreen,
|
|
2568 |
newLocationOnScreen);
|
|
2569 |
|
|
2570 |
oldLocationOnScreen = newLocationOnScreen;
|
|
2571 |
} catch (IllegalComponentStateException iae) {
|
|
2572 |
}
|
|
2573 |
}
|
|
2574 |
});
|
|
2575 |
}
|
|
2576 |
|
|
2577 |
/**
|
|
2578 |
* Handles caret updates (fire appropriate property change event,
|
|
2579 |
* which are AccessibleContext.ACCESSIBLE_CARET_PROPERTY and
|
|
2580 |
* AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY).
|
|
2581 |
* This keeps track of the dot position internally. When the caret
|
|
2582 |
* moves, the internal position is updated after firing the event.
|
|
2583 |
*
|
|
2584 |
* @param e the CaretEvent
|
|
2585 |
*/
|
|
2586 |
public void caretUpdate(CaretEvent e) {
|
|
2587 |
int dot = e.getDot();
|
|
2588 |
int mark = e.getMark();
|
|
2589 |
if (caretPos != dot) {
|
|
2590 |
// the caret moved
|
|
2591 |
firePropertyChange(ACCESSIBLE_CARET_PROPERTY,
|
|
2592 |
new Integer(caretPos), new Integer(dot));
|
|
2593 |
caretPos = dot;
|
|
2594 |
|
|
2595 |
try {
|
|
2596 |
oldLocationOnScreen = getLocationOnScreen();
|
|
2597 |
} catch (IllegalComponentStateException iae) {
|
|
2598 |
}
|
|
2599 |
}
|
|
2600 |
if (mark != dot) {
|
|
2601 |
// there is a selection
|
|
2602 |
firePropertyChange(ACCESSIBLE_SELECTION_PROPERTY, null,
|
|
2603 |
getSelectedText());
|
|
2604 |
}
|
|
2605 |
}
|
|
2606 |
|
|
2607 |
// DocumentListener methods
|
|
2608 |
|
|
2609 |
/**
|
|
2610 |
* Handles document insert (fire appropriate property change event
|
|
2611 |
* which is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY).
|
|
2612 |
* This tracks the changed offset via the event.
|
|
2613 |
*
|
|
2614 |
* @param e the DocumentEvent
|
|
2615 |
*/
|
|
2616 |
public void insertUpdate(DocumentEvent e) {
|
|
2617 |
final Integer pos = new Integer (e.getOffset());
|
|
2618 |
if (SwingUtilities.isEventDispatchThread()) {
|
|
2619 |
firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, pos);
|
|
2620 |
} else {
|
|
2621 |
Runnable doFire = new Runnable() {
|
|
2622 |
public void run() {
|
|
2623 |
firePropertyChange(ACCESSIBLE_TEXT_PROPERTY,
|
|
2624 |
null, pos);
|
|
2625 |
}
|
|
2626 |
};
|
|
2627 |
SwingUtilities.invokeLater(doFire);
|
|
2628 |
}
|
|
2629 |
}
|
|
2630 |
|
|
2631 |
/**
|
|
2632 |
* Handles document remove (fire appropriate property change event,
|
|
2633 |
* which is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY).
|
|
2634 |
* This tracks the changed offset via the event.
|
|
2635 |
*
|
|
2636 |
* @param e the DocumentEvent
|
|
2637 |
*/
|
|
2638 |
public void removeUpdate(DocumentEvent e) {
|
|
2639 |
final Integer pos = new Integer (e.getOffset());
|
|
2640 |
if (SwingUtilities.isEventDispatchThread()) {
|
|
2641 |
firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, pos);
|
|
2642 |
} else {
|
|
2643 |
Runnable doFire = new Runnable() {
|
|
2644 |
public void run() {
|
|
2645 |
firePropertyChange(ACCESSIBLE_TEXT_PROPERTY,
|
|
2646 |
null, pos);
|
|
2647 |
}
|
|
2648 |
};
|
|
2649 |
SwingUtilities.invokeLater(doFire);
|
|
2650 |
}
|
|
2651 |
}
|
|
2652 |
|
|
2653 |
/**
|
|
2654 |
* Handles document remove (fire appropriate property change event,
|
|
2655 |
* which is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY).
|
|
2656 |
* This tracks the changed offset via the event.
|
|
2657 |
*
|
|
2658 |
* @param e the DocumentEvent
|
|
2659 |
*/
|
|
2660 |
public void changedUpdate(DocumentEvent e) {
|
|
2661 |
final Integer pos = new Integer (e.getOffset());
|
|
2662 |
if (SwingUtilities.isEventDispatchThread()) {
|
|
2663 |
firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, pos);
|
|
2664 |
} else {
|
|
2665 |
Runnable doFire = new Runnable() {
|
|
2666 |
public void run() {
|
|
2667 |
firePropertyChange(ACCESSIBLE_TEXT_PROPERTY,
|
|
2668 |
null, pos);
|
|
2669 |
}
|
|
2670 |
};
|
|
2671 |
SwingUtilities.invokeLater(doFire);
|
|
2672 |
}
|
|
2673 |
}
|
|
2674 |
|
|
2675 |
/**
|
|
2676 |
* Gets the state set of the JTextComponent.
|
|
2677 |
* The AccessibleStateSet of an object is composed of a set of
|
|
2678 |
* unique AccessibleState's. A change in the AccessibleStateSet
|
|
2679 |
* of an object will cause a PropertyChangeEvent to be fired
|
|
2680 |
* for the AccessibleContext.ACCESSIBLE_STATE_PROPERTY property.
|
|
2681 |
*
|
|
2682 |
* @return an instance of AccessibleStateSet containing the
|
|
2683 |
* current state set of the object
|
|
2684 |
* @see AccessibleStateSet
|
|
2685 |
* @see AccessibleState
|
|
2686 |
* @see #addPropertyChangeListener
|
|
2687 |
*/
|
|
2688 |
public AccessibleStateSet getAccessibleStateSet() {
|
|
2689 |
AccessibleStateSet states = super.getAccessibleStateSet();
|
|
2690 |
if (JTextComponent.this.isEditable()) {
|
|
2691 |
states.add(AccessibleState.EDITABLE);
|
|
2692 |
}
|
|
2693 |
return states;
|
|
2694 |
}
|
|
2695 |
|
|
2696 |
|
|
2697 |
/**
|
|
2698 |
* Gets the role of this object.
|
|
2699 |
*
|
|
2700 |
* @return an instance of AccessibleRole describing the role of the
|
|
2701 |
* object (AccessibleRole.TEXT)
|
|
2702 |
* @see AccessibleRole
|
|
2703 |
*/
|
|
2704 |
public AccessibleRole getAccessibleRole() {
|
|
2705 |
return AccessibleRole.TEXT;
|
|
2706 |
}
|
|
2707 |
|
|
2708 |
/**
|
|
2709 |
* Get the AccessibleText associated with this object. In the
|
|
2710 |
* implementation of the Java Accessibility API for this class,
|
|
2711 |
* return this object, which is responsible for implementing the
|
|
2712 |
* AccessibleText interface on behalf of itself.
|
|
2713 |
*
|
|
2714 |
* @return this object
|
|
2715 |
*/
|
|
2716 |
public AccessibleText getAccessibleText() {
|
|
2717 |
return this;
|
|
2718 |
}
|
|
2719 |
|
|
2720 |
|
|
2721 |
// --- interface AccessibleText methods ------------------------
|
|
2722 |
|
|
2723 |
/**
|
|
2724 |
* Many of these methods are just convenience methods; they
|
|
2725 |
* just call the equivalent on the parent
|
|
2726 |
*/
|
|
2727 |
|
|
2728 |
/**
|
|
2729 |
* Given a point in local coordinates, return the zero-based index
|
|
2730 |
* of the character under that Point. If the point is invalid,
|
|
2731 |
* this method returns -1.
|
|
2732 |
*
|
|
2733 |
* @param p the Point in local coordinates
|
|
2734 |
* @return the zero-based index of the character under Point p.
|
|
2735 |
*/
|
|
2736 |
public int getIndexAtPoint(Point p) {
|
|
2737 |
if (p == null) {
|
|
2738 |
return -1;
|
|
2739 |
}
|
|
2740 |
return JTextComponent.this.viewToModel(p);
|
|
2741 |
}
|
|
2742 |
|
|
2743 |
/**
|
|
2744 |
* Gets the editor's drawing rectangle. Stolen
|
|
2745 |
* from the unfortunately named
|
|
2746 |
* BasicTextUI.getVisibleEditorRect()
|
|
2747 |
*
|
|
2748 |
* @return the bounding box for the root view
|
|
2749 |
*/
|
|
2750 |
Rectangle getRootEditorRect() {
|
|
2751 |
Rectangle alloc = JTextComponent.this.getBounds();
|
|
2752 |
if ((alloc.width > 0) && (alloc.height > 0)) {
|
|
2753 |
alloc.x = alloc.y = 0;
|
|
2754 |
Insets insets = JTextComponent.this.getInsets();
|
|
2755 |
alloc.x += insets.left;
|
|
2756 |
alloc.y += insets.top;
|
|
2757 |
alloc.width -= insets.left + insets.right;
|
|
2758 |
alloc.height -= insets.top + insets.bottom;
|
|
2759 |
return alloc;
|
|
2760 |
}
|
|
2761 |
return null;
|
|
2762 |
}
|
|
2763 |
|
|
2764 |
/**
|
|
2765 |
* Determines the bounding box of the character at the given
|
|
2766 |
* index into the string. The bounds are returned in local
|
|
2767 |
* coordinates. If the index is invalid a null rectangle
|
|
2768 |
* is returned.
|
|
2769 |
*
|
|
2770 |
* The screen coordinates returned are "unscrolled coordinates"
|
|
2771 |
* if the JTextComponent is contained in a JScrollPane in which
|
|
2772 |
* case the resulting rectangle should be composed with the parent
|
|
2773 |
* coordinates. A good algorithm to use is:
|
|
2774 |
* <nf>
|
|
2775 |
* Accessible a:
|
|
2776 |
* AccessibleText at = a.getAccessibleText();
|
|
2777 |
* AccessibleComponent ac = a.getAccessibleComponent();
|
|
2778 |
* Rectangle r = at.getCharacterBounds();
|
|
2779 |
* Point p = ac.getLocation();
|
|
2780 |
* r.x += p.x;
|
|
2781 |
* r.y += p.y;
|
|
2782 |
* </nf>
|
|
2783 |
*
|
|
2784 |
* Note: the JTextComponent must have a valid size (e.g. have
|
|
2785 |
* been added to a parent container whose ancestor container
|
|
2786 |
* is a valid top-level window) for this method to be able
|
|
2787 |
* to return a meaningful (non-null) value.
|
|
2788 |
*
|
|
2789 |
* @param i the index into the String >= 0
|
|
2790 |
* @return the screen coordinates of the character's bounding box
|
|
2791 |
*/
|
|
2792 |
public Rectangle getCharacterBounds(int i) {
|
|
2793 |
if (i < 0 || i > model.getLength()-1) {
|
|
2794 |
return null;
|
|
2795 |
}
|
|
2796 |
TextUI ui = getUI();
|
|
2797 |
if (ui == null) {
|
|
2798 |
return null;
|
|
2799 |
}
|
|
2800 |
Rectangle rect = null;
|
|
2801 |
Rectangle alloc = getRootEditorRect();
|
|
2802 |
if (alloc == null) {
|
|
2803 |
return null;
|
|
2804 |
}
|
|
2805 |
if (model instanceof AbstractDocument) {
|
|
2806 |
((AbstractDocument)model).readLock();
|
|
2807 |
}
|
|
2808 |
try {
|
|
2809 |
View rootView = ui.getRootView(JTextComponent.this);
|
|
2810 |
if (rootView != null) {
|
|
2811 |
rootView.setSize(alloc.width, alloc.height);
|
|
2812 |
|
|
2813 |
Shape bounds = rootView.modelToView(i,
|
|
2814 |
Position.Bias.Forward, i+1,
|
|
2815 |
Position.Bias.Backward, alloc);
|
|
2816 |
|
|
2817 |
rect = (bounds instanceof Rectangle) ?
|
|
2818 |
(Rectangle)bounds : bounds.getBounds();
|
|
2819 |
|
|
2820 |
}
|
|
2821 |
} catch (BadLocationException e) {
|
|
2822 |
} finally {
|
|
2823 |
if (model instanceof AbstractDocument) {
|
|
2824 |
((AbstractDocument)model).readUnlock();
|
|
2825 |
}
|
|
2826 |
}
|
|
2827 |
return rect;
|
|
2828 |
}
|
|
2829 |
|
|
2830 |
/**
|
|
2831 |
* Returns the number of characters (valid indices)
|
|
2832 |
*
|
|
2833 |
* @return the number of characters >= 0
|
|
2834 |
*/
|
|
2835 |
public int getCharCount() {
|
|
2836 |
return model.getLength();
|
|
2837 |
}
|
|
2838 |
|
|
2839 |
/**
|
|
2840 |
* Returns the zero-based offset of the caret.
|
|
2841 |
*
|
|
2842 |
* Note: The character to the right of the caret will have the
|
|
2843 |
* same index value as the offset (the caret is between
|
|
2844 |
* two characters).
|
|
2845 |
*
|
|
2846 |
* @return the zero-based offset of the caret.
|
|
2847 |
*/
|
|
2848 |
public int getCaretPosition() {
|
|
2849 |
return JTextComponent.this.getCaretPosition();
|
|
2850 |
}
|
|
2851 |
|
|
2852 |
/**
|
|
2853 |
* Returns the AttributeSet for a given character (at a given index).
|
|
2854 |
*
|
|
2855 |
* @param i the zero-based index into the text
|
|
2856 |
* @return the AttributeSet of the character
|
|
2857 |
*/
|
|
2858 |
public AttributeSet getCharacterAttribute(int i) {
|
|
2859 |
Element e = null;
|
|
2860 |
if (model instanceof AbstractDocument) {
|
|
2861 |
((AbstractDocument)model).readLock();
|
|
2862 |
}
|
|
2863 |
try {
|
|
2864 |
for (e = model.getDefaultRootElement(); ! e.isLeaf(); ) {
|
|
2865 |
int index = e.getElementIndex(i);
|
|
2866 |
e = e.getElement(index);
|
|
2867 |
}
|
|
2868 |
} finally {
|
|
2869 |
if (model instanceof AbstractDocument) {
|
|
2870 |
((AbstractDocument)model).readUnlock();
|
|
2871 |
}
|
|
2872 |
}
|
|
2873 |
return e.getAttributes();
|
|
2874 |
}
|
|
2875 |
|
|
2876 |
|
|
2877 |
/**
|
|
2878 |
* Returns the start offset within the selected text.
|
|
2879 |
* If there is no selection, but there is
|
|
2880 |
* a caret, the start and end offsets will be the same.
|
|
2881 |
* Return 0 if the text is empty, or the caret position
|
|
2882 |
* if no selection.
|
|
2883 |
*
|
|
2884 |
* @return the index into the text of the start of the selection >= 0
|
|
2885 |
*/
|
|
2886 |
public int getSelectionStart() {
|
|
2887 |
return JTextComponent.this.getSelectionStart();
|
|
2888 |
}
|
|
2889 |
|
|
2890 |
/**
|
|
2891 |
* Returns the end offset within the selected text.
|
|
2892 |
* If there is no selection, but there is
|
|
2893 |
* a caret, the start and end offsets will be the same.
|
|
2894 |
* Return 0 if the text is empty, or the caret position
|
|
2895 |
* if no selection.
|
|
2896 |
*
|
|
2897 |
* @return the index into teh text of the end of the selection >= 0
|
|
2898 |
*/
|
|
2899 |
public int getSelectionEnd() {
|
|
2900 |
return JTextComponent.this.getSelectionEnd();
|
|
2901 |
}
|
|
2902 |
|
|
2903 |
/**
|
|
2904 |
* Returns the portion of the text that is selected.
|
|
2905 |
*
|
|
2906 |
* @return the text, null if no selection
|
|
2907 |
*/
|
|
2908 |
public String getSelectedText() {
|
|
2909 |
return JTextComponent.this.getSelectedText();
|
|
2910 |
}
|
|
2911 |
|
|
2912 |
/**
|
|
2913 |
* IndexedSegment extends Segment adding the offset into the
|
|
2914 |
* the model the <code>Segment</code> was asked for.
|
|
2915 |
*/
|
|
2916 |
private class IndexedSegment extends Segment {
|
|
2917 |
/**
|
|
2918 |
* Offset into the model that the position represents.
|
|
2919 |
*/
|
|
2920 |
public int modelOffset;
|
|
2921 |
}
|
|
2922 |
|
|
2923 |
|
|
2924 |
// TIGER - 4170173
|
|
2925 |
/**
|
|
2926 |
* Returns the String at a given index. Whitespace
|
|
2927 |
* between words is treated as a word.
|
|
2928 |
*
|
|
2929 |
* @param part the CHARACTER, WORD, or SENTENCE to retrieve
|
|
2930 |
* @param index an index within the text
|
|
2931 |
* @return the letter, word, or sentence.
|
|
2932 |
*
|
|
2933 |
*/
|
|
2934 |
public String getAtIndex(int part, int index) {
|
|
2935 |
return getAtIndex(part, index, 0);
|
|
2936 |
}
|
|
2937 |
|
|
2938 |
|
|
2939 |
/**
|
|
2940 |
* Returns the String after a given index. Whitespace
|
|
2941 |
* between words is treated as a word.
|
|
2942 |
*
|
|
2943 |
* @param part the CHARACTER, WORD, or SENTENCE to retrieve
|
|
2944 |
* @param index an index within the text
|
|
2945 |
* @return the letter, word, or sentence.
|
|
2946 |
*/
|
|
2947 |
public String getAfterIndex(int part, int index) {
|
|
2948 |
return getAtIndex(part, index, 1);
|
|
2949 |
}
|
|
2950 |
|
|
2951 |
|
|
2952 |
/**
|
|
2953 |
* Returns the String before a given index. Whitespace
|
|
2954 |
* between words is treated a word.
|
|
2955 |
*
|
|
2956 |
* @param part the CHARACTER, WORD, or SENTENCE to retrieve
|
|
2957 |
* @param index an index within the text
|
|
2958 |
* @return the letter, word, or sentence.
|
|
2959 |
*/
|
|
2960 |
public String getBeforeIndex(int part, int index) {
|
|
2961 |
return getAtIndex(part, index, -1);
|
|
2962 |
}
|
|
2963 |
|
|
2964 |
|
|
2965 |
/**
|
|
2966 |
* Gets the word, sentence, or character at <code>index</code>.
|
|
2967 |
* If <code>direction</code> is non-null this will find the
|
|
2968 |
* next/previous word/sentence/character.
|
|
2969 |
*/
|
|
2970 |
private String getAtIndex(int part, int index, int direction) {
|
|
2971 |
if (model instanceof AbstractDocument) {
|
|
2972 |
((AbstractDocument)model).readLock();
|
|
2973 |
}
|
|
2974 |
try {
|
|
2975 |
if (index < 0 || index >= model.getLength()) {
|
|
2976 |
return null;
|
|
2977 |
}
|
|
2978 |
switch (part) {
|
|
2979 |
case AccessibleText.CHARACTER:
|
|
2980 |
if (index + direction < model.getLength() &&
|
|
2981 |
index + direction >= 0) {
|
|
2982 |
return model.getText(index + direction, 1);
|
|
2983 |
}
|
|
2984 |
break;
|
|
2985 |
|
|
2986 |
|
|
2987 |
case AccessibleText.WORD:
|
|
2988 |
case AccessibleText.SENTENCE:
|
|
2989 |
IndexedSegment seg = getSegmentAt(part, index);
|
|
2990 |
if (seg != null) {
|
|
2991 |
if (direction != 0) {
|
|
2992 |
int next;
|
|
2993 |
|
|
2994 |
|
|
2995 |
if (direction < 0) {
|
|
2996 |
next = seg.modelOffset - 1;
|
|
2997 |
}
|
|
2998 |
else {
|
|
2999 |
next = seg.modelOffset + direction * seg.count;
|
|
3000 |
}
|
|
3001 |
if (next >= 0 && next <= model.getLength()) {
|
|
3002 |
seg = getSegmentAt(part, next);
|
|
3003 |
}
|
|
3004 |
else {
|
|
3005 |
seg = null;
|
|
3006 |
}
|
|
3007 |
}
|
|
3008 |
if (seg != null) {
|
|
3009 |
return new String(seg.array, seg.offset,
|
|
3010 |
seg.count);
|
|
3011 |
}
|
|
3012 |
}
|
|
3013 |
break;
|
|
3014 |
|
|
3015 |
|
|
3016 |
default:
|
|
3017 |
break;
|
|
3018 |
}
|
|
3019 |
} catch (BadLocationException e) {
|
|
3020 |
} finally {
|
|
3021 |
if (model instanceof AbstractDocument) {
|
|
3022 |
((AbstractDocument)model).readUnlock();
|
|
3023 |
}
|
|
3024 |
}
|
|
3025 |
return null;
|
|
3026 |
}
|
|
3027 |
|
|
3028 |
|
|
3029 |
/*
|
|
3030 |
* Returns the paragraph element for the specified index.
|
|
3031 |
*/
|
|
3032 |
private Element getParagraphElement(int index) {
|
|
3033 |
if (model instanceof PlainDocument ) {
|
|
3034 |
PlainDocument sdoc = (PlainDocument)model;
|
|
3035 |
return sdoc.getParagraphElement(index);
|
|
3036 |
} else if (model instanceof StyledDocument) {
|
|
3037 |
StyledDocument sdoc = (StyledDocument)model;
|
|
3038 |
return sdoc.getParagraphElement(index);
|
|
3039 |
} else {
|
|
3040 |
Element para = null;
|
|
3041 |
for (para = model.getDefaultRootElement(); ! para.isLeaf(); ) {
|
|
3042 |
int pos = para.getElementIndex(index);
|
|
3043 |
para = para.getElement(pos);
|
|
3044 |
}
|
|
3045 |
if (para == null) {
|
|
3046 |
return null;
|
|
3047 |
}
|
|
3048 |
return para.getParentElement();
|
|
3049 |
}
|
|
3050 |
}
|
|
3051 |
|
|
3052 |
/*
|
|
3053 |
* Returns a <code>Segment</code> containing the paragraph text
|
|
3054 |
* at <code>index</code>, or null if <code>index</code> isn't
|
|
3055 |
* valid.
|
|
3056 |
*/
|
|
3057 |
private IndexedSegment getParagraphElementText(int index)
|
|
3058 |
throws BadLocationException {
|
|
3059 |
Element para = getParagraphElement(index);
|
|
3060 |
|
|
3061 |
|
|
3062 |
if (para != null) {
|
|
3063 |
IndexedSegment segment = new IndexedSegment();
|
|
3064 |
try {
|
|
3065 |
int length = para.getEndOffset() - para.getStartOffset();
|
|
3066 |
model.getText(para.getStartOffset(), length, segment);
|
|
3067 |
} catch (BadLocationException e) {
|
|
3068 |
return null;
|
|
3069 |
}
|
|
3070 |
segment.modelOffset = para.getStartOffset();
|
|
3071 |
return segment;
|
|
3072 |
}
|
|
3073 |
return null;
|
|
3074 |
}
|
|
3075 |
|
|
3076 |
|
|
3077 |
/**
|
|
3078 |
* Returns the Segment at <code>index</code> representing either
|
|
3079 |
* the paragraph or sentence as identified by <code>part</code>, or
|
|
3080 |
* null if a valid paragraph/sentence can't be found. The offset
|
|
3081 |
* will point to the start of the word/sentence in the array, and
|
|
3082 |
* the modelOffset will point to the location of the word/sentence
|
|
3083 |
* in the model.
|
|
3084 |
*/
|
|
3085 |
private IndexedSegment getSegmentAt(int part, int index) throws
|
|
3086 |
BadLocationException {
|
|
3087 |
IndexedSegment seg = getParagraphElementText(index);
|
|
3088 |
if (seg == null) {
|
|
3089 |
return null;
|
|
3090 |
}
|
|
3091 |
BreakIterator iterator;
|
|
3092 |
switch (part) {
|
|
3093 |
case AccessibleText.WORD:
|
|
3094 |
iterator = BreakIterator.getWordInstance(getLocale());
|
|
3095 |
break;
|
|
3096 |
case AccessibleText.SENTENCE:
|
|
3097 |
iterator = BreakIterator.getSentenceInstance(getLocale());
|
|
3098 |
break;
|
|
3099 |
default:
|
|
3100 |
return null;
|
|
3101 |
}
|
|
3102 |
seg.first();
|
|
3103 |
iterator.setText(seg);
|
|
3104 |
int end = iterator.following(index - seg.modelOffset + seg.offset);
|
|
3105 |
if (end == BreakIterator.DONE) {
|
|
3106 |
return null;
|
|
3107 |
}
|
|
3108 |
if (end > seg.offset + seg.count) {
|
|
3109 |
return null;
|
|
3110 |
}
|
|
3111 |
int begin = iterator.previous();
|
|
3112 |
if (begin == BreakIterator.DONE ||
|
|
3113 |
begin >= seg.offset + seg.count) {
|
|
3114 |
return null;
|
|
3115 |
}
|
|
3116 |
seg.modelOffset = seg.modelOffset + begin - seg.offset;
|
|
3117 |
seg.offset = begin;
|
|
3118 |
seg.count = end - begin;
|
|
3119 |
return seg;
|
|
3120 |
}
|
|
3121 |
|
|
3122 |
// begin AccessibleEditableText methods -----
|
|
3123 |
|
|
3124 |
/**
|
|
3125 |
* Returns the AccessibleEditableText interface for
|
|
3126 |
* this text component.
|
|
3127 |
*
|
|
3128 |
* @return the AccessibleEditableText interface
|
|
3129 |
* @since 1.4
|
|
3130 |
*/
|
|
3131 |
public AccessibleEditableText getAccessibleEditableText() {
|
|
3132 |
return this;
|
|
3133 |
}
|
|
3134 |
|
|
3135 |
/**
|
|
3136 |
* Sets the text contents to the specified string.
|
|
3137 |
*
|
|
3138 |
* @param s the string to set the text contents
|
|
3139 |
* @since 1.4
|
|
3140 |
*/
|
|
3141 |
public void setTextContents(String s) {
|
|
3142 |
JTextComponent.this.setText(s);
|
|
3143 |
}
|
|
3144 |
|
|
3145 |
/**
|
|
3146 |
* Inserts the specified string at the given index
|
|
3147 |
*
|
|
3148 |
* @param index the index in the text where the string will
|
|
3149 |
* be inserted
|
|
3150 |
* @param s the string to insert in the text
|
|
3151 |
* @since 1.4
|
|
3152 |
*/
|
|
3153 |
public void insertTextAtIndex(int index, String s) {
|
|
3154 |
Document doc = JTextComponent.this.getDocument();
|
|
3155 |
if (doc != null) {
|
|
3156 |
try {
|
|
3157 |
if (s != null && s.length() > 0) {
|
|
3158 |
boolean composedTextSaved = saveComposedText(index);
|
|
3159 |
doc.insertString(index, s, null);
|
|
3160 |
if (composedTextSaved) {
|
|
3161 |
restoreComposedText();
|
|
3162 |
}
|
|
3163 |
}
|
|
3164 |
} catch (BadLocationException e) {
|
|
3165 |
UIManager.getLookAndFeel().provideErrorFeedback(JTextComponent.this);
|
|
3166 |
}
|
|
3167 |
}
|
|
3168 |
}
|
|
3169 |
|
|
3170 |
/**
|
|
3171 |
* Returns the text string between two indices.
|
|
3172 |
*
|
|
3173 |
* @param startIndex the starting index in the text
|
|
3174 |
* @param endIndex the ending index in the text
|
|
3175 |
* @return the text string between the indices
|
|
3176 |
* @since 1.4
|
|
3177 |
*/
|
|
3178 |
public String getTextRange(int startIndex, int endIndex) {
|
|
3179 |
String txt = null;
|
|
3180 |
int p0 = Math.min(startIndex, endIndex);
|
|
3181 |
int p1 = Math.max(startIndex, endIndex);
|
|
3182 |
if (p0 != p1) {
|
|
3183 |
try {
|
|
3184 |
Document doc = JTextComponent.this.getDocument();
|
|
3185 |
txt = doc.getText(p0, p1 - p0);
|
|
3186 |
} catch (BadLocationException e) {
|
|
3187 |
throw new IllegalArgumentException(e.getMessage());
|
|
3188 |
}
|
|
3189 |
}
|
|
3190 |
return txt;
|
|
3191 |
}
|
|
3192 |
|
|
3193 |
/**
|
|
3194 |
* Deletes the text between two indices
|
|
3195 |
*
|
|
3196 |
* @param startIndex the starting index in the text
|
|
3197 |
* @param endIndex the ending index in the text
|
|
3198 |
* @since 1.4
|
|
3199 |
*/
|
|
3200 |
public void delete(int startIndex, int endIndex) {
|
|
3201 |
if (isEditable() && isEnabled()) {
|
|
3202 |
try {
|
|
3203 |
int p0 = Math.min(startIndex, endIndex);
|
|
3204 |
int p1 = Math.max(startIndex, endIndex);
|
|
3205 |
if (p0 != p1) {
|
|
3206 |
Document doc = getDocument();
|
|
3207 |
doc.remove(p0, p1 - p0);
|
|
3208 |
}
|
|
3209 |
} catch (BadLocationException e) {
|
|
3210 |
}
|
|
3211 |
} else {
|
|
3212 |
UIManager.getLookAndFeel().provideErrorFeedback(JTextComponent.this);
|
|
3213 |
}
|
|
3214 |
}
|
|
3215 |
|
|
3216 |
/**
|
|
3217 |
* Cuts the text between two indices into the system clipboard.
|
|
3218 |
*
|
|
3219 |
* @param startIndex the starting index in the text
|
|
3220 |
* @param endIndex the ending index in the text
|
|
3221 |
* @since 1.4
|
|
3222 |
*/
|
|
3223 |
public void cut(int startIndex, int endIndex) {
|
|
3224 |
selectText(startIndex, endIndex);
|
|
3225 |
JTextComponent.this.cut();
|
|
3226 |
}
|
|
3227 |
|
|
3228 |
/**
|
|
3229 |
* Pastes the text from the system clipboard into the text
|
|
3230 |
* starting at the specified index.
|
|
3231 |
*
|
|
3232 |
* @param startIndex the starting index in the text
|
|
3233 |
* @since 1.4
|
|
3234 |
*/
|
|
3235 |
public void paste(int startIndex) {
|
|
3236 |
setCaretPosition(startIndex);
|
|
3237 |
JTextComponent.this.paste();
|
|
3238 |
}
|
|
3239 |
|
|
3240 |
/**
|
|
3241 |
* Replaces the text between two indices with the specified
|
|
3242 |
* string.
|
|
3243 |
*
|
|
3244 |
* @param startIndex the starting index in the text
|
|
3245 |
* @param endIndex the ending index in the text
|
|
3246 |
* @param s the string to replace the text between two indices
|
|
3247 |
* @since 1.4
|
|
3248 |
*/
|
|
3249 |
public void replaceText(int startIndex, int endIndex, String s) {
|
|
3250 |
selectText(startIndex, endIndex);
|
|
3251 |
JTextComponent.this.replaceSelection(s);
|
|
3252 |
}
|
|
3253 |
|
|
3254 |
/**
|
|
3255 |
* Selects the text between two indices.
|
|
3256 |
*
|
|
3257 |
* @param startIndex the starting index in the text
|
|
3258 |
* @param endIndex the ending index in the text
|
|
3259 |
* @since 1.4
|
|
3260 |
*/
|
|
3261 |
public void selectText(int startIndex, int endIndex) {
|
|
3262 |
JTextComponent.this.select(startIndex, endIndex);
|
|
3263 |
}
|
|
3264 |
|
|
3265 |
/**
|
|
3266 |
* Sets attributes for the text between two indices.
|
|
3267 |
*
|
|
3268 |
* @param startIndex the starting index in the text
|
|
3269 |
* @param endIndex the ending index in the text
|
|
3270 |
* @param as the attribute set
|
|
3271 |
* @see AttributeSet
|
|
3272 |
* @since 1.4
|
|
3273 |
*/
|
|
3274 |
public void setAttributes(int startIndex, int endIndex,
|
|
3275 |
AttributeSet as) {
|
|
3276 |
|
|
3277 |
// Fixes bug 4487492
|
|
3278 |
Document doc = JTextComponent.this.getDocument();
|
|
3279 |
if (doc != null && doc instanceof StyledDocument) {
|
|
3280 |
StyledDocument sDoc = (StyledDocument)doc;
|
|
3281 |
int offset = startIndex;
|
|
3282 |
int length = endIndex - startIndex;
|
|
3283 |
sDoc.setCharacterAttributes(offset, length, as, true);
|
|
3284 |
}
|
|
3285 |
}
|
|
3286 |
|
|
3287 |
// ----- end AccessibleEditableText methods
|
|
3288 |
|
|
3289 |
|
|
3290 |
// ----- begin AccessibleExtendedText methods
|
|
3291 |
|
|
3292 |
// Probably should replace the helper method getAtIndex() to return
|
|
3293 |
// instead an AccessibleTextSequence also for LINE & ATTRIBUTE_RUN
|
|
3294 |
// and then make the AccessibleText methods get[At|After|Before]Point
|
|
3295 |
// call this new method instead and return only the string portion
|
|
3296 |
|
|
3297 |
/**
|
|
3298 |
* Returns the AccessibleTextSequence at a given <code>index</code>.
|
|
3299 |
* If <code>direction</code> is non-null this will find the
|
|
3300 |
* next/previous word/sentence/character.
|
|
3301 |
*
|
|
3302 |
* @param part the <code>CHARACTER</code>, <code>WORD</code>,
|
|
3303 |
* <code>SENTENCE</code>, <code>LINE</code> or
|
|
3304 |
* <code>ATTRIBUTE_RUN</code> to retrieve
|
|
3305 |
* @param index an index within the text
|
|
3306 |
* @param direction is either -1, 0, or 1
|
|
3307 |
* @return an <code>AccessibleTextSequence</code> specifying the text
|
|
3308 |
* if <code>part</code> and <code>index</code> are valid. Otherwise,
|
|
3309 |
* <code>null</code> is returned.
|
|
3310 |
*
|
|
3311 |
* @see javax.accessibility.AccessibleText#CHARACTER
|
|
3312 |
* @see javax.accessibility.AccessibleText#WORD
|
|
3313 |
* @see javax.accessibility.AccessibleText#SENTENCE
|
|
3314 |
* @see javax.accessibility.AccessibleExtendedText#LINE
|
|
3315 |
* @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
|
|
3316 |
*
|
|
3317 |
* @since 1.6
|
|
3318 |
*/
|
|
3319 |
private AccessibleTextSequence getSequenceAtIndex(int part,
|
|
3320 |
int index, int direction) {
|
|
3321 |
if (index < 0 || index >= model.getLength()) {
|
|
3322 |
return null;
|
|
3323 |
}
|
|
3324 |
if (direction < -1 || direction > 1) {
|
|
3325 |
return null; // direction must be 1, 0, or -1
|
|
3326 |
}
|
|
3327 |
|
|
3328 |
switch (part) {
|
|
3329 |
case AccessibleText.CHARACTER:
|
|
3330 |
if (model instanceof AbstractDocument) {
|
|
3331 |
((AbstractDocument)model).readLock();
|
|
3332 |
}
|
|
3333 |
AccessibleTextSequence charSequence = null;
|
|
3334 |
try {
|
|
3335 |
if (index + direction < model.getLength() &&
|
|
3336 |
index + direction >= 0) {
|
|
3337 |
charSequence =
|
|
3338 |
new AccessibleTextSequence(index + direction,
|
|
3339 |
index + direction + 1,
|
|
3340 |
model.getText(index + direction, 1));
|
|
3341 |
}
|
|
3342 |
|
|
3343 |
} catch (BadLocationException e) {
|
|
3344 |
// we are intentionally silent; our contract says we return
|
|
3345 |
// null if there is any failure in this method
|
|
3346 |
} finally {
|
|
3347 |
if (model instanceof AbstractDocument) {
|
|
3348 |
((AbstractDocument)model).readUnlock();
|
|
3349 |
}
|
|
3350 |
}
|
|
3351 |
return charSequence;
|
|
3352 |
|
|
3353 |
case AccessibleText.WORD:
|
|
3354 |
case AccessibleText.SENTENCE:
|
|
3355 |
if (model instanceof AbstractDocument) {
|
|
3356 |
((AbstractDocument)model).readLock();
|
|
3357 |
}
|
|
3358 |
AccessibleTextSequence rangeSequence = null;
|
|
3359 |
try {
|
|
3360 |
IndexedSegment seg = getSegmentAt(part, index);
|
|
3361 |
if (seg != null) {
|
|
3362 |
if (direction != 0) {
|
|
3363 |
int next;
|
|
3364 |
|
|
3365 |
if (direction < 0) {
|
|
3366 |
next = seg.modelOffset - 1;
|
|
3367 |
}
|
|
3368 |
else {
|
|
3369 |
next = seg.modelOffset + seg.count;
|
|
3370 |
}
|
|
3371 |
if (next >= 0 && next <= model.getLength()) {
|
|
3372 |
seg = getSegmentAt(part, next);
|
|
3373 |
}
|
|
3374 |
else {
|
|
3375 |
seg = null;
|
|
3376 |
}
|
|
3377 |
}
|
|
3378 |
if (seg != null &&
|
|
3379 |
(seg.offset + seg.count) <= model.getLength()) {
|
|
3380 |
rangeSequence =
|
|
3381 |
new AccessibleTextSequence (seg.offset,
|
|
3382 |
seg.offset + seg.count,
|
|
3383 |
new String(seg.array, seg.offset, seg.count));
|
|
3384 |
} // else we leave rangeSequence set to null
|
|
3385 |
}
|
|
3386 |
} catch(BadLocationException e) {
|
|
3387 |
// we are intentionally silent; our contract says we return
|
|
3388 |
// null if there is any failure in this method
|
|
3389 |
} finally {
|
|
3390 |
if (model instanceof AbstractDocument) {
|
|
3391 |
((AbstractDocument)model).readUnlock();
|
|
3392 |
}
|
|
3393 |
}
|
|
3394 |
return rangeSequence;
|
|
3395 |
|
|
3396 |
case AccessibleExtendedText.LINE:
|
|
3397 |
AccessibleTextSequence lineSequence = null;
|
|
3398 |
if (model instanceof AbstractDocument) {
|
|
3399 |
((AbstractDocument)model).readLock();
|
|
3400 |
}
|
|
3401 |
try {
|
|
3402 |
int startIndex =
|
|
3403 |
Utilities.getRowStart(JTextComponent.this, index);
|
|
3404 |
int endIndex =
|
|
3405 |
Utilities.getRowEnd(JTextComponent.this, index);
|
|
3406 |
if (startIndex >= 0 && endIndex >= startIndex) {
|
|
3407 |
if (direction == 0) {
|
|
3408 |
lineSequence =
|
|
3409 |
new AccessibleTextSequence(startIndex, endIndex,
|
|
3410 |
model.getText(startIndex,
|
|
3411 |
endIndex - startIndex + 1));
|
|
3412 |
} else if (direction == -1 && startIndex > 0) {
|
|
3413 |
endIndex =
|
|
3414 |
Utilities.getRowEnd(JTextComponent.this,
|
|
3415 |
startIndex - 1);
|
|
3416 |
startIndex =
|
|
3417 |
Utilities.getRowStart(JTextComponent.this,
|
|
3418 |
startIndex - 1);
|
|
3419 |
if (startIndex >= 0 && endIndex >= startIndex) {
|
|
3420 |
lineSequence =
|
|
3421 |
new AccessibleTextSequence(startIndex,
|
|
3422 |
endIndex,
|
|
3423 |
model.getText(startIndex,
|
|
3424 |
endIndex - startIndex + 1));
|
|
3425 |
}
|
|
3426 |
} else if (direction == 1 &&
|
|
3427 |
endIndex < model.getLength()) {
|
|
3428 |
startIndex =
|
|
3429 |
Utilities.getRowStart(JTextComponent.this,
|
|
3430 |
endIndex + 1);
|
|
3431 |
endIndex =
|
|
3432 |
Utilities.getRowEnd(JTextComponent.this,
|
|
3433 |
endIndex + 1);
|
|
3434 |
if (startIndex >= 0 && endIndex >= startIndex) {
|
|
3435 |
lineSequence =
|
|
3436 |
new AccessibleTextSequence(startIndex,
|
|
3437 |
endIndex, model.getText(startIndex,
|
|
3438 |
endIndex - startIndex + 1));
|
|
3439 |
}
|
|
3440 |
}
|
|
3441 |
// already validated 'direction' above...
|
|
3442 |
}
|
|
3443 |
} catch(BadLocationException e) {
|
|
3444 |
// we are intentionally silent; our contract says we return
|
|
3445 |
// null if there is any failure in this method
|
|
3446 |
} finally {
|
|
3447 |
if (model instanceof AbstractDocument) {
|
|
3448 |
((AbstractDocument)model).readUnlock();
|
|
3449 |
}
|
|
3450 |
}
|
|
3451 |
return lineSequence;
|
|
3452 |
|
|
3453 |
case AccessibleExtendedText.ATTRIBUTE_RUN:
|
|
3454 |
// assumptions: (1) that all characters in a single element
|
|
3455 |
// share the same attribute set; (2) that adjacent elements
|
|
3456 |
// *may* share the same attribute set
|
|
3457 |
|
|
3458 |
int attributeRunStartIndex, attributeRunEndIndex;
|
|
3459 |
String runText = null;
|
|
3460 |
if (model instanceof AbstractDocument) {
|
|
3461 |
((AbstractDocument)model).readLock();
|
|
3462 |
}
|
|
3463 |
|
|
3464 |
try {
|
|
3465 |
attributeRunStartIndex = attributeRunEndIndex =
|
|
3466 |
Integer.MIN_VALUE;
|
|
3467 |
int tempIndex = index;
|
|
3468 |
switch (direction) {
|
|
3469 |
case -1:
|
|
3470 |
// going backwards, so find left edge of this run -
|
|
3471 |
// that'll be the end of the previous run
|
|
3472 |
// (off-by-one counting)
|
|
3473 |
attributeRunEndIndex = getRunEdge(index, direction);
|
|
3474 |
// now set ourselves up to find the left edge of the
|
|
3475 |
// prev. run
|
|
3476 |
tempIndex = attributeRunEndIndex - 1;
|
|
3477 |
break;
|
|
3478 |
case 1:
|
|
3479 |
// going forward, so find right edge of this run -
|
|
3480 |
// that'll be the start of the next run
|
|
3481 |
// (off-by-one counting)
|
|
3482 |
attributeRunStartIndex = getRunEdge(index, direction);
|
|
3483 |
// now set ourselves up to find the right edge of the
|
|
3484 |
// next run
|
|
3485 |
tempIndex = attributeRunStartIndex;
|
|
3486 |
break;
|
|
3487 |
case 0:
|
|
3488 |
// interested in the current run, so nothing special to
|
|
3489 |
// set up in advance...
|
|
3490 |
break;
|
|
3491 |
default:
|
|
3492 |
// only those three values of direction allowed...
|
|
3493 |
throw new AssertionError(direction);
|
|
3494 |
}
|
|
3495 |
|
|
3496 |
// set the unset edge; if neither set then we're getting
|
|
3497 |
// both edges of the current run around our 'index'
|
|
3498 |
attributeRunStartIndex =
|
|
3499 |
(attributeRunStartIndex != Integer.MIN_VALUE) ?
|
|
3500 |
attributeRunStartIndex : getRunEdge(tempIndex, -1);
|
|
3501 |
attributeRunEndIndex =
|
|
3502 |
(attributeRunEndIndex != Integer.MIN_VALUE) ?
|
|
3503 |
attributeRunEndIndex : getRunEdge(tempIndex, 1);
|
|
3504 |
|
|
3505 |
runText = model.getText(attributeRunStartIndex,
|
|
3506 |
attributeRunEndIndex -
|
|
3507 |
attributeRunStartIndex);
|
|
3508 |
} catch (BadLocationException e) {
|
|
3509 |
// we are intentionally silent; our contract says we return
|
|
3510 |
// null if there is any failure in this method
|
|
3511 |
return null;
|
|
3512 |
} finally {
|
|
3513 |
if (model instanceof AbstractDocument) {
|
|
3514 |
((AbstractDocument)model).readUnlock();
|
|
3515 |
}
|
|
3516 |
}
|
|
3517 |
return new AccessibleTextSequence(attributeRunStartIndex,
|
|
3518 |
attributeRunEndIndex,
|
|
3519 |
runText);
|
|
3520 |
|
|
3521 |
default:
|
|
3522 |
break;
|
|
3523 |
}
|
|
3524 |
return null;
|
|
3525 |
}
|
|
3526 |
|
|
3527 |
|
|
3528 |
/**
|
|
3529 |
* Starting at text position <code>index</code>, and going in
|
|
3530 |
* <code>direction</code>, return the edge of run that shares the
|
|
3531 |
* same <code>AttributeSet</code> and parent element as those at
|
|
3532 |
* <code>index</code>.
|
|
3533 |
*
|
|
3534 |
* Note: we assume the document is already locked...
|
|
3535 |
*/
|
|
3536 |
private int getRunEdge(int index, int direction) throws
|
|
3537 |
BadLocationException {
|
|
3538 |
if (index < 0 || index >= model.getLength()) {
|
|
3539 |
throw new BadLocationException("Location out of bounds", index);
|
|
3540 |
}
|
|
3541 |
// locate the Element at index
|
|
3542 |
Element indexElement = null;
|
|
3543 |
// locate the Element at our index/offset
|
|
3544 |
int elementIndex = -1; // test for initialization
|
|
3545 |
for (indexElement = model.getDefaultRootElement();
|
|
3546 |
! indexElement.isLeaf(); ) {
|
|
3547 |
elementIndex = indexElement.getElementIndex(index);
|
|
3548 |
indexElement = indexElement.getElement(elementIndex);
|
|
3549 |
}
|
|
3550 |
if (elementIndex == -1) {
|
|
3551 |
throw new AssertionError(index);
|
|
3552 |
}
|
|
3553 |
// cache the AttributeSet and parentElement atindex
|
|
3554 |
AttributeSet indexAS = indexElement.getAttributes();
|
|
3555 |
Element parent = indexElement.getParentElement();
|
|
3556 |
|
|
3557 |
// find the first Element before/after ours w/the same AttributeSet
|
|
3558 |
// if we are already at edge of the first element in our parent
|
|
3559 |
// then return that edge
|
|
3560 |
Element edgeElement = indexElement;
|
|
3561 |
switch (direction) {
|
|
3562 |
case -1:
|
|
3563 |
case 1:
|
|
3564 |
int edgeElementIndex = elementIndex;
|
|
3565 |
int elementCount = parent.getElementCount();
|
|
3566 |
while ((edgeElementIndex + direction) > 0 &&
|
|
3567 |
((edgeElementIndex + direction) < elementCount) &&
|
|
3568 |
parent.getElement(edgeElementIndex
|
|
3569 |
+ direction).getAttributes().isEqual(indexAS)) {
|
|
3570 |
edgeElementIndex += direction;
|
|
3571 |
}
|
|
3572 |
edgeElement = parent.getElement(edgeElementIndex);
|
|
3573 |
break;
|
|
3574 |
default:
|
|
3575 |
throw new AssertionError(direction);
|
|
3576 |
}
|
|
3577 |
switch (direction) {
|
|
3578 |
case -1:
|
|
3579 |
return edgeElement.getStartOffset();
|
|
3580 |
case 1:
|
|
3581 |
return edgeElement.getEndOffset();
|
|
3582 |
default:
|
|
3583 |
// we already caught this case earlier; this is to satisfy
|
|
3584 |
// the compiler...
|
|
3585 |
return Integer.MIN_VALUE;
|
|
3586 |
}
|
|
3587 |
}
|
|
3588 |
|
|
3589 |
// getTextRange() not needed; defined in AccessibleEditableText
|
|
3590 |
|
|
3591 |
/**
|
|
3592 |
* Returns the <code>AccessibleTextSequence</code> at a given
|
|
3593 |
* <code>index</code>.
|
|
3594 |
*
|
|
3595 |
* @param part the <code>CHARACTER</code>, <code>WORD</code>,
|
|
3596 |
* <code>SENTENCE</code>, <code>LINE</code> or
|
|
3597 |
* <code>ATTRIBUTE_RUN</code> to retrieve
|
|
3598 |
* @param index an index within the text
|
|
3599 |
* @return an <code>AccessibleTextSequence</code> specifying the text if
|
|
3600 |
* <code>part</code> and <code>index</code> are valid. Otherwise,
|
|
3601 |
* <code>null</code> is returned
|
|
3602 |
*
|
|
3603 |
* @see javax.accessibility.AccessibleText#CHARACTER
|
|
3604 |
* @see javax.accessibility.AccessibleText#WORD
|
|
3605 |
* @see javax.accessibility.AccessibleText#SENTENCE
|
|
3606 |
* @see javax.accessibility.AccessibleExtendedText#LINE
|
|
3607 |
* @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
|
|
3608 |
*
|
|
3609 |
* @since 1.6
|
|
3610 |
*/
|
|
3611 |
public AccessibleTextSequence getTextSequenceAt(int part, int index) {
|
|
3612 |
return getSequenceAtIndex(part, index, 0);
|
|
3613 |
}
|
|
3614 |
|
|
3615 |
/**
|
|
3616 |
* Returns the <code>AccessibleTextSequence</code> after a given
|
|
3617 |
* <code>index</code>.
|
|
3618 |
*
|
|
3619 |
* @param part the <code>CHARACTER</code>, <code>WORD</code>,
|
|
3620 |
* <code>SENTENCE</code>, <code>LINE</code> or
|
|
3621 |
* <code>ATTRIBUTE_RUN</code> to retrieve
|
|
3622 |
* @param index an index within the text
|
|
3623 |
* @return an <code>AccessibleTextSequence</code> specifying the text
|
|
3624 |
* if <code>part</code> and <code>index</code> are valid. Otherwise,
|
|
3625 |
* <code>null</code> is returned
|
|
3626 |
*
|
|
3627 |
* @see javax.accessibility.AccessibleText#CHARACTER
|
|
3628 |
* @see javax.accessibility.AccessibleText#WORD
|
|
3629 |
* @see javax.accessibility.AccessibleText#SENTENCE
|
|
3630 |
* @see javax.accessibility.AccessibleExtendedText#LINE
|
|
3631 |
* @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
|
|
3632 |
*
|
|
3633 |
* @since 1.6
|
|
3634 |
*/
|
|
3635 |
public AccessibleTextSequence getTextSequenceAfter(int part, int index) {
|
|
3636 |
return getSequenceAtIndex(part, index, 1);
|
|
3637 |
}
|
|
3638 |
|
|
3639 |
/**
|
|
3640 |
* Returns the <code>AccessibleTextSequence</code> before a given
|
|
3641 |
* <code>index</code>.
|
|
3642 |
*
|
|
3643 |
* @param part the <code>CHARACTER</code>, <code>WORD</code>,
|
|
3644 |
* <code>SENTENCE</code>, <code>LINE</code> or
|
|
3645 |
* <code>ATTRIBUTE_RUN</code> to retrieve
|
|
3646 |
* @param index an index within the text
|
|
3647 |
* @return an <code>AccessibleTextSequence</code> specifying the text
|
|
3648 |
* if <code>part</code> and <code>index</code> are valid. Otherwise,
|
|
3649 |
* <code>null</code> is returned
|
|
3650 |
*
|
|
3651 |
* @see javax.accessibility.AccessibleText#CHARACTER
|
|
3652 |
* @see javax.accessibility.AccessibleText#WORD
|
|
3653 |
* @see javax.accessibility.AccessibleText#SENTENCE
|
|
3654 |
* @see javax.accessibility.AccessibleExtendedText#LINE
|
|
3655 |
* @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
|
|
3656 |
*
|
|
3657 |
* @since 1.6
|
|
3658 |
*/
|
|
3659 |
public AccessibleTextSequence getTextSequenceBefore(int part, int index) {
|
|
3660 |
return getSequenceAtIndex(part, index, -1);
|
|
3661 |
}
|
|
3662 |
|
|
3663 |
/**
|
|
3664 |
* Returns the <code>Rectangle</code> enclosing the text between
|
|
3665 |
* two indicies.
|
|
3666 |
*
|
|
3667 |
* @param startIndex the start index in the text
|
|
3668 |
* @param endIndex the end index in the text
|
|
3669 |
* @return the bounding rectangle of the text if the indices are valid.
|
|
3670 |
* Otherwise, <code>null</code> is returned
|
|
3671 |
*
|
|
3672 |
* @since 1.6
|
|
3673 |
*/
|
|
3674 |
public Rectangle getTextBounds(int startIndex, int endIndex) {
|
|
3675 |
if (startIndex < 0 || startIndex > model.getLength()-1 ||
|
|
3676 |
endIndex < 0 || endIndex > model.getLength()-1 ||
|
|
3677 |
startIndex > endIndex) {
|
|
3678 |
return null;
|
|
3679 |
}
|
|
3680 |
TextUI ui = getUI();
|
|
3681 |
if (ui == null) {
|
|
3682 |
return null;
|
|
3683 |
}
|
|
3684 |
Rectangle rect = null;
|
|
3685 |
Rectangle alloc = getRootEditorRect();
|
|
3686 |
if (alloc == null) {
|
|
3687 |
return null;
|
|
3688 |
}
|
|
3689 |
if (model instanceof AbstractDocument) {
|
|
3690 |
((AbstractDocument)model).readLock();
|
|
3691 |
}
|
|
3692 |
try {
|
|
3693 |
View rootView = ui.getRootView(JTextComponent.this);
|
|
3694 |
if (rootView != null) {
|
|
3695 |
Shape bounds = rootView.modelToView(startIndex,
|
|
3696 |
Position.Bias.Forward, endIndex,
|
|
3697 |
Position.Bias.Backward, alloc);
|
|
3698 |
|
|
3699 |
rect = (bounds instanceof Rectangle) ?
|
|
3700 |
(Rectangle)bounds : bounds.getBounds();
|
|
3701 |
|
|
3702 |
}
|
|
3703 |
} catch (BadLocationException e) {
|
|
3704 |
} finally {
|
|
3705 |
if (model instanceof AbstractDocument) {
|
|
3706 |
((AbstractDocument)model).readUnlock();
|
|
3707 |
}
|
|
3708 |
}
|
|
3709 |
return rect;
|
|
3710 |
}
|
|
3711 |
|
|
3712 |
// ----- end AccessibleExtendedText methods
|
|
3713 |
|
|
3714 |
|
|
3715 |
// --- interface AccessibleAction methods ------------------------
|
|
3716 |
|
|
3717 |
public AccessibleAction getAccessibleAction() {
|
|
3718 |
return this;
|
|
3719 |
}
|
|
3720 |
|
|
3721 |
/**
|
|
3722 |
* Returns the number of accessible actions available in this object
|
|
3723 |
* If there are more than one, the first one is considered the
|
|
3724 |
* "default" action of the object.
|
|
3725 |
*
|
|
3726 |
* @return the zero-based number of Actions in this object
|
|
3727 |
* @since 1.4
|
|
3728 |
*/
|
|
3729 |
public int getAccessibleActionCount() {
|
|
3730 |
Action [] actions = JTextComponent.this.getActions();
|
|
3731 |
return actions.length;
|
|
3732 |
}
|
|
3733 |
|
|
3734 |
/**
|
|
3735 |
* Returns a description of the specified action of the object.
|
|
3736 |
*
|
|
3737 |
* @param i zero-based index of the actions
|
|
3738 |
* @return a String description of the action
|
|
3739 |
* @see #getAccessibleActionCount
|
|
3740 |
* @since 1.4
|
|
3741 |
*/
|
|
3742 |
public String getAccessibleActionDescription(int i) {
|
|
3743 |
Action [] actions = JTextComponent.this.getActions();
|
|
3744 |
if (i < 0 || i >= actions.length) {
|
|
3745 |
return null;
|
|
3746 |
}
|
|
3747 |
return (String)actions[i].getValue(Action.NAME);
|
|
3748 |
}
|
|
3749 |
|
|
3750 |
/**
|
|
3751 |
* Performs the specified Action on the object
|
|
3752 |
*
|
|
3753 |
* @param i zero-based index of actions
|
|
3754 |
* @return true if the action was performed; otherwise false.
|
|
3755 |
* @see #getAccessibleActionCount
|
|
3756 |
* @since 1.4
|
|
3757 |
*/
|
|
3758 |
public boolean doAccessibleAction(int i) {
|
|
3759 |
Action [] actions = JTextComponent.this.getActions();
|
|
3760 |
if (i < 0 || i >= actions.length) {
|
|
3761 |
return false;
|
|
3762 |
}
|
|
3763 |
ActionEvent ae =
|
|
3764 |
new ActionEvent(JTextComponent.this,
|
|
3765 |
ActionEvent.ACTION_PERFORMED, null,
|
|
3766 |
EventQueue.getMostRecentEventTime(),
|
|
3767 |
getCurrentEventModifiers());
|
|
3768 |
actions[i].actionPerformed(ae);
|
|
3769 |
return true;
|
|
3770 |
}
|
|
3771 |
|
|
3772 |
// ----- end AccessibleAction methods
|
|
3773 |
|
|
3774 |
|
|
3775 |
}
|
|
3776 |
|
|
3777 |
|
|
3778 |
// --- serialization ---------------------------------------------
|
|
3779 |
|
|
3780 |
private void readObject(ObjectInputStream s)
|
|
3781 |
throws IOException, ClassNotFoundException
|
|
3782 |
{
|
|
3783 |
s.defaultReadObject();
|
|
3784 |
caretEvent = new MutableCaretEvent(this);
|
|
3785 |
addMouseListener(caretEvent);
|
|
3786 |
addFocusListener(caretEvent);
|
|
3787 |
}
|
|
3788 |
|
|
3789 |
// --- member variables ----------------------------------
|
|
3790 |
|
|
3791 |
/**
|
|
3792 |
* The document model.
|
|
3793 |
*/
|
|
3794 |
private Document model;
|
|
3795 |
|
|
3796 |
/**
|
|
3797 |
* The caret used to display the insert position
|
|
3798 |
* and navigate throughout the document.
|
|
3799 |
*
|
|
3800 |
* PENDING(prinz)
|
|
3801 |
* This should be serializable, default installed
|
|
3802 |
* by UI.
|
|
3803 |
*/
|
|
3804 |
private transient Caret caret;
|
|
3805 |
|
|
3806 |
/**
|
|
3807 |
* Object responsible for restricting the cursor navigation.
|
|
3808 |
*/
|
|
3809 |
private NavigationFilter navigationFilter;
|
|
3810 |
|
|
3811 |
/**
|
|
3812 |
* The object responsible for managing highlights.
|
|
3813 |
*
|
|
3814 |
* PENDING(prinz)
|
|
3815 |
* This should be serializable, default installed
|
|
3816 |
* by UI.
|
|
3817 |
*/
|
|
3818 |
private transient Highlighter highlighter;
|
|
3819 |
|
|
3820 |
/**
|
|
3821 |
* The current key bindings in effect.
|
|
3822 |
*
|
|
3823 |
* PENDING(prinz)
|
|
3824 |
* This should be serializable, default installed
|
|
3825 |
* by UI.
|
|
3826 |
*/
|
|
3827 |
private transient Keymap keymap;
|
|
3828 |
|
|
3829 |
private transient MutableCaretEvent caretEvent;
|
|
3830 |
private Color caretColor;
|
|
3831 |
private Color selectionColor;
|
|
3832 |
private Color selectedTextColor;
|
|
3833 |
private Color disabledTextColor;
|
|
3834 |
private boolean editable;
|
|
3835 |
private Insets margin;
|
|
3836 |
private char focusAccelerator;
|
|
3837 |
private boolean dragEnabled;
|
|
3838 |
|
|
3839 |
/**
|
|
3840 |
* The drop mode for this component.
|
|
3841 |
*/
|
|
3842 |
private DropMode dropMode = DropMode.USE_SELECTION;
|
|
3843 |
|
|
3844 |
/**
|
|
3845 |
* The drop location.
|
|
3846 |
*/
|
|
3847 |
private transient DropLocation dropLocation;
|
|
3848 |
|
|
3849 |
/**
|
|
3850 |
* Represents a drop location for <code>JTextComponent</code>s.
|
|
3851 |
*
|
|
3852 |
* @see #getDropLocation
|
|
3853 |
* @since 1.6
|
|
3854 |
*/
|
|
3855 |
public static final class DropLocation extends TransferHandler.DropLocation {
|
|
3856 |
private final int index;
|
|
3857 |
private final Position.Bias bias;
|
|
3858 |
|
|
3859 |
private DropLocation(Point p, int index, Position.Bias bias) {
|
|
3860 |
super(p);
|
|
3861 |
this.index = index;
|
|
3862 |
this.bias = bias;
|
|
3863 |
}
|
|
3864 |
|
|
3865 |
/**
|
|
3866 |
* Returns the index where dropped data should be inserted into the
|
|
3867 |
* associated component. This index represents a position between
|
|
3868 |
* characters, as would be interpreted by a caret.
|
|
3869 |
*
|
|
3870 |
* @return the drop index
|
|
3871 |
*/
|
|
3872 |
public int getIndex() {
|
|
3873 |
return index;
|
|
3874 |
}
|
|
3875 |
|
|
3876 |
/**
|
|
3877 |
* Returns the bias for the drop index.
|
|
3878 |
*
|
|
3879 |
* @return the drop bias
|
|
3880 |
*/
|
|
3881 |
public Position.Bias getBias() {
|
|
3882 |
return bias;
|
|
3883 |
}
|
|
3884 |
|
|
3885 |
/**
|
|
3886 |
* Returns a string representation of this drop location.
|
|
3887 |
* This method is intended to be used for debugging purposes,
|
|
3888 |
* and the content and format of the returned string may vary
|
|
3889 |
* between implementations.
|
|
3890 |
*
|
|
3891 |
* @return a string representation of this drop location
|
|
3892 |
*/
|
|
3893 |
public String toString() {
|
|
3894 |
return getClass().getName()
|
|
3895 |
+ "[dropPoint=" + getDropPoint() + ","
|
|
3896 |
+ "index=" + index + ","
|
|
3897 |
+ "bias=" + bias + "]";
|
|
3898 |
}
|
|
3899 |
}
|
|
3900 |
|
|
3901 |
/**
|
|
3902 |
* TransferHandler used if one hasn't been supplied by the UI.
|
|
3903 |
*/
|
|
3904 |
private static DefaultTransferHandler defaultTransferHandler;
|
|
3905 |
|
|
3906 |
/**
|
|
3907 |
* Maps from class name to Boolean indicating if
|
|
3908 |
* <code>processInputMethodEvent</code> has been overriden.
|
|
3909 |
*/
|
|
3910 |
private static Map overrideMap;
|
|
3911 |
|
|
3912 |
/**
|
|
3913 |
* Returns a string representation of this <code>JTextComponent</code>.
|
|
3914 |
* This method is intended to be used only for debugging purposes, and the
|
|
3915 |
* content and format of the returned string may vary between
|
|
3916 |
* implementations. The returned string may be empty but may not
|
|
3917 |
* be <code>null</code>.
|
|
3918 |
* <P>
|
|
3919 |
* Overriding <code>paramString</code> to provide information about the
|
|
3920 |
* specific new aspects of the JFC components.
|
|
3921 |
*
|
|
3922 |
* @return a string representation of this <code>JTextComponent</code>
|
|
3923 |
*/
|
|
3924 |
protected String paramString() {
|
|
3925 |
String editableString = (editable ?
|
|
3926 |
"true" : "false");
|
|
3927 |
String caretColorString = (caretColor != null ?
|
|
3928 |
caretColor.toString() : "");
|
|
3929 |
String selectionColorString = (selectionColor != null ?
|
|
3930 |
selectionColor.toString() : "");
|
|
3931 |
String selectedTextColorString = (selectedTextColor != null ?
|
|
3932 |
selectedTextColor.toString() : "");
|
|
3933 |
String disabledTextColorString = (disabledTextColor != null ?
|
|
3934 |
disabledTextColor.toString() : "");
|
|
3935 |
String marginString = (margin != null ?
|
|
3936 |
margin.toString() : "");
|
|
3937 |
|
|
3938 |
return super.paramString() +
|
|
3939 |
",caretColor=" + caretColorString +
|
|
3940 |
",disabledTextColor=" + disabledTextColorString +
|
|
3941 |
",editable=" + editableString +
|
|
3942 |
",margin=" + marginString +
|
|
3943 |
",selectedTextColor=" + selectedTextColorString +
|
|
3944 |
",selectionColor=" + selectionColorString;
|
|
3945 |
}
|
|
3946 |
|
|
3947 |
|
|
3948 |
/**
|
|
3949 |
* A Simple TransferHandler that exports the data as a String, and
|
|
3950 |
* imports the data from the String clipboard. This is only used
|
|
3951 |
* if the UI hasn't supplied one, which would only happen if someone
|
|
3952 |
* hasn't subclassed Basic.
|
|
3953 |
*/
|
|
3954 |
static class DefaultTransferHandler extends TransferHandler implements
|
|
3955 |
UIResource {
|
|
3956 |
public void exportToClipboard(JComponent comp, Clipboard clipboard,
|
|
3957 |
int action) throws IllegalStateException {
|
|
3958 |
if (comp instanceof JTextComponent) {
|
|
3959 |
JTextComponent text = (JTextComponent)comp;
|
|
3960 |
int p0 = text.getSelectionStart();
|
|
3961 |
int p1 = text.getSelectionEnd();
|
|
3962 |
if (p0 != p1) {
|
|
3963 |
try {
|
|
3964 |
Document doc = text.getDocument();
|
|
3965 |
String srcData = doc.getText(p0, p1 - p0);
|
|
3966 |
StringSelection contents =new StringSelection(srcData);
|
|
3967 |
|
|
3968 |
// this may throw an IllegalStateException,
|
|
3969 |
// but it will be caught and handled in the
|
|
3970 |
// action that invoked this method
|
|
3971 |
clipboard.setContents(contents, null);
|
|
3972 |
|
|
3973 |
if (action == TransferHandler.MOVE) {
|
|
3974 |
doc.remove(p0, p1 - p0);
|
|
3975 |
}
|
|
3976 |
} catch (BadLocationException ble) {}
|
|
3977 |
}
|
|
3978 |
}
|
|
3979 |
}
|
|
3980 |
public boolean importData(JComponent comp, Transferable t) {
|
|
3981 |
if (comp instanceof JTextComponent) {
|
|
3982 |
DataFlavor flavor = getFlavor(t.getTransferDataFlavors());
|
|
3983 |
|
|
3984 |
if (flavor != null) {
|
|
3985 |
InputContext ic = comp.getInputContext();
|
|
3986 |
if (ic != null) {
|
|
3987 |
ic.endComposition();
|
|
3988 |
}
|
|
3989 |
try {
|
|
3990 |
String data = (String)t.getTransferData(flavor);
|
|
3991 |
|
|
3992 |
((JTextComponent)comp).replaceSelection(data);
|
|
3993 |
return true;
|
|
3994 |
} catch (UnsupportedFlavorException ufe) {
|
|
3995 |
} catch (IOException ioe) {
|
|
3996 |
}
|
|
3997 |
}
|
|
3998 |
}
|
|
3999 |
return false;
|
|
4000 |
}
|
|
4001 |
public boolean canImport(JComponent comp,
|
|
4002 |
DataFlavor[] transferFlavors) {
|
|
4003 |
JTextComponent c = (JTextComponent)comp;
|
|
4004 |
if (!(c.isEditable() && c.isEnabled())) {
|
|
4005 |
return false;
|
|
4006 |
}
|
|
4007 |
return (getFlavor(transferFlavors) != null);
|
|
4008 |
}
|
|
4009 |
public int getSourceActions(JComponent c) {
|
|
4010 |
return NONE;
|
|
4011 |
}
|
|
4012 |
private DataFlavor getFlavor(DataFlavor[] flavors) {
|
|
4013 |
if (flavors != null) {
|
|
4014 |
for (int counter = 0; counter < flavors.length; counter++) {
|
|
4015 |
if (flavors[counter].equals(DataFlavor.stringFlavor)) {
|
|
4016 |
return flavors[counter];
|
|
4017 |
}
|
|
4018 |
}
|
|
4019 |
}
|
|
4020 |
return null;
|
|
4021 |
}
|
|
4022 |
}
|
|
4023 |
|
|
4024 |
/**
|
|
4025 |
* Returns the JTextComponent that most recently had focus. The returned
|
|
4026 |
* value may currently have focus.
|
|
4027 |
*/
|
|
4028 |
static final JTextComponent getFocusedComponent() {
|
|
4029 |
return (JTextComponent)AppContext.getAppContext().
|
|
4030 |
get(FOCUSED_COMPONENT);
|
|
4031 |
}
|
|
4032 |
|
|
4033 |
private int getCurrentEventModifiers() {
|
|
4034 |
int modifiers = 0;
|
|
4035 |
AWTEvent currentEvent = EventQueue.getCurrentEvent();
|
|
4036 |
if (currentEvent instanceof InputEvent) {
|
|
4037 |
modifiers = ((InputEvent)currentEvent).getModifiers();
|
|
4038 |
} else if (currentEvent instanceof ActionEvent) {
|
|
4039 |
modifiers = ((ActionEvent)currentEvent).getModifiers();
|
|
4040 |
}
|
|
4041 |
return modifiers;
|
|
4042 |
}
|
|
4043 |
|
|
4044 |
private static final Object KEYMAP_TABLE =
|
|
4045 |
new StringBuilder("JTextComponent_KeymapTable");
|
|
4046 |
private JTextComponent editor;
|
|
4047 |
//
|
|
4048 |
// member variables used for on-the-spot input method
|
|
4049 |
// editing style support
|
|
4050 |
//
|
|
4051 |
private transient InputMethodRequests inputMethodRequestsHandler;
|
|
4052 |
private SimpleAttributeSet composedTextAttribute;
|
|
4053 |
private String composedTextContent;
|
|
4054 |
private Position composedTextStart;
|
|
4055 |
private Position composedTextEnd;
|
|
4056 |
private Position latestCommittedTextStart;
|
|
4057 |
private Position latestCommittedTextEnd;
|
|
4058 |
private ComposedTextCaret composedTextCaret;
|
|
4059 |
private transient Caret originalCaret;
|
|
4060 |
/**
|
|
4061 |
* Set to true after the check for the override of processInputMethodEvent
|
|
4062 |
* has been checked.
|
|
4063 |
*/
|
|
4064 |
private boolean checkedInputOverride;
|
|
4065 |
private boolean needToSendKeyTypedEvent;
|
|
4066 |
|
|
4067 |
static class DefaultKeymap implements Keymap {
|
|
4068 |
|
|
4069 |
DefaultKeymap(String nm, Keymap parent) {
|
|
4070 |
this.nm = nm;
|
|
4071 |
this.parent = parent;
|
|
4072 |
bindings = new Hashtable();
|
|
4073 |
}
|
|
4074 |
|
|
4075 |
/**
|
|
4076 |
* Fetch the default action to fire if a
|
|
4077 |
* key is typed (ie a KEY_TYPED KeyEvent is received)
|
|
4078 |
* and there is no binding for it. Typically this
|
|
4079 |
* would be some action that inserts text so that
|
|
4080 |
* the keymap doesn't require an action for each
|
|
4081 |
* possible key.
|
|
4082 |
*/
|
|
4083 |
public Action getDefaultAction() {
|
|
4084 |
if (defaultAction != null) {
|
|
4085 |
return defaultAction;
|
|
4086 |
}
|
|
4087 |
return (parent != null) ? parent.getDefaultAction() : null;
|
|
4088 |
}
|
|
4089 |
|
|
4090 |
/**
|
|
4091 |
* Set the default action to fire if a key is typed.
|
|
4092 |
*/
|
|
4093 |
public void setDefaultAction(Action a) {
|
|
4094 |
defaultAction = a;
|
|
4095 |
}
|
|
4096 |
|
|
4097 |
public String getName() {
|
|
4098 |
return nm;
|
|
4099 |
}
|
|
4100 |
|
|
4101 |
public Action getAction(KeyStroke key) {
|
|
4102 |
Action a = (Action) bindings.get(key);
|
|
4103 |
if ((a == null) && (parent != null)) {
|
|
4104 |
a = parent.getAction(key);
|
|
4105 |
}
|
|
4106 |
return a;
|
|
4107 |
}
|
|
4108 |
|
|
4109 |
public KeyStroke[] getBoundKeyStrokes() {
|
|
4110 |
KeyStroke[] keys = new KeyStroke[bindings.size()];
|
|
4111 |
int i = 0;
|
|
4112 |
for (Enumeration e = bindings.keys() ; e.hasMoreElements() ;) {
|
|
4113 |
keys[i++] = (KeyStroke) e.nextElement();
|
|
4114 |
}
|
|
4115 |
return keys;
|
|
4116 |
}
|
|
4117 |
|
|
4118 |
public Action[] getBoundActions() {
|
|
4119 |
Action[] actions = new Action[bindings.size()];
|
|
4120 |
int i = 0;
|
|
4121 |
for (Enumeration e = bindings.elements() ; e.hasMoreElements() ;) {
|
|
4122 |
actions[i++] = (Action) e.nextElement();
|
|
4123 |
}
|
|
4124 |
return actions;
|
|
4125 |
}
|
|
4126 |
|
|
4127 |
public KeyStroke[] getKeyStrokesForAction(Action a) {
|
|
4128 |
if (a == null) {
|
|
4129 |
return null;
|
|
4130 |
}
|
|
4131 |
KeyStroke[] retValue = null;
|
|
4132 |
// Determine local bindings first.
|
|
4133 |
Vector keyStrokes = null;
|
|
4134 |
for (Enumeration enum_ = bindings.keys();
|
|
4135 |
enum_.hasMoreElements();) {
|
|
4136 |
Object key = enum_.nextElement();
|
|
4137 |
if (bindings.get(key) == a) {
|
|
4138 |
if (keyStrokes == null) {
|
|
4139 |
keyStrokes = new Vector();
|
|
4140 |
}
|
|
4141 |
keyStrokes.addElement(key);
|
|
4142 |
}
|
|
4143 |
}
|
|
4144 |
// See if the parent has any.
|
|
4145 |
if (parent != null) {
|
|
4146 |
KeyStroke[] pStrokes = parent.getKeyStrokesForAction(a);
|
|
4147 |
if (pStrokes != null) {
|
|
4148 |
// Remove any bindings defined in the parent that
|
|
4149 |
// are locally defined.
|
|
4150 |
int rCount = 0;
|
|
4151 |
for (int counter = pStrokes.length - 1; counter >= 0;
|
|
4152 |
counter--) {
|
|
4153 |
if (isLocallyDefined(pStrokes[counter])) {
|
|
4154 |
pStrokes[counter] = null;
|
|
4155 |
rCount++;
|
|
4156 |
}
|
|
4157 |
}
|
|
4158 |
if (rCount > 0 && rCount < pStrokes.length) {
|
|
4159 |
if (keyStrokes == null) {
|
|
4160 |
keyStrokes = new Vector();
|
|
4161 |
}
|
|
4162 |
for (int counter = pStrokes.length - 1; counter >= 0;
|
|
4163 |
counter--) {
|
|
4164 |
if (pStrokes[counter] != null) {
|
|
4165 |
keyStrokes.addElement(pStrokes[counter]);
|
|
4166 |
}
|
|
4167 |
}
|
|
4168 |
}
|
|
4169 |
else if (rCount == 0) {
|
|
4170 |
if (keyStrokes == null) {
|
|
4171 |
retValue = pStrokes;
|
|
4172 |
}
|
|
4173 |
else {
|
|
4174 |
retValue = new KeyStroke[keyStrokes.size() +
|
|
4175 |
pStrokes.length];
|
|
4176 |
keyStrokes.copyInto(retValue);
|
|
4177 |
System.arraycopy(pStrokes, 0, retValue,
|
|
4178 |
keyStrokes.size(), pStrokes.length);
|
|
4179 |
keyStrokes = null;
|
|
4180 |
}
|
|
4181 |
}
|
|
4182 |
}
|
|
4183 |
}
|
|
4184 |
if (keyStrokes != null) {
|
|
4185 |
retValue = new KeyStroke[keyStrokes.size()];
|
|
4186 |
keyStrokes.copyInto(retValue);
|
|
4187 |
}
|
|
4188 |
return retValue;
|
|
4189 |
}
|
|
4190 |
|
|
4191 |
public boolean isLocallyDefined(KeyStroke key) {
|
|
4192 |
return bindings.containsKey(key);
|
|
4193 |
}
|
|
4194 |
|
|
4195 |
public void addActionForKeyStroke(KeyStroke key, Action a) {
|
|
4196 |
bindings.put(key, a);
|
|
4197 |
}
|
|
4198 |
|
|
4199 |
public void removeKeyStrokeBinding(KeyStroke key) {
|
|
4200 |
bindings.remove(key);
|
|
4201 |
}
|
|
4202 |
|
|
4203 |
public void removeBindings() {
|
|
4204 |
bindings.clear();
|
|
4205 |
}
|
|
4206 |
|
|
4207 |
public Keymap getResolveParent() {
|
|
4208 |
return parent;
|
|
4209 |
}
|
|
4210 |
|
|
4211 |
public void setResolveParent(Keymap parent) {
|
|
4212 |
this.parent = parent;
|
|
4213 |
}
|
|
4214 |
|
|
4215 |
/**
|
|
4216 |
* String representation of the keymap... potentially
|
|
4217 |
* a very long string.
|
|
4218 |
*/
|
|
4219 |
public String toString() {
|
|
4220 |
return "Keymap[" + nm + "]" + bindings;
|
|
4221 |
}
|
|
4222 |
|
|
4223 |
String nm;
|
|
4224 |
Keymap parent;
|
|
4225 |
Hashtable bindings;
|
|
4226 |
Action defaultAction;
|
|
4227 |
}
|
|
4228 |
|
|
4229 |
|
|
4230 |
/**
|
|
4231 |
* KeymapWrapper wraps a Keymap inside an InputMap. For KeymapWrapper
|
|
4232 |
* to be useful it must be used with a KeymapActionMap.
|
|
4233 |
* KeymapWrapper for the most part, is an InputMap with two parents.
|
|
4234 |
* The first parent visited is ALWAYS the Keymap, with the second
|
|
4235 |
* parent being the parent inherited from InputMap. If
|
|
4236 |
* <code>keymap.getAction</code> returns null, implying the Keymap
|
|
4237 |
* does not have a binding for the KeyStroke,
|
|
4238 |
* the parent is then visited. If the Keymap has a binding, the
|
|
4239 |
* Action is returned, if not and the KeyStroke represents a
|
|
4240 |
* KeyTyped event and the Keymap has a defaultAction,
|
|
4241 |
* <code>DefaultActionKey</code> is returned.
|
|
4242 |
* <p>KeymapActionMap is then able to transate the object passed in
|
|
4243 |
* to either message the Keymap, or message its default implementation.
|
|
4244 |
*/
|
|
4245 |
static class KeymapWrapper extends InputMap {
|
|
4246 |
static final Object DefaultActionKey = new Object();
|
|
4247 |
|
|
4248 |
private Keymap keymap;
|
|
4249 |
|
|
4250 |
KeymapWrapper(Keymap keymap) {
|
|
4251 |
this.keymap = keymap;
|
|
4252 |
}
|
|
4253 |
|
|
4254 |
public KeyStroke[] keys() {
|
|
4255 |
KeyStroke[] sKeys = super.keys();
|
|
4256 |
KeyStroke[] keymapKeys = keymap.getBoundKeyStrokes();
|
|
4257 |
int sCount = (sKeys == null) ? 0 : sKeys.length;
|
|
4258 |
int keymapCount = (keymapKeys == null) ? 0 : keymapKeys.length;
|
|
4259 |
if (sCount == 0) {
|
|
4260 |
return keymapKeys;
|
|
4261 |
}
|
|
4262 |
if (keymapCount == 0) {
|
|
4263 |
return sKeys;
|
|
4264 |
}
|
|
4265 |
KeyStroke[] retValue = new KeyStroke[sCount + keymapCount];
|
|
4266 |
// There may be some duplication here...
|
|
4267 |
System.arraycopy(sKeys, 0, retValue, 0, sCount);
|
|
4268 |
System.arraycopy(keymapKeys, 0, retValue, sCount, keymapCount);
|
|
4269 |
return retValue;
|
|
4270 |
}
|
|
4271 |
|
|
4272 |
public int size() {
|
|
4273 |
// There may be some duplication here...
|
|
4274 |
KeyStroke[] keymapStrokes = keymap.getBoundKeyStrokes();
|
|
4275 |
int keymapCount = (keymapStrokes == null) ? 0:
|
|
4276 |
keymapStrokes.length;
|
|
4277 |
return super.size() + keymapCount;
|
|
4278 |
}
|
|
4279 |
|
|
4280 |
public Object get(KeyStroke keyStroke) {
|
|
4281 |
Object retValue = keymap.getAction(keyStroke);
|
|
4282 |
if (retValue == null) {
|
|
4283 |
retValue = super.get(keyStroke);
|
|
4284 |
if (retValue == null &&
|
|
4285 |
keyStroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED &&
|
|
4286 |
keymap.getDefaultAction() != null) {
|
|
4287 |
// Implies this is a KeyTyped event, use the default
|
|
4288 |
// action.
|
|
4289 |
retValue = DefaultActionKey;
|
|
4290 |
}
|
|
4291 |
}
|
|
4292 |
return retValue;
|
|
4293 |
}
|
|
4294 |
}
|
|
4295 |
|
|
4296 |
|
|
4297 |
/**
|
|
4298 |
* Wraps a Keymap inside an ActionMap. This is used with
|
|
4299 |
* a KeymapWrapper. If <code>get</code> is passed in
|
|
4300 |
* <code>KeymapWrapper.DefaultActionKey</code>, the default action is
|
|
4301 |
* returned, otherwise if the key is an Action, it is returned.
|
|
4302 |
*/
|
|
4303 |
static class KeymapActionMap extends ActionMap {
|
|
4304 |
private Keymap keymap;
|
|
4305 |
|
|
4306 |
KeymapActionMap(Keymap keymap) {
|
|
4307 |
this.keymap = keymap;
|
|
4308 |
}
|
|
4309 |
|
|
4310 |
public Object[] keys() {
|
|
4311 |
Object[] sKeys = super.keys();
|
|
4312 |
Object[] keymapKeys = keymap.getBoundActions();
|
|
4313 |
int sCount = (sKeys == null) ? 0 : sKeys.length;
|
|
4314 |
int keymapCount = (keymapKeys == null) ? 0 : keymapKeys.length;
|
|
4315 |
boolean hasDefault = (keymap.getDefaultAction() != null);
|
|
4316 |
if (hasDefault) {
|
|
4317 |
keymapCount++;
|
|
4318 |
}
|
|
4319 |
if (sCount == 0) {
|
|
4320 |
if (hasDefault) {
|
|
4321 |
Object[] retValue = new Object[keymapCount];
|
|
4322 |
if (keymapCount > 1) {
|
|
4323 |
System.arraycopy(keymapKeys, 0, retValue, 0,
|
|
4324 |
keymapCount - 1);
|
|
4325 |
}
|
|
4326 |
retValue[keymapCount - 1] = KeymapWrapper.DefaultActionKey;
|
|
4327 |
return retValue;
|
|
4328 |
}
|
|
4329 |
return keymapKeys;
|
|
4330 |
}
|
|
4331 |
if (keymapCount == 0) {
|
|
4332 |
return sKeys;
|
|
4333 |
}
|
|
4334 |
Object[] retValue = new Object[sCount + keymapCount];
|
|
4335 |
// There may be some duplication here...
|
|
4336 |
System.arraycopy(sKeys, 0, retValue, 0, sCount);
|
|
4337 |
if (hasDefault) {
|
|
4338 |
if (keymapCount > 1) {
|
|
4339 |
System.arraycopy(keymapKeys, 0, retValue, sCount,
|
|
4340 |
keymapCount - 1);
|
|
4341 |
}
|
|
4342 |
retValue[sCount + keymapCount - 1] = KeymapWrapper.
|
|
4343 |
DefaultActionKey;
|
|
4344 |
}
|
|
4345 |
else {
|
|
4346 |
System.arraycopy(keymapKeys, 0, retValue, sCount, keymapCount);
|
|
4347 |
}
|
|
4348 |
return retValue;
|
|
4349 |
}
|
|
4350 |
|
|
4351 |
public int size() {
|
|
4352 |
// There may be some duplication here...
|
|
4353 |
Object[] actions = keymap.getBoundActions();
|
|
4354 |
int keymapCount = (actions == null) ? 0 : actions.length;
|
|
4355 |
if (keymap.getDefaultAction() != null) {
|
|
4356 |
keymapCount++;
|
|
4357 |
}
|
|
4358 |
return super.size() + keymapCount;
|
|
4359 |
}
|
|
4360 |
|
|
4361 |
public Action get(Object key) {
|
|
4362 |
Action retValue = super.get(key);
|
|
4363 |
if (retValue == null) {
|
|
4364 |
// Try the Keymap.
|
|
4365 |
if (key == KeymapWrapper.DefaultActionKey) {
|
|
4366 |
retValue = keymap.getDefaultAction();
|
|
4367 |
}
|
|
4368 |
else if (key instanceof Action) {
|
|
4369 |
// This is a little iffy, technically an Action is
|
|
4370 |
// a valid Key. We're assuming the Action came from
|
|
4371 |
// the InputMap though.
|
|
4372 |
retValue = (Action)key;
|
|
4373 |
}
|
|
4374 |
}
|
|
4375 |
return retValue;
|
|
4376 |
}
|
|
4377 |
}
|
|
4378 |
|
|
4379 |
private static final Object FOCUSED_COMPONENT =
|
|
4380 |
new StringBuilder("JTextComponent_FocusedComponent");
|
|
4381 |
|
|
4382 |
/**
|
|
4383 |
* The default keymap that will be shared by all
|
|
4384 |
* <code>JTextComponent</code> instances unless they
|
|
4385 |
* have had a different keymap set.
|
|
4386 |
*/
|
|
4387 |
public static final String DEFAULT_KEYMAP = "default";
|
|
4388 |
|
|
4389 |
/**
|
|
4390 |
* Event to use when firing a notification of change to caret
|
|
4391 |
* position. This is mutable so that the event can be reused
|
|
4392 |
* since caret events can be fairly high in bandwidth.
|
|
4393 |
*/
|
|
4394 |
static class MutableCaretEvent extends CaretEvent implements ChangeListener, FocusListener, MouseListener {
|
|
4395 |
|
|
4396 |
MutableCaretEvent(JTextComponent c) {
|
|
4397 |
super(c);
|
|
4398 |
}
|
|
4399 |
|
|
4400 |
final void fire() {
|
|
4401 |
JTextComponent c = (JTextComponent) getSource();
|
|
4402 |
if (c != null) {
|
|
4403 |
Caret caret = c.getCaret();
|
|
4404 |
dot = caret.getDot();
|
|
4405 |
mark = caret.getMark();
|
|
4406 |
c.fireCaretUpdate(this);
|
|
4407 |
}
|
|
4408 |
}
|
|
4409 |
|
|
4410 |
public final String toString() {
|
|
4411 |
return "dot=" + dot + "," + "mark=" + mark;
|
|
4412 |
}
|
|
4413 |
|
|
4414 |
// --- CaretEvent methods -----------------------
|
|
4415 |
|
|
4416 |
public final int getDot() {
|
|
4417 |
return dot;
|
|
4418 |
}
|
|
4419 |
|
|
4420 |
public final int getMark() {
|
|
4421 |
return mark;
|
|
4422 |
}
|
|
4423 |
|
|
4424 |
// --- ChangeListener methods -------------------
|
|
4425 |
|
|
4426 |
public final void stateChanged(ChangeEvent e) {
|
|
4427 |
if (! dragActive) {
|
|
4428 |
fire();
|
|
4429 |
}
|
|
4430 |
}
|
|
4431 |
|
|
4432 |
// --- FocusListener methods -----------------------------------
|
|
4433 |
public void focusGained(FocusEvent fe) {
|
|
4434 |
AppContext.getAppContext().put(FOCUSED_COMPONENT,
|
|
4435 |
fe.getSource());
|
|
4436 |
}
|
|
4437 |
|
|
4438 |
public void focusLost(FocusEvent fe) {
|
|
4439 |
}
|
|
4440 |
|
|
4441 |
// --- MouseListener methods -----------------------------------
|
|
4442 |
|
|
4443 |
/**
|
|
4444 |
* Requests focus on the associated
|
|
4445 |
* text component, and try to set the cursor position.
|
|
4446 |
*
|
|
4447 |
* @param e the mouse event
|
|
4448 |
* @see MouseListener#mousePressed
|
|
4449 |
*/
|
|
4450 |
public final void mousePressed(MouseEvent e) {
|
|
4451 |
dragActive = true;
|
|
4452 |
}
|
|
4453 |
|
|
4454 |
/**
|
|
4455 |
* Called when the mouse is released.
|
|
4456 |
*
|
|
4457 |
* @param e the mouse event
|
|
4458 |
* @see MouseListener#mouseReleased
|
|
4459 |
*/
|
|
4460 |
public final void mouseReleased(MouseEvent e) {
|
|
4461 |
dragActive = false;
|
|
4462 |
fire();
|
|
4463 |
}
|
|
4464 |
|
|
4465 |
public final void mouseClicked(MouseEvent e) {
|
|
4466 |
}
|
|
4467 |
|
|
4468 |
public final void mouseEntered(MouseEvent e) {
|
|
4469 |
}
|
|
4470 |
|
|
4471 |
public final void mouseExited(MouseEvent e) {
|
|
4472 |
}
|
|
4473 |
|
|
4474 |
private boolean dragActive;
|
|
4475 |
private int dot;
|
|
4476 |
private int mark;
|
|
4477 |
}
|
|
4478 |
|
|
4479 |
//
|
|
4480 |
// Process any input method events that the component itself
|
|
4481 |
// recognizes. The default on-the-spot handling for input method
|
|
4482 |
// composed(uncommitted) text is done here after all input
|
|
4483 |
// method listeners get called for stealing the events.
|
|
4484 |
//
|
|
4485 |
protected void processInputMethodEvent(InputMethodEvent e) {
|
|
4486 |
// let listeners handle the events
|
|
4487 |
super.processInputMethodEvent(e);
|
|
4488 |
|
|
4489 |
if (!e.isConsumed()) {
|
|
4490 |
if (! isEditable()) {
|
|
4491 |
return;
|
|
4492 |
} else {
|
|
4493 |
switch (e.getID()) {
|
|
4494 |
case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
|
|
4495 |
replaceInputMethodText(e);
|
|
4496 |
|
|
4497 |
// fall through
|
|
4498 |
|
|
4499 |
case InputMethodEvent.CARET_POSITION_CHANGED:
|
|
4500 |
setInputMethodCaretPosition(e);
|
|
4501 |
break;
|
|
4502 |
}
|
|
4503 |
}
|
|
4504 |
|
|
4505 |
e.consume();
|
|
4506 |
}
|
|
4507 |
}
|
|
4508 |
|
|
4509 |
//
|
|
4510 |
// Overrides this method to become an active input method client.
|
|
4511 |
//
|
|
4512 |
public InputMethodRequests getInputMethodRequests() {
|
|
4513 |
if (inputMethodRequestsHandler == null) {
|
|
4514 |
inputMethodRequestsHandler =
|
|
4515 |
(InputMethodRequests)new InputMethodRequestsHandler();
|
|
4516 |
Document doc = getDocument();
|
|
4517 |
if (doc != null) {
|
|
4518 |
doc.addDocumentListener((DocumentListener)inputMethodRequestsHandler);
|
|
4519 |
}
|
|
4520 |
}
|
|
4521 |
|
|
4522 |
return inputMethodRequestsHandler;
|
|
4523 |
}
|
|
4524 |
|
|
4525 |
//
|
|
4526 |
// Overrides this method to watch the listener installed.
|
|
4527 |
//
|
|
4528 |
public void addInputMethodListener(InputMethodListener l) {
|
|
4529 |
super.addInputMethodListener(l);
|
|
4530 |
if (l != null) {
|
|
4531 |
needToSendKeyTypedEvent = false;
|
|
4532 |
checkedInputOverride = true;
|
|
4533 |
}
|
|
4534 |
}
|
|
4535 |
|
|
4536 |
|
|
4537 |
//
|
|
4538 |
// Default implementation of the InputMethodRequests interface.
|
|
4539 |
//
|
|
4540 |
class InputMethodRequestsHandler implements InputMethodRequests, DocumentListener {
|
|
4541 |
|
|
4542 |
// --- InputMethodRequests methods ---
|
|
4543 |
|
|
4544 |
public AttributedCharacterIterator cancelLatestCommittedText(
|
|
4545 |
Attribute[] attributes) {
|
|
4546 |
Document doc = getDocument();
|
|
4547 |
if ((doc != null) && (latestCommittedTextStart != null)
|
|
4548 |
&& (!latestCommittedTextStart.equals(latestCommittedTextEnd))) {
|
|
4549 |
try {
|
|
4550 |
int startIndex = latestCommittedTextStart.getOffset();
|
|
4551 |
int endIndex = latestCommittedTextEnd.getOffset();
|
|
4552 |
String latestCommittedText =
|
|
4553 |
doc.getText(startIndex, endIndex - startIndex);
|
|
4554 |
doc.remove(startIndex, endIndex - startIndex);
|
|
4555 |
return new AttributedString(latestCommittedText).getIterator();
|
|
4556 |
} catch (BadLocationException ble) {}
|
|
4557 |
}
|
|
4558 |
return null;
|
|
4559 |
}
|
|
4560 |
|
|
4561 |
public AttributedCharacterIterator getCommittedText(int beginIndex,
|
|
4562 |
int endIndex, Attribute[] attributes) {
|
|
4563 |
int composedStartIndex = 0;
|
|
4564 |
int composedEndIndex = 0;
|
|
4565 |
if (composedTextExists()) {
|
|
4566 |
composedStartIndex = composedTextStart.getOffset();
|
|
4567 |
composedEndIndex = composedTextEnd.getOffset();
|
|
4568 |
}
|
|
4569 |
|
|
4570 |
String committed;
|
|
4571 |
try {
|
|
4572 |
if (beginIndex < composedStartIndex) {
|
|
4573 |
if (endIndex <= composedStartIndex) {
|
|
4574 |
committed = getText(beginIndex, endIndex - beginIndex);
|
|
4575 |
} else {
|
|
4576 |
int firstPartLength = composedStartIndex - beginIndex;
|
|
4577 |
committed = getText(beginIndex, firstPartLength) +
|
|
4578 |
getText(composedEndIndex, endIndex - beginIndex - firstPartLength);
|
|
4579 |
}
|
|
4580 |
} else {
|
|
4581 |
committed = getText(beginIndex + (composedEndIndex - composedStartIndex),
|
|
4582 |
endIndex - beginIndex);
|
|
4583 |
}
|
|
4584 |
} catch (BadLocationException ble) {
|
|
4585 |
throw new IllegalArgumentException("Invalid range");
|
|
4586 |
}
|
|
4587 |
return new AttributedString(committed).getIterator();
|
|
4588 |
}
|
|
4589 |
|
|
4590 |
public int getCommittedTextLength() {
|
|
4591 |
Document doc = getDocument();
|
|
4592 |
int length = 0;
|
|
4593 |
if (doc != null) {
|
|
4594 |
length = doc.getLength();
|
|
4595 |
if (composedTextContent != null) {
|
|
4596 |
if (composedTextEnd == null
|
|
4597 |
|| composedTextStart == null) {
|
|
4598 |
/*
|
|
4599 |
* fix for : 6355666
|
|
4600 |
* this is the case when this method is invoked
|
|
4601 |
* from DocumentListener. At this point
|
|
4602 |
* composedTextEnd and composedTextStart are
|
|
4603 |
* not defined yet.
|
|
4604 |
*/
|
|
4605 |
length -= composedTextContent.length();
|
|
4606 |
} else {
|
|
4607 |
length -= composedTextEnd.getOffset() -
|
|
4608 |
composedTextStart.getOffset();
|
|
4609 |
}
|
|
4610 |
}
|
|
4611 |
}
|
|
4612 |
return length;
|
|
4613 |
}
|
|
4614 |
|
|
4615 |
public int getInsertPositionOffset() {
|
|
4616 |
int composedStartIndex = 0;
|
|
4617 |
int composedEndIndex = 0;
|
|
4618 |
if (composedTextExists()) {
|
|
4619 |
composedStartIndex = composedTextStart.getOffset();
|
|
4620 |
composedEndIndex = composedTextEnd.getOffset();
|
|
4621 |
}
|
|
4622 |
int caretIndex = getCaretPosition();
|
|
4623 |
|
|
4624 |
if (caretIndex < composedStartIndex) {
|
|
4625 |
return caretIndex;
|
|
4626 |
} else if (caretIndex < composedEndIndex) {
|
|
4627 |
return composedStartIndex;
|
|
4628 |
} else {
|
|
4629 |
return caretIndex - (composedEndIndex - composedStartIndex);
|
|
4630 |
}
|
|
4631 |
}
|
|
4632 |
|
|
4633 |
public TextHitInfo getLocationOffset(int x, int y) {
|
|
4634 |
if (composedTextAttribute == null) {
|
|
4635 |
return null;
|
|
4636 |
} else {
|
|
4637 |
Point p = getLocationOnScreen();
|
|
4638 |
p.x = x - p.x;
|
|
4639 |
p.y = y - p.y;
|
|
4640 |
int pos = viewToModel(p);
|
|
4641 |
if ((pos >= composedTextStart.getOffset()) &&
|
|
4642 |
(pos <= composedTextEnd.getOffset())) {
|
|
4643 |
return TextHitInfo.leading(pos - composedTextStart.getOffset());
|
|
4644 |
} else {
|
|
4645 |
return null;
|
|
4646 |
}
|
|
4647 |
}
|
|
4648 |
}
|
|
4649 |
|
|
4650 |
public Rectangle getTextLocation(TextHitInfo offset) {
|
|
4651 |
Rectangle r;
|
|
4652 |
|
|
4653 |
try {
|
|
4654 |
r = modelToView(getCaretPosition());
|
|
4655 |
if (r != null) {
|
|
4656 |
Point p = getLocationOnScreen();
|
|
4657 |
r.translate(p.x, p.y);
|
|
4658 |
}
|
|
4659 |
} catch (BadLocationException ble) {
|
|
4660 |
r = null;
|
|
4661 |
}
|
|
4662 |
|
|
4663 |
if (r == null)
|
|
4664 |
r = new Rectangle();
|
|
4665 |
|
|
4666 |
return r;
|
|
4667 |
}
|
|
4668 |
|
|
4669 |
public AttributedCharacterIterator getSelectedText(
|
|
4670 |
Attribute[] attributes) {
|
|
4671 |
String selection = JTextComponent.this.getSelectedText();
|
|
4672 |
if (selection != null) {
|
|
4673 |
return new AttributedString(selection).getIterator();
|
|
4674 |
} else {
|
|
4675 |
return null;
|
|
4676 |
}
|
|
4677 |
}
|
|
4678 |
|
|
4679 |
// --- DocumentListener methods ---
|
|
4680 |
|
|
4681 |
public void changedUpdate(DocumentEvent e) {
|
|
4682 |
latestCommittedTextStart = latestCommittedTextEnd = null;
|
|
4683 |
}
|
|
4684 |
|
|
4685 |
public void insertUpdate(DocumentEvent e) {
|
|
4686 |
latestCommittedTextStart = latestCommittedTextEnd = null;
|
|
4687 |
}
|
|
4688 |
|
|
4689 |
public void removeUpdate(DocumentEvent e) {
|
|
4690 |
latestCommittedTextStart = latestCommittedTextEnd = null;
|
|
4691 |
}
|
|
4692 |
}
|
|
4693 |
|
|
4694 |
//
|
|
4695 |
// Replaces the current input method (composed) text according to
|
|
4696 |
// the passed input method event. This method also inserts the
|
|
4697 |
// committed text into the document.
|
|
4698 |
//
|
|
4699 |
private void replaceInputMethodText(InputMethodEvent e) {
|
|
4700 |
int commitCount = e.getCommittedCharacterCount();
|
|
4701 |
AttributedCharacterIterator text = e.getText();
|
|
4702 |
int composedTextIndex;
|
|
4703 |
|
|
4704 |
// old composed text deletion
|
|
4705 |
Document doc = getDocument();
|
|
4706 |
if (composedTextExists()) {
|
|
4707 |
try {
|
|
4708 |
doc.remove(composedTextStart.getOffset(),
|
|
4709 |
composedTextEnd.getOffset() -
|
|
4710 |
composedTextStart.getOffset());
|
|
4711 |
} catch (BadLocationException ble) {}
|
|
4712 |
composedTextStart = composedTextEnd = null;
|
|
4713 |
composedTextAttribute = null;
|
|
4714 |
composedTextContent = null;
|
|
4715 |
}
|
|
4716 |
|
|
4717 |
if (text != null) {
|
|
4718 |
text.first();
|
|
4719 |
int committedTextStartIndex = 0;
|
|
4720 |
int committedTextEndIndex = 0;
|
|
4721 |
|
|
4722 |
// committed text insertion
|
|
4723 |
if (commitCount > 0) {
|
|
4724 |
// Remember latest committed text start index
|
|
4725 |
committedTextStartIndex = caret.getDot();
|
|
4726 |
|
|
4727 |
// Need to generate KeyTyped events for the committed text for components
|
|
4728 |
// that are not aware they are active input method clients.
|
|
4729 |
if (shouldSynthensizeKeyEvents()) {
|
|
4730 |
for (char c = text.current(); commitCount > 0;
|
|
4731 |
c = text.next(), commitCount--) {
|
|
4732 |
KeyEvent ke = new KeyEvent(this, KeyEvent.KEY_TYPED,
|
|
4733 |
EventQueue.getMostRecentEventTime(),
|
|
4734 |
0, KeyEvent.VK_UNDEFINED, c);
|
|
4735 |
processKeyEvent(ke);
|
|
4736 |
}
|
|
4737 |
} else {
|
|
4738 |
StringBuffer strBuf = new StringBuffer();
|
|
4739 |
for (char c = text.current(); commitCount > 0;
|
|
4740 |
c = text.next(), commitCount--) {
|
|
4741 |
strBuf.append(c);
|
|
4742 |
}
|
|
4743 |
|
|
4744 |
// map it to an ActionEvent
|
|
4745 |
mapCommittedTextToAction(new String(strBuf));
|
|
4746 |
}
|
|
4747 |
|
|
4748 |
// Remember latest committed text end index
|
|
4749 |
committedTextEndIndex = caret.getDot();
|
|
4750 |
}
|
|
4751 |
|
|
4752 |
// new composed text insertion
|
|
4753 |
composedTextIndex = text.getIndex();
|
|
4754 |
if (composedTextIndex < text.getEndIndex()) {
|
|
4755 |
createComposedTextAttribute(composedTextIndex, text);
|
|
4756 |
try {
|
|
4757 |
replaceSelection(null);
|
|
4758 |
doc.insertString(caret.getDot(), composedTextContent,
|
|
4759 |
composedTextAttribute);
|
|
4760 |
composedTextStart = doc.createPosition(caret.getDot() -
|
|
4761 |
composedTextContent.length());
|
|
4762 |
composedTextEnd = doc.createPosition(caret.getDot());
|
|
4763 |
} catch (BadLocationException ble) {
|
|
4764 |
composedTextStart = composedTextEnd = null;
|
|
4765 |
composedTextAttribute = null;
|
|
4766 |
composedTextContent = null;
|
|
4767 |
}
|
|
4768 |
}
|
|
4769 |
|
|
4770 |
// Save the latest committed text information
|
|
4771 |
if (committedTextStartIndex != committedTextEndIndex) {
|
|
4772 |
try {
|
|
4773 |
latestCommittedTextStart = doc.
|
|
4774 |
createPosition(committedTextStartIndex);
|
|
4775 |
latestCommittedTextEnd = doc.
|
|
4776 |
createPosition(committedTextEndIndex);
|
|
4777 |
} catch (BadLocationException ble) {
|
|
4778 |
latestCommittedTextStart =
|
|
4779 |
latestCommittedTextEnd = null;
|
|
4780 |
}
|
|
4781 |
} else {
|
|
4782 |
latestCommittedTextStart =
|
|
4783 |
latestCommittedTextEnd = null;
|
|
4784 |
}
|
|
4785 |
}
|
|
4786 |
}
|
|
4787 |
|
|
4788 |
private void createComposedTextAttribute(int composedIndex,
|
|
4789 |
AttributedCharacterIterator text) {
|
|
4790 |
Document doc = getDocument();
|
|
4791 |
StringBuffer strBuf = new StringBuffer();
|
|
4792 |
|
|
4793 |
// create attributed string with no attributes
|
|
4794 |
for (char c = text.setIndex(composedIndex);
|
|
4795 |
c != CharacterIterator.DONE; c = text.next()) {
|
|
4796 |
strBuf.append(c);
|
|
4797 |
}
|
|
4798 |
|
|
4799 |
composedTextContent = new String(strBuf);
|
|
4800 |
composedTextAttribute = new SimpleAttributeSet();
|
|
4801 |
composedTextAttribute.addAttribute(StyleConstants.ComposedTextAttribute,
|
|
4802 |
new AttributedString(text, composedIndex, text.getEndIndex()));
|
|
4803 |
}
|
|
4804 |
|
|
4805 |
private boolean saveComposedText(int pos) {
|
|
4806 |
if (composedTextExists()) {
|
|
4807 |
int start = composedTextStart.getOffset();
|
|
4808 |
int len = composedTextEnd.getOffset() -
|
|
4809 |
composedTextStart.getOffset();
|
|
4810 |
if (pos >= start && pos <= start + len) {
|
|
4811 |
try {
|
|
4812 |
getDocument().remove(start, len);
|
|
4813 |
return true;
|
|
4814 |
} catch (BadLocationException ble) {}
|
|
4815 |
}
|
|
4816 |
}
|
|
4817 |
return false;
|
|
4818 |
}
|
|
4819 |
|
|
4820 |
private void restoreComposedText() {
|
|
4821 |
Document doc = getDocument();
|
|
4822 |
try {
|
|
4823 |
doc.insertString(caret.getDot(),
|
|
4824 |
composedTextContent,
|
|
4825 |
composedTextAttribute);
|
|
4826 |
composedTextStart = doc.createPosition(caret.getDot() -
|
|
4827 |
composedTextContent.length());
|
|
4828 |
composedTextEnd = doc.createPosition(caret.getDot());
|
|
4829 |
} catch (BadLocationException ble) {}
|
|
4830 |
}
|
|
4831 |
|
|
4832 |
//
|
|
4833 |
// Map committed text to an ActionEvent. If the committed text length is 1,
|
|
4834 |
// treat it as a KeyStroke, otherwise or there is no KeyStroke defined,
|
|
4835 |
// treat it just as a default action.
|
|
4836 |
//
|
|
4837 |
private void mapCommittedTextToAction(String committedText) {
|
|
4838 |
Keymap binding = getKeymap();
|
|
4839 |
if (binding != null) {
|
|
4840 |
Action a = null;
|
|
4841 |
if (committedText.length() == 1) {
|
|
4842 |
KeyStroke k = KeyStroke.getKeyStroke(committedText.charAt(0));
|
|
4843 |
a = binding.getAction(k);
|
|
4844 |
}
|
|
4845 |
|
|
4846 |
if (a == null) {
|
|
4847 |
a = binding.getDefaultAction();
|
|
4848 |
}
|
|
4849 |
|
|
4850 |
if (a != null) {
|
|
4851 |
ActionEvent ae =
|
|
4852 |
new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
|
|
4853 |
committedText,
|
|
4854 |
EventQueue.getMostRecentEventTime(),
|
|
4855 |
getCurrentEventModifiers());
|
|
4856 |
a.actionPerformed(ae);
|
|
4857 |
}
|
|
4858 |
}
|
|
4859 |
}
|
|
4860 |
|
|
4861 |
//
|
|
4862 |
// Sets the caret position according to the passed input method
|
|
4863 |
// event. Also, sets/resets composed text caret appropriately.
|
|
4864 |
//
|
|
4865 |
private void setInputMethodCaretPosition(InputMethodEvent e) {
|
|
4866 |
int dot;
|
|
4867 |
|
|
4868 |
if (composedTextExists()) {
|
|
4869 |
dot = composedTextStart.getOffset();
|
|
4870 |
if (!(caret instanceof ComposedTextCaret)) {
|
|
4871 |
if (composedTextCaret == null) {
|
|
4872 |
composedTextCaret = new ComposedTextCaret();
|
|
4873 |
}
|
|
4874 |
originalCaret = caret;
|
|
4875 |
// Sets composed text caret
|
|
4876 |
exchangeCaret(originalCaret, composedTextCaret);
|
|
4877 |
}
|
|
4878 |
|
|
4879 |
TextHitInfo caretPos = e.getCaret();
|
|
4880 |
if (caretPos != null) {
|
|
4881 |
int index = caretPos.getInsertionIndex();
|
|
4882 |
dot += index;
|
|
4883 |
if (index == 0) {
|
|
4884 |
// Scroll the component if needed so that the composed text
|
|
4885 |
// becomes visible.
|
|
4886 |
try {
|
|
4887 |
Rectangle d = modelToView(dot);
|
|
4888 |
Rectangle end = modelToView(composedTextEnd.getOffset());
|
|
4889 |
Rectangle b = getBounds();
|
|
4890 |
d.x += Math.min(end.x - d.x, b.width);
|
|
4891 |
scrollRectToVisible(d);
|
|
4892 |
} catch (BadLocationException ble) {}
|
|
4893 |
}
|
|
4894 |
}
|
|
4895 |
caret.setDot(dot);
|
|
4896 |
} else if (caret instanceof ComposedTextCaret) {
|
|
4897 |
dot = caret.getDot();
|
|
4898 |
// Restores original caret
|
|
4899 |
exchangeCaret(caret, originalCaret);
|
|
4900 |
caret.setDot(dot);
|
|
4901 |
}
|
|
4902 |
}
|
|
4903 |
|
|
4904 |
private void exchangeCaret(Caret oldCaret, Caret newCaret) {
|
|
4905 |
int blinkRate = oldCaret.getBlinkRate();
|
|
4906 |
setCaret(newCaret);
|
|
4907 |
caret.setBlinkRate(blinkRate);
|
|
4908 |
caret.setVisible(hasFocus());
|
|
4909 |
}
|
|
4910 |
|
|
4911 |
/**
|
|
4912 |
* Returns true if KeyEvents should be synthesized from an InputEvent.
|
|
4913 |
*/
|
|
4914 |
private boolean shouldSynthensizeKeyEvents() {
|
|
4915 |
if (!checkedInputOverride) {
|
|
4916 |
checkedInputOverride = true;
|
|
4917 |
needToSendKeyTypedEvent =
|
|
4918 |
!isProcessInputMethodEventOverridden();
|
|
4919 |
}
|
|
4920 |
return needToSendKeyTypedEvent;
|
|
4921 |
}
|
|
4922 |
|
|
4923 |
//
|
|
4924 |
// Checks whether the client code overrides processInputMethodEvent. If it is overridden,
|
|
4925 |
// need not to generate KeyTyped events for committed text. If it's not, behave as an
|
|
4926 |
// passive input method client.
|
|
4927 |
//
|
|
4928 |
private boolean isProcessInputMethodEventOverridden() {
|
|
4929 |
if (overrideMap == null) {
|
|
4930 |
overrideMap = Collections.synchronizedMap(new HashMap());
|
|
4931 |
}
|
|
4932 |
Boolean retValue = (Boolean)overrideMap.get(getClass().getName());
|
|
4933 |
|
|
4934 |
if (retValue != null) {
|
|
4935 |
return retValue.booleanValue();
|
|
4936 |
}
|
|
4937 |
Boolean ret = (Boolean)AccessController.doPrivileged(new
|
|
4938 |
PrivilegedAction() {
|
|
4939 |
public Object run() {
|
|
4940 |
return isProcessInputMethodEventOverridden(
|
|
4941 |
JTextComponent.this.getClass());
|
|
4942 |
}
|
|
4943 |
});
|
|
4944 |
|
|
4945 |
return ret.booleanValue();
|
|
4946 |
}
|
|
4947 |
|
|
4948 |
//
|
|
4949 |
// Checks whether a composed text in this text component
|
|
4950 |
//
|
|
4951 |
boolean composedTextExists() {
|
|
4952 |
return (composedTextStart != null);
|
|
4953 |
}
|
|
4954 |
|
|
4955 |
//
|
|
4956 |
// Caret implementation for editing the composed text.
|
|
4957 |
//
|
|
4958 |
class ComposedTextCaret extends DefaultCaret implements Serializable {
|
|
4959 |
Color bg;
|
|
4960 |
|
|
4961 |
//
|
|
4962 |
// Get the background color of the component
|
|
4963 |
//
|
|
4964 |
public void install(JTextComponent c) {
|
|
4965 |
super.install(c);
|
|
4966 |
|
|
4967 |
Document doc = c.getDocument();
|
|
4968 |
if (doc instanceof StyledDocument) {
|
|
4969 |
StyledDocument sDoc = (StyledDocument)doc;
|
|
4970 |
Element elem = sDoc.getCharacterElement(c.composedTextStart.getOffset());
|
|
4971 |
AttributeSet attr = elem.getAttributes();
|
|
4972 |
bg = sDoc.getBackground(attr);
|
|
4973 |
}
|
|
4974 |
|
|
4975 |
if (bg == null) {
|
|
4976 |
bg = c.getBackground();
|
|
4977 |
}
|
|
4978 |
}
|
|
4979 |
|
|
4980 |
//
|
|
4981 |
// Draw caret in XOR mode.
|
|
4982 |
//
|
|
4983 |
public void paint(Graphics g) {
|
|
4984 |
if(isVisible()) {
|
|
4985 |
try {
|
|
4986 |
Rectangle r = component.modelToView(getDot());
|
|
4987 |
g.setXORMode(bg);
|
|
4988 |
g.drawLine(r.x, r.y, r.x, r.y + r.height - 1);
|
|
4989 |
g.setPaintMode();
|
|
4990 |
} catch (BadLocationException e) {
|
|
4991 |
// can't render I guess
|
|
4992 |
//System.err.println("Can't render cursor");
|
|
4993 |
}
|
|
4994 |
}
|
|
4995 |
}
|
|
4996 |
|
|
4997 |
//
|
|
4998 |
// If some area other than the composed text is clicked by mouse,
|
|
4999 |
// issue endComposition() to force commit the composed text.
|
|
5000 |
//
|
|
5001 |
protected void positionCaret(MouseEvent me) {
|
|
5002 |
JTextComponent host = component;
|
|
5003 |
Point pt = new Point(me.getX(), me.getY());
|
|
5004 |
int offset = host.viewToModel(pt);
|
|
5005 |
int composedStartIndex = host.composedTextStart.getOffset();
|
|
5006 |
if ((offset < composedStartIndex) ||
|
|
5007 |
(offset > composedTextEnd.getOffset())) {
|
|
5008 |
try {
|
|
5009 |
// Issue endComposition
|
|
5010 |
Position newPos = host.getDocument().createPosition(offset);
|
|
5011 |
host.getInputContext().endComposition();
|
|
5012 |
|
|
5013 |
// Post a caret positioning runnable to assure that the positioning
|
|
5014 |
// occurs *after* committing the composed text.
|
|
5015 |
EventQueue.invokeLater(new DoSetCaretPosition(host, newPos));
|
|
5016 |
} catch (BadLocationException ble) {
|
|
5017 |
System.err.println(ble);
|
|
5018 |
}
|
|
5019 |
} else {
|
|
5020 |
// Normal processing
|
|
5021 |
super.positionCaret(me);
|
|
5022 |
}
|
|
5023 |
}
|
|
5024 |
}
|
|
5025 |
|
|
5026 |
//
|
|
5027 |
// Runnable class for invokeLater() to set caret position later.
|
|
5028 |
//
|
|
5029 |
private class DoSetCaretPosition implements Runnable {
|
|
5030 |
JTextComponent host;
|
|
5031 |
Position newPos;
|
|
5032 |
|
|
5033 |
DoSetCaretPosition(JTextComponent host, Position newPos) {
|
|
5034 |
this.host = host;
|
|
5035 |
this.newPos = newPos;
|
|
5036 |
}
|
|
5037 |
|
|
5038 |
public void run() {
|
|
5039 |
host.setCaretPosition(newPos.getOffset());
|
|
5040 |
}
|
|
5041 |
}
|
|
5042 |
}
|