KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > codetemplates > SyncDocumentRegion


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.lib.editor.codetemplates;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.event.DocumentEvent JavaDoc;
26 import javax.swing.event.DocumentListener JavaDoc;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.Position JavaDoc;
30 import org.netbeans.lib.editor.util.CharSequenceUtilities;
31 import org.netbeans.lib.editor.util.swing.DocumentUtilities;
32 import org.netbeans.lib.editor.util.swing.MutablePositionRegion;
33 import org.netbeans.lib.editor.util.swing.PositionRegion;
34 import org.openide.ErrorManager;
35
36 /**
37  * Maintain the same text in the selected regions of the text document.
38  *
39  * @author Miloslav Metelka
40  */

41 public final class SyncDocumentRegion {
42     
43     private Document JavaDoc doc;
44     
45     private List JavaDoc/*<MutablePositionRegion>*/ regions;
46     
47     private List JavaDoc/*<MutablePositionRegion>*/ sortedRegions;
48     
49     private boolean regionsSortPerformed;
50     
51     /**
52      * Construct synchronized document regions.
53      *
54      * @param doc document on which to operate.
55      * @param regions regions that should be kept synchronized.
56      * The first region is the master. All the regions need to have
57      * the initial position to have the backward bias.
58      */

59     public SyncDocumentRegion(Document JavaDoc doc, List JavaDoc/*<MutablePositionRegion>*/ regions) {
60         this.doc = doc;
61         this.regions = regions;
62         // Check bounds correctness and whether they are sorted
63
regionsSortPerformed = PositionRegion.isRegionsSorted(regions);
64         if (regionsSortPerformed) {
65             sortedRegions = regions;
66         } else {
67             sortedRegions = new ArrayList JavaDoc(regions);
68             Collections.sort(sortedRegions, PositionRegion.getComparator());
69         }
70     }
71     
72     public int getRegionCount() {
73         return regions.size();
74     }
75     
76     public MutablePositionRegion getRegion(int regionIndex) {
77         return (MutablePositionRegion)regions.get(regionIndex);
78     }
79
80     public int getFirstRegionStartOffset() {
81         return getRegion(0).getStartOffset();
82     }
83     
84     public int getFirstRegionEndOffset() {
85         return getRegion(0).getEndOffset();
86     }
87     
88     public int getFirstRegionLength() {
89         return getFirstRegionEndOffset() - getFirstRegionStartOffset();
90     }
91     
92     /**
93      * Get region in a sorted list of the regions.
94      *
95      * @param regionIndex of the region.
96      * @return region in a sorted list of the regions.
97      */

98     public MutablePositionRegion getSortedRegion(int regionIndex) {
99          return (MutablePositionRegion)sortedRegions.get(regionIndex);
100     }
101
102     /**
103      * Propagate text of the first region into all other regions.
104      *
105      * @param moveStartDownLength how much to move starting position
106      * down. It may be 0 to signal that the startng position should
107      * stay as is.
108      */

109     public void sync(int moveStartDownLength) {
110         if (moveStartDownLength != 0) {
111             // Move first region's start offset down
112
MutablePositionRegion firstRegion = getRegion(0);
113             try {
114                 Position JavaDoc newStartPos = doc.createPosition(
115                         firstRegion.getStartOffset() - moveStartDownLength);
116                 firstRegion.setStartPosition(newStartPos);
117                 
118             } catch (BadLocationException JavaDoc e) {
119                 ErrorManager.getDefault().notify(e);
120             }
121             
122         }
123         
124         String JavaDoc firstRegionText = getFirstRegionText();
125         if (firstRegionText != null) {
126             int regionCount = getRegionCount();
127             for (int i = 1; i < regionCount; i++) {
128                 MutablePositionRegion region = getRegion(i);
129                 int offset = region.getStartOffset();
130                 int length = region.getEndOffset() - offset;
131                 try {
132                     if (!CharSequenceUtilities.textEquals(firstRegionText, DocumentUtilities.getText(doc, offset, length))) {
133                         doc.remove(offset, length);
134                         if (firstRegionText.length() > 0) {
135                             doc.insertString(offset, firstRegionText, null);
136                         }
137                     }
138                     // Recreate the start position as the position are put together
139
Position JavaDoc newStartPos = doc.createPosition(offset);
140                     region.setStartPosition(newStartPos);
141                 } catch (BadLocationException JavaDoc e) {
142                     ErrorManager.getDefault().notify(e);
143                 }
144
145             }
146         }
147     }
148
149     private String JavaDoc getFirstRegionText() {
150         return getRegionText(0);
151     }
152     
153     private String JavaDoc getRegionText(int regionIndex) {
154         try {
155             MutablePositionRegion region = getRegion(regionIndex);
156             int offset = region.getStartOffset();
157             int length = region.getEndOffset() - offset;
158             return doc.getText(offset, length);
159         } catch (BadLocationException JavaDoc e) {
160             ErrorManager.getDefault().notify(e);
161             return null;
162         }
163     }
164     
165 }
166
Popular Tags