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 @ 42198
History | View | Annotate | Download (11.1 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 |
/**
|
38 |
* <p>
|
39 |
* <code>ExtentHistory</code> is designed for managing a history of extents.
|
40 |
* </p>
|
41 |
* <p>
|
42 |
* Note: An <i>extent</i> is a rectangular area, with information of its
|
43 |
* top-left 2D corner.
|
44 |
* </p>
|
45 |
*
|
46 |
* @author Vicente Caballero Navarro
|
47 |
*/
|
48 |
public class ExtentHistory implements Persistent { |
49 |
|
50 |
private static final String FIELD_CURRENT_EXTENT = "current"; |
51 |
private static final String FIELD_NUM_PREVIOUS = "num"; |
52 |
private static final String FIELD_NUM_NEXT = "numnext"; |
53 |
private static final String FIELD_NUM_RECORDS = "numrec"; |
54 |
private static final String FIELD_PREVIOUS_EXTENTS = "extents"; |
55 |
private static final String FIELD_NEXT_EXTENTS = "extentsNext"; |
56 |
|
57 |
/**
|
58 |
* <p>
|
59 |
* Maximum number of extents that can store.
|
60 |
* </p>
|
61 |
*/
|
62 |
private int NUMREC; |
63 |
|
64 |
/**
|
65 |
* <p>
|
66 |
* Array with the previous extents.
|
67 |
* </p>
|
68 |
*
|
69 |
* @see #hasPrevious()
|
70 |
* @see #put(Rectangle2D)
|
71 |
* @see #getPrev()
|
72 |
* @see #popPrev()
|
73 |
*/
|
74 |
private Rectangle2D[] extentsPrev; |
75 |
|
76 |
/**
|
77 |
* <p>
|
78 |
* Array with the next extents.
|
79 |
* </p>
|
80 |
*
|
81 |
* @see #hasNext()
|
82 |
* @see #putNext(Rectangle2D)
|
83 |
* @see #getNext()
|
84 |
* @see #popNext()
|
85 |
*/
|
86 |
private Rectangle2D[] extentsNext; |
87 |
|
88 |
/**
|
89 |
* <p>
|
90 |
* The current extent of the viewport.
|
91 |
* </p>
|
92 |
*/
|
93 |
private Rectangle2D currentExtent; |
94 |
|
95 |
/**
|
96 |
* <p>
|
97 |
* Number of previous extents stored.
|
98 |
* </p>
|
99 |
*
|
100 |
* @see #hasPrevious()
|
101 |
* @see #put(Rectangle2D)
|
102 |
* @see #getPrev()
|
103 |
* @see #popPrev()
|
104 |
*/
|
105 |
private int numPrev = 0; |
106 |
|
107 |
/**
|
108 |
* <p>
|
109 |
* Number of next extents stored.
|
110 |
* </p>
|
111 |
*
|
112 |
* @see #hasNext()
|
113 |
* @see #putNext(Rectangle2D)
|
114 |
* @see #getNext()
|
115 |
* @see #popNext()
|
116 |
*/
|
117 |
private int numNext = 0; |
118 |
|
119 |
/**
|
120 |
* <p>
|
121 |
* Creates a new instance of <code>ExtentsHistory</code> with an history of 10
|
122 |
* extents.
|
123 |
* </p>
|
124 |
*/
|
125 |
public ExtentHistory() {
|
126 |
this(10); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* <p>
|
131 |
* Creates a new instance of <code>ExtentsHistory</code> with an history of
|
132 |
* <code>numEntries</code> extents.
|
133 |
* </p>
|
134 |
*
|
135 |
* @param numEntries the maximum number of extents that will store the
|
136 |
* instance
|
137 |
*/
|
138 |
public ExtentHistory(int numEntries) { |
139 |
NUMREC = numEntries; |
140 |
extentsPrev = new Rectangle2D[NUMREC]; |
141 |
extentsNext = new Rectangle2D[NUMREC]; |
142 |
} |
143 |
|
144 |
/**
|
145 |
* <p>
|
146 |
* Appends the specified extent at the end of the array of previous zooms.
|
147 |
* </p>
|
148 |
*
|
149 |
* @param ext the new extent
|
150 |
*/
|
151 |
public void put(Rectangle2D ext) { |
152 |
|
153 |
// Si al cargar de la persistencia currentExtent es null ViewPort nos dar? su extent
|
154 |
if(currentExtent == null) { |
155 |
this.currentExtent = ext;
|
156 |
return;
|
157 |
} |
158 |
|
159 |
if ( !ext.equals(getPrev()) && !ext.equals(getNext()) ) {
|
160 |
clearNumNext(); |
161 |
} |
162 |
|
163 |
pushPrevious(currentExtent); |
164 |
|
165 |
this.currentExtent = ext;
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* <p>
|
170 |
* Returns <code>true</code> if there are previous extents registered.
|
171 |
* </p>
|
172 |
*
|
173 |
* @return <code>true</code> if there are previous extents registered;
|
174 |
* <code>false</code> otherwise
|
175 |
*/
|
176 |
public boolean hasPrevious() { |
177 |
return numPrev > 0; |
178 |
} |
179 |
|
180 |
/**
|
181 |
* <p>
|
182 |
* Returns <code>true</code> if there are next extents registered.
|
183 |
* </p>
|
184 |
*
|
185 |
* @return <code>true</code> if there are next extents registered;
|
186 |
* <code>false</code> otherwise
|
187 |
*/
|
188 |
public boolean hasNext() { |
189 |
return numNext > 0; |
190 |
} |
191 |
|
192 |
/**
|
193 |
* <p>
|
194 |
* Returns the last previous extent from the history.
|
195 |
* </p>
|
196 |
*
|
197 |
* @return the last previous extent from the history
|
198 |
*/
|
199 |
public Rectangle2D getPrev() { |
200 |
if (numPrev <= 0) { |
201 |
return null; |
202 |
} |
203 |
Rectangle2D ext = extentsPrev[numPrev - 1]; |
204 |
|
205 |
return ext;
|
206 |
} |
207 |
|
208 |
/**
|
209 |
* <p>
|
210 |
* Returns the last next extent from the history.
|
211 |
* </p>
|
212 |
*
|
213 |
* @return the last next extent from the history
|
214 |
*/
|
215 |
public Rectangle2D getNext() { |
216 |
if (numNext <= 0) { |
217 |
return null; |
218 |
} |
219 |
|
220 |
Rectangle2D ext = extentsNext[numNext - 1]; |
221 |
return ext;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* <p>
|
226 |
* Extracts (removing) the last previous extent from the history.
|
227 |
* </p>
|
228 |
*
|
229 |
* @return last previous extent from the history
|
230 |
*/
|
231 |
private Rectangle2D popPrev() { |
232 |
if (numPrev <= 0) { |
233 |
return null; |
234 |
} |
235 |
|
236 |
Rectangle2D ext = extentsPrev[--numPrev];
|
237 |
return ext;
|
238 |
} |
239 |
|
240 |
/**
|
241 |
* <p>
|
242 |
* Extracts (removing) the last next extent from the history.
|
243 |
* </p>
|
244 |
*
|
245 |
* @return last next extent from the history
|
246 |
*/
|
247 |
private Rectangle2D popNext() { |
248 |
if (numNext <= 0) { |
249 |
return null; |
250 |
} |
251 |
|
252 |
Rectangle2D ext = extentsNext[--numNext];
|
253 |
return ext;
|
254 |
} |
255 |
|
256 |
/**
|
257 |
* <p>
|
258 |
* Sets to zero the number of previous extents from the history.
|
259 |
* </p>
|
260 |
*/
|
261 |
private void clearNumPrev() { |
262 |
numPrev = 0;
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* <p>
|
267 |
* Sets to zero the number of next extents from the history.
|
268 |
* </p>
|
269 |
*/
|
270 |
private void clearNumNext() { |
271 |
numNext = 0;
|
272 |
} |
273 |
|
274 |
/**
|
275 |
* <p>
|
276 |
* Adds the current extent to the next extent history and sets the last
|
277 |
* previous extent as the new current extent.
|
278 |
* </p>
|
279 |
*
|
280 |
* @return the last previous extent as the new current extent
|
281 |
*/
|
282 |
public Rectangle2D setPreviousExtent() { |
283 |
|
284 |
if (currentExtent != null) { |
285 |
pushNext(currentExtent); |
286 |
} |
287 |
currentExtent = popPrev(); |
288 |
return currentExtent;
|
289 |
} |
290 |
|
291 |
/**
|
292 |
* <p>
|
293 |
* Adds the specified extent to the next extent history.
|
294 |
* If the array of next extents is complete, loses the
|
295 |
* first extent of the array and adds the new one at the end.
|
296 |
* </p>
|
297 |
*
|
298 |
*/
|
299 |
private void pushNext(Rectangle2D ext) { |
300 |
if (numNext < (NUMREC)) {
|
301 |
extentsNext[numNext] = ext; |
302 |
numNext = numNext + 1;
|
303 |
} |
304 |
else {
|
305 |
for (int i = 0; i < (NUMREC - 1); i++) { |
306 |
extentsNext[i] = extentsNext[i + 1];
|
307 |
} |
308 |
extentsNext[numNext - 1] = ext;
|
309 |
} |
310 |
} |
311 |
|
312 |
/**
|
313 |
* <p>
|
314 |
* Adds the current extent to the previous extent history and sets the last
|
315 |
* next extent as the new current extent.
|
316 |
* </p>
|
317 |
*
|
318 |
* @return the last next extent as the new current extent
|
319 |
*/
|
320 |
public Rectangle2D setNextExtent() { |
321 |
|
322 |
if (currentExtent != null) { |
323 |
pushPrevious(currentExtent); |
324 |
|
325 |
} |
326 |
currentExtent = popNext(); |
327 |
return currentExtent;
|
328 |
} |
329 |
|
330 |
/**
|
331 |
* <p>
|
332 |
* Adds the specified extent to the previous extent history.
|
333 |
* If the array of previous extents is complete, loses the
|
334 |
* first extent of the array and adds the new one at the end.
|
335 |
* </p>
|
336 |
*
|
337 |
*/
|
338 |
private void pushPrevious(Rectangle2D ext) { |
339 |
if (numPrev < (NUMREC)) {
|
340 |
extentsPrev[numPrev] = ext; |
341 |
numPrev++; |
342 |
} else {
|
343 |
for (int i = 0; i < (NUMREC - 1); i++) { |
344 |
extentsPrev[i] = extentsPrev[i + 1];
|
345 |
} |
346 |
extentsPrev[numPrev - 1] = ext;
|
347 |
} |
348 |
} |
349 |
|
350 |
/**
|
351 |
* <p>
|
352 |
* Returns the current extent.
|
353 |
* </p>
|
354 |
*
|
355 |
* @return the current extent
|
356 |
*/
|
357 |
public Rectangle2D getCurrent() { |
358 |
return currentExtent;
|
359 |
} |
360 |
|
361 |
public void loadFromState(PersistentState state) throws PersistenceException { |
362 |
|
363 |
if (state.hasValue("num")) { |
364 |
numPrev = state.getInt("num");
|
365 |
} |
366 |
else {
|
367 |
clearNumPrev(); |
368 |
} |
369 |
|
370 |
if (state.hasValue("numnext")) { |
371 |
numNext = state.getInt("numnext");
|
372 |
} |
373 |
else {
|
374 |
clearNumNext(); |
375 |
} |
376 |
|
377 |
if (state.hasValue("numrec")) { |
378 |
NUMREC = state.getInt("numrec");
|
379 |
} |
380 |
else {
|
381 |
NUMREC = 10;
|
382 |
} |
383 |
|
384 |
if (state.hasValue("extents")) { |
385 |
extentsPrev = (Rectangle2D[]) state |
386 |
.getArray("extents", Rectangle2D.class); |
387 |
} |
388 |
else {
|
389 |
extentsPrev = new Rectangle2D[NUMREC]; |
390 |
clearNumPrev(); |
391 |
} |
392 |
|
393 |
if (state.hasValue("extentsNext")) { |
394 |
extentsNext = (Rectangle2D[]) state.getArray("extentsNext", |
395 |
Rectangle2D.class);
|
396 |
} |
397 |
else {
|
398 |
extentsNext = new Rectangle2D[NUMREC]; |
399 |
clearNumNext(); |
400 |
} |
401 |
|
402 |
if (state.hasValue("current")) { |
403 |
currentExtent = (Rectangle2D) state.get("current"); |
404 |
} |
405 |
else {
|
406 |
currentExtent = null;
|
407 |
} |
408 |
} |
409 |
|
410 |
/**
|
411 |
* <p>
|
412 |
* Returns information of this object. All information is stored as
|
413 |
* properties:<br>
|
414 |
* </p>
|
415 |
* <p>
|
416 |
* <b>Properties:</b>
|
417 |
* <ul>
|
418 |
* <li><i>className</i>: name of this class.
|
419 |
* <li><i>num</i>: number of extents registered.
|
420 |
* <li><i>numrec</i>: maximum number of extents that can register.
|
421 |
* <li><i>extents</i>: .
|
422 |
* </ul>
|
423 |
* </p>
|
424 |
*/
|
425 |
public void saveToState(PersistentState state) throws PersistenceException { |
426 |
|
427 |
state.set(FIELD_NUM_PREVIOUS, numPrev); |
428 |
state.set(FIELD_NUM_NEXT, numNext); |
429 |
state.set(FIELD_NUM_RECORDS, NUMREC); |
430 |
state.set(FIELD_PREVIOUS_EXTENTS, extentsPrev); |
431 |
state.set(FIELD_NEXT_EXTENTS, extentsNext); |
432 |
state.set(FIELD_CURRENT_EXTENT, currentExtent); |
433 |
} |
434 |
|
435 |
public static class RegisterPersistence implements Callable { |
436 |
|
437 |
public Object call() { |
438 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
439 |
DynStruct definition = manager.addDefinition(ExtentHistory.class, |
440 |
"ExtentHistory", "ExtentHistory Persistence definition", null, null); |
441 |
definition.addDynFieldInt(FIELD_NUM_PREVIOUS).setMandatory(true);
|
442 |
definition.addDynFieldInt(FIELD_NUM_NEXT).setMandatory(true);
|
443 |
definition.addDynFieldInt(FIELD_NUM_RECORDS).setMandatory(true);
|
444 |
definition.addDynFieldArray(FIELD_PREVIOUS_EXTENTS).setClassOfItems(Rectangle2D.class)
|
445 |
.setMandatory(true);
|
446 |
definition.addDynFieldArray(FIELD_NEXT_EXTENTS) |
447 |
.setClassOfItems(Rectangle2D.class).setMandatory(true); |
448 |
definition.addDynFieldObject(FIELD_CURRENT_EXTENT) |
449 |
.setClassOfValue(Rectangle2D.class).setMandatory(false); |
450 |
|
451 |
return Boolean.TRUE; |
452 |
} |
453 |
} |
454 |
} |