KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > guards > InteriorSectionImpl


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
20 package org.netbeans.modules.editor.guards;
21
22 import javax.swing.text.BadLocationException JavaDoc;
23 import javax.swing.text.Position JavaDoc;
24 import javax.swing.text.StyledDocument JavaDoc;
25
26 /**
27  * Represents an advanced guarded block.
28  * It consists of three pieces: a header, body, and footer.
29  * The header and footer are guarded but the body is not.
30  */

31 public final class InteriorSectionImpl extends GuardedSectionImpl {
32     /** Text range of the header. */
33     private PositionBounds header;
34
35     /** Text range of the header. */
36     private PositionBounds body;
37
38     /** Text range of the footer. */
39     private PositionBounds footer;
40
41     /**
42      * Creates new section.
43      * @param name Name of the new section.
44      */

45     InteriorSectionImpl(String JavaDoc 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     /**
53      * Set the text of the body.
54      * @param text the new text
55      */

56     public void setBody(String JavaDoc text) {
57         setText(body, text, false);
58     }
59
60     public String JavaDoc getBody() {
61         String JavaDoc s = null;
62         if (isValid()) {
63             try {
64                 s = body.getText();
65             } catch (BadLocationException JavaDoc ex) {
66                 throw new IllegalStateException JavaDoc(ex);
67             }
68         }
69         return s;
70     }
71
72     /**
73      * Set the text of the header.
74      * @param text the new text
75      */

76     public void setHeader(String JavaDoc text) {
77         setText(header, text, true);
78     }
79
80     /**
81      * Returns the contents of the header part of the section. If the
82      * section is invalid the method returns null.
83      * @return contents of the header or null, if the section is not valid.
84      */

85     public String JavaDoc getHeader() {
86         String JavaDoc s = null;
87         if (isValid()) {
88             try {
89                 s = header.getText();
90             } catch (BadLocationException JavaDoc ex) {
91                 throw new IllegalStateException JavaDoc(ex);
92             }
93         }
94         return s;
95     }
96
97     /**
98      * Set the text of the footer.
99      * Note that the footer of the section must have exactly one line.
100      * So, all interior newline characters will be replaced by spaces.
101      *
102      * @param text the new text
103      */

104     public void setFooter(String JavaDoc text) {
105         boolean endsWithEol = text.endsWith("\n"); // NOI18N
106
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     /**
119      * Returns the contents of the footer part of the guarded section.
120      * The method will return null, if the section is not valid.
121      * @return contents of the footer part, or null if the section is not valid.
122      */

123     public String JavaDoc getFooter() {
124         String JavaDoc s = null;
125         if (isValid()) {
126             try {
127                 s = footer.getText();
128             } catch (BadLocationException JavaDoc ex) {
129                 throw new IllegalStateException JavaDoc(ex);
130             }
131         }
132         return s;
133     }
134
135     /**
136      * Gets the begin of section. To this position is set the cursor
137      * when section is open in the editor.
138      * @return the begin position of the body section - the place where
139      * is possible to edit.
140      */

141     public Position JavaDoc getCaretPosition() {
142         return body.getBegin();
143     }
144
145     /**
146      * Marks the section as guarded.
147      * @param doc The styled document where this section placed in.
148      */

149     void markGuarded(StyledDocument JavaDoc doc) {
150         markGuarded(doc, header, true);
151         markGuarded(doc, footer, true);
152     }
153
154     /**
155      * Unmarks the section as guarded.
156      * @param doc The styled document where this section placed in.
157      */

158     void unmarkGuarded(StyledDocument JavaDoc doc) {
159         markGuarded(doc, header, false);
160         markGuarded(doc, footer, false);
161     }
162
163     /**
164      * Gets the text contained in the section.
165      * @return The text contained in the section.
166      */

167     public String JavaDoc getText() {
168         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
169         try {
170             buf.append(header.getText());
171             buf.append(body.getText());
172             buf.append(footer.getText());
173         } catch (Exception JavaDoc e) {
174         }
175         return buf.toString();
176     }
177
178     /*
179     public String toString() {
180       StringBuffer buf = new StringBuffer("InteriorSection:"+name); // NOI18N
181       try {
182         buf.append("HEADER:\""); // NOI18N
183         buf.append(header.getText());
184         buf.append("\"");
185         buf.append("BODY:\""); // NOI18N
186         buf.append(body.getText());
187         buf.append("\"");
188         buf.append("BOTTOM:\""); // NOI18N
189         buf.append(footer.getText());
190         buf.append("\"");
191       }
192       catch (Exception e) {
193         buf.append("EXCEPTION:"); // NOI18N
194         buf.append(e.getMessage());
195       }
196       return buf.toString();
197     }*/

198     public boolean contains(Position JavaDoc 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 JavaDoc getStartPosition() {
213         return header.getBegin();
214     }
215
216     public Position JavaDoc getEndPosition() {
217         return footer.getEnd();
218     }
219     
220     public Position JavaDoc getBodyStartPosition() {
221         return body.getBegin();
222     }
223     
224     public Position JavaDoc getBodyEndPosition() {
225         return body.getEnd();
226     }
227
228     public void resolvePositions() throws BadLocationException JavaDoc {
229         header.resolvePositions();
230         body.resolvePositions();
231         footer.resolvePositions();
232     }
233
234     // just for to unit testing purposes
235
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