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

History | View | Annotate | Download (11.9 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
        } else {
366
            clearNumPrev();
367
        }
368

    
369
        if (state.hasValue("numnext")) {
370
            numNext = state.getInt("numnext");
371
        } else {
372
            clearNumNext();
373
        }
374

    
375
        if (state.hasValue("numrec")) {
376
            NUMREC = state.getInt("numrec");
377
        } else {
378
            NUMREC = 10;
379
        }
380

    
381
        extentsPrev = new Rectangle2D[NUMREC];
382
        clearNumPrev();
383
        if (state.hasValue("extents")) {
384
            Rectangle2D[] storedExtentPrev = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class);
385
            if (storedExtentPrev.length >= NUMREC) {
386
                extentsPrev = storedExtentPrev;
387
                NUMREC = storedExtentPrev.length;
388
            } else {
389
                for (int i = 0; i < storedExtentPrev.length; i++) {
390
                    extentsPrev[i] = storedExtentPrev[i];
391
                }
392
            }
393
        }
394

    
395
        extentsNext = new Rectangle2D[NUMREC];
396
        clearNumNext();
397
        if (state.hasValue("extentsNext")) {
398
            Rectangle2D[] storedExtentsNext = (Rectangle2D[]) state.getArray("extentsNext", Rectangle2D.class);
399
            if (storedExtentsNext.length >= NUMREC) {
400
                extentsNext = storedExtentsNext;
401
                NUMREC = storedExtentsNext.length;
402
            } else {
403
                for (int i = 0; i < storedExtentsNext.length; i++) {
404
                    extentsNext[i] = storedExtentsNext[i];
405
                }
406
            }
407
        }
408

    
409
        if (state.hasValue("current")) {
410
            currentExtent = (Rectangle2D) state.get("current");
411
        } else {
412
            currentExtent = null;
413
        }
414
    }
415

    
416
  /**
417
   * <p>
418
   * Returns information of this object. All information is stored as
419
   * properties:<br>
420
   * </p>
421
   * <p>
422
   * <b>Properties:</b>
423
   * <ul>
424
   * <li><i>className</i>: name of this class.
425
   * <li><i>num</i>: number of extents registered.
426
   * <li><i>numrec</i>: maximum number of extents that can register.
427
   * <li><i>extents</i>: .
428
   * </ul>
429
   * </p>
430
   */
431
  public void saveToState(PersistentState state) throws PersistenceException {
432

    
433
    state.set(FIELD_NUM_PREVIOUS, numPrev);
434
    state.set(FIELD_NUM_NEXT, numNext);
435
    state.set(FIELD_NUM_RECORDS, NUMREC);
436
    state.set(FIELD_PREVIOUS_EXTENTS, extentsPrev);
437
    state.set(FIELD_NEXT_EXTENTS, extentsNext);
438
    state.set(FIELD_CURRENT_EXTENT, currentExtent);
439
  }
440

    
441
  public static class RegisterPersistence implements Callable {
442

    
443
    public Object call() {
444
      PersistenceManager manager = ToolsLocator.getPersistenceManager();
445
      DynStruct definition = manager.addDefinition(ExtentHistory.class,
446
          "ExtentHistory", "ExtentHistory Persistence definition", null, null);
447
      definition.addDynFieldInt(FIELD_NUM_PREVIOUS).setMandatory(true);
448
      definition.addDynFieldInt(FIELD_NUM_NEXT).setMandatory(true);
449
      definition.addDynFieldInt(FIELD_NUM_RECORDS).setMandatory(true);
450
      definition.addDynFieldArray(FIELD_PREVIOUS_EXTENTS).setClassOfItems(Rectangle2D.class)
451
          .setMandatory(true);
452
      definition.addDynFieldArray(FIELD_NEXT_EXTENTS)
453
          .setClassOfItems(Rectangle2D.class).setMandatory(true);
454
      definition.addDynFieldObject(FIELD_CURRENT_EXTENT)
455
      .setClassOfValue(Rectangle2D.class).setMandatory(false);
456

    
457
      return Boolean.TRUE;
458
    }
459
  }
460
}