schmitzm.swing
Class SelectionPreservingCaret

java.lang.Object
  extended by java.awt.geom.RectangularShape
      extended by java.awt.geom.Rectangle2D
          extended by java.awt.Rectangle
              extended by javax.swing.text.DefaultCaret
                  extended by schmitzm.swing.SelectionPreservingCaret
All Implemented Interfaces:
FocusListener, MouseListener, MouseMotionListener, Shape, Serializable, Cloneable, EventListener, Caret

public class SelectionPreservingCaret
extends DefaultCaret

Caret implementation that doesn't blow away the selection when we lose focus.

Code taken from javatechniques.com and extended with the preserve-caret functionality.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.geom.Rectangle2D
Rectangle2D.Double, Rectangle2D.Float
 
Field Summary
 
Fields inherited from class javax.swing.text.DefaultCaret
ALWAYS_UPDATE, changeEvent, listenerList, NEVER_UPDATE, UPDATE_WHEN_ON_EDT
 
Fields inherited from class java.awt.Rectangle
height, width, x, y
 
Fields inherited from class java.awt.geom.Rectangle2D
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP
 
Constructor Summary
SelectionPreservingCaret()
          Constructs a new caret which does preserve the selection, but not the caret.
SelectionPreservingCaret(boolean preserveCaret)
          Constructs a new caret which does preserve the selection.
 
Method Summary
 void focusGained(FocusEvent evt)
          Called when the component containing the caret gains focus.
 void focusLost(FocusEvent evt)
          Called when the component containing the caret loses focus.
protected  void hide()
          Delivers a defered focusLost event to this caret.
 
Methods inherited from class javax.swing.text.DefaultCaret
addChangeListener, adjustVisibility, damage, deinstall, equals, fireStateChanged, getBlinkRate, getChangeListeners, getComponent, getDot, getDotBias, getListeners, getMagicCaretPosition, getMark, getMarkBias, getSelectionPainter, getUpdatePolicy, install, isActive, isSelectionVisible, isVisible, mouseClicked, mouseDragged, mouseEntered, mouseExited, mouseMoved, mousePressed, mouseReleased, moveCaret, moveDot, moveDot, paint, positionCaret, removeChangeListener, repaint, setBlinkRate, setDot, setDot, setMagicCaretPosition, setSelectionVisible, setUpdatePolicy, setVisible, toString
 
Methods inherited from class java.awt.Rectangle
add, add, add, contains, contains, contains, contains, createIntersection, createUnion, getBounds, getBounds2D, getHeight, getLocation, getSize, getWidth, getX, getY, grow, inside, intersection, intersects, isEmpty, move, outcode, reshape, resize, setBounds, setBounds, setLocation, setLocation, setRect, setSize, setSize, translate, union
 
Methods inherited from class java.awt.geom.Rectangle2D
add, add, add, contains, contains, getPathIterator, getPathIterator, hashCode, intersect, intersects, intersectsLine, intersectsLine, outcode, setFrame, setRect, union
 
Methods inherited from class java.awt.geom.RectangularShape
clone, contains, contains, getCenterX, getCenterY, getFrame, getMaxX, getMaxY, getMinX, getMinY, intersects, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.Shape
contains, contains, contains, contains, getPathIterator, getPathIterator, intersects, intersects
 

Constructor Detail

SelectionPreservingCaret

public SelectionPreservingCaret(boolean preserveCaret)
Constructs a new caret which does preserve the selection.

Parameters:
preserveCaret - indicates whether the caret is also preserved

SelectionPreservingCaret

public SelectionPreservingCaret()
Constructs a new caret which does preserve the selection, but not the caret.

Method Detail

focusGained

public void focusGained(FocusEvent evt)
Called when the component containing the caret gains focus. DefaultCaret does most of the work, while the subclass checks to see if another instance of SelectionPreservingCaret previously had focus.

Specified by:
focusGained in interface FocusListener
Overrides:
focusGained in class DefaultCaret
Parameters:
evt - the focus event
See Also:
FocusListener.focusGained(java.awt.event.FocusEvent)

focusLost

public void focusLost(FocusEvent evt)
Called when the component containing the caret loses focus. Instead of hiding both the caret and the selection, the subclass only hides the caret and saves a (static) reference to the event and this specific caret instance so that the event can be delivered later if appropriate.

Specified by:
focusLost in interface FocusListener
Overrides:
focusLost in class DefaultCaret
Parameters:
evt - the focus event
See Also:
FocusListener.focusLost(java.awt.event.FocusEvent)

hide

protected void hide()
Delivers a defered focusLost event to this caret.