1 19 package org.openide.text; 20 21 import org.openide.util.NbBundle; 22 23 import java.io.*; 24 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.Position ; 27 import javax.swing.text.StyledDocument ; 28 29 30 34 public final class PositionBounds extends Object implements Serializable { 35 static final long serialVersionUID = 3338509625548836633L; 36 37 38 private PositionRef begin; 39 40 41 private PositionRef end; 42 43 47 public PositionBounds(PositionRef begin, PositionRef end) { 48 this.begin = begin; 49 this.end = end; 50 } 51 52 56 public PositionRef getBegin() { 57 return begin; 58 } 59 60 64 public PositionRef getEnd() { 65 return end; 66 } 67 68 75 public void setText(final String text) throws IOException, BadLocationException { 76 final CloneableEditorSupport editor = begin.getCloneableEditorSupport(); 77 final StyledDocument doc = editor.openDocument(); 78 final BadLocationException [] hold = new BadLocationException [] { null }; 79 Runnable run = new Runnable () { 80 public void run() { 81 try { 82 int p1 = begin.getOffset(); 83 int p2 = end.getOffset(); 84 int len = text.length(); 85 86 if (len == 0) { 88 if (p2 > p1) { 89 doc.remove(p1, p2 - p1); 90 } 91 } else { 93 int docLen = doc.getLength(); 94 95 if ((p2 - p1) >= 2) { 96 doc.insertString(p1 + 1, text, null); 97 98 len = doc.getLength() - docLen; 100 doc.remove(p1 + 1 + len, p2 - p1 - 1); 101 doc.remove(p1, 1); 102 } else { 103 doc.insertString(p1, text, null); 109 110 len = doc.getLength() - docLen; 112 113 if (p2 > p1) { 114 doc.remove(p1 + len, p2 - p1); 115 } 116 117 if (begin.getOffset() != p1) { 118 begin = editor.createPositionRef(p1, begin.getPositionBias()); 119 } 120 121 if ((end.getOffset() - p1) != len) { 122 end = editor.createPositionRef(p1 + len, end.getPositionBias()); 123 } 124 } 125 } 126 } catch (BadLocationException e) { 127 hold[0] = e; 128 } 129 } 130 }; 131 132 NbDocument.runAtomic(doc, run); 133 134 if (hold[0] != null) { 135 throw hold[0]; 136 } 137 } 138 139 145 public PositionBounds insertAfter(final String text) 146 throws IOException, BadLocationException { 147 if (text.length() == 0) { 148 throw new BadLocationException ( 149 NbBundle.getBundle(PositionBounds.class).getString("MSG_Empty_string"), begin.getOffset() 150 ); 151 } 152 153 final CloneableEditorSupport editor = begin.getCloneableEditorSupport(); 154 final StyledDocument doc = editor.openDocument(); 155 final Object [] hold = new Object [] { null, null }; 156 157 Runnable run = new Runnable () { 158 public void run() { 159 synchronized (editor.getLock()) { 160 162 try { 163 int docLen = doc.getLength(); 168 169 int p1 = end.getOffset(); 170 doc.insertString(p1, text, null); 171 172 int p2 = (p1 + doc.getLength()) - docLen; 173 174 end = editor.createPositionRef(p1, end.getPositionBias()); 175 176 PositionRef posBegin = editor.createPositionRef(p1, Position.Bias.Forward); 177 PositionRef posEnd = editor.createPositionRef(p2, Position.Bias.Backward); 178 hold[1] = new PositionBounds(posBegin, posEnd); 179 } catch (BadLocationException e) { 180 hold[0] = e; 181 } 182 } 183 } 184 }; 185 186 NbDocument.runAtomic(doc, run); 187 188 if (hold[0] != null) { 189 throw (BadLocationException ) hold[0]; 190 } else { 191 return (PositionBounds) hold[1]; 192 } 193 } 194 195 200 public String getText() throws BadLocationException , IOException { 201 StyledDocument doc = begin.getCloneableEditorSupport().openDocument(); 202 int p1 = begin.getOffset(); 203 int p2 = end.getOffset(); 204 205 return doc.getText(p1, p2 - p1); 206 } 207 208 209 public String toString() { 210 StringBuffer buf = new StringBuffer ("Position bounds["); 212 try { 213 String content = getText(); 214 buf.append(begin); 215 buf.append(","); buf.append(end); 217 buf.append(",\""); buf.append(content); 219 buf.append("\""); } catch (IOException e) { 221 buf.append("Invalid: "); buf.append(e.getMessage()); 223 } catch (BadLocationException e) { 224 buf.append("Invalid: "); buf.append(e.getMessage()); 226 } 227 228 buf.append("]"); 230 return buf.toString(); 231 } 232 } 233 | Popular Tags |