1 19 20 package org.netbeans.editor; 21 22 import javax.swing.text.BadLocationException ; 23 import javax.swing.text.Document ; 24 import javax.swing.text.Position ; 25 26 32 33 public class MarkChain { 34 35 36 protected MarkFactory.ChainDrawMark chain; 37 38 39 protected MarkFactory.ChainDrawMark curMark; 40 41 42 protected BaseDocument doc; 43 44 47 protected String layerName; 48 49 52 private MarkFactory.ChainDrawMark recentlyAddedMark; 53 54 55 public MarkChain(BaseDocument doc, String layerName) { 56 this.doc = doc; 57 this.layerName = layerName; 58 } 59 60 public final MarkFactory.ChainDrawMark getChain() { 61 return chain; 62 } 63 64 public final MarkFactory.ChainDrawMark getCurMark() { 65 return curMark; 66 } 67 68 73 public int compareMark(int pos) { 74 try { 75 if (curMark == null) { 76 curMark = chain; 77 if (curMark == null) { 78 return -1; } 80 } 81 82 int rel; 83 boolean after = false; 84 boolean before = false; 85 while ((rel = curMark.compare(pos)) != 0) { if (rel > 0) { if (before) { 88 return rel; 89 } 90 if (curMark.prev != null) { 91 after = true; 92 curMark = curMark.prev; 93 } else { return rel; 95 } 96 } else { if (after) { 98 return rel; 99 } 100 if (curMark.next != null) { 101 before = true; 102 curMark = curMark.next; 103 } else { return rel; 105 } 106 } 107 } 108 return 0; } catch (InvalidMarkException e) { 110 Utilities.annotateLoggable(e); 111 return -1; } 113 } 114 115 protected MarkFactory.ChainDrawMark createAndInsertNewMark(int pos) 116 throws BadLocationException { 117 MarkFactory.ChainDrawMark mark = createMark(); 118 try { 119 mark.insert(doc, pos); 120 } catch (InvalidMarkException e) { 121 Utilities.annotateLoggable(e); 122 } 123 return mark; 124 } 125 126 protected MarkFactory.ChainDrawMark createMark() { 127 MarkFactory.ChainDrawMark mark = new MarkFactory.ChainDrawMark(layerName, null, Position.Bias.Backward); 128 mark.activateLayer = true; 129 return mark; 130 } 131 132 public boolean addMark(int pos) throws BadLocationException { 133 return addMark(pos, false); 134 } 135 136 143 public boolean addMark(int pos, boolean forceAdd) throws BadLocationException { 144 int rel = compareMark(pos); 145 if (rel == 0) { 146 if (forceAdd) { MarkFactory.ChainDrawMark mark = createAndInsertNewMark(pos); 148 recentlyAddedMark = mark; 149 if (curMark == chain) { chain = curMark.insertChain(mark); 151 } else { curMark.insertChain(mark); 153 } 154 155 } else { recentlyAddedMark = curMark; 157 return false; } 159 160 } else if (rel > 0) { MarkFactory.ChainDrawMark mark = createAndInsertNewMark(pos); 162 recentlyAddedMark = mark; 163 if (curMark != null) { 164 if (curMark == chain) { chain = curMark.insertChain(mark); 166 } else { curMark.insertChain(mark); 168 } 169 } else { chain = mark; 171 } 172 } else { MarkFactory.ChainDrawMark mark = createAndInsertNewMark(pos); 174 recentlyAddedMark = mark; 175 if (curMark != null) { 176 if (curMark.next != null) { 177 curMark.next.insertChain(mark); 178 } else { curMark.setNextChain(mark); 180 } 181 } else { chain = mark; 183 } 184 } 185 return true; 186 } 187 188 191 public MarkFactory.ChainDrawMark getAddedMark() { 192 return recentlyAddedMark; 193 } 194 195 196 public boolean removeMark(int pos) { 197 int rel = compareMark(pos); 198 if (rel == 0) { 199 boolean first = (curMark == chain); 200 curMark = curMark.removeChain(); 201 if (first) { 202 chain = curMark; 203 } 204 return true; 205 } else { return false; 207 } 208 } 209 210 public boolean removeMark(MarkFactory.ChainDrawMark mark) { 211 if (mark == null) { 212 throw new NullPointerException (); 213 } 214 215 if (curMark != mark) { 216 curMark = chain; 218 while (curMark != null) { 219 if (curMark == mark) { 220 break; 221 } 222 curMark = curMark.next; 223 } 224 } 225 226 if (curMark != null) { 227 boolean first = (curMark == chain); 228 curMark = curMark.removeChain(); 229 if (first) { 230 chain = curMark; 231 } 232 return true; 233 } else { 234 return false; 235 } 236 } 237 238 239 public boolean isMark(int pos) { 240 return (compareMark(pos) == 0); 241 } 242 243 248 public boolean toggleMark(int pos) throws BadLocationException { 249 int rel = compareMark(pos); 250 if (rel == 0) { removeMark(pos); 252 return false; 253 } else { addMark(pos); 255 return true; 256 } 257 } 258 259 260 public String toString() { 261 return "MarkChain: curMark=" + curMark + ", mark chain: " + (chain != null ? ("\n" + chain.toStringChain()) : "Empty"); } 264 265 } 266 | Popular Tags |