1 33 34 package edu.rice.cs.util.swing; 35 36 import java.util.Stack ; 37 import java.util.Vector ; 38 39 import javax.swing.text.JTextComponent ; 40 import javax.swing.text.Highlighter ; 41 import javax.swing.text.BadLocationException ; 42 import javax.swing.text.Position ; 43 44 import edu.rice.cs.util.UnexpectedException; 45 46 47 public class HighlightManager { 48 49 51 56 private Vector <Stack <HighlightInfo>> _highlights; 57 58 61 private JTextComponent _component; 62 63 66 public HighlightManager(JTextComponent jtc) { 67 _component = jtc; 68 _highlights = new Vector <Stack <HighlightInfo>>(); 69 } 70 71 72 73 public String toString() { return "HighLightManager(" + _highlights + ")"; } 74 75 76 77 public int size() { return _highlights.size(); } 78 79 88 public synchronized HighlightInfo addHighlight(int startOffset, int endOffset, Highlighter.HighlightPainter p) { 89 90 HighlightInfo newLite = new HighlightInfo(startOffset,endOffset,p); 91 92 Stack <HighlightInfo> lineStack = _getStackAt(newLite); 94 95 if (lineStack != null) { 96 int searchResult = lineStack.search(newLite); 97 if (searchResult == 1) return lineStack.peek(); 98 if (searchResult > 1) { 99 lineStack.remove(newLite); 100 } 101 } 102 else { 103 lineStack = new Stack <HighlightInfo>(); 105 _highlights.add(lineStack); 106 } 107 108 try { 109 Object highlightTag = _component.getHighlighter().addHighlight(startOffset,endOffset,p); 110 newLite.setHighlightTag(highlightTag); 111 lineStack.push(newLite); 112 return newLite; 113 } 114 catch (BadLocationException ble) { 115 if (lineStack.isEmpty()) { 117 _highlights.remove(lineStack); 118 } 119 throw new UnexpectedException(ble); 120 } 121 } 122 123 129 private Stack <HighlightInfo> _getStackAt (HighlightInfo h) { 130 131 for (Stack <HighlightInfo> stack : _highlights) { 132 if (stack.get(0).matchesRegion(h)) { 133 return stack; 134 } 135 } 136 return null; 138 } 139 140 147 public synchronized void removeHighlight(int startOffset, int endOffset, Highlighter.HighlightPainter p) { 148 HighlightInfo newLite = new HighlightInfo(startOffset,endOffset,p); 149 removeHighlight(newLite); 150 } 151 152 156 public void removeHighlight (HighlightInfo newLite) { 157 158 159 162 Stack <HighlightInfo> lineStack = _getStackAt(newLite); 163 164 if (lineStack== null) { 165 return; 167 } 168 169 int searchResult = lineStack.search(newLite); 170 172 if (searchResult == 1) { 173 HighlightInfo liteToRemove = lineStack.pop(); 174 _component.getHighlighter().removeHighlight(liteToRemove.getHighlightTag()); 175 } 177 else if (searchResult > 1) { 178 lineStack.remove(newLite); 180 _component.getHighlighter().removeHighlight(newLite.getHighlightTag()); 181 } 182 183 if (lineStack.isEmpty()) { 184 _highlights.remove(lineStack); 187 } 188 189 } 190 191 195 public class HighlightInfo { 196 private Object _highlightTag; 197 private Position _startPos; 198 private Position _endPos; 199 private Highlighter.HighlightPainter _painter; 200 201 206 public HighlightInfo(int from, int to, Highlighter.HighlightPainter p) { 207 208 _highlightTag = null; 209 try { 210 _startPos = _component.getDocument().createPosition(from); 211 _endPos = _component.getDocument().createPosition(to); 212 } 213 catch (BadLocationException ble) { 214 throw new UnexpectedException(ble); 215 } 216 217 _painter = p; 218 } 219 220 223 public void setHighlightTag ( Object highlightTag) { _highlightTag = highlightTag; } 224 225 230 public boolean equals( Object o) { 231 232 if (o == null) return false; 233 234 if (o instanceof HighlightInfo) { 235 236 HighlightInfo obj = (HighlightInfo)o; 237 242 boolean result = getStartOffset() == obj.getStartOffset() && 243 getEndOffset() == obj.getEndOffset() && 244 getPainter() == obj.getPainter(); 245 246 return result; 248 } 249 else return false; 250 } 251 252 253 public int hashCode() { return getPainter().hashCode() ^ getStartOffset() ^ getEndOffset(); } 254 255 public void remove() { removeHighlight(this); } 256 257 260 public Object getHighlightTag() { return _highlightTag; } 261 262 265 public Highlighter.HighlightPainter getPainter() { return _painter; } 266 267 270 public int getStartOffset() { return _startPos.getOffset(); } 271 272 275 public int getEndOffset() { return _endPos.getOffset(); } 276 277 281 public boolean matchesRegion(HighlightInfo h) { 282 return (getStartOffset() == h.getStartOffset() && getEndOffset() == h.getEndOffset()); 283 } 284 285 286 public void refresh (Highlighter.HighlightPainter p ) { 287 288 this.remove(); 289 HighlightInfo newHighlight = addHighlight(getStartOffset(), getEndOffset(), p); 290 291 _painter = p; 292 _highlightTag = newHighlight.getHighlightTag(); 294 } 295 } 296 } 297 | Popular Tags |