1 19 package org.netbeans.api.editor.guards; 20 import javax.swing.text.BadLocationException ; 21 import javax.swing.text.Element ; 22 import javax.swing.text.StyledDocument ; 23 import junit.framework.TestCase; 24 import org.netbeans.editor.GuardedDocument; 25 import org.netbeans.modules.editor.guards.GuardedSectionsImpl; 26 import org.netbeans.modules.editor.guards.GuardsAccessor; 27 import org.netbeans.modules.editor.guards.PositionBounds; 28 import org.openide.text.NbDocument; 29 30 34 public final class GuardUtils { 35 36 37 public static void verifyPositions(TestCase test, GuardedSection gs, int start, int end) { 38 test.assertEquals("start position", start, gs.getStartPosition().getOffset()); 39 test.assertEquals("end position", end, gs.getEndPosition().getOffset()); 40 } 41 42 public static void verifyPositions(TestCase test, InteriorSection gs, int headerStart, int headerEnd, int bodyStart, int bodyEnd, int footerStart, int footerEnd) { 43 test.assertEquals("header begin position", headerStart, gs.getImpl().getHeaderBounds().getBegin().getOffset()); 44 test.assertEquals("header end position", headerEnd, gs.getImpl().getHeaderBounds().getEnd().getOffset()); 45 test.assertEquals("body begin position", bodyStart, gs.getBodyStartPosition().getOffset()); 46 test.assertEquals("body end position", bodyEnd, gs.getBodyEndPosition().getOffset()); 47 test.assertEquals("footer begin position", footerStart, gs.getImpl().getFooterBounds().getBegin().getOffset()); 48 test.assertEquals("footer end position", footerEnd, gs.getImpl().getFooterBounds().getEnd().getOffset()); 49 } 50 51 public static void verifyGuardAttr(TestCase test, StyledDocument doc, SimpleSection gs) { 52 verifyGuardAttr(test, (GuardedDocument) doc, gs.getStartPosition().getOffset(), gs.getEndPosition().getOffset()); 53 } 54 55 public static void verifyGuardAttr(TestCase test, StyledDocument doc, InteriorSection gs) { 56 PositionBounds bounds = gs.getImpl().getHeaderBounds(); 57 verifyGuardAttr(test, (GuardedDocument) doc, bounds.getBegin().getOffset(), bounds.getEnd().getOffset()); 58 bounds = gs.getImpl().getFooterBounds(); 59 verifyGuardAttr(test, (GuardedDocument) doc, bounds.getBegin().getOffset(), bounds.getEnd().getOffset()); 60 61 } 62 63 public static void verifyGuardAttr(TestCase test, GuardedDocument doc, int startPosition, int endPosition) { 64 for (int i = startPosition; i <= endPosition; i++) { 65 test.assertTrue("element should be guarded; pos: " + i, doc.isPosGuarded(i)); 66 } 67 } 68 69 public static boolean isGuarded(StyledDocument doc, int offset) { 70 GuardedDocument gdoc = (GuardedDocument) doc; 71 return gdoc.isPosGuarded(offset); 72 } 73 74 public static void dumpGuardedAttr(StyledDocument doc) { 75 System.out.println("" + ((GuardedDocument) doc).toStringDetail()); 76 } 89 90 public static void dumpDocument(StyledDocument doc) throws BadLocationException { 91 char[] cs = doc.getText(0, doc.getLength()).toCharArray(); 92 StringBuilder sb = new StringBuilder (); 93 sb.append("0:"); 94 for (int i = 0; i < cs.length; i++) { 95 char c = cs[i]; 96 if (c == ' ') 97 c = '.'; 98 else if (c == '\n') { 99 sb.append('$').append(c).append((i + 1) + ":"); 100 continue; 101 } 102 sb.append(c); 103 } 104 105 System.out.println("Document: "+ doc.getLength() + "\n" + sb.toString()); 106 } 107 108 public static void print(SimpleSection gs) { 109 System.out.println( 110 "SimplS: " + gs.getName() + 111 ", s: " + gs.getStartPosition().getOffset() + 112 ", e: " + gs.getEndPosition().getOffset() + 113 ", v: " + gs.isValid() + 114 ", t: '" + gs.getText() + "'"); 115 } 116 117 public static void print(InteriorSection gs) { 118 System.out.println( 119 "InterS: " + gs.getName() + 120 ", hs: " + gs.getImpl().getHeaderBounds().getBegin().getOffset() + 121 ", he: " + gs.getImpl().getHeaderBounds().getEnd().getOffset() + 122 ", bs: " + gs.getImpl().getBodyBounds().getBegin().getOffset() + 123 ", be: " + gs.getImpl().getBodyBounds().getEnd().getOffset() + 124 ", fs: " + gs.getImpl().getFooterBounds().getBegin().getOffset() + 125 ", fe: " + gs.getImpl().getFooterBounds().getEnd().getOffset() + 126 ", v: " + gs.isValid() + 127 ", t: '" + gs.getText() + "'"); 128 } 129 130 static String toString(Element el) { 131 return el.toString() + "{el s:" + el.getStartOffset() + ", e:" + el.getEndOffset() + "}"; 132 } 133 134 public static void initManager(Editor editor) { 135 initManager(editor, null); 136 } 137 138 public static void initManager(Editor editor, GuardedSectionsImpl gimpl) { 139 if (gimpl == null) { 140 gimpl = new GuardedSectionsImpl(editor); 141 } 142 GuardedSectionManager api = GuardsAccessor.DEFAULT.createGuardedSections(gimpl); 143 editor.doc.putProperty(GuardedSectionManager.class, api); 144 } 145 } 146 | Popular Tags |