1 19 20 package org.netbeans.modules.editor.guards; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyVetoException ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Position ; 26 import javax.swing.text.StyledDocument ; 27 import org.netbeans.api.editor.guards.GuardedSection; 28 import org.openide.text.NbDocument; 29 30 32 public abstract class GuardedSectionImpl { 33 34 String name; 35 36 37 boolean valid = false; 38 39 final GuardedSectionsImpl guards; 40 41 GuardedSection guard; 42 43 46 public String getName() { 47 return name; 48 } 49 50 53 GuardedSectionImpl(String name, GuardedSectionsImpl guards) { 54 this.name = name; 55 this.guards = guards; 56 } 57 58 public final void attach(GuardedSection guard) { 59 this.guard = guard; 60 valid = true; 61 } 62 63 67 public void setName(String name) throws PropertyVetoException { 68 if (!this.name.equals(name)) { 69 synchronized (this.guards.sections) { 70 if (valid) { 71 if (this.guards.sections.get(name) != null) 72 throw new PropertyVetoException ("", new PropertyChangeEvent (this, "name", this.name, name)); this.guards.sections.remove(this.name); 74 this.name = name; 75 this.guards.sections.put(name, this); 76 } 77 } 78 } 79 80 } 81 82 86 public void deleteSection() { 87 synchronized (this.guards.sections) { 88 if (valid) { 89 try { 90 this.guards.sections.remove(name); 91 unmarkGuarded(this.guards.getDocument()); 95 deleteText(); 96 valid = false; 97 } catch (BadLocationException e) { 98 throw new IllegalStateException (e); 99 } 100 } 101 } 102 } 103 104 108 public boolean isValid() { 109 return valid; 110 } 111 112 118 public void removeSection() { 119 synchronized (this.guards.sections) { 120 if (valid) { 121 this.guards.sections.remove(name); 122 unmarkGuarded(this.guards.getDocument()); 126 valid = false; 127 } 128 } 129 } 130 131 141 protected boolean setText(PositionBounds bounds, String text, boolean minLen) { 142 if (!valid) 143 return false; 144 145 if (minLen) { 148 if (text.length() == 0 || text.length() == 1 && text.equals("\n")) 149 text = " "; } 151 152 if (text.endsWith("\n")) text = text.substring(0, text.length() - 1); 154 155 try { 156 bounds.setText(text); 157 return true; 158 } catch (BadLocationException e) { 159 } 160 return false; 161 } 162 163 168 void markGuarded(StyledDocument doc, PositionBounds bounds, boolean mark) { 169 int begin = bounds.getBegin().getOffset(); 170 int end = bounds.getEnd().getOffset(); 171 if (mark) { 172 NbDocument.markGuarded(doc, begin, end - begin + 1); 173 } else 174 NbDocument.unmarkGuarded(doc, begin, end - begin + 1); 175 } 176 177 180 abstract void markGuarded(StyledDocument doc); 181 182 185 abstract void unmarkGuarded(StyledDocument doc); 186 187 190 final void deleteText() throws BadLocationException { 191 if (valid) { 192 final StyledDocument doc = guards.getDocument(); 193 final BadLocationException [] blex = new BadLocationException [1]; 194 NbDocument.runAtomic(doc, new Runnable () { 195 public void run() { 196 try { 197 int start = getStartPosition().getOffset(); 198 if (start > 0 && "\n".equals(doc.getText(start - 1, 1))) { start--; 200 } 201 doc.remove(start, getEndPosition().getOffset() - start + 1); 202 } catch (BadLocationException ex) { 203 blex[0] = ex; 204 } 205 } 206 }); 207 208 if (blex[0] != null) { 209 throw blex[0]; 210 } 211 } 212 } 213 214 217 public abstract Position getCaretPosition(); 218 219 222 public abstract String getText(); 223 224 231 public abstract boolean contains(Position pos, boolean permitHoles); 232 234 public abstract Position getEndPosition(); 235 237 public abstract Position getStartPosition(); 238 239 public abstract void resolvePositions() throws BadLocationException ; 240 241 } 242 | Popular Tags |