1 19 20 package org.netbeans.editor; 21 22 import javax.swing.text.BadLocationException ; 23 import javax.swing.text.Document ; 24 import javax.swing.text.Segment ; 25 26 32 33 public class DocumentUtilities { 34 35 static final SegmentCache SEGMENT_CACHE = new SegmentCache(); 36 37 private DocumentUtilities() { 38 } 40 41 45 public static int getGapStart(Document doc) { 46 GapStart gs = (GapStart)doc.getProperty(GapStart.class); 47 return (gs != null) ? gs.getGapStart() : -1; 48 } 49 50 59 public static void copyText(Document srcDoc, int srcStartOffset, 60 int srcEndOffset, char[] dst, int dstOffset) throws BadLocationException { 61 Segment text = SEGMENT_CACHE.getSegment(); 62 try { 63 int gapStart = getGapStart(srcDoc); 64 if (gapStart != -1 && srcStartOffset < gapStart && gapStart < srcEndOffset) { 65 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 |