1 19 package org.netbeans.modules.editor.guards; 20 21 import javax.swing.text.BadLocationException ; 22 import javax.swing.text.Position ; 23 import javax.swing.text.StyledDocument ; 24 import org.openide.text.NbDocument; 25 26 32 public final class PositionBounds { 33 34 35 private Position begin; 36 37 38 private Position end; 39 40 private final GuardedSectionsImpl guards; 41 42 private static final class UnresolvedPosition implements Position { 43 44 private int offset; 45 46 public UnresolvedPosition(int offset) { 47 this.offset = offset; 48 } 49 50 public int getOffset() { 51 return this.offset; 52 } 53 } 54 55 private static final class BackwardPosition implements Position { 56 57 private Position delegate; 58 59 public BackwardPosition(Position delegate) { 60 this.delegate = delegate; 61 } 62 63 public int getOffset() { 64 return this.delegate.getOffset() + 1; 65 } 66 } 67 68 72 public PositionBounds(Position begin, Position end, GuardedSectionsImpl guards) { 73 this.begin = begin; 74 this.end = end; 75 this.guards = guards; 76 } 77 78 public static PositionBounds create(int begin, int end, GuardedSectionsImpl guards) throws BadLocationException { 79 StyledDocument doc = guards.getDocument(); 80 return new PositionBounds(doc.createPosition(begin), doc.createPosition(end), guards); 81 } 82 83 88 public static PositionBounds createBodyBounds(int begin, int end, GuardedSectionsImpl guards) throws BadLocationException { 89 StyledDocument doc = guards.getDocument(); 90 return new PositionBounds(new BackwardPosition(doc.createPosition(begin - 1)), doc.createPosition(end), guards); 91 } 92 93 97 public static PositionBounds createUnresolved(int begin, int end, GuardedSectionsImpl guards) throws BadLocationException { 98 StyledDocument doc = guards.getDocument(); 99 return new PositionBounds(new UnresolvedPosition(begin), new UnresolvedPosition(end), guards); 100 } 101 102 106 public static PositionBounds createBodyUnresolved(int begin, int end, GuardedSectionsImpl guards) throws BadLocationException { 107 return new PositionBounds(new BackwardPosition(new UnresolvedPosition(begin - 1)), new UnresolvedPosition(end), guards); 108 } 109 110 public void resolvePositions() throws BadLocationException { 111 StyledDocument doc = guards.getDocument(); 112 Position b, e; 113 if (end instanceof UnresolvedPosition) { 114 if (begin instanceof BackwardPosition) { 115 b = ((BackwardPosition) begin).delegate = doc.createPosition( 116 ((BackwardPosition) begin).delegate.getOffset()); 117 } else { 118 b = doc.createPosition(begin.getOffset()); 119 } 120 e = doc.createPosition(end.getOffset()); 121 this.begin = b; 122 this.end = e; 123 } 124 } 125 126 130 public Position getBegin() { 131 return begin; 132 } 133 134 138 public Position getEnd() { 139 return end; 140 } 141 142 148 public void setText(final String text) throws BadLocationException { 149 final StyledDocument doc = guards.getDocument(); 150 final BadLocationException [] hold = new BadLocationException [] { null }; 151 Runnable run = new Runnable () { 152 public void run() { 153 try { 154 int p1 = begin.getOffset(); 155 int p2 = end.getOffset(); 156 int len = text.length(); 157 158 if (len == 0) { 160 if (p2 > p1) { 161 doc.remove(p1, p2 - p1); 162 } 163 } else { 165 int docLen = doc.getLength(); 166 167 if ((p2 - p1) >= 1) { 168 doc.insertString(p1 + 1, text, null); 169 170 len = doc.getLength() - docLen; 172 doc.remove(p1 + 1 + len, p2 - p1 - 1); 173 doc.remove(p1, 1); 174 } else { 175 doc.insertString(p1, text, null); 181 182 len = doc.getLength() - docLen; 184 185 if (p2 > p1) { 186 doc.remove(p1 + len, p2 - p1); 187 } 188 189 if (begin.getOffset() != p1) { 190 begin = doc.createPosition(p1); 191 } 192 193 if ((end.getOffset() - p1) != len) { 194 end = doc.createPosition(p1 + len); 195 } 196 } 197 } 198 } catch (BadLocationException e) { 199 hold[0] = e; 200 } 201 } 202 }; 203 204 NbDocument.runAtomic(doc, run); 205 206 if (hold[0] != null) { 207 throw hold[0]; 208 } 209 } 210 211 215 public String getText() throws BadLocationException { 216 StyledDocument doc = this.guards.getDocument(); 217 int p1 = begin.getOffset(); 218 int p2 = end.getOffset(); 219 220 return doc.getText(p1, p2 - p1); 221 } 222 223 224 public String toString() { 225 StringBuilder buf = new StringBuilder ("Position bounds["); 227 try { 228 String content = getText(); 229 buf.append(begin); 230 buf.append(","); buf.append(end); 232 buf.append(",\""); buf.append(content); 234 buf.append("\""); } catch (BadLocationException e) { 236 buf.append("Invalid: "); buf.append(e.getMessage()); 238 } 239 240 buf.append("]"); 242 return buf.toString(); 243 } 244 } 245 | Popular Tags |