svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / ExtentHistory.java @ 42173
History | View | Annotate | Download (6.96 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.mapcontext; |
25 |
|
26 |
import java.awt.geom.Rectangle2D; |
27 |
|
28 |
import org.gvsig.tools.ToolsLocator; |
29 |
import org.gvsig.tools.dynobject.DynStruct; |
30 |
import org.gvsig.tools.persistence.PersistenceManager; |
31 |
import org.gvsig.tools.persistence.Persistent; |
32 |
import org.gvsig.tools.persistence.PersistentState; |
33 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
34 |
import org.gvsig.tools.util.Callable; |
35 |
|
36 |
/**
|
37 |
* <p>
|
38 |
* <code>ExtentHistory</code> is designed for managing a history of extents.
|
39 |
* </p>
|
40 |
* <p>
|
41 |
* Note: An <i>extent</i> is a rectangular area, with information of its
|
42 |
* top-left 2D corner.
|
43 |
* </p>
|
44 |
*
|
45 |
* @author Vicente Caballero Navarro
|
46 |
*/
|
47 |
public class ExtentHistory implements Persistent { |
48 |
|
49 |
/**
|
50 |
* <p>
|
51 |
* Maximum number of extents that can store.
|
52 |
* </p>
|
53 |
*/
|
54 |
private int NUMREC; |
55 |
|
56 |
/**
|
57 |
* <p>
|
58 |
* Array with the extents.
|
59 |
* </p>
|
60 |
*
|
61 |
* @see #hasPrevious()
|
62 |
* @see #put(Rectangle2D)
|
63 |
* @see #get()
|
64 |
* @see #removePrev()
|
65 |
*/
|
66 |
private Rectangle2D[] extents; |
67 |
|
68 |
private Rectangle2D[] extentsNext; |
69 |
|
70 |
/**
|
71 |
* <p>
|
72 |
* Number of extents stored.
|
73 |
* </p>
|
74 |
*
|
75 |
* @see #hasPrevious()
|
76 |
* @see #put(Rectangle2D)
|
77 |
* @see #get()
|
78 |
* @see #removePrev()
|
79 |
*/
|
80 |
private int num = 0; |
81 |
|
82 |
private int numNext = 0; |
83 |
|
84 |
/**
|
85 |
* <p>
|
86 |
* Creates a new instance of <code>ExtentsHistory</code> with an history of 10
|
87 |
* extents.
|
88 |
* </p>
|
89 |
*/
|
90 |
public ExtentHistory() {
|
91 |
this(10); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* <p>
|
96 |
* Creates a new instance of <code>ExtentsHistory</code> with an history of
|
97 |
* <code>numEntries</code> extents.
|
98 |
* </p>
|
99 |
*
|
100 |
* @param numEntries the maximum number of extents that will store the
|
101 |
* instance
|
102 |
*/
|
103 |
public ExtentHistory(int numEntries) { |
104 |
NUMREC = numEntries; |
105 |
extents = new Rectangle2D[NUMREC]; |
106 |
extentsNext = new Rectangle2D[NUMREC]; |
107 |
} |
108 |
|
109 |
/**
|
110 |
* <p>
|
111 |
* Appends the specified extent to the end of this history.
|
112 |
* </p>
|
113 |
*
|
114 |
* @param ext the new extent
|
115 |
* @see #get()
|
116 |
* @see #hasPrevious()
|
117 |
*/
|
118 |
public void put(Rectangle2D ext) { |
119 |
if ((ext != null) && ((num < 1) || (ext != extents[num - 1]))) { |
120 |
if (num < (NUMREC)) {
|
121 |
extents[num] = ext; |
122 |
num = num + 1;
|
123 |
} |
124 |
else {
|
125 |
for (int i = 0; i < (NUMREC - 1); i++) { |
126 |
extents[i] = extents[i + 1];
|
127 |
} |
128 |
extents[num - 1] = ext;
|
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
public void putNext(Rectangle2D ext) { |
134 |
if ((ext != null) && ((numNext < 1) || (ext != extentsNext[numNext - 1])) ) { |
135 |
if (numNext < (NUMREC)) {
|
136 |
extentsNext[numNext] = ext; |
137 |
numNext++; |
138 |
} else {
|
139 |
for (int i = 0; i < (NUMREC - 1); i++) { |
140 |
extentsNext[i] = extentsNext[i+1];
|
141 |
} |
142 |
extentsNext[numNext - 1] = ext;
|
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
/**
|
148 |
* <p>
|
149 |
* Returns <code>true</code> if there are extents registered.
|
150 |
* </p>
|
151 |
*
|
152 |
* @return <code>true</code> if there are extents registered;
|
153 |
* <code>false</code> otherwise
|
154 |
* @see #put(Rectangle2D)
|
155 |
* @see #removePrev()
|
156 |
* @see #get()
|
157 |
*/
|
158 |
public boolean hasPrevious() { |
159 |
return num > 0; |
160 |
} |
161 |
|
162 |
public boolean hasNext() { |
163 |
return numNext > 0; |
164 |
} |
165 |
|
166 |
/**
|
167 |
* <p>
|
168 |
* Returns the last extent in the history.
|
169 |
* </p>
|
170 |
*
|
171 |
* @return the last extent in the history
|
172 |
* @see #put(Rectangle2D)
|
173 |
* @see #getXMLEntity()
|
174 |
*/
|
175 |
public Rectangle2D get() { |
176 |
if (num <= 0) { |
177 |
return null; |
178 |
} |
179 |
Rectangle2D ext = extents[num - 1]; |
180 |
|
181 |
return ext;
|
182 |
} |
183 |
|
184 |
public Rectangle2D getNext() { |
185 |
if (numNext <= 0) { |
186 |
return null; |
187 |
} |
188 |
|
189 |
Rectangle2D ext = extentsNext[numNext - 1]; |
190 |
|
191 |
return ext;
|
192 |
} |
193 |
|
194 |
/**
|
195 |
* <p>
|
196 |
* Extracts (removing) the last extent from the history.
|
197 |
* </p>
|
198 |
*
|
199 |
* @return last extent in the history
|
200 |
* @see #hasPrevious()
|
201 |
*/
|
202 |
public Rectangle2D removePrev() { |
203 |
if (num <= 0) { |
204 |
return null; |
205 |
} |
206 |
|
207 |
Rectangle2D ext = extents[--num];
|
208 |
return ext;
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* <p>
|
213 |
* Extracts (removing) the last extent from the history.
|
214 |
* </p>
|
215 |
*
|
216 |
* @return last extent in the history
|
217 |
* @see #hasNext()
|
218 |
*/
|
219 |
public Rectangle2D removeNext() { |
220 |
if (numNext <= 0) { |
221 |
return null; |
222 |
} |
223 |
|
224 |
Rectangle2D ext = extentsNext[--numNext];
|
225 |
return ext;
|
226 |
} |
227 |
|
228 |
public void clear() { |
229 |
numNext = 0;
|
230 |
} |
231 |
|
232 |
public void loadFromState(PersistentState state) throws PersistenceException { |
233 |
|
234 |
num = state.getInt("num");
|
235 |
NUMREC = state.getInt("numrec");
|
236 |
extents = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class); |
237 |
extentsNext = (Rectangle2D[]) state.getArray("extentsNext", |
238 |
Rectangle2D.class);
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* <p>
|
243 |
* Returns information of this object. All information is stored as
|
244 |
* properties:<br>
|
245 |
* </p>
|
246 |
* <p>
|
247 |
* <b>Properties:</b>
|
248 |
* <ul>
|
249 |
* <li><i>className</i>: name of this class.
|
250 |
* <li><i>num</i>: number of extents registered.
|
251 |
* <li><i>numrec</i>: maximum number of extents that can register.
|
252 |
* <li><i>extents</i>: .
|
253 |
* </ul>
|
254 |
* </p>
|
255 |
*/
|
256 |
public void saveToState(PersistentState state) throws PersistenceException { |
257 |
|
258 |
state.set("num", num);
|
259 |
state.set("numrec", NUMREC);
|
260 |
state.set("extents", extents);
|
261 |
state.set("extentsNext", extentsNext);
|
262 |
} |
263 |
|
264 |
public static class RegisterPersistence implements Callable { |
265 |
|
266 |
public Object call() { |
267 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
268 |
DynStruct definition = manager.addDefinition(ExtentHistory.class, |
269 |
"ExtentHistory", "ExtentHistory Persistence definition", null, null); |
270 |
definition.addDynFieldInt("num").setMandatory(true); |
271 |
definition.addDynFieldInt("numrec").setMandatory(true); |
272 |
definition.addDynFieldArray("extents").setClassOfItems(Rectangle2D.class) |
273 |
.setMandatory(true);
|
274 |
definition.addDynFieldArray("extentsNext")
|
275 |
.setClassOfItems(Rectangle2D.class).setMandatory(true); |
276 |
return Boolean.TRUE; |
277 |
} |
278 |
} |
279 |
} |