KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > swing > PositionRegion


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.util.swing;
21
22 import java.util.Comparator JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import javax.swing.text.Position JavaDoc;
27
28 /**
29  * A pair of positions delimiting a text region in a swing document.
30  * <br/>
31  * At all times it should be satisfied that
32  * {@link #getStartOffset()} &lt;= {@link #getEndOffset()}.
33  *
34  * @author Miloslav Metelka
35  * @since 1.6
36  */

37
38 public class PositionRegion {
39
40     /** Copmarator for position regions */
41     private static Comparator JavaDoc<PositionRegion> comparator;
42     
43     /**
44      * Get comparator for position regions comparing start offsets
45      * of the two given regions.
46      *
47      * @return non-null comparator comparing the start offsets of the two given
48      * regions.
49      */

50     public static final Comparator JavaDoc<PositionRegion> getComparator() {
51         if (comparator == null) {
52             comparator = new Comparator JavaDoc<PositionRegion>() {
53                 public int compare(PositionRegion pr1, PositionRegion pr2) {
54                     return pr1.getStartOffset() - pr2.getStartOffset();
55                 }
56             };
57         }
58         return comparator;
59     }
60
61     /**
62      * Create a fixed position instance that just wraps a given integer offset.
63      * <br/>
64      * This may be useful for situations where a position needs to be used
65      * but the document is not available yet. Once the document becomes
66      * available the regular position instance (over an existing document)
67      * may be used instead.
68      *
69      * @param offset &gt;=0 offset at which the position should be created.
70      * @since 1.10
71      */

72     public static Position JavaDoc createFixedPosition(final int offset) {
73         if (offset < 0) {
74             throw new IllegalArgumentException JavaDoc("offset < 0");
75         }
76         return new Position JavaDoc() {
77             public int getOffset() {
78                 return offset;
79             }
80         };
81     }
82
83     /**
84      * Check whether a list of position regions is sorted
85      * according the start offsets of the regions.
86      *
87      * @param positionRegionList list of the regions to be compared.
88      * @return true if the regions are sorted according to the starting offset
89      * of the given regions or false otherwise.
90      */

91     public static boolean isRegionsSorted(List JavaDoc<PositionRegion> positionRegionList) {
92         for (int i = positionRegionList.size() - 2; i >= 0; i--) {
93             if (getComparator().compare(positionRegionList.get(i),
94                     positionRegionList.get(i + 1)) > 0) {
95                 return false;
96             }
97         }
98         return true;
99     }
100
101     private Position JavaDoc startPosition;
102     
103     private Position JavaDoc endPosition;
104     
105     /**
106      * Construct new position region.
107      *
108      * @param startPosition non-null start position of the region &lt;= end position.
109      * @param endPosition non-null end position of the region &gt;= start position.
110      */

111     public PositionRegion(Position JavaDoc startPosition, Position JavaDoc endPosition) {
112         assertPositionsValid(startPosition, endPosition);
113         this.startPosition = startPosition;
114         this.endPosition = endPosition;
115     }
116     
117     /**
118      * Construct new position region based on the knowledge
119      * of the document and starting and ending offset.
120      */

121     public PositionRegion(Document JavaDoc doc, int startOffset, int endOffset) throws BadLocationException JavaDoc {
122         this(doc.createPosition(startOffset), doc.createPosition(endOffset));
123     }
124     
125     /**
126      * Get starting offset of this region.
127      *
128      * @return &gt;=0 starting offset of this region.
129      */

130     public final int getStartOffset() {
131         return startPosition.getOffset();
132     }
133     
134     /**
135      * Get starting position of this region.
136      *
137      * @return non-null starting position of this region.
138      */

139     public final Position JavaDoc getStartPosition() {
140         return startPosition;
141     }
142     
143     /**
144      * Get ending offset of this region.
145      *
146      * @return &gt;=0 ending offset of this region.
147      */

148     public final int getEndOffset() {
149         return endPosition.getOffset();
150     }
151     
152     /**
153      * Get ending position of this region.
154      *
155      * @return non-null ending position of this region.
156      */

157     public final Position JavaDoc getEndPosition() {
158         return endPosition;
159     }
160     
161     /**
162      * Get length of this region.
163      *
164      * @return &gt;=0 length of this region
165      * computed as <code>getEndOffset() - getStartOffset()</code>.
166      */

167     public final int getLength() {
168         return getEndOffset() - getStartOffset();
169     }
170
171     /**
172      * {@link MutablePositionRegion} uses this package private method
173      * to set a new start position of this region.
174      */

175     void resetImpl(Position JavaDoc startPosition, Position JavaDoc endPosition) {
176         assertPositionsValid(startPosition, endPosition);
177         this.startPosition = startPosition;
178         this.endPosition = endPosition;
179     }
180     
181     /**
182      * {@link MutablePositionRegion} uses this package private method
183      * to set a new start position of this region.
184      */

185     void setStartPositionImpl(Position JavaDoc startPosition) {
186         assertPositionsValid(startPosition, endPosition);
187         this.startPosition = startPosition;
188     }
189
190     /**
191      * {@link MutablePositionRegion} uses this package private method
192      * to set a new start position of this region.
193      */

194     void setEndPositionImpl(Position JavaDoc endPosition) {
195         assertPositionsValid(startPosition, endPosition);
196         this.endPosition = endPosition;
197     }
198
199     private static void assertPositionsValid(Position JavaDoc startPos, Position JavaDoc endPos) {
200         assert (startPos.getOffset() <= endPos.getOffset())
201             : "startPosition=" + startPos.getOffset() + " > endPosition=" // NOI18N
202
+ endPos;
203     }
204
205 }
206
Popular Tags