001: // Copyright (c) 2000, 2005 BlueJ Group, Deakin University
002: //
003: // This software is made available under the terms of the "MIT License"
004: // A copy of this license is included with this source distribution
005: // in "license.txt" and is also available at:
006: // http://www.opensource.org/licenses/mit-license.html
007: // Any queries should be directed to Michael Kolling mik@bluej.org
008:
009: package bluej.editor.moe;
010:
011: import bluej.utility.Debug;
012:
013: import java.awt.*;
014: import java.awt.event.*;
015:
016: import javax.swing.text.*;
017:
018: /**
019: * A customised caret for Moe. It gets most of its bahaviour from
020: * Swing's "DefaultCaret" and adds some functionality.
021: *
022: * @author Michael Kolling
023: */
024:
025: public class MoeCaret extends DefaultCaret {
026: private static final Color bracketHighlightColour = new Color(196,
027: 196, 196);
028:
029: private static final LayeredHighlighter.LayerPainter bracketPainter = new BracketMatchPainter(
030: bracketHighlightColour);
031:
032: private MoeEditor editor;
033:
034: private boolean persistentHighlight = false;
035:
036: // matching bracket highlight holder
037: private Object matchingBracketHighlight;
038:
039: /**
040: * Constructs a Moe Caret
041: */
042: public MoeCaret(MoeEditor editor) {
043: super ();
044: this .editor = editor;
045: setBlinkRate(0);
046: }
047:
048: /**
049: * Redefinition of caret positioning (after mouse click). Here, we
050: * first check whether the click was in the tag line. If it was, we
051: * toggle the breakpoint, if not we just position the caret as usual.
052: */
053: protected void positionCaret(MouseEvent e) {
054: editor.caretMoved();
055: Point pt = new Point(e.getX(), e.getY());
056: Position.Bias[] biasRet = new Position.Bias[1];
057: int pos = getComponent().getUI().viewToModel(getComponent(),
058: pt, biasRet);
059:
060: if (e.getX() > BlueJSyntaxView.TAG_WIDTH) {
061: if (biasRet[0] == null)
062: biasRet[0] = Position.Bias.Forward;
063: if (pos >= 0) {
064: setDot(pos);
065: // setMagicCaretPosition(null);
066: }
067: } else {
068: editor.toggleBreakpoint(pos);
069: }
070: }
071:
072: /**
073: * Tries to move the position of the caret from
074: * the coordinates of a mouse event, using viewToModel().
075: * This will cause a selection if the dot and mark
076: * are different.
077: *
078: * @param e the mouse event
079: */
080: protected void moveCaret(MouseEvent e) {
081: if (e.getX() > BlueJSyntaxView.TAG_WIDTH) {
082: super .moveCaret(e);
083: }
084: }
085:
086: /**
087: * Set the dot and mark position
088: */
089: public void setDot(int pos) {
090: persistentHighlight = false;
091: super .setDot(pos);
092: }
093:
094: /**
095: * Set the dot position (leave the mark where it is).
096: */
097: public void moveDot(int pos) {
098: persistentHighlight = false;
099: super .moveDot(pos);
100: }
101:
102: /**
103: * Fire a state canged event.
104: */
105: protected void fireStateChanged() {
106: editor.caretMoved();
107: super .fireStateChanged();
108: }
109:
110: /**
111: * Target text component lost focus.
112: */
113: public void focusLost(FocusEvent e) {
114: super .focusLost(e);
115: if (persistentHighlight)
116: setSelectionVisible(true);
117: }
118:
119: /**
120: * Set the highlight (of the selection) as persistent - that is, it won't
121: * become invisible if the component loses focus. This lasts until the
122: * caret position is changed.
123: */
124: public void setPersistentHighlight() {
125: setSelectionVisible(true);
126: persistentHighlight = true;
127: }
128:
129: /**
130: * paint matching bracket if caret is directly after a bracket
131: *
132: */
133: public void paintMatchingBracket() {
134: int matchBracket = editor.getBracketMatch();
135: // remove existing bracket if needed
136: removeBracket();
137: if (matchBracket != -1) {
138: try {
139: matchingBracketHighlight = getComponent()
140: .getHighlighter().addHighlight(matchBracket,
141: matchBracket + 1, bracketPainter);
142: } catch (BadLocationException ble) {
143: Debug.reportError("bad location exception thrown");
144: ble.printStackTrace();
145: }
146: }
147: }
148:
149: /**
150: * remove the existing matching bracket if it exists
151: */
152: public void removeBracket() {
153: if (matchingBracketHighlight != null) {
154: getComponent().getHighlighter().removeHighlight(
155: matchingBracketHighlight);
156: matchingBracketHighlight = null;
157: }
158: }
159:
160: }
|