KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > DocumentUtilities


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.editor;
21
22 import javax.swing.text.BadLocationException JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import javax.swing.text.Segment JavaDoc;
25
26 /**
27  * Various document-related utilities.
28  *
29  * @author Miloslav Metelka
30  * @version 1.00
31  */

32
33 public class DocumentUtilities {
34
35     static final SegmentCache SEGMENT_CACHE = new SegmentCache();
36
37     private DocumentUtilities() {
38         // no instances
39
}
40
41     /**
42      * @return >=0 offset of the gap start in the document's content.
43      * -1 if the document does not export <CODE>GapStart</CODE> interface.
44      */

45     public static int getGapStart(Document JavaDoc doc) {
46         GapStart gs = (GapStart)doc.getProperty(GapStart.class);
47         return (gs != null) ? gs.getGapStart() : -1;
48     }
49     
50     /**
51      * Copy portion of the document into target character array.
52      * @param srcDoc document from which to copy.
53      * @param srcStartOffset offset of the first character to copy.
54      * @param srcEndOffset offset that follows the last character to copy.
55      * @param dst destination character array into which the data will be copied.
56      * @param dstOffset offset in the destination array at which the putting
57      * of the characters starts.
58      */

59     public static void copyText(Document JavaDoc srcDoc, int srcStartOffset,
60     int srcEndOffset, char[] dst, int dstOffset) throws BadLocationException JavaDoc {
61         Segment JavaDoc text = SEGMENT_CACHE.getSegment();
62         try {
63             int gapStart = getGapStart(srcDoc);
64             if (gapStart != -1 && srcStartOffset < gapStart && gapStart < srcEndOffset) {
65                 // Get part below gap
66
srcDoc.getText(srcStartOffset, gapStart - srcStartOffset, text);
67                 System.arraycopy(text.array, text.offset, dst, dstOffset, text.count);
68                 dstOffset += text.count;
69                 srcStartOffset = gapStart;
70             }
71
72             srcDoc.getText(srcStartOffset, srcEndOffset - srcStartOffset, text);
73             System.arraycopy(text.array, text.offset, dst, dstOffset, srcEndOffset - srcStartOffset);
74             
75         } finally {
76             SEGMENT_CACHE.releaseSegment(text);
77         }
78     }
79
80 }
81
Popular Tags