KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > 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.openide.text;
20
21 import org.openide.util.NbBundle;
22
23 import java.io.*;
24
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.Position JavaDoc;
27 import javax.swing.text.StyledDocument JavaDoc;
28
29
30 /** A range bounded by two {@link PositionRef}s.
31 *
32 * @author Petr Hamernik
33 */

34 public final class PositionBounds extends Object JavaDoc implements Serializable {
35     static final long serialVersionUID = 3338509625548836633L;
36
37     /** Begin */
38     private PositionRef begin;
39
40     /** End */
41     private PositionRef end;
42
43     /** Creates new <code>PositionBounds</code>.
44      * @param begin the start position of the range
45      * @param end the end position of the range
46     */

47     public PositionBounds(PositionRef begin, PositionRef end) {
48         this.begin = begin;
49         this.end = end;
50     }
51
52     /**
53      * Get the starting position of this range.
54      * @return the begin position
55      */

56     public PositionRef getBegin() {
57         return begin;
58     }
59
60     /**
61      * Get the ending position of this range.
62      * @return the end position
63      */

64     public PositionRef getEnd() {
65         return end;
66     }
67
68     /** Replaces the text contained in this range.
69     * This replacement is done atomically, and so is preferable to manual inserts & removes.
70     * <p>If you are running this from user-oriented code, you may want to wrap it in {@link NbDocument#runAtomicAsUser}.
71     * @param text new text to insert over existing text
72     * @exception IOException if any problem occurred during document loading (if that was necessary)
73     * @exception BadLocationException if the positions are out of the bounds of the document
74     */

75     public void setText(final String JavaDoc text) throws IOException, BadLocationException JavaDoc {
76         final CloneableEditorSupport editor = begin.getCloneableEditorSupport();
77         final StyledDocument JavaDoc doc = editor.openDocument();
78         final BadLocationException JavaDoc[] hold = new BadLocationException JavaDoc[] { null };
79         Runnable JavaDoc run = new Runnable JavaDoc() {
80                 public void run() {
81                     try {
82                         int p1 = begin.getOffset();
83                         int p2 = end.getOffset();
84                         int len = text.length();
85
86                         if (len == 0) { // 1) set empty string
87

88                             if (p2 > p1) {
89                                 doc.remove(p1, p2 - p1);
90                             }
91                         } else { // 2) set non empty string
92

93                             int docLen = doc.getLength();
94
95                             if ((p2 - p1) >= 2) {
96                                 doc.insertString(p1 + 1, text, null);
97
98                                 // [MaM] compute length of inserted string
99
len = doc.getLength() - docLen;
100                                 doc.remove(p1 + 1 + len, p2 - p1 - 1);
101                                 doc.remove(p1, 1);
102                             } else {
103                                 // zero or exactly one character:
104
// adjust the positions if they are
105
// biased to not absorb the text inserted at the start/end
106
// it would be ridiculous not to have text set by setText
107
// be part of the bounds.
108
doc.insertString(p1, text, null);
109
110                                 // [MaM] compute length of inserted string
111
len = doc.getLength() - docLen;
112
113                                 if (p2 > p1) {
114                                     doc.remove(p1 + len, p2 - p1);
115                                 }
116
117                                 if (begin.getOffset() != p1) {
118                                     begin = editor.createPositionRef(p1, begin.getPositionBias());
119                                 }
120
121                                 if ((end.getOffset() - p1) != len) {
122                                     end = editor.createPositionRef(p1 + len, end.getPositionBias());
123                                 }
124                             }
125                         }
126                     } catch (BadLocationException JavaDoc e) {
127                         hold[0] = e;
128                     }
129                 }
130             };
131
132         NbDocument.runAtomic(doc, run);
133
134         if (hold[0] != null) {
135             throw hold[0];
136         }
137     }
138
139     /** Inserts the text after this PositionBounds.
140     * @param text The text to insert. The text must not be empty.
141     * @return the range of inserted text.
142     * @exception IOException if any problem occurred during document loading (if that was necessary)
143     * @exception BadLocationException if the positions are out of the bounds of the document
144     */

145     public PositionBounds insertAfter(final String JavaDoc text)
146     throws IOException, BadLocationException JavaDoc {
147         if (text.length() == 0) {
148             throw new BadLocationException JavaDoc(
149                 NbBundle.getBundle(PositionBounds.class).getString("MSG_Empty_string"), begin.getOffset()
150             );
151         }
152
153         final CloneableEditorSupport editor = begin.getCloneableEditorSupport();
154         final StyledDocument JavaDoc doc = editor.openDocument();
155         final Object JavaDoc[] hold = new Object JavaDoc[] { null, null };
156
157         Runnable JavaDoc run = new Runnable JavaDoc() {
158                 public void run() {
159                     synchronized (editor.getLock()) {
160                         /* editor.getLock(): fixes deadlock - this lock is acquired later anyhow,
161                         so we are changing the order in which the locks are acquired */

162                         try {
163                             // [MaM] remember doclen to compute new length
164
// of the inserted string (the length changes
165
// because insertString removes \r characters
166
// from it)
167
int docLen = doc.getLength();
168
169                             int p1 = end.getOffset();
170                             doc.insertString(p1, text, null);
171
172                             int p2 = (p1 + doc.getLength()) - docLen;
173
174                             end = editor.createPositionRef(p1, end.getPositionBias());
175
176                             PositionRef posBegin = editor.createPositionRef(p1, Position.Bias.Forward);
177                             PositionRef posEnd = editor.createPositionRef(p2, Position.Bias.Backward);
178                             hold[1] = new PositionBounds(posBegin, posEnd);
179                         } catch (BadLocationException JavaDoc e) {
180                             hold[0] = e;
181                         }
182                     }
183                 }
184             };
185
186         NbDocument.runAtomic(doc, run);
187
188         if (hold[0] != null) {
189             throw (BadLocationException JavaDoc) hold[0];
190         } else {
191             return (PositionBounds) hold[1];
192         }
193     }
194
195     /** Finds the text contained in this range.
196     * @return the text
197     * @exception IOException if any I/O problem occurred during document loading (if that was necessary)
198     * @exception BadLocationException if the positions are out of the bounds of the document
199     */

200     public String JavaDoc getText() throws BadLocationException JavaDoc, IOException {
201         StyledDocument JavaDoc doc = begin.getCloneableEditorSupport().openDocument();
202         int p1 = begin.getOffset();
203         int p2 = end.getOffset();
204
205         return doc.getText(p1, p2 - p1);
206     }
207
208     /* @return the bounds as the string. */
209     public String JavaDoc toString() {
210         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Position bounds["); // NOI18N
211

212         try {
213             String JavaDoc content = getText();
214             buf.append(begin);
215             buf.append(","); // NOI18N
216
buf.append(end);
217             buf.append(",\""); // NOI18N
218
buf.append(content);
219             buf.append("\""); // NOI18N
220
} catch (IOException e) {
221             buf.append("Invalid: "); // NOI18N
222
buf.append(e.getMessage());
223         } catch (BadLocationException JavaDoc e) {
224             buf.append("Invalid: "); // NOI18N
225
buf.append(e.getMessage());
226         }
227
228         buf.append("]"); // NOI18N
229

230         return buf.toString();
231     }
232 }
233
Popular Tags