KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > lib2 > DocUtils


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.lib2;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import javax.swing.text.Element JavaDoc;
28 import org.openide.util.NbBundle;
29
30 /**
31  * This class contains useful methods for working with documents.
32  *
33  * @author Vita Stejskal
34  */

35 public final class DocUtils {
36
37     private static final Logger JavaDoc LOG = Logger.getLogger(DocUtils.class.getName());
38     
39     public static int getRowStart(Document JavaDoc doc, int offset, int lineShift)
40     throws BadLocationException JavaDoc {
41         
42         checkOffsetValid(doc, offset);
43
44         if (lineShift != 0) {
45             Element JavaDoc lineRoot = doc.getDefaultRootElement();
46             int line = lineRoot.getElementIndex(offset);
47             line += lineShift;
48             if (line < 0 || line >= lineRoot.getElementCount()) {
49                 return -1; // invalid line shift
50
}
51             return lineRoot.getElement(line).getStartOffset();
52
53         } else { // no shift
54
return doc.getDefaultRootElement().getElement(
55                    doc.getDefaultRootElement().getElementIndex(offset)).getStartOffset();
56         }
57     }
58
59     public static int getRowEnd(Document JavaDoc doc, int offset)
60     throws BadLocationException JavaDoc {
61         checkOffsetValid(doc, offset);
62
63         return doc.getDefaultRootElement().getElement(
64                doc.getDefaultRootElement().getElementIndex(offset)).getEndOffset() - 1;
65     }
66     
67     /**
68      * Return line offset (line number - 1) for some position in the document.
69      *
70      * @param doc document to operate on
71      * @param offset position in document where to start searching
72      */

73     public static int getLineOffset(Document JavaDoc doc, int offset) throws BadLocationException JavaDoc {
74         checkOffsetValid(offset, doc.getLength() + 1);
75
76         Element JavaDoc lineRoot = doc.getDefaultRootElement();
77         return lineRoot.getElementIndex(offset);
78     }
79
80     public static String JavaDoc debugPosition(Document JavaDoc doc, int offset) {
81         String JavaDoc ret;
82
83         if (offset >= 0) {
84             try {
85                 int line = getLineOffset(doc, offset) + 1;
86                 int col = getVisualColumn(doc, offset) + 1;
87                 ret = String.valueOf(line) + ":" + String.valueOf(col); // NOI18N
88
} catch (BadLocationException JavaDoc e) {
89                 ret = NbBundle.getBundle(DocUtils.class).getString("wrong_position")
90                       + ' ' + offset + " > " + doc.getLength(); // NOI18N
91
}
92         } else {
93             ret = String.valueOf(offset);
94         }
95
96         return ret;
97     }
98
99     /** Return visual column (with expanded tabs) on the line.
100     * @param doc document to operate on
101     * @param offset position in document for which the visual column should be found
102     * @return visual column on the line determined by position
103     */

104     public static int getVisualColumn(Document JavaDoc doc, int offset) throws BadLocationException JavaDoc {
105         int docLen = doc.getLength();
106         if (offset == docLen + 1) { // at ending extra '\n' => make docLen to proceed without BLE
107
offset = docLen;
108         }
109
110         // TODO: fix this, do not use reflection
111
try {
112             Method JavaDoc m = doc.getClass().getMethod("getVisColFromPos", Integer.TYPE);
113             return (Integer JavaDoc) m.invoke(doc, offset);
114 // return doc.getVisColFromPos(offset);
115
} catch (Exception JavaDoc e) {
116             return -1;
117         }
118     }
119     
120     public static boolean isIdentifierPart(Document JavaDoc doc, char ch) {
121         // TODO: make this configurable
122
return AcceptorFactory.LETTER_DIGIT.accept(ch);
123     }
124     
125     public static boolean isWhitespace(char ch) {
126         // TODO: make this configurable
127
return AcceptorFactory.WHITESPACE.accept(ch);
128     }
129     
130     public static void atomicLock(Document JavaDoc doc) {
131         // TODO: fix this, do not use reflection
132
try {
133             Method JavaDoc lockMethod = doc.getClass().getMethod("atomicLock");
134             lockMethod.invoke(doc);
135         } catch (Exception JavaDoc e) {
136             LOG.log(Level.WARNING, e.getMessage(), e);
137         }
138     }
139     
140     public static void atomicUnlock(Document JavaDoc doc) {
141         // TODO: fix this, do not use reflection
142
try {
143             Method JavaDoc unlockMethod = doc.getClass().getMethod("atomicUnlock");
144             unlockMethod.invoke(doc);
145         } catch (Exception JavaDoc e) {
146             LOG.log(Level.WARNING, e.getMessage(), e);
147         }
148     }
149     
150     private static void checkOffsetValid(Document JavaDoc doc, int offset) throws BadLocationException JavaDoc {
151         checkOffsetValid(offset, doc.getLength());
152     }
153
154     private static void checkOffsetValid(int offset, int limitOffset) throws BadLocationException JavaDoc {
155         if (offset < 0 || offset > limitOffset) {
156             throw new BadLocationException JavaDoc("Invalid offset=" + offset // NOI18N
157
+ " not within <0, " + limitOffset + ">", // NOI18N
158
offset);
159         }
160     }
161     
162     /** Creates a new instance of DocUtils */
163     private DocUtils() {
164     }
165     
166 }
167
Popular Tags