KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bluej > editor > moe > MoeCaret


1 // Copyright (c) 2000, 2005 BlueJ Group, Deakin University
2
//
3
// This software is made available under the terms of the "MIT License"
4
// A copy of this license is included with this source distribution
5
// in "license.txt" and is also available at:
6
// http://www.opensource.org/licenses/mit-license.html
7
// Any queries should be directed to Michael Kolling mik@bluej.org
8

9 package bluej.editor.moe;
10
11 import bluej.utility.Debug;
12
13 import java.awt.*;
14 import java.awt.event.*;
15
16 import javax.swing.text.*;
17
18
19 /**
20  * A customised caret for Moe. It gets most of its bahaviour from
21  * Swing's "DefaultCaret" and adds some functionality.
22  *
23  * @author Michael Kolling
24  */

25
26 public class MoeCaret extends DefaultCaret
27 {
28     private static final Color bracketHighlightColour = new Color(196, 196, 196);
29     
30     private static final LayeredHighlighter.LayerPainter bracketPainter =
31         new BracketMatchPainter(bracketHighlightColour);
32         
33     private MoeEditor editor;
34
35     private boolean persistentHighlight = false;
36     
37     // matching bracket highlight holder
38
private Object JavaDoc matchingBracketHighlight;
39
40     /**
41      * Constructs a Moe Caret
42      */

43     public MoeCaret(MoeEditor editor)
44     {
45         super();
46         this.editor = editor;
47         setBlinkRate(0);
48     }
49
50     /**
51      * Redefinition of caret positioning (after mouse click). Here, we
52      * first check whether the click was in the tag line. If it was, we
53      * toggle the breakpoint, if not we just position the caret as usual.
54      */

55     protected void positionCaret(MouseEvent e)
56     {
57         editor.caretMoved();
58         Point pt = new Point(e.getX(), e.getY());
59         Position.Bias[] biasRet = new Position.Bias[1];
60         int pos = getComponent().getUI().viewToModel(getComponent(), pt, biasRet);
61
62         if (e.getX() > BlueJSyntaxView.TAG_WIDTH) {
63             if(biasRet[0] == null)
64                 biasRet[0] = Position.Bias.Forward;
65             if (pos >= 0) {
66                 setDot(pos);
67 // setMagicCaretPosition(null);
68
}
69         }
70         else {
71             editor.toggleBreakpoint(pos);
72         }
73     }
74
75     /**
76      * Tries to move the position of the caret from
77      * the coordinates of a mouse event, using viewToModel().
78      * This will cause a selection if the dot and mark
79      * are different.
80      *
81      * @param e the mouse event
82      */

83     protected void moveCaret(MouseEvent e)
84     {
85         if (e.getX() > BlueJSyntaxView.TAG_WIDTH) {
86             super.moveCaret(e);
87         }
88     }
89     
90     /**
91      * Set the dot and mark position
92      */

93     public void setDot(int pos)
94     {
95         persistentHighlight = false;
96         super.setDot(pos);
97     }
98     
99     /**
100      * Set the dot position (leave the mark where it is).
101      */

102     public void moveDot(int pos)
103     {
104         persistentHighlight = false;
105         super.moveDot(pos);
106     }
107
108     /**
109      * Fire a state canged event.
110      */

111     protected void fireStateChanged()
112     {
113         editor.caretMoved();
114         super.fireStateChanged();
115     }
116     
117     /**
118      * Target text component lost focus.
119      */

120     public void focusLost(FocusEvent e)
121     {
122         super.focusLost(e);
123         if (persistentHighlight)
124             setSelectionVisible(true);
125     }
126     
127     /**
128      * Set the highlight (of the selection) as persistent - that is, it won't
129      * become invisible if the component loses focus. This lasts until the
130      * caret position is changed.
131      */

132     public void setPersistentHighlight()
133     {
134         setSelectionVisible(true);
135         persistentHighlight = true;
136     }
137      
138     /**
139      * paint matching bracket if caret is directly after a bracket
140      *
141      */

142     public void paintMatchingBracket()
143     {
144         int matchBracket = editor.getBracketMatch();
145         // remove existing bracket if needed
146
removeBracket();
147         if(matchBracket != -1) {
148             try {
149                 matchingBracketHighlight = getComponent().getHighlighter().addHighlight(matchBracket, matchBracket + 1, bracketPainter);
150             }
151             catch(BadLocationException ble) {
152                 Debug.reportError("bad location exception thrown");
153                 ble.printStackTrace();
154             }
155         }
156     }
157     
158     /**
159      * remove the existing matching bracket if it exists
160      */

161     public void removeBracket()
162     {
163         if(matchingBracketHighlight != null) {
164             getComponent().getHighlighter().removeHighlight(matchingBracketHighlight);
165             matchingBracketHighlight = null;
166         }
167     }
168     
169 }
170
171
172
Popular Tags