Source for javax.swing.text.DefaultCaret

   1: /* DefaultCaret.java --
   2:    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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)
   9: any later version.
  10: 
  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.
  15: 
  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
  19: 02110-1301 USA.
  20: 
  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
  24: combination.
  25: 
  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. */
  37: 
  38: package javax.swing.text;
  39: 
  40: import java.awt.Graphics;
  41: import java.awt.Point;
  42: import java.awt.Rectangle;
  43: import java.awt.event.FocusEvent;
  44: import java.awt.event.FocusListener;
  45: import java.awt.event.MouseEvent;
  46: import java.awt.event.MouseListener;
  47: import java.awt.event.MouseMotionListener;
  48: import java.util.EventListener;
  49: 
  50: import javax.swing.event.ChangeEvent;
  51: import javax.swing.event.ChangeListener;
  52: import javax.swing.event.EventListenerList;
  53: 
  54: 
  55: public class DefaultCaret extends Rectangle
  56:   implements Caret, FocusListener, MouseListener, MouseMotionListener
  57: {
  58:   private static final long serialVersionUID = 228155774675466193L;
  59:   
  60:   protected ChangeEvent changeEvent = new ChangeEvent(this);
  61:   protected EventListenerList listenerList = new EventListenerList();
  62:   
  63:   private JTextComponent textComponent;
  64:   
  65:   private boolean selectionVisible = true;
  66:   private int blinkRate = 500;
  67:   private int dot = 0;
  68:   private int mark = 0;
  69:   private Point magicCaretPosition = null;
  70:   private boolean visible = true;
  71:   private Object highlightEntry;
  72: 
  73:   public void mouseDragged(MouseEvent event)
  74:   {
  75:   }
  76: 
  77:   public void mouseMoved(MouseEvent event)
  78:   {
  79:   }
  80: 
  81:   public void mouseClicked(MouseEvent event)
  82:   {
  83:   }
  84: 
  85:   public void mouseEntered(MouseEvent event)
  86:   {
  87:   }
  88: 
  89:   public void mouseExited(MouseEvent event)
  90:   {
  91:   }
  92: 
  93:   public void mousePressed(MouseEvent event)
  94:   {
  95:   }
  96: 
  97:   public void mouseReleased(MouseEvent event)
  98:   {
  99:   }
 100: 
 101:   public void focusGained(FocusEvent event)
 102:   {
 103:   }
 104: 
 105:   public void focusLost(FocusEvent event)
 106:   {
 107:   }
 108: 
 109:   protected void moveCaret(MouseEvent event)
 110:   {
 111:   }
 112: 
 113:   protected void positionCaret(MouseEvent event)
 114:   {
 115:   }
 116: 
 117:   public void deinstall(JTextComponent c)
 118:   {
 119:     textComponent.removeFocusListener(this);
 120:     textComponent.removeMouseListener(this);
 121:     textComponent.removeMouseMotionListener(this);
 122:     textComponent = null;
 123:   }
 124: 
 125:   public void install(JTextComponent c)
 126:   {
 127:     textComponent = c;
 128:     textComponent.addFocusListener(this);
 129:     textComponent.addMouseListener(this);
 130:     textComponent.addMouseMotionListener(this);
 131:     repaint();
 132:   }
 133: 
 134:   public void setMagicCaretPosition(Point p)
 135:   {
 136:     magicCaretPosition = p;
 137:   }
 138: 
 139:   public Point getMagicCaretPosition()
 140:   {
 141:     return magicCaretPosition;
 142:   }
 143: 
 144:   public int getMark()
 145:   {
 146:     return mark;
 147:   }
 148: 
 149:   private void handleHighlight()
 150:   {
 151:     Highlighter highlighter = textComponent.getHighlighter();
 152:     
 153:     if (highlighter == null)
 154:       return;
 155:     
 156:     int p0 = Math.min(dot, mark);
 157:     int p1 = Math.max(dot, mark);
 158:     
 159:     if (selectionVisible && p0 != p1)
 160:       {
 161:     try
 162:       {
 163:         if (highlightEntry == null)
 164:           highlightEntry = highlighter.addHighlight(p0, p1, getSelectionPainter());
 165:         else
 166:           highlighter.changeHighlight(highlightEntry, p0, p1);
 167:       }
 168:     catch (BadLocationException e)
 169:       {
 170:         // This should never happen.
 171:         throw new InternalError();
 172:       }
 173:       }
 174:     else
 175:       {
 176:     if (highlightEntry != null)
 177:       {
 178:         highlighter.removeHighlight(highlightEntry);
 179:         highlightEntry = null;
 180:       }
 181:       }
 182:   }
 183: 
 184:   public void setSelectionVisible(boolean v)
 185:   {
 186:     if (selectionVisible == v)
 187:       return;
 188:     
 189:     selectionVisible = v;
 190:     handleHighlight();
 191:     repaint();
 192:   }
 193: 
 194:   public boolean isSelectionVisible()
 195:   {
 196:     return selectionVisible;
 197:   }
 198: 
 199:   protected final void repaint()
 200:   {
 201:     if (textComponent != null)
 202:       textComponent.repaint();
 203:   }
 204: 
 205:   public void paint(Graphics g)
 206:   {
 207:     if (textComponent == null)
 208:       return;
 209: 
 210:     int dot = getDot();
 211:     Rectangle rect = null;
 212: 
 213:     try
 214:       {
 215:     rect = textComponent.modelToView(dot);
 216:       }
 217:     catch (BadLocationException e)
 218:       {
 219:     // This should never happen as dot should be always valid.
 220:     return;
 221:       }
 222: 
 223:     if (rect == null)
 224:       return;
 225:     
 226:     // First we need to delete the old caret.
 227:     // FIXME: Implement deleting of old caret.
 228:     
 229:     // Now draw the caret on the new position if visible.
 230:     if (visible)
 231:       {
 232:     g.setColor(textComponent.getCaretColor());
 233:     g.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height);
 234:       }
 235:   }
 236: 
 237:   public EventListener[] getListeners(Class listenerType)
 238:   {
 239:     return listenerList.getListeners(listenerType);
 240:   }
 241: 
 242:   public void addChangeListener(ChangeListener listener)
 243:   {
 244:     listenerList.add(ChangeListener.class, listener);
 245:   }
 246: 
 247:   public void removeChangeListener(ChangeListener listener)
 248:   {
 249:     listenerList.remove(ChangeListener.class, listener);
 250:   }
 251: 
 252:   public ChangeListener[] getChangeListeners()
 253:   {
 254:     return (ChangeListener[]) getListeners(ChangeListener.class);
 255:   }
 256: 
 257:   protected void fireStateChanged()
 258:   {
 259:     ChangeListener[] listeners = getChangeListeners();
 260: 
 261:     for (int index = 0; index < listeners.length; ++index)
 262:       listeners[index].stateChanged(changeEvent);
 263:   }
 264: 
 265:   protected final JTextComponent getComponent()
 266:   {
 267:     return textComponent;
 268:   }
 269:   
 270:   public int getBlinkRate()
 271:   {
 272:     return blinkRate;
 273:   }
 274: 
 275:   public void setBlinkRate(int rate)
 276:   {
 277:     blinkRate = rate;
 278:   }
 279: 
 280:   public int getDot()
 281:   {
 282:     return dot;
 283:   }
 284: 
 285:   public void moveDot(int dot)
 286:   {
 287:     this.dot = dot;
 288:     handleHighlight();
 289:     repaint();
 290:   }
 291: 
 292:   public void setDot(int dot)
 293:   {
 294:     this.dot = dot;
 295:     this.mark = dot;
 296:     handleHighlight();
 297:     repaint();
 298:   }
 299: 
 300:   public boolean isVisible()
 301:   {
 302:     return visible;
 303:   }
 304: 
 305:   public void setVisible(boolean v)
 306:   {
 307:     visible = v;
 308:     repaint();
 309:   }
 310: 
 311:   protected Highlighter.HighlightPainter getSelectionPainter()
 312:   {
 313:     return DefaultHighlighter.DefaultPainter;
 314:   }
 315: }