KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > editor > guards > GuardUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.editor.guards;
20 import javax.swing.text.BadLocationException JavaDoc;
21 import javax.swing.text.Element JavaDoc;
22 import javax.swing.text.StyledDocument JavaDoc;
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 /**
31  *
32  * @author Jan Pokorsky
33  */

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 JavaDoc doc, SimpleSection gs) {
52         verifyGuardAttr(test, (GuardedDocument) doc, gs.getStartPosition().getOffset(), gs.getEndPosition().getOffset());
53     }
54     
55     public static void verifyGuardAttr(TestCase test, StyledDocument JavaDoc 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 JavaDoc doc, int offset) {
70         GuardedDocument gdoc = (GuardedDocument) doc;
71         return gdoc.isPosGuarded(offset);
72     }
73     
74     public static void dumpGuardedAttr(StyledDocument JavaDoc doc) {
75         System.out.println("" + ((GuardedDocument) doc).toStringDetail());
76 // int start = 0;
77
// int end = doc.getLength();
78
// Element el = null;
79
// System.out.println("Document.guards: " +
80
// ", s: " + start + ", e: " + end);
81
// do {
82
// el = doc.getCharacterElement(start);
83
// System.out.println("s: " + el.getStartOffset() + ", e: " + el.getEndOffset() +
84
// ", g: " + Boolean.TRUE.equals(el.getAttributes().getAttribute(NbDocument.GUARDED)));
85
// start = el.getEndOffset() + 1;
86
// } while (end > el.getEndOffset());
87
// System.out.println("-------");
88
}
89     
90     public static void dumpDocument(StyledDocument JavaDoc doc) throws BadLocationException JavaDoc {
91         char[] cs = doc.getText(0, doc.getLength()).toCharArray();
92         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
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 JavaDoc toString(Element JavaDoc 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