KickJava   Java API By Example, From Geeks To Geeks.

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


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.modules.editor.guards;
20
21 import javax.swing.text.BadLocationException JavaDoc;
22 import javax.swing.text.Position JavaDoc;
23 import javax.swing.text.StyledDocument JavaDoc;
24 import org.openide.text.NbDocument;
25
26 /**
27  * A range bounded by two {@link Position}s. This class is derived from
28  * {@link org.openide.text.PositionBounds} in fact.
29  *
30  * @author Petr Hamernik
31  */

32 public final class PositionBounds {
33
34     /** Begin */
35     private Position JavaDoc begin;
36
37     /** End */
38     private Position JavaDoc end;
39
40     private final GuardedSectionsImpl guards;
41     
42     private static final class UnresolvedPosition implements Position JavaDoc {
43
44         private int offset;
45         
46         public UnresolvedPosition(int offset) {
47             this.offset = offset;
48         }
49         
50         public int getOffset() {
51             return this.offset;
52         }
53     }
54     
55     private static final class BackwardPosition implements Position JavaDoc {
56         
57         private Position JavaDoc delegate;
58         
59         public BackwardPosition(Position JavaDoc delegate) {
60             this.delegate = delegate;
61         }
62     
63         public int getOffset() {
64             return this.delegate.getOffset() + 1;
65         }
66     }
67
68     /** Creates new <code>PositionBounds</code>.
69      * @param begin the start position of the range
70      * @param end the end position of the range
71     */

72     public PositionBounds(Position JavaDoc begin, Position JavaDoc end, GuardedSectionsImpl guards) {
73         this.begin = begin;
74         this.end = end;
75         this.guards = guards;
76     }
77     
78     public static PositionBounds create(int begin, int end, GuardedSectionsImpl guards) throws BadLocationException JavaDoc {
79         StyledDocument JavaDoc doc = guards.getDocument();
80         return new PositionBounds(doc.createPosition(begin), doc.createPosition(end), guards);
81     }
82     
83     /**
84      * creates bounds with backward begin position allowing to insert text to
85      * begin position while the begin position remains unchanged. The behavior
86      * desired for body sections but not for header or footer sections.
87      */

88     public static PositionBounds createBodyBounds(int begin, int end, GuardedSectionsImpl guards) throws BadLocationException JavaDoc {
89         StyledDocument JavaDoc doc = guards.getDocument();
90         return new PositionBounds(new BackwardPosition(doc.createPosition(begin - 1)), doc.createPosition(end), guards);
91     }
92     
93     /**
94      * creates a position bounds object without checking position validity since the document may be empty yet.
95      * @see #resolvePositions
96      */

97     public static PositionBounds createUnresolved(int begin, int end, GuardedSectionsImpl guards) throws BadLocationException JavaDoc {
98         StyledDocument JavaDoc doc = guards.getDocument();
99         return new PositionBounds(new UnresolvedPosition(begin), new UnresolvedPosition(end), guards);
100     }
101     
102     /**
103      * @see #createBodyBounds
104      * @see #resolvePositions
105      */

106     public static PositionBounds createBodyUnresolved(int begin, int end, GuardedSectionsImpl guards) throws BadLocationException JavaDoc {
107         return new PositionBounds(new BackwardPosition(new UnresolvedPosition(begin - 1)), new UnresolvedPosition(end), guards);
108     }
109     
110     public void resolvePositions() throws BadLocationException JavaDoc {
111         StyledDocument JavaDoc doc = guards.getDocument();
112         Position JavaDoc b, e;
113         if (end instanceof UnresolvedPosition) {
114             if (begin instanceof BackwardPosition) {
115                 b = ((BackwardPosition) begin).delegate = doc.createPosition(
116                         ((BackwardPosition) begin).delegate.getOffset());
117             } else {
118                 b = doc.createPosition(begin.getOffset());
119             }
120             e = doc.createPosition(end.getOffset());
121             this.begin = b;
122             this.end = e;
123         }
124     }
125
126     /**
127      * Get the starting position of this range.
128      * @return the begin position
129      */

130     public Position JavaDoc getBegin() {
131         return begin;
132     }
133
134     /**
135      * Get the ending position of this range.
136      * @return the end position
137      */

138     public Position JavaDoc getEnd() {
139         return end;
140     }
141
142     /** Replaces the text contained in this range.
143     * This replacement is done atomically, and so is preferable to manual inserts & removes.
144     * <p>If you are running this from user-oriented code, you may want to wrap it in {@link NbDocument#runAtomicAsUser}.
145     * @param text new text to insert over existing text
146     * @exception BadLocationException if the positions are out of the bounds of the document
147     */

148     public void setText(final String JavaDoc text) throws BadLocationException JavaDoc {
149         final StyledDocument JavaDoc doc = guards.getDocument();
150         final BadLocationException JavaDoc[] hold = new BadLocationException JavaDoc[] { null };
151         Runnable JavaDoc run = new Runnable JavaDoc() {
152                 public void run() {
153                     try {
154                         int p1 = begin.getOffset();
155                         int p2 = end.getOffset();
156                         int len = text.length();
157
158                         if (len == 0) { // 1) set empty string
159

160                             if (p2 > p1) {
161                                 doc.remove(p1, p2 - p1);
162                             }
163                         } else { // 2) set non empty string
164

165                             int docLen = doc.getLength();
166
167                             if ((p2 - p1) >= 1) {
168                                 doc.insertString(p1 + 1, text, null);
169
170                                 // [MaM] compute length of inserted string
171
len = doc.getLength() - docLen;
172                                 doc.remove(p1 + 1 + len, p2 - p1 - 1);
173                                 doc.remove(p1, 1);
174                             } else {
175                                 // zero or exactly one character:
176
// adjust the positions if they are
177
// biased to not absorb the text inserted at the start/end
178
// it would be ridiculous not to have text set by setText
179
// be part of the bounds.
180
doc.insertString(p1, text, null);
181
182                                 // [MaM] compute length of inserted string
183
len = doc.getLength() - docLen;
184
185                                 if (p2 > p1) {
186                                     doc.remove(p1 + len, p2 - p1);
187                                 }
188
189                                 if (begin.getOffset() != p1) {
190                                     begin = doc.createPosition(p1);
191                                 }
192
193                                 if ((end.getOffset() - p1) != len) {
194                                     end = doc.createPosition(p1 + len);
195                                 }
196                             }
197                         }
198                     } catch (BadLocationException JavaDoc e) {
199                         hold[0] = e;
200                     }
201                 }
202             };
203
204         NbDocument.runAtomic(doc, run);
205
206         if (hold[0] != null) {
207             throw hold[0];
208         }
209     }
210
211     /** Finds the text contained in this range.
212     * @return the text
213     * @exception BadLocationException if the positions are out of the bounds of the document
214     */

215     public String JavaDoc getText() throws BadLocationException JavaDoc {
216         StyledDocument JavaDoc doc = this.guards.getDocument();
217         int p1 = begin.getOffset();
218         int p2 = end.getOffset();
219
220         return doc.getText(p1, p2 - p1);
221     }
222
223     /* @return the bounds as the string. */
224     public String JavaDoc toString() {
225         StringBuilder JavaDoc buf = new StringBuilder JavaDoc("Position bounds["); // NOI18N
226

227         try {
228             String JavaDoc content = getText();
229             buf.append(begin);
230             buf.append(","); // NOI18N
231
buf.append(end);
232             buf.append(",\""); // NOI18N
233
buf.append(content);
234             buf.append("\""); // NOI18N
235
} catch (BadLocationException JavaDoc e) {
236             buf.append("Invalid: "); // NOI18N
237
buf.append(e.getMessage());
238         }
239
240         buf.append("]"); // NOI18N
241

242         return buf.toString();
243     }
244 }
245
Popular Tags