1 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 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 private Object matchingBracketHighlight; 39 40 43 public MoeCaret(MoeEditor editor) 44 { 45 super(); 46 this.editor = editor; 47 setBlinkRate(0); 48 } 49 50 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 } 69 } 70 else { 71 editor.toggleBreakpoint(pos); 72 } 73 } 74 75 83 protected void moveCaret(MouseEvent e) 84 { 85 if (e.getX() > BlueJSyntaxView.TAG_WIDTH) { 86 super.moveCaret(e); 87 } 88 } 89 90 93 public void setDot(int pos) 94 { 95 persistentHighlight = false; 96 super.setDot(pos); 97 } 98 99 102 public void moveDot(int pos) 103 { 104 persistentHighlight = false; 105 super.moveDot(pos); 106 } 107 108 111 protected void fireStateChanged() 112 { 113 editor.caretMoved(); 114 super.fireStateChanged(); 115 } 116 117 120 public void focusLost(FocusEvent e) 121 { 122 super.focusLost(e); 123 if (persistentHighlight) 124 setSelectionVisible(true); 125 } 126 127 132 public void setPersistentHighlight() 133 { 134 setSelectionVisible(true); 135 persistentHighlight = true; 136 } 137 138 142 public void paintMatchingBracket() 143 { 144 int matchBracket = editor.getBracketMatch(); 145 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 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 |