source: tmcsimulator/trunk/src/tmcsim/client/CADCaret.java @ 2

Revision 2, 4.8 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files

Line 
1package tmcsim.client;
2
3import java.awt.Color;
4import java.awt.Graphics;
5import java.awt.Point;
6import java.awt.Rectangle;
7import java.awt.event.FocusEvent;
8import java.awt.event.MouseEvent;
9
10import javax.swing.text.BadLocationException;
11import javax.swing.text.DefaultCaret;
12import javax.swing.text.JTextComponent;
13
14/**
15 * CADCaret extends from DefaultCaret to provide a blinking Caret.  The Caret's
16 * blink rate is 500ms and appears as a solid underline that is the width of one
17 * character.  All superclass methods that position the Caret are overloaded
18 * to control the position of the Caret manually.
19 *
20 * @author Matthew
21 * @version
22 */
23@SuppressWarnings("serial")
24public class CADCaret extends DefaultCaret {
25
26    /** Caret's current position.  (>= 0) */
27    private int currentCaretPos = 0;
28
29    /**
30     * Constructor.
31     */
32    public CADCaret() {
33        setBlinkRate(500); // half a second
34        setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
35    }
36   
37    /**
38     * Overloaded method.  The Caret's position is set with the internal
39     * position count.
40     */
41    public void setMagicCaretPosition(Point arg0) {
42        super.setDot(currentCaretPos);
43    }
44   
45
46    /**
47     * The internal Caret position is incremented by the parameter offset
48     * value.  After position adjustment, the Caret is repositioned.
49     *
50     * @param offset Number of characters to increment the Caret's position by.
51     */
52    public void moveCaretForward(int offset) {
53        currentCaretPos += offset;
54        super.setDot(currentCaretPos);
55    }
56   
57    /**
58     * The internal Caret position is decremented by the parameter offset
59     * value.  If this resulting position is negative, the position is
60     * set to 0.  After position adjustment, the Caret is repositioned.
61     *
62     * @param offset Number of characters to decrement the Caret's position by.
63     */
64    public void moveCaretBackward(int offset) {
65        currentCaretPos -= offset;     
66       
67        if(currentCaretPos < 0)
68            currentCaretPos = 0;
69       
70        super.setDot(currentCaretPos);
71    }
72   
73    /**
74     * The internal Caret position is reset to 0 and the Caret is repositioned.
75     */
76    public void resetCaretPosition() {
77        currentCaretPos = 0;
78        super.setDot(currentCaretPos);     
79    }
80
81    @Override
82    public void setVisible(boolean visible) 
83    {
84        if(visible)
85        {
86            getComponent().setCaretColor(Color.black);
87        }
88        else
89        {
90            getComponent().setCaretColor(Color.yellow);
91        }
92    }
93   
94    public void mouseClicked(MouseEvent arg0)     { }
95    public void mouseDragged(MouseEvent arg0)     { }
96    public void mouseEntered(MouseEvent arg0)     { }
97    public void mouseExited(MouseEvent arg0)      { }
98    public void mouseMoved(MouseEvent arg0)       { }
99    public void mousePressed(MouseEvent arg0)     { }   
100    public void mouseReleased(MouseEvent arg0)    { }
101    protected void moveCaret(MouseEvent arg0)     { }
102    public void moveDot(int arg0)                 { }
103    public void setDot(int arg0)                  { }   
104    protected void positionCaret(MouseEvent arg0) { }   
105    public void focusLost(FocusEvent arg0)        { }
106   
107    protected synchronized void damage(Rectangle r) 
108    {
109        if (r == null)
110            return;
111        // give values to x,y,width,height (inherited from
112        // java.awt.Rectangle)
113        x = r.x;
114        y = r.y + (r.height * 4 / 5 - 3);
115        width  = 10;
116        height = 5;
117        repaint(); // calls getComponent().repaint(x, y, width, height)
118    }
119
120    public void paint(Graphics g) {
121        JTextComponent comp = getComponent();
122        if (comp == null)
123            return;
124
125        int dot = getDot();
126        Rectangle r = null;
127        try {
128            r = comp.modelToView(dot);
129        } catch (BadLocationException e) {
130            return;
131        }
132        if (r == null)
133            return;
134
135        // will be distance from r.y to top
136        int dist = r.height * 4 / 5 - 3; 
137
138        if ((x != r.x) || (y != r.y + dist)) {
139            // paint() has been called directly, without a previous call to
140            // damage(), so do some cleanup. (This happens, for example,
141            // when
142            // the
143            // text component is resized.)
144            repaint(); // erase previous location of caret
145            x = r.x; // set new values for x,y,width,height
146            y = r.y + dist;
147            width  = 10;
148            height = 5;
149        }
150
151        if (isVisible()) {
152            g.setColor(comp.getCaretColor());
153            // 5 vertical pixels
154            //g.drawLine(r.x, r.y + dist, r.x, r.y + dist + 4);
155           
156            // 5 horizontal pixels
157            g.drawLine(r.x, r.y + dist + 4, r.x + 8, r.y + dist + 4); 
158        }
159    }
160   
161}
Note: See TracBrowser for help on using the repository browser.