/[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.7 by mark, Thu Jul 22 19:45:39 2004 UTC revision 1.8 by mark, Sat Sep 4 17:14:01 2004 UTC
# Line 61  public class DefaultCaret extends Rectan Line 61  public class DefaultCaret extends Rectan
61    protected ChangeEvent changeEvent = new ChangeEvent(this);    protected ChangeEvent changeEvent = new ChangeEvent(this);
62    protected EventListenerList listenerList = new EventListenerList();    protected EventListenerList listenerList = new EventListenerList();
63        
64    Color color = new Color(0, 0, 0);    private JTextComponent textComponent;
65    JTextComponent parent;    
66    Point magic = null;    private boolean selectionVisible = true;
67    int mark = 0;    private int blinkRate = 0;
68    boolean vis_sel = true;    private int dot = 0;
69    int blink = 500;    private int mark = 0;
70    int dot = 0;    private Point magicCaretPosition = null;
71    boolean vis = true;    private boolean visible = true;
72      private Object highlightEntry;
73    
74    public void mouseDragged(MouseEvent event)    public void mouseDragged(MouseEvent event)
75    {    {
# Line 117  public class DefaultCaret extends Rectan Line 117  public class DefaultCaret extends Rectan
117    
118    public void deinstall(JTextComponent c)    public void deinstall(JTextComponent c)
119    {    {
120      parent.removeFocusListener(this);      textComponent.removeFocusListener(this);
121      parent.removeMouseListener(this);      textComponent.removeMouseListener(this);
122      parent = null;      textComponent.removeMouseMotionListener(this);
123        textComponent = null;
124    }    }
125    
126    public void install(JTextComponent c)    public void install(JTextComponent c)
127    {    {
128      parent.addFocusListener(this);      textComponent = c;
129      parent.addMouseListener(this);      textComponent.addFocusListener(this);
130      parent = c;      textComponent.addMouseListener(this);
131        textComponent.addMouseMotionListener(this);
132      repaint();      repaint();
133    }    }
134    
135    public void setMagicCaretPosition(Point p)    public void setMagicCaretPosition(Point p)
136    {    {
137      magic = p;      magicCaretPosition = p;
138    }    }
139    
140    public Point getMagicCaretPosition()    public Point getMagicCaretPosition()
141    {    {
142      return magic;      return magicCaretPosition;
143    }    }
144    
145    public int getMark()    public int getMark()
# Line 145  public class DefaultCaret extends Rectan Line 147  public class DefaultCaret extends Rectan
147      return mark;      return mark;
148    }    }
149    
150      private void handleHighlight()
151      {
152        Highlighter highlighter = textComponent.getHighlighter();
153        
154        if (highlighter == null)
155          return;
156        
157        int p0 = Math.min(dot, mark);
158        int p1 = Math.max(dot, mark);
159        
160        if (selectionVisible && p0 != p1)
161          {
162            try
163              {
164                if (highlightEntry == null)
165                  highlightEntry = highlighter.addHighlight(p0, p1, getSelectionPainter());
166                else
167                  highlighter.changeHighlight(highlightEntry, p0, p1);
168              }
169            catch (BadLocationException e)
170              {
171                // This should never happen.
172                throw new InternalError();
173              }
174          }
175        else
176          {
177            if (highlightEntry != null)
178              {
179                highlighter.removeHighlight(highlightEntry);
180                highlightEntry = null;
181              }
182          }
183      }
184    
185    public void setSelectionVisible(boolean v)    public void setSelectionVisible(boolean v)
186    {    {
187      vis_sel = v;      if (selectionVisible == v)
188          return;
189        
190        selectionVisible = v;
191        handleHighlight();
192      repaint();      repaint();
193    }    }
194    
195    public boolean isSelectionVisible()    public boolean isSelectionVisible()
196    {    {
197      return vis_sel;      return selectionVisible;
198    }    }
199    
200    protected final void repaint()    protected final void repaint()
201    {    {
202      if (parent != null)      if (textComponent != null)
203        parent.repaint();        textComponent.repaint();
204    }    }
205    
206    public void paint(Graphics g)    public void paint(Graphics g)
207    {    {
208      g.setColor(color);      if (textComponent == null)
209      g.drawLine(x, y, x, y + height);        return;
210    
211        int dot = getDot();
212        Rectangle rect = null;
213    
214        try
215          {
216            rect = textComponent.modelToView(dot);
217          }
218        catch (BadLocationException e)
219          {
220            // This should never happen as dot should be always valid.
221            return;
222          }
223    
224        if (rect == null)
225          return;
226        
227        // First we need to delete the old caret.
228        // FIXME: Implement deleting of old caret.
229        
230        // Now draw the caret on the new position if visible.
231        if (visible)
232          {
233            g.setColor(textComponent.getCaretColor());
234            g.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height);
235          }
236    }    }
237    
238    public EventListener[] getListeners(Class listenerType)    public EventListener[] getListeners(Class listenerType)
# Line 198  public class DefaultCaret extends Rectan Line 265  public class DefaultCaret extends Rectan
265    
266    protected final JTextComponent getComponent()    protected final JTextComponent getComponent()
267    {    {
268      return parent;      return textComponent;
269    }    }
270        
271    public int getBlinkRate()    public int getBlinkRate()
272    {    {
273      return blink;      return blinkRate;
274    }    }
275    
276    public void setBlinkRate(int rate)    public void setBlinkRate(int rate)
277    {    {
278      blink = rate;      blinkRate = rate;
279    }    }
280    
281    public int getDot()    public int getDot()
# Line 218  public class DefaultCaret extends Rectan Line 285  public class DefaultCaret extends Rectan
285    
286    public void moveDot(int dot)    public void moveDot(int dot)
287    {    {
288      setDot(dot);      this.dot = dot;
289        handleHighlight();
290        repaint();
291    }    }
292    
293    public void setDot(int dot)    public void setDot(int dot)
294    {    {
295      this.dot = dot;      this.dot = dot;
296        this.mark = dot;
297        handleHighlight();
298      repaint();      repaint();
299    }    }
300    
301    public boolean isVisible()    public boolean isVisible()
302    {    {
303      return vis;      return visible;
304    }    }
305    
306    public void setVisible(boolean v)    public void setVisible(boolean v)
307    {    {
308      vis = v;      visible = v;
309      repaint();      repaint();
310    }    }
311    
312      protected Highlighter.HighlightPainter getSelectionPainter()
313      {
314        return DefaultHighlighter.DefaultPainter;
315      }
316  }  }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

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