1 19 20 package org.netbeans.modules.editor.guards; 21 22 import javax.swing.text.BadLocationException ; 23 import javax.swing.text.Position ; 24 import javax.swing.text.StyledDocument ; 25 26 31 public final class InteriorSectionImpl extends GuardedSectionImpl { 32 33 private PositionBounds header; 34 35 36 private PositionBounds body; 37 38 39 private PositionBounds footer; 40 41 45 InteriorSectionImpl(String name, PositionBounds header, PositionBounds body, PositionBounds footer, GuardedSectionsImpl guards) { 46 super(name, guards); 47 this.header = header; 48 this.body = body; 49 this.footer = footer; 50 } 51 52 56 public void setBody(String text) { 57 setText(body, text, false); 58 } 59 60 public String getBody() { 61 String s = null; 62 if (isValid()) { 63 try { 64 s = body.getText(); 65 } catch (BadLocationException ex) { 66 throw new IllegalStateException (ex); 67 } 68 } 69 return s; 70 } 71 72 76 public void setHeader(String text) { 77 setText(header, text, true); 78 } 79 80 85 public String getHeader() { 86 String s = null; 87 if (isValid()) { 88 try { 89 s = header.getText(); 90 } catch (BadLocationException ex) { 91 throw new IllegalStateException (ex); 92 } 93 } 94 return s; 95 } 96 97 104 public void setFooter(String text) { 105 boolean endsWithEol = text.endsWith("\n"); int firstEol = text.indexOf('\n'); 107 int lastEol = text.lastIndexOf('\n'); 108 109 if ((firstEol != lastEol) || (endsWithEol && (firstEol != -1))) { 110 if (endsWithEol) { 111 text = text.substring(0, text.length() - 1); 112 } 113 text = text.replace('\n', ' '); 114 } 115 setText(footer, text, true); 116 } 117 118 123 public String getFooter() { 124 String s = null; 125 if (isValid()) { 126 try { 127 s = footer.getText(); 128 } catch (BadLocationException ex) { 129 throw new IllegalStateException (ex); 130 } 131 } 132 return s; 133 } 134 135 141 public Position getCaretPosition() { 142 return body.getBegin(); 143 } 144 145 149 void markGuarded(StyledDocument doc) { 150 markGuarded(doc, header, true); 151 markGuarded(doc, footer, true); 152 } 153 154 158 void unmarkGuarded(StyledDocument doc) { 159 markGuarded(doc, header, false); 160 markGuarded(doc, footer, false); 161 } 162 163 167 public String getText() { 168 StringBuffer buf = new StringBuffer (); 169 try { 170 buf.append(header.getText()); 171 buf.append(body.getText()); 172 buf.append(footer.getText()); 173 } catch (Exception e) { 174 } 175 return buf.toString(); 176 } 177 178 198 public boolean contains(Position pos, boolean allowHoles) { 199 if (!allowHoles) { 200 return header.getBegin().getOffset() <= pos.getOffset() && 201 footer.getEnd().getOffset() >= pos.getOffset(); 202 } else { 203 if (header.getBegin().getOffset() <= pos.getOffset() && 204 header.getEnd().getOffset() >= pos.getOffset()) { 205 return true; 206 } 207 return footer.getBegin().getOffset() <= pos.getOffset() && 208 footer.getEnd().getOffset() >= pos.getOffset(); 209 } 210 } 211 212 public Position getStartPosition() { 213 return header.getBegin(); 214 } 215 216 public Position getEndPosition() { 217 return footer.getEnd(); 218 } 219 220 public Position getBodyStartPosition() { 221 return body.getBegin(); 222 } 223 224 public Position getBodyEndPosition() { 225 return body.getEnd(); 226 } 227 228 public void resolvePositions() throws BadLocationException { 229 header.resolvePositions(); 230 body.resolvePositions(); 231 footer.resolvePositions(); 232 } 233 234 public PositionBounds getHeaderBounds() { 236 return this.header; 237 } 238 239 public PositionBounds getBodyBounds() { 240 return this.body; 241 } 242 243 public PositionBounds getFooterBounds() { 244 return this.footer; 245 } 246 } 247 | Popular Tags |