Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / text / DefaultCaret.java
blob15a76699601326a53c36bdd67f52da6ae4c3931f
1 /* DefaultCaret.java --
2 Copyright (C) 2002, 2004 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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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.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;
50 import javax.swing.event.ChangeEvent;
51 import javax.swing.event.ChangeListener;
52 import javax.swing.event.EventListenerList;
55 public class DefaultCaret extends Rectangle
56 implements Caret, FocusListener, MouseListener, MouseMotionListener
58 private static final long serialVersionUID = 228155774675466193L;
60 protected ChangeEvent changeEvent = new ChangeEvent(this);
61 protected EventListenerList listenerList = new EventListenerList();
63 private JTextComponent textComponent;
65 private boolean selectionVisible = true;
66 private int blinkRate = 0;
67 private int dot = 0;
68 private int mark = 0;
69 private Point magicCaretPosition = null;
70 private boolean visible = true;
71 private Object highlightEntry;
73 public void mouseDragged(MouseEvent event)
77 public void mouseMoved(MouseEvent event)
81 public void mouseClicked(MouseEvent event)
85 public void mouseEntered(MouseEvent event)
89 public void mouseExited(MouseEvent event)
93 public void mousePressed(MouseEvent event)
97 public void mouseReleased(MouseEvent event)
101 public void focusGained(FocusEvent event)
105 public void focusLost(FocusEvent event)
109 protected void moveCaret(MouseEvent event)
113 protected void positionCaret(MouseEvent event)
117 public void deinstall(JTextComponent c)
119 textComponent.removeFocusListener(this);
120 textComponent.removeMouseListener(this);
121 textComponent.removeMouseMotionListener(this);
122 textComponent = null;
125 public void install(JTextComponent c)
127 textComponent = c;
128 textComponent.addFocusListener(this);
129 textComponent.addMouseListener(this);
130 textComponent.addMouseMotionListener(this);
131 repaint();
134 public void setMagicCaretPosition(Point p)
136 magicCaretPosition = p;
139 public Point getMagicCaretPosition()
141 return magicCaretPosition;
144 public int getMark()
146 return mark;
149 private void handleHighlight()
151 Highlighter highlighter = textComponent.getHighlighter();
153 if (highlighter == null)
154 return;
156 int p0 = Math.min(dot, mark);
157 int p1 = Math.max(dot, mark);
159 if (selectionVisible && p0 != p1)
163 if (highlightEntry == null)
164 highlightEntry = highlighter.addHighlight(p0, p1, getSelectionPainter());
165 else
166 highlighter.changeHighlight(highlightEntry, p0, p1);
168 catch (BadLocationException e)
170 // This should never happen.
171 throw new InternalError();
174 else
176 if (highlightEntry != null)
178 highlighter.removeHighlight(highlightEntry);
179 highlightEntry = null;
184 public void setSelectionVisible(boolean v)
186 if (selectionVisible == v)
187 return;
189 selectionVisible = v;
190 handleHighlight();
191 repaint();
194 public boolean isSelectionVisible()
196 return selectionVisible;
199 protected final void repaint()
201 if (textComponent != null)
202 textComponent.repaint();
205 public void paint(Graphics g)
207 if (textComponent == null)
208 return;
210 int dot = getDot();
211 Rectangle rect = null;
215 rect = textComponent.modelToView(dot);
217 catch (BadLocationException e)
219 // This should never happen as dot should be always valid.
220 return;
223 if (rect == null)
224 return;
226 // First we need to delete the old caret.
227 // FIXME: Implement deleting of old caret.
229 // Now draw the caret on the new position if visible.
230 if (visible)
232 g.setColor(textComponent.getCaretColor());
233 g.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height);
237 public EventListener[] getListeners(Class listenerType)
239 return listenerList.getListeners(listenerType);
242 public void addChangeListener(ChangeListener listener)
244 listenerList.add(ChangeListener.class, listener);
247 public void removeChangeListener(ChangeListener listener)
249 listenerList.remove(ChangeListener.class, listener);
252 public ChangeListener[] getChangeListeners()
254 return (ChangeListener[]) getListeners(ChangeListener.class);
257 protected void fireStateChanged()
259 ChangeListener[] listeners = getChangeListeners();
261 for (int index = 0; index < listeners.length; ++index)
262 listeners[index].stateChanged(changeEvent);
265 protected final JTextComponent getComponent()
267 return textComponent;
270 public int getBlinkRate()
272 return blinkRate;
275 public void setBlinkRate(int rate)
277 blinkRate = rate;
280 public int getDot()
282 return dot;
285 public void moveDot(int dot)
287 this.dot = dot;
288 handleHighlight();
289 repaint();
292 public void setDot(int dot)
294 this.dot = dot;
295 this.mark = dot;
296 handleHighlight();
297 repaint();
300 public boolean isVisible()
302 return visible;
305 public void setVisible(boolean v)
307 visible = v;
308 repaint();
311 protected Highlighter.HighlightPainter getSelectionPainter()
313 return DefaultHighlighter.DefaultPainter;