1 /* DefaultCaret.java --
2 Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing.text;
40 import java.awt.Graphics;
41 import java.awt.Point;
42 import java.awt.Rectangle;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.event.FocusEvent;
46 import java.awt.event.FocusListener;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.awt.event.MouseMotionListener;
50 import java.beans.PropertyChangeEvent;
51 import java.beans.PropertyChangeListener;
52 import java.util.EventListener;
54 import javax.swing.JComponent;
55 import javax.swing.SwingUtilities;
56 import javax.swing.Timer;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
59 import javax.swing.event.DocumentEvent;
60 import javax.swing.event.DocumentListener;
61 import javax.swing.event.EventListenerList;
62 import javax.swing.text.Position.Bias;
65 * The default implementation of the {@link Caret} interface.
67 * @author original author unknown
68 * @author Roman Kennke (roman@kennke.org)
70 public class DefaultCaret extends Rectangle
71 implements Caret, FocusListener, MouseListener, MouseMotionListener
74 /** A text component in the current VM which currently has a
75 * text selection or <code>null</code>.
77 static JTextComponent componentWithSelection;
79 /** An implementation of NavigationFilter.FilterBypass which delegates
80 * to the corresponding methods of the <code>DefaultCaret</code>.
82 * @author Robert Schuster (robertschuster@fsfe.org)
84 class Bypass extends NavigationFilter.FilterBypass
87 public Caret getCaret()
89 return DefaultCaret.this;
92 public void moveDot(int dot, Bias bias)
94 DefaultCaret.this.moveDotImpl(dot);
97 public void setDot(int dot, Bias bias)
99 DefaultCaret.this.setDotImpl(dot);
105 * Controls the blinking of the caret.
107 * @author Roman Kennke (kennke@aicas.com)
108 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
110 private class BlinkTimerListener implements ActionListener
113 * Forces the next event to be ignored. The next event should be ignored
114 * if we force the caret to appear. We do not know how long will it take
115 * to fire the comming event; this may be near immediately. Better to leave
116 * the caret visible one iteration longer.
118 boolean ignoreNextEvent;
121 * Receives notification when the blink timer fires and updates the visible
122 * state of the caret.
124 * @param event the action event
126 public void actionPerformed(ActionEvent event)
129 ignoreNextEvent = false;
139 * Listens for changes in the text component's document and updates the
142 * @author Roman Kennke (kennke@aicas.com)
144 private class DocumentHandler implements DocumentListener
147 * Receives notification that some text attributes have changed. No action
150 * @param event the document event
152 public void changedUpdate(DocumentEvent event)
154 // Nothing to do here.
158 * Receives notification that some text has been inserted from the text
159 * component. The caret is moved forward accordingly.
161 * @param event the document event
163 public void insertUpdate(DocumentEvent event)
165 if (policy == ALWAYS_UPDATE ||
166 (SwingUtilities.isEventDispatchThread() &&
167 policy == UPDATE_WHEN_ON_EDT))
170 setDot(dot + event.getLength());
175 * Receives notification that some text has been removed into the text
176 * component. The caret is moved backwards accordingly.
178 * @param event the document event
180 public void removeUpdate(DocumentEvent event)
182 if (policy == ALWAYS_UPDATE
183 || (SwingUtilities.isEventDispatchThread()
184 && policy == UPDATE_WHEN_ON_EDT))
187 setDot(dot - event.getLength());
189 else if (policy == NEVER_UPDATE
190 || (! SwingUtilities.isEventDispatchThread()
191 && policy == UPDATE_WHEN_ON_EDT))
193 int docLength = event.getDocument().getLength();
194 if (getDot() > docLength)
201 * Listens for property changes on the text document. This is used to add and
202 * remove our document listener, if the document of the text component has
205 * @author Roman Kennke (kennke@aicas.com)
207 private class PropertyChangeHandler implements PropertyChangeListener
211 * Receives notification when a property has changed on the text component.
212 * This adds/removes our document listener from the text component's
213 * document when the document changes.
215 * @param e the property change event
217 public void propertyChange(PropertyChangeEvent e)
219 String name = e.getPropertyName();
221 if (name.equals("document"))
223 Document oldDoc = (Document) e.getOldValue();
225 oldDoc.removeDocumentListener(documentListener);
227 Document newDoc = (Document) e.getNewValue();
229 newDoc.addDocumentListener(documentListener);
231 else if (name.equals("editable"))
233 active = (((Boolean) e.getNewValue()).booleanValue()
234 && textComponent.isEnabled());
236 else if (name.equals("enabled"))
238 active = (((Boolean) e.getNewValue()).booleanValue()
239 && textComponent.isEditable());
246 /** The serialization UID (compatible with JDK1.5). */
247 private static final long serialVersionUID = 4325555698756477346L;
250 * Indicates the Caret position should always be updated after Document
251 * changes even if the updates are not performed on the Event Dispatching
256 public static final int ALWAYS_UPDATE = 2;
259 * Indicates the Caret position should not be changed unless the Document
260 * length becomes less than the Caret position, in which case the Caret
261 * is moved to the end of the Document.
265 public static final int NEVER_UPDATE = 1;
268 * Indicates the Caret position should be updated only if Document changes
269 * are made on the Event Dispatcher thread.
273 public static final int UPDATE_WHEN_ON_EDT = 0;
275 /** Keeps track of the current update policy **/
276 int policy = UPDATE_WHEN_ON_EDT;
279 * The <code>ChangeEvent</code> that is fired by {@link #fireStateChanged()}.
281 protected ChangeEvent changeEvent = new ChangeEvent(this);
284 * Stores all registered event listeners.
286 protected EventListenerList listenerList = new EventListenerList();
289 * Our document listener.
291 DocumentListener documentListener;
294 * Our property listener.
296 PropertyChangeListener propertyChangeListener;
299 * The text component in which this caret is installed.
301 * (Package private to avoid synthetic accessor method.)
303 JTextComponent textComponent;
306 * Indicates if the selection should be visible or not.
308 private boolean selectionVisible = true;
311 * The blink rate of this <code>Caret</code>.
313 private int blinkRate = 500;
316 * The current dot position.
321 * The current mark position.
323 private int mark = 0;
326 * The current visual caret position.
328 private Point magicCaretPosition = null;
331 * Indicates if this <code>Caret</code> is currently visible or not. This is
332 * package private to avoid an accessor method.
334 boolean visible = false;
336 /** Indicates whether the text component where the caret is installed is
337 * editable and enabled. If either of these properties is <code>false</code>
338 * the caret is not drawn.
340 boolean active = true;
343 * The current highlight entry.
345 private Object highlightEntry;
347 private Timer blinkTimer;
349 private BlinkTimerListener blinkListener;
352 * A <code>NavigationFilter.FilterBypass</code> instance which
353 * is provided to the a <code>NavigationFilter</code> to
354 * unconditionally set or move the caret.
356 NavigationFilter.FilterBypass bypass;
359 * Creates a new <code>DefaultCaret</code> instance.
361 public DefaultCaret()
363 // Nothing to do here.
366 /** Returns the caret's <code>NavigationFilter.FilterBypass</code> instance
367 * and creates it if it does not yet exist.
369 * @return The caret's <code>NavigationFilter.FilterBypass</code> instance.
371 private NavigationFilter.FilterBypass getBypass()
373 return (bypass == null) ? bypass = new Bypass() : bypass;
377 * Sets the Caret update policy.
379 * @param policy the new policy. Valid values are:
380 * ALWAYS_UPDATE: always update the Caret position, even when Document
381 * updates don't occur on the Event Dispatcher thread.
382 * NEVER_UPDATE: don't update the Caret position unless the Document
383 * length becomes less than the Caret position (then update the
384 * Caret to the end of the Document).
385 * UPDATE_WHEN_ON_EDT: update the Caret position when the
386 * Document updates occur on the Event Dispatcher thread. This is the
390 * @throws IllegalArgumentException if policy is not one of the above.
392 public void setUpdatePolicy (int policy)
394 if (policy != ALWAYS_UPDATE && policy != NEVER_UPDATE
395 && policy != UPDATE_WHEN_ON_EDT)
397 IllegalArgumentException
398 ("policy must be ALWAYS_UPDATE, NEVER__UPDATE, or UPDATE_WHEN_ON_EDT");
399 this.policy = policy;
403 * Gets the caret update policy.
405 * @return the caret update policy.
408 public int getUpdatePolicy ()
414 * Moves the caret position when the mouse is dragged over the text
415 * component, modifying the selectiony.
417 * <p>When the text component where the caret is installed is disabled,
418 * the selection is not change but you can still scroll the text and
419 * update the caret's location.</p>
421 * @param event the <code>MouseEvent</code> describing the drag operation
423 public void mouseDragged(MouseEvent event)
425 if (event.getButton() == MouseEvent.BUTTON1)
427 if (textComponent.isEnabled())
430 positionCaret(event);
435 * Indicates a mouse movement over the text component. Does nothing here.
437 * @param event the <code>MouseEvent</code> describing the mouse operation
439 public void mouseMoved(MouseEvent event)
441 // Nothing to do here.
445 * When the click is received from Button 1 then the following actions
446 * are performed here:
449 * <li>If we receive a double click, the caret position (dot) is set
450 * to the position associated to the mouse click and the word at
451 * this location is selected. If there is no word at the pointer
452 * the gap is selected instead.</li>
453 * <li>If we receive a triple click, the caret position (dot) is set
454 * to the position associated to the mouse click and the line at
455 * this location is selected.</li>
458 * @param event the <code>MouseEvent</code> describing the click operation
460 public void mouseClicked(MouseEvent event)
462 // Do not modify selection if component is disabled.
463 if (!textComponent.isEnabled())
466 int count = event.getClickCount();
468 if (event.getButton() == MouseEvent.BUTTON1 && count >= 2)
470 int newDot = getComponent().viewToModel(event.getPoint());
471 JTextComponent t = getComponent();
477 setDot(Utilities.getRowStart(t, newDot));
478 moveDot( Utilities.getRowEnd(t, newDot));
482 int wordStart = Utilities.getWordStart(t, newDot);
484 // When the mouse points at the offset of the first character
485 // in a word Utilities().getPreviousWord will not return that
486 // word but we want to select that. We have to use
487 // Utilities.getWordStart() to get it.
488 if (newDot == wordStart)
491 moveDot(Utilities.getWordEnd(t, wordStart));
495 int nextWord = Utilities.getNextWord(t, newDot);
496 int previousWord = Utilities.getPreviousWord(t, newDot);
497 int previousWordEnd = Utilities.getWordEnd(t, previousWord);
499 // If the user clicked in the space between two words,
500 // then select the space.
501 if (newDot >= previousWordEnd && newDot <= nextWord)
503 setDot(previousWordEnd);
506 // Otherwise select the word under the mouse pointer.
509 setDot(previousWord);
510 moveDot(previousWordEnd);
515 catch(BadLocationException ble)
517 // TODO: Swallowing ok here?
524 * Indicates that the mouse has entered the text component. Nothing is done
527 * @param event the <code>MouseEvent</code> describing the mouse operation
529 public void mouseEntered(MouseEvent event)
531 // Nothing to do here.
535 * Indicates that the mouse has exited the text component. Nothing is done
538 * @param event the <code>MouseEvent</code> describing the mouse operation
540 public void mouseExited(MouseEvent event)
542 // Nothing to do here.
546 * If the button 1 is pressed, the caret position is updated to the
547 * position of the mouse click and the text component requests the input
548 * focus if it is enabled. If the SHIFT key is held down, the caret will
549 * be moved, which might select the text between the old and new location.
551 * @param event the <code>MouseEvent</code> describing the press operation
553 public void mousePressed(MouseEvent event)
556 // The implementation assumes that consuming the event makes the AWT event
557 // mechanism forget about this event instance and not transfer focus.
558 // By observing how the RI reacts the following behavior has been
559 // implemented (in regard to text components):
560 // - a left-click moves the caret
561 // - a left-click when shift is held down expands the selection
562 // - a right-click or click with any additional mouse button
563 // on a text component is ignored
564 // - a middle-click positions the caret and pastes the clipboard
566 // - a middle-click when shift is held down is ignored
568 if (SwingUtilities.isLeftMouseButton(event))
571 if (event.isShiftDown() && getDot() != -1)
577 positionCaret(event);
581 if (textComponent != null && textComponent.isEnabled()
582 && textComponent.isRequestFocusEnabled())
584 textComponent.requestFocus();
587 // TODO: Handle double click for selecting words.
589 else if(event.getButton() == MouseEvent.BUTTON2)
591 // Special handling for X11-style pasting.
592 if (! event.isShiftDown())
594 positionCaret(event);
595 textComponent.paste();
601 * Indicates that a mouse button has been released on the text component.
602 * Nothing is done here.
604 * @param event the <code>MouseEvent</code> describing the mouse operation
606 public void mouseReleased(MouseEvent event)
608 // Nothing to do here.
612 * Sets the caret to <code>visible</code> if the text component is editable.
614 * @param event the <code>FocusEvent</code>
616 public void focusGained(FocusEvent event)
618 if (textComponent.isEditable())
626 * Sets the caret to <code>invisible</code>.
628 * @param event the <code>FocusEvent</code>
630 public void focusLost(FocusEvent event)
632 if (textComponent.isEditable() && event.isTemporary() == false)
636 // Stop the blinker, if running.
637 if (blinkTimer != null && blinkTimer.isRunning())
643 * Install (if not present) and start the timer, if the caret must blink. The
644 * caret does not blink if it is invisible, or the component is disabled or
647 private void updateTimerStatus()
649 if (textComponent.isEnabled() && textComponent.isEditable())
651 if (blinkTimer == null)
653 if (!blinkTimer.isRunning())
658 if (blinkTimer != null)
664 * Moves the caret to the position specified in the <code>MouseEvent</code>.
665 * This will cause a selection if the dot and mark are different.
667 * @param event the <code>MouseEvent</code> from which to fetch the position
669 protected void moveCaret(MouseEvent event)
671 int newDot = getComponent().viewToModel(event.getPoint());
676 * Repositions the caret to the position specified in the
677 * <code>MouseEvent</code>.
679 * @param event the <code>MouseEvent</code> from which to fetch the position
681 protected void positionCaret(MouseEvent event)
683 int newDot = getComponent().viewToModel(event.getPoint());
688 * Deinstalls this <code>Caret</code> from the specified
689 * <code>JTextComponent</code>. This removes any listeners that have been
690 * registered by this <code>Caret</code>.
692 * @param c the text component from which to install this caret
694 public void deinstall(JTextComponent c)
696 textComponent.removeFocusListener(this);
697 textComponent.removeMouseListener(this);
698 textComponent.removeMouseMotionListener(this);
699 textComponent.getDocument().removeDocumentListener(documentListener);
700 documentListener = null;
701 textComponent.removePropertyChangeListener(propertyChangeListener);
702 propertyChangeListener = null;
703 textComponent = null;
705 // Deinstall blink timer if present.
706 if (blinkTimer != null)
712 * Installs this <code>Caret</code> on the specified
713 * <code>JTextComponent</code>. This registers a couple of listeners
714 * on the text component.
716 * @param c the text component on which to install this caret
718 public void install(JTextComponent c)
721 textComponent.addFocusListener(this);
722 textComponent.addMouseListener(this);
723 textComponent.addMouseMotionListener(this);
724 propertyChangeListener = new PropertyChangeHandler();
725 textComponent.addPropertyChangeListener(propertyChangeListener);
726 documentListener = new DocumentHandler();
728 Document doc = textComponent.getDocument();
730 doc.addDocumentListener(documentListener);
732 active = textComponent.isEditable() && textComponent.isEnabled();
738 * Sets the current visual position of this <code>Caret</code>.
740 * @param p the Point to use for the saved location. May be <code>null</code>
741 * to indicate that there is no visual location
743 public void setMagicCaretPosition(Point p)
745 magicCaretPosition = p;
749 * Returns the current visual position of this <code>Caret</code>.
751 * @return the current visual position of this <code>Caret</code>
753 * @see #setMagicCaretPosition
755 public Point getMagicCaretPosition()
757 return magicCaretPosition;
761 * Returns the current position of the <code>mark</code>. The
762 * <code>mark</code> marks the location in the <code>Document</code> that
763 * is the end of a selection. If there is no selection, the <code>mark</code>
764 * is the same as the <code>dot</code>.
766 * @return the current position of the mark
773 private void clearHighlight()
775 Highlighter highlighter = textComponent.getHighlighter();
777 if (highlighter == null)
780 if (selectionVisible)
784 if (highlightEntry != null)
785 highlighter.changeHighlight(highlightEntry, 0, 0);
787 // Free the global variable which stores the text component with an active
789 if (componentWithSelection == textComponent)
790 componentWithSelection = null;
792 catch (BadLocationException e)
794 // This should never happen.
795 throw new InternalError();
800 if (highlightEntry != null)
802 highlighter.removeHighlight(highlightEntry);
803 highlightEntry = null;
808 private void handleHighlight()
810 Highlighter highlighter = textComponent.getHighlighter();
812 if (highlighter == null)
815 int p0 = Math.min(dot, mark);
816 int p1 = Math.max(dot, mark);
818 if (selectionVisible)
822 if (highlightEntry == null)
823 highlightEntry = highlighter.addHighlight(p0, p1, getSelectionPainter());
825 highlighter.changeHighlight(highlightEntry, p0, p1);
827 // If another component currently has a text selection clear that selection
829 if (componentWithSelection != null)
830 if (componentWithSelection != textComponent)
832 Caret c = componentWithSelection.getCaret();
833 c.setDot(c.getDot());
835 componentWithSelection = textComponent;
838 catch (BadLocationException e)
840 // This should never happen.
841 throw new InternalError();
846 if (highlightEntry != null)
848 highlighter.removeHighlight(highlightEntry);
849 highlightEntry = null;
855 * Sets the visiblity state of the selection.
857 * @param v <code>true</code> if the selection should be visible,
858 * <code>false</code> otherwise
860 public void setSelectionVisible(boolean v)
862 if (selectionVisible == v)
865 selectionVisible = v;
871 * Returns <code>true</code> if the selection is currently visible,
872 * <code>false</code> otherwise.
874 * @return <code>true</code> if the selection is currently visible,
875 * <code>false</code> otherwise
877 public boolean isSelectionVisible()
879 return selectionVisible;
883 * Causes the <code>Caret</code> to repaint itself.
885 protected final void repaint()
887 getComponent().repaint(x, y, width, height);
891 * Paints this <code>Caret</code> using the specified <code>Graphics</code>
894 * @param g the graphics context to use
896 public void paint(Graphics g)
898 JTextComponent comp = getComponent();
902 // Make sure the dot has a sane position.
903 dot = Math.min(dot, textComponent.getDocument().getLength());
904 dot = Math.max(dot, 0);
906 Rectangle rect = null;
910 rect = textComponent.modelToView(dot);
912 catch (BadLocationException e)
914 // Let's ignore that. This shouldn't really occur. But if it
915 // does (it seems that this happens when the model is mutating),
916 // it causes no real damage. Uncomment this for debugging.
917 // e.printStackTrace();
923 // Check if paint has possibly been called directly, without a previous
924 // call to damage(). In this case we need to do some cleanup first.
925 if ((x != rect.x) || (y != rect.y))
927 repaint(); // Erase previous location of caret.
931 height = rect.height;
934 // Now draw the caret on the new position if visible.
935 if (visible && active)
937 g.setColor(textComponent.getCaretColor());
938 g.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
943 * Returns all registered event listeners of the specified type.
945 * @param listenerType the type of listener to return
947 * @return all registered event listeners of the specified type
949 public <T extends EventListener> T[] getListeners(Class<T> listenerType)
951 return listenerList.getListeners(listenerType);
955 * Registers a {@link ChangeListener} that is notified whenever that state
956 * of this <code>Caret</code> changes.
958 * @param listener the listener to register to this caret
960 public void addChangeListener(ChangeListener listener)
962 listenerList.add(ChangeListener.class, listener);
966 * Removes a {@link ChangeListener} from the list of registered listeners.
968 * @param listener the listener to remove
970 public void removeChangeListener(ChangeListener listener)
972 listenerList.remove(ChangeListener.class, listener);
976 * Returns all registered {@link ChangeListener}s of this <code>Caret</code>.
978 * @return all registered {@link ChangeListener}s of this <code>Caret</code>
980 public ChangeListener[] getChangeListeners()
982 return (ChangeListener[]) getListeners(ChangeListener.class);
986 * Notifies all registered {@link ChangeListener}s that the state
987 * of this <code>Caret</code> has changed.
989 protected void fireStateChanged()
991 ChangeListener[] listeners = getChangeListeners();
993 for (int index = 0; index < listeners.length; ++index)
994 listeners[index].stateChanged(changeEvent);
998 * Returns the <code>JTextComponent</code> on which this <code>Caret</code>
1001 * @return the <code>JTextComponent</code> on which this <code>Caret</code>
1004 protected final JTextComponent getComponent()
1006 return textComponent;
1010 * Returns the blink rate of this <code>Caret</code> in milliseconds.
1011 * A value of <code>0</code> means that the caret does not blink.
1013 * @return the blink rate of this <code>Caret</code> or <code>0</code> if
1014 * this caret does not blink
1016 public int getBlinkRate()
1022 * Sets the blink rate of this <code>Caret</code> in milliseconds.
1023 * A value of <code>0</code> means that the caret does not blink.
1025 * @param rate the new blink rate to set
1027 public void setBlinkRate(int rate)
1029 if (blinkTimer != null)
1030 blinkTimer.setDelay(rate);
1035 * Returns the current position of this <code>Caret</code> within the
1036 * <code>Document</code>.
1038 * @return the current position of this <code>Caret</code> within the
1039 * <code>Document</code>
1047 * Moves the <code>dot</code> location without touching the
1048 * <code>mark</code>. This is used when making a selection.
1050 * <p>If the underlying text component has a {@link NavigationFilter}
1051 * installed the caret will call the corresponding method of that object.</p>
1053 * @param dot the location where to move the dot
1057 public void moveDot(int dot)
1059 NavigationFilter filter = textComponent.getNavigationFilter();
1061 filter.moveDot(getBypass(), dot, Bias.Forward);
1066 void moveDotImpl(int dot)
1070 Document doc = textComponent.getDocument();
1072 this.dot = Math.min(dot, doc.getLength());
1073 this.dot = Math.max(this.dot, 0);
1082 * Sets the current position of this <code>Caret</code> within the
1083 * <code>Document</code>. This also sets the <code>mark</code> to the new
1086 * <p>If the underlying text component has a {@link NavigationFilter}
1087 * installed the caret will call the corresponding method of that object.</p>
1090 * the new position to be set
1091 * @see #moveDot(int)
1093 public void setDot(int dot)
1095 NavigationFilter filter = textComponent.getNavigationFilter();
1097 filter.setDot(getBypass(), dot, Bias.Forward);
1102 void setDotImpl(int dot)
1106 Document doc = textComponent.getDocument();
1108 this.dot = Math.min(dot, doc.getLength());
1109 this.dot = Math.max(this.dot, 0);
1110 this.mark = this.dot;
1119 * Show the caret (may be hidden due blinking) and adjust the timer not to
1120 * hide it (possibly immediately).
1122 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
1126 // All machinery is only required if the carret is blinking.
1127 if (blinkListener != null)
1129 blinkListener.ignoreNextEvent = true;
1131 // If the caret is visible, erase the current position by repainting
1136 // Draw the caret in the new position.
1139 Rectangle area = null;
1143 area = getComponent().modelToView(dot);
1145 catch (BadLocationException e)
1147 // Let's ignore that. This shouldn't really occur. But if it
1148 // does (it seems that this happens when the model is mutating),
1149 // it causes no real damage. Uncomment this for debugging.
1150 // e.printStackTrace();
1154 adjustVisibility(area);
1155 if (getMagicCaretPosition() == null)
1156 setMagicCaretPosition(new Point(area.x, area.y));
1164 * Returns <code>true</code> if this <code>Caret</code> is blinking,
1165 * and <code>false</code> if not. The returned value is independent of
1166 * the visiblity of this <code>Caret</code> as returned by {@link #isVisible()}.
1168 * @return <code>true</code> if this <code>Caret</code> is blinking,
1169 * and <code>false</code> if not.
1173 public boolean isActive()
1175 if (blinkTimer != null)
1176 return blinkTimer.isRunning();
1182 * Returns <code>true</code> if this <code>Caret</code> is currently visible,
1183 * and <code>false</code> if it is not.
1185 * @return <code>true</code> if this <code>Caret</code> is currently visible,
1186 * and <code>false</code> if it is not
1188 public boolean isVisible()
1190 return visible && active;
1194 * Sets the visibility state of the caret. <code>true</code> shows the
1195 * <code>Caret</code>, <code>false</code> hides it.
1197 * @param v the visibility to set
1199 public void setVisible(boolean v)
1204 updateTimerStatus();
1205 Rectangle area = null;
1209 area = getComponent().modelToView(dot);
1211 catch (BadLocationException e)
1214 ae = new AssertionError("Unexpected bad caret location: " + dot);
1224 * Returns the {@link Highlighter.HighlightPainter} that should be used
1225 * to paint the selection.
1227 * @return the {@link Highlighter.HighlightPainter} that should be used
1228 * to paint the selection
1230 protected Highlighter.HighlightPainter getSelectionPainter()
1232 return DefaultHighlighter.DefaultPainter;
1236 * Updates the carets rectangle properties to the specified rectangle and
1237 * repaints the caret.
1239 * @param r the rectangle to set as the caret rectangle
1241 protected void damage(Rectangle r)
1248 // height is normally set in paint and we leave it untouched. However, we
1249 // must set a valid value here, since otherwise the painting mechanism
1250 // sets a zero clip and never calls paint.
1254 height = textComponent.modelToView(dot).height;
1256 catch (BadLocationException ble)
1258 // Should not happen.
1259 throw new InternalError("Caret location not within document range.");
1266 * Adjusts the text component so that the caret is visible. This default
1267 * implementation simply calls
1268 * {@link JComponent#scrollRectToVisible(Rectangle)} on the text component.
1269 * Subclasses may wish to change this.
1271 protected void adjustVisibility(Rectangle rect)
1273 getComponent().scrollRectToVisible(rect);
1277 * Initializes the blink timer.
1279 private void initBlinkTimer()
1281 // Setup the blink timer.
1282 blinkListener = new BlinkTimerListener();
1283 blinkTimer = new Timer(getBlinkRate(), blinkListener);
1284 blinkTimer.setRepeats(true);