/[classpath]/classpath/javax/swing/text/DefaultCaret.java
ViewVC logotype

Diff of /classpath/javax/swing/text/DefaultCaret.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.11 by mark, Sat Jul 2 20:32:51 2005 UTC revision 1.12 by rabbit78, Fri Aug 5 10:10:17 2005 UTC
# Line 1  Line 1 
1  /* DefaultCaret.java --  /* DefaultCaret.java --
2     Copyright (C) 2002, 2004 Free Software Foundation, Inc.     Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 51  import javax.swing.event.ChangeEvent; Line 51  import javax.swing.event.ChangeEvent;
51  import javax.swing.event.ChangeListener;  import javax.swing.event.ChangeListener;
52  import javax.swing.event.EventListenerList;  import javax.swing.event.EventListenerList;
53    
54    /**
55     * The default implementation of the {@link Caret} interface.
56     *
57     * @author orgininal author unknown
58     * @author Roman Kennke (roman@kennke.org)
59     */
60  public class DefaultCaret extends Rectangle  public class DefaultCaret extends Rectangle
61    implements Caret, FocusListener, MouseListener, MouseMotionListener    implements Caret, FocusListener, MouseListener, MouseMotionListener
62  {  {
63      /**
64       * The serial version UID for DefaultCaret.
65       */
66    private static final long serialVersionUID = 228155774675466193L;    private static final long serialVersionUID = 228155774675466193L;
67      
68      /**
69       * The <code>ChangeEvent</code> that is fired by {@link #fireStateChanged()}.
70       */
71    protected ChangeEvent changeEvent = new ChangeEvent(this);    protected ChangeEvent changeEvent = new ChangeEvent(this);
72    
73      /**
74       * Stores all registered event listeners.
75       */
76    protected EventListenerList listenerList = new EventListenerList();    protected EventListenerList listenerList = new EventListenerList();
77      
78      /**
79       * The text component in which this caret is installed.
80       */
81    private JTextComponent textComponent;    private JTextComponent textComponent;
82      
83      /**
84       * Indicates if the selection should be visible or not.
85       */
86    private boolean selectionVisible = true;    private boolean selectionVisible = true;
87    
88      /**
89       * The blink rate of this <code>Caret</code>.
90       */
91    private int blinkRate = 500;    private int blinkRate = 500;
92    
93      /**
94       * The current dot position.
95       */
96    private int dot = 0;    private int dot = 0;
97    
98      /**
99       * The current mark position.
100       */
101    private int mark = 0;    private int mark = 0;
102    
103      /**
104       * The current visual caret position.
105       */
106    private Point magicCaretPosition = null;    private Point magicCaretPosition = null;
107    
108      /**
109       * Indicates if this <code>Caret</code> is currently visible or not.
110       */
111    private boolean visible = true;    private boolean visible = true;
112    
113      /**
114       * The current highlight entry.
115       */
116    private Object highlightEntry;    private Object highlightEntry;
117    
118      /**
119       * Moves the caret position when the mouse is dragged over the text
120       * component, modifying the selection accordingly.
121       *
122       * @param event the <code>MouseEvent</code> describing the drag operation
123       */
124    public void mouseDragged(MouseEvent event)    public void mouseDragged(MouseEvent event)
125    {    {
126        // FIXME: Implement this properly.
127    }    }
128    
129      /**
130       * Indicates a mouse movement over the text component. Does nothing here.
131       *
132       * @param event the <code>MouseEvent</code> describing the mouse operation
133       */
134    public void mouseMoved(MouseEvent event)    public void mouseMoved(MouseEvent event)
135    {    {
136        // Nothing to do here.
137    }    }
138    
139      /**
140       * When the click is received from Button 1 then the following actions
141       * are performed here:
142       *
143       * <ul>
144       * <li>If we receive a double click, the caret position (dot) is set
145       *   to the position associated to the mouse click and the word at
146       *   this location is selected.</li>
147       * <li>If we receive a triple click, the caret position (dot) is set
148       *   to the position associated to the mouse click and the line at
149       *   this location is selected.</li>
150       * </ul>
151       *
152       * @param event the <code>MouseEvent</code> describing the click operation
153       */
154    public void mouseClicked(MouseEvent event)    public void mouseClicked(MouseEvent event)
155    {    {
156        // FIXME: Implement this properly.
157    }    }
158    
159      /**
160       * Indicates that the mouse has entered the text component. Nothing is done
161       * here.
162       *
163       * @param event the <code>MouseEvent</code> describing the mouse operation
164       */
165    public void mouseEntered(MouseEvent event)    public void mouseEntered(MouseEvent event)
166    {    {
167        // Nothing to do here.
168    }    }
169    
170      /**
171       * Indicates that the mouse has exited the text component. Nothing is done
172       * here.
173       *
174       * @param event the <code>MouseEvent</code> describing the mouse operation
175       */
176    public void mouseExited(MouseEvent event)    public void mouseExited(MouseEvent event)
177    {    {
178    }    }
179    
180      /**
181       * If the button 1 is pressed, the caret position is updated to the
182       * position of the mouse click and the text component requests the input
183       * focus if it is enabled. If the SHIFT key is held down, the caret will
184       * be moved, which might select the text between the old and new location.
185       *
186       * @param event the <code>MouseEvent</code> describing the press operation
187       */
188    public void mousePressed(MouseEvent event)    public void mousePressed(MouseEvent event)
189    {    {
190        // FIXME: Implement this properly.
191    }    }
192    
193      /**
194       * Indicates that a mouse button has been released on the text component.
195       * Nothing is done here.
196       *
197       * @param event the <code>MouseEvent</code> describing the mouse operation
198       */
199    public void mouseReleased(MouseEvent event)    public void mouseReleased(MouseEvent event)
200    {    {
201        // Nothing to do here.
202    }    }
203    
204      /**
205       * Sets the caret to <code>visible</code> if the text component is editable.
206       *
207       * @param event the <code>FocusEvent</code>
208       */
209    public void focusGained(FocusEvent event)    public void focusGained(FocusEvent event)
210    {    {
211    }    }
212    
213      /**
214       * Sets the caret to <code>invisible</code>.
215       *
216       * @param event the <code>FocusEvent</code>
217       */
218    public void focusLost(FocusEvent event)    public void focusLost(FocusEvent event)
219    {    {
220    }    }
221    
222      /**
223       * Moves the caret to the position specified in the <code>MouseEvent</code>.
224       * This will cause a selection if the dot and mark are different.
225       *
226       * @param event the <code>MouseEvent</code> from which to fetch the position
227       */
228    protected void moveCaret(MouseEvent event)    protected void moveCaret(MouseEvent event)
229    {    {
230        // FIXME: Implement this properly.
231    }    }
232    
233      /**
234       * Repositions the caret to the position specified in the
235       * <code>MouseEvent</code>.
236       *
237       * @param event the <code>MouseEvent</code> from which to fetch the position
238       */
239    protected void positionCaret(MouseEvent event)    protected void positionCaret(MouseEvent event)
240    {    {
241        // FIXME: Implement this properly.
242    }    }
243    
244      /**
245       * Deinstalls this <code>Caret</code> from the specified
246       * <code>JTextComponent</code>. This removes any listeners that have been
247       * registered by this <code>Caret</code>.
248       *
249       * @param c the text component from which to install this caret
250       */
251    public void deinstall(JTextComponent c)    public void deinstall(JTextComponent c)
252    {    {
253      textComponent.removeFocusListener(this);      textComponent.removeFocusListener(this);
# Line 122  public class DefaultCaret extends Rectan Line 256  public class DefaultCaret extends Rectan
256      textComponent = null;      textComponent = null;
257    }    }
258    
259      /**
260       * Installs this <code>Caret</code> on the specified
261       * <code>JTextComponent</code>. This registers a couple of listeners
262       * on the text component.
263       *
264       * @param c the text component on which to install this caret
265       */
266    public void install(JTextComponent c)    public void install(JTextComponent c)
267    {    {
268      textComponent = c;      textComponent = c;
# Line 131  public class DefaultCaret extends Rectan Line 272  public class DefaultCaret extends Rectan
272      repaint();      repaint();
273    }    }
274    
275      /**
276       * Sets the current visual position of this <code>Caret</code>.
277       *
278       * @param p the Point to use for the saved location. May be <code>null</code>
279       *        to indicate that there is no visual location
280       */
281    public void setMagicCaretPosition(Point p)    public void setMagicCaretPosition(Point p)
282    {    {
283      magicCaretPosition = p;      magicCaretPosition = p;
284    }    }
285    
286      /**
287       * Returns the current visual position of this <code>Caret</code>.
288       *
289       * @return the current visual position of this <code>Caret</code>
290       *
291       * @see {@link #setMagicCaretPosition}
292       */
293    public Point getMagicCaretPosition()    public Point getMagicCaretPosition()
294    {    {
295      return magicCaretPosition;      return magicCaretPosition;
296    }    }
297    
298      /**
299       * Returns the current position of the <code>mark</code>. The
300       * <code>mark</code> marks the location in the <code>Document</code> that
301       * is the end of a selection. If there is no selection, the <code>mark</code>
302       * is the same as the <code>dot</code>.
303       *
304       * @return the current position of the mark
305       */
306    public int getMark()    public int getMark()
307    {    {
308      return mark;      return mark;
# Line 181  public class DefaultCaret extends Rectan Line 343  public class DefaultCaret extends Rectan
343        }        }
344    }    }
345    
346      /**
347       * Sets the visiblity state of the selection.
348       *
349       * @param v <code>true</code> if the selection should be visible,
350       *        <code>false</code> otherwise
351       */
352    public void setSelectionVisible(boolean v)    public void setSelectionVisible(boolean v)
353    {    {
354      if (selectionVisible == v)      if (selectionVisible == v)
# Line 191  public class DefaultCaret extends Rectan Line 359  public class DefaultCaret extends Rectan
359      repaint();      repaint();
360    }    }
361    
362      /**
363       * Returns <code>true</code> if the selection is currently visible,
364       * <code>false</code> otherwise.
365       *
366       * @return <code>true</code> if the selection is currently visible,
367       *         <code>false</code> otherwise
368       */
369    public boolean isSelectionVisible()    public boolean isSelectionVisible()
370    {    {
371      return selectionVisible;      return selectionVisible;
372    }    }
373    
374      /**
375       * Causes the <code>Caret</code> to repaint itself.
376       */
377    protected final void repaint()    protected final void repaint()
378    {    {
379        // FIXME: Is this good? This possibly causes alot of the component
380        // hierarchy to be repainted on every caret blink.
381      if (textComponent != null)      if (textComponent != null)
382        textComponent.repaint();        textComponent.repaint();
383    }    }
384    
385      /**
386       * Paints this <code>Caret</code> using the specified <code>Graphics</code>
387       * context.
388       *
389       * @param g the graphics context to use
390       */
391    public void paint(Graphics g)    public void paint(Graphics g)
392    {    {
393      if (textComponent == null)      if (textComponent == null)
# Line 234  public class DefaultCaret extends Rectan Line 420  public class DefaultCaret extends Rectan
420        }        }
421    }    }
422    
423      /**
424       * Returns all registered event listeners of the specified type.
425       *
426       * @param listenerType the type of listener to return
427       *
428       * @return all registered event listeners of the specified type
429       */
430    public EventListener[] getListeners(Class listenerType)    public EventListener[] getListeners(Class listenerType)
431    {    {
432      return listenerList.getListeners(listenerType);      return listenerList.getListeners(listenerType);
433    }    }
434    
435      /**
436       * Registers a {@link ChangeListener} that is notified whenever that state
437       * of this <code>Caret</code> changes.
438       *
439       * @param l the listener to register to this caret
440       */
441    public void addChangeListener(ChangeListener listener)    public void addChangeListener(ChangeListener listener)
442    {    {
443      listenerList.add(ChangeListener.class, listener);      listenerList.add(ChangeListener.class, listener);
444    }    }
445    
446      /**
447       * Removes a {@link ChangeListener} from the list of registered listeners.
448       *
449       * @param l the listener to remove
450       */
451    public void removeChangeListener(ChangeListener listener)    public void removeChangeListener(ChangeListener listener)
452    {    {
453      listenerList.remove(ChangeListener.class, listener);      listenerList.remove(ChangeListener.class, listener);
454    }    }
455    
456      /**
457       * Returns all registered {@link ChangeListener}s of this <code>Caret</code>.
458       *
459       * @return all registered {@link ChangeListener}s of this <code>Caret</code>
460       */
461    public ChangeListener[] getChangeListeners()    public ChangeListener[] getChangeListeners()
462    {    {
463      return (ChangeListener[]) getListeners(ChangeListener.class);      return (ChangeListener[]) getListeners(ChangeListener.class);
464    }    }
465    
466      /**
467       * Notifies all registered {@link ChangeListener}s that the state
468       * of this <code>Caret</code> has changed.
469       */
470    protected void fireStateChanged()    protected void fireStateChanged()
471    {    {
472      ChangeListener[] listeners = getChangeListeners();      ChangeListener[] listeners = getChangeListeners();
# Line 262  public class DefaultCaret extends Rectan Line 475  public class DefaultCaret extends Rectan
475        listeners[index].stateChanged(changeEvent);        listeners[index].stateChanged(changeEvent);
476    }    }
477    
478      /**
479       * Returns the <code>JTextComponent</code> on which this <code>Caret</code>
480       * is installed.
481       *
482       * @return the <code>JTextComponent</code> on which this <code>Caret</code>
483       *         is installed
484       */
485    protected final JTextComponent getComponent()    protected final JTextComponent getComponent()
486    {    {
487      return textComponent;      return textComponent;
488    }    }
489      
490      /**
491       * Returns the blink rate of this <code>Caret</code> in milliseconds.
492       * A value of <code>0</code> means that the caret does not blink.
493       *
494       * @return the blink rate of this <code>Caret</code> or <code>0</code> if
495       *         this caret does not blink
496       */
497    public int getBlinkRate()    public int getBlinkRate()
498    {    {
499      return blinkRate;      return blinkRate;
500    }    }
501    
502      /**
503       * Sets the blink rate of this <code>Caret</code> in milliseconds.
504       * A value of <code>0</code> means that the caret does not blink.
505       *
506       * @param rate the new blink rate to set
507       */
508    public void setBlinkRate(int rate)    public void setBlinkRate(int rate)
509    {    {
510      blinkRate = rate;      blinkRate = rate;
511    }    }
512    
513      /**
514       * Returns the current position of this <code>Caret</code> within the
515       * <code>Document</code>.
516       *
517       * @return the current position of this <code>Caret</code> within the
518       *         <code>Document</code>
519       */
520    public int getDot()    public int getDot()
521    {    {
522      return dot;      return dot;
523    }    }
524    
525      /**
526       * Moves the <code>dot</code> location without touching the
527       * <code>mark</code>. This is used when making a selection.
528       *
529       * @param dot the location where to move the dot
530       *
531       * @see {@link #setDot(int)}
532       */
533    public void moveDot(int dot)    public void moveDot(int dot)
534    {    {
535      this.dot = dot;      this.dot = dot;
# Line 289  public class DefaultCaret extends Rectan Line 537  public class DefaultCaret extends Rectan
537      repaint();      repaint();
538    }    }
539    
540      /**
541       * Sets the current position of this <code>Caret</code> within the
542       * <code>Document</code>. This also sets the <code>mark</code> to the
543       * new location.
544       *
545       * @param dot the new position to be set
546       *
547       * @see {@link #moveDot(int)}
548       */
549    public void setDot(int dot)    public void setDot(int dot)
550    {    {
551      this.dot = dot;      this.dot = dot;
# Line 297  public class DefaultCaret extends Rectan Line 554  public class DefaultCaret extends Rectan
554      repaint();      repaint();
555    }    }
556    
557      /**
558       * Returns <code>true</code> if this <code>Caret</code> is currently visible,
559       * and <code>false</code> if it is not.
560       *
561       * @return <code>true</code> if this <code>Caret</code> is currently visible,
562       *         and <code>false</code> if it is not
563       */
564    public boolean isVisible()    public boolean isVisible()
565    {    {
566      return visible;      return visible;
567    }    }
568    
569      /**
570       * Sets the visibility state of the caret. <code>true</code> shows the
571       * <code>Caret</code>, <code>false</code> hides it.
572       *
573       * @param v the visibility to set
574       */  
575    public void setVisible(boolean v)    public void setVisible(boolean v)
576    {    {
577      visible = v;      visible = v;
578      repaint();      repaint();
579    }    }
580    
581      /**
582       * Returns the {@link Highlighter.HighlightPainter} that should be used
583       * to paint the selection.
584       *
585       * @return the {@link Highlighter.HighlightPainter} that should be used
586       *         to paint the selection
587       */
588    protected Highlighter.HighlightPainter getSelectionPainter()    protected Highlighter.HighlightPainter getSelectionPainter()
589    {    {
590      return DefaultHighlighter.DefaultPainter;      return DefaultHighlighter.DefaultPainter;

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26