Statistics
| Revision:

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 @ 42192

History | View | Annotate | Download (10.6 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 in the history.
195
   * </p>
196
   *
197
   * @return the last previous extent in 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 in the history.
211
   * </p>
212
   *
213
   * @return the last next extent in 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 in 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 in 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 extents 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
  private void pushNext(Rectangle2D ext) {
292
    if (numNext < (NUMREC)) {
293
      extentsNext[numNext] = ext;
294
      numNext = numNext + 1;
295
    }
296
    else {
297
      for (int i = 0; i < (NUMREC - 1); i++) {
298
        extentsNext[i] = extentsNext[i + 1];
299
      }
300
      extentsNext[numNext - 1] = ext;
301
    }
302
  }
303

    
304
  /**
305
   * <p>
306
   * Adds the current extent to the previous extents history and sets the last
307
   * next extent as the new current extent.
308
   * </p>
309
   * 
310
   * @return the last next extent as the new current extent
311
   */
312
  public Rectangle2D setNextExtent() {
313

    
314
    if (currentExtent != null) {
315
      pushPrevious(currentExtent);
316
      
317
    }
318
    currentExtent = popNext();
319
    return currentExtent;
320
  }
321

    
322
  private void pushPrevious(Rectangle2D ext) {
323
    if (numPrev < (NUMREC)) {
324
      extentsPrev[numPrev] = ext;
325
      numPrev++;
326
    } else {
327
      for (int i = 0; i < (NUMREC - 1); i++) {
328
        extentsPrev[i] = extentsPrev[i + 1];
329
      }
330
      extentsPrev[numPrev - 1] = ext;
331
    }
332
  }
333
  
334
  public Rectangle2D getCurrent() {
335
    return currentExtent;
336
  }
337

    
338
  public void loadFromState(PersistentState state) throws PersistenceException {
339

    
340
    if (state.hasValue("num")) {
341
      numPrev = state.getInt("num");
342
    }
343
    else {
344
      clearNumPrev();
345
    }
346

    
347
    if (state.hasValue("numnext")) {
348
      numNext = state.getInt("numnext");
349
    }
350
    else {
351
      clearNumNext();
352
    }
353

    
354
    if (state.hasValue("numrec")) {
355
      NUMREC = state.getInt("numrec");
356
    }
357
    else {
358
      NUMREC = 10;
359
    }
360

    
361
    if (state.hasValue("extents")) {
362
      extentsPrev = (Rectangle2D[]) state
363
          .getArray("extents", Rectangle2D.class);
364
    }
365
    else {
366
      extentsPrev = new Rectangle2D[NUMREC];
367
      clearNumPrev();
368
    }
369

    
370
    if (state.hasValue("extentsNext")) {
371
      extentsNext = (Rectangle2D[]) state.getArray("extentsNext",
372
          Rectangle2D.class);
373
    }
374
    else {
375
      extentsNext = new Rectangle2D[NUMREC];
376
      clearNumNext();
377
    }
378
    
379
    if (state.hasValue("current")) {
380
      currentExtent = (Rectangle2D) state.get("current");
381
    }
382
    else {
383
      currentExtent = null;
384
    }
385
  }
386

    
387
  /**
388
   * <p>
389
   * Returns information of this object. All information is stored as
390
   * properties:<br>
391
   * </p>
392
   * <p>
393
   * <b>Properties:</b>
394
   * <ul>
395
   * <li><i>className</i>: name of this class.
396
   * <li><i>num</i>: number of extents registered.
397
   * <li><i>numrec</i>: maximum number of extents that can register.
398
   * <li><i>extents</i>: .
399
   * </ul>
400
   * </p>
401
   */
402
  public void saveToState(PersistentState state) throws PersistenceException {
403

    
404
    state.set(FIELD_NUM_PREVIOUS, numPrev);
405
    state.set(FIELD_NUM_NEXT, numNext);
406
    state.set(FIELD_NUM_RECORDS, NUMREC);
407
    state.set(FIELD_PREVIOUS_EXTENTS, extentsPrev);
408
    state.set(FIELD_NEXT_EXTENTS, extentsNext);
409
    state.set(FIELD_CURRENT_EXTENT, currentExtent);
410
  }
411

    
412
  public static class RegisterPersistence implements Callable {
413

    
414
    public Object call() {
415
      PersistenceManager manager = ToolsLocator.getPersistenceManager();
416
      DynStruct definition = manager.addDefinition(ExtentHistory.class,
417
          "ExtentHistory", "ExtentHistory Persistence definition", null, null);
418
      definition.addDynFieldInt(FIELD_NUM_PREVIOUS).setMandatory(true);
419
      definition.addDynFieldInt(FIELD_NUM_NEXT).setMandatory(true);
420
      definition.addDynFieldInt(FIELD_NUM_RECORDS).setMandatory(true);
421
      definition.addDynFieldArray(FIELD_PREVIOUS_EXTENTS).setClassOfItems(Rectangle2D.class)
422
          .setMandatory(true);
423
      definition.addDynFieldArray(FIELD_NEXT_EXTENTS)
424
          .setClassOfItems(Rectangle2D.class).setMandatory(true);
425
      definition.addDynFieldObject(FIELD_CURRENT_EXTENT)
426
      .setClassOfValue(Rectangle2D.class).setMandatory(false);
427

    
428
      return Boolean.TRUE;
429
    }
430
  }
431
}