KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > buffer > PositionManager


1 /*
2  * PositionManager.java - Manages positions
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2001, 2005 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.buffer;
24
25 //{{{ Imports
26
import javax.swing.text.Position JavaDoc;
27 import java.util.*;
28 import org.gjt.sp.util.Log;
29 //}}}
30

31 /**
32  * A class internal to jEdit's document model. You should not use it
33  * directly.
34  *
35  * @author Slava Pestov
36  * @version $Id: PositionManager.java 5339 2006-01-25 23:12:07Z spestov $
37  * @since jEdit 4.2pre3
38  */

39 public class PositionManager
40 {
41     //{{{ PositionManager constructor
42
public PositionManager(JEditBuffer buffer)
43     {
44         this.buffer = buffer;
45     } //}}}
46

47     //{{{ createPosition() method
48
public synchronized Position JavaDoc createPosition(int offset)
49     {
50         PosBottomHalf bh = new PosBottomHalf(offset);
51         PosBottomHalf existing = (PosBottomHalf)positions.get(bh);
52         if(existing == null)
53         {
54             positions.put(bh,bh);
55             existing = bh;
56         }
57
58         return new PosTopHalf(existing);
59     } //}}}
60

61     //{{{ contentInserted() method
62
public synchronized void contentInserted(int offset, int length)
63     {
64         if(positions.size() == 0)
65             return;
66
67         /* get all positions from offset to the end, inclusive */
68         Iterator iter = positions.tailMap(new PosBottomHalf(offset))
69             .keySet().iterator();
70
71         iteration = true;
72         while(iter.hasNext())
73         {
74             ((PosBottomHalf)iter.next())
75                 .contentInserted(offset,length);
76         }
77         iteration = false;
78     } //}}}
79

80     //{{{ contentRemoved() method
81
public synchronized void contentRemoved(int offset, int length)
82     {
83         if(positions.size() == 0)
84             return;
85
86         /* get all positions from offset to the end, inclusive */
87         Iterator iter = positions.tailMap(new PosBottomHalf(offset))
88             .keySet().iterator();
89
90         iteration = true;
91         while(iter.hasNext())
92         {
93             ((PosBottomHalf)iter.next())
94                 .contentRemoved(offset,length);
95         }
96         iteration = false;
97
98     } //}}}
99

100     boolean iteration;
101
102     //{{{ Private members
103
private JEditBuffer buffer;
104     private SortedMap positions = new TreeMap();
105     //}}}
106

107     //{{{ Inner classes
108

109     //{{{ PosTopHalf class
110
class PosTopHalf implements Position JavaDoc
111     {
112         PosBottomHalf bh;
113
114         //{{{ PosTopHalf constructor
115
PosTopHalf(PosBottomHalf bh)
116         {
117             this.bh = bh;
118             bh.ref();
119         } //}}}
120

121         //{{{ getOffset() method
122
public int getOffset()
123         {
124             return bh.offset;
125         } //}}}
126

127         //{{{ finalize() method
128
protected void finalize()
129         {
130             synchronized(PositionManager.this)
131             {
132                 bh.unref();
133             }
134         } //}}}
135
} //}}}
136

137     //{{{ PosBottomHalf class
138
class PosBottomHalf implements Comparable JavaDoc
139     {
140         int offset;
141         int ref;
142
143         //{{{ PosBottomHalf constructor
144
PosBottomHalf(int offset)
145         {
146             this.offset = offset;
147         } //}}}
148

149         //{{{ ref() method
150
void ref()
151         {
152             ref++;
153         } //}}}
154

155         //{{{ unref() method
156
void unref()
157         {
158             if(--ref == 0)
159                 positions.remove(this);
160         } //}}}
161

162         //{{{ contentInserted() method
163
void contentInserted(int offset, int length)
164         {
165             if(offset > this.offset)
166                 throw new ArrayIndexOutOfBoundsException JavaDoc();
167             this.offset += length;
168             checkInvariants();
169         } //}}}
170

171         //{{{ contentRemoved() method
172
void contentRemoved(int offset, int length)
173         {
174             if(offset > this.offset)
175                 throw new ArrayIndexOutOfBoundsException JavaDoc();
176             if(this.offset <= offset + length)
177                 this.offset = offset;
178             else
179                 this.offset -= length;
180             checkInvariants();
181         } //}}}
182

183         //{{{ equals() method
184
public boolean equals(Object JavaDoc o)
185         {
186             if(!(o instanceof PosBottomHalf))
187                 return false;
188
189             return ((PosBottomHalf)o).offset == offset;
190         } //}}}
191

192         //{{{ compareTo() method
193
public int compareTo(Object JavaDoc o)
194         {
195             if(iteration)
196                 Log.log(Log.ERROR,this,"Consistency failure");
197             return offset - ((PosBottomHalf)o).offset;
198         } //}}}
199

200         //{{{ checkInvariants() method
201
private void checkInvariants()
202         {
203             if(offset < 0 || offset > buffer.getLength())
204                 throw new ArrayIndexOutOfBoundsException JavaDoc();
205         } //}}}
206
} //}}}
207

208     //}}}
209
}
210
Popular Tags