001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.gsfret;
043:
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.List;
047: import javax.swing.text.BadLocationException;
048: import javax.swing.text.Document;
049: import javax.swing.text.Position;
050: import org.netbeans.lib.editor.util.CharSequenceUtilities;
051: import org.netbeans.lib.editor.util.swing.DocumentUtilities;
052: import org.netbeans.lib.editor.util.swing.MutablePositionRegion;
053: import org.netbeans.lib.editor.util.swing.PositionRegion;
054: import org.openide.ErrorManager;
055:
056: /**Copied from editor/codetemplates and adjusted for the needs of the instant rename.
057: *
058: * Maintain the same text in the selected regions of the text document.
059: *
060: * @author Miloslav Metelka
061: */
062: public final class SyncDocumentRegion {
063:
064: private Document doc;
065:
066: private List<? extends MutablePositionRegion> regions;
067:
068: private List<? extends MutablePositionRegion> sortedRegions;
069:
070: private boolean regionsSortPerformed;
071:
072: /**
073: * Construct synchronized document regions.
074: *
075: * @param doc document on which to operate.
076: * @param regions regions that should be kept synchronized.
077: * The first region is the master. All the regions need to have
078: * the initial position to have the backward bias.
079: */
080: public SyncDocumentRegion(Document doc,
081: List<? extends MutablePositionRegion> regions) {
082: this .doc = doc;
083: this .regions = regions;
084: // Check bounds correctness and whether they are sorted
085: regionsSortPerformed = PositionRegion.isRegionsSorted(regions);
086: if (regionsSortPerformed) {
087: sortedRegions = regions;
088: } else {
089: sortedRegions = new ArrayList<MutablePositionRegion>(
090: regions);
091: Collections.sort(sortedRegions, PositionRegion
092: .getComparator());
093: }
094: }
095:
096: public int getRegionCount() {
097: return regions.size();
098: }
099:
100: public MutablePositionRegion getRegion(int regionIndex) {
101: return regions.get(regionIndex);
102: }
103:
104: public int getFirstRegionStartOffset() {
105: return getRegion(0).getStartOffset();
106: }
107:
108: public int getFirstRegionEndOffset() {
109: return getRegion(0).getEndOffset();
110: }
111:
112: public int getFirstRegionLength() {
113: return getFirstRegionEndOffset() - getFirstRegionStartOffset();
114: }
115:
116: /**
117: * Get region in a sorted list of the regions.
118: *
119: * @param regionIndex of the region.
120: * @return region in a sorted list of the regions.
121: */
122: public MutablePositionRegion getSortedRegion(int regionIndex) {
123: return sortedRegions.get(regionIndex);
124: }
125:
126: /**
127: * Propagate text of the first region into all other regions.
128: *
129: * @param moveStartDownLength how much to move starting position
130: * down. It may be 0 to signal that the startng position should
131: * stay as is.
132: */
133: public void sync(int moveStartDownLength) {
134: if (moveStartDownLength != 0) {
135: // Move first region's start offset down
136: MutablePositionRegion firstRegion = getRegion(0);
137: try {
138: Position newStartPos = doc.createPosition(firstRegion
139: .getStartOffset()
140: - moveStartDownLength);
141: firstRegion.setStartPosition(newStartPos);
142:
143: } catch (BadLocationException e) {
144: ErrorManager.getDefault().notify(e);
145: }
146:
147: }
148:
149: String firstRegionText = getFirstRegionText();
150: if (firstRegionText != null) {
151: int regionCount = getRegionCount();
152: for (int i = 1; i < regionCount; i++) {
153: MutablePositionRegion region = getRegion(i);
154: int offset = region.getStartOffset();
155: int length = region.getEndOffset() - offset;
156: try {
157: if (!CharSequenceUtilities.textEquals(
158: firstRegionText, DocumentUtilities.getText(
159: doc, offset, length))) {
160: if (firstRegionText.length() > 0) {
161: doc.insertString(offset, firstRegionText,
162: null);
163: }
164: doc.remove(offset + firstRegionText.length(),
165: length);
166: }
167: } catch (BadLocationException e) {
168: ErrorManager.getDefault().notify(e);
169: }
170:
171: }
172: }
173: }
174:
175: private String getFirstRegionText() {
176: return getRegionText(0);
177: }
178:
179: private String getRegionText(int regionIndex) {
180: try {
181: MutablePositionRegion region = getRegion(regionIndex);
182: int offset = region.getStartOffset();
183: int length = region.getEndOffset() - offset;
184: return doc.getText(offset, length);
185: } catch (BadLocationException e) {
186: ErrorManager.getDefault().notify(e);
187: return null;
188: }
189: }
190:
191: }
|