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

History | View | Annotate | Download (8.06 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 previous 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
  /**
83
   * <p>
84
   * Number of next extents stored.
85
   * </p>
86
   *
87
   * @see #hasNext()
88
   * @see #putNext(Rectangle2D)
89
   * @see #getNext()
90
   * @see #removeNext()
91
   */
92
  private int numNext = 0;
93

    
94
  /**
95
   * <p>
96
   * Creates a new instance of <code>ExtentsHistory</code> with an history of 10
97
   * extents.
98
   * </p>
99
   */
100
  public ExtentHistory() {
101
    this(10);
102
  }
103

    
104
  /**
105
   * <p>
106
   * Creates a new instance of <code>ExtentsHistory</code> with an history of
107
   * <code>numEntries</code> extents.
108
   * </p>
109
   *
110
   * @param numEntries the maximum number of extents that will store the
111
   *          instance
112
   */
113
  public ExtentHistory(int numEntries) {
114
    NUMREC = numEntries;
115
    extents = new Rectangle2D[NUMREC];
116
    extentsNext = new Rectangle2D[NUMREC];
117
  }
118

    
119
  /**
120
   * <p>
121
   * Appends the specified extent at the end of the array of previous zooms.
122
   * </p>
123
   *
124
   * @param ext the new extent
125
   * @see #get()
126
   * @see #hasPrevious()
127
   */
128
  public void put(Rectangle2D ext) {
129
    if ((ext != null) && ((num < 1) || (ext != extents[num - 1]))) {
130
      if (num < (NUMREC)) {
131
        extents[num] = ext;
132
        num = num + 1;
133
      }
134
      else {
135
        for (int i = 0; i < (NUMREC - 1); i++) {
136
          extents[i] = extents[i + 1];
137
        }
138
        extents[num - 1] = ext;
139
      }
140
    }
141
  }
142

    
143
  /**
144
   * <p>
145
   * Appends the specified extent at the end of the array of next zooms.
146
   * </p>
147
   *
148
   * @param ext the new extent
149
   * @see #getNext()
150
   * @see #hasNext()
151
   */
152
  public void putNext(Rectangle2D ext) {
153
    if ((ext != null) && ((numNext < 1) || (ext != extentsNext[numNext - 1]))) {
154
      if (numNext < (NUMREC)) {
155
        extentsNext[numNext] = ext;
156
        numNext++;
157
      }
158
      else {
159
        for (int i = 0; i < (NUMREC - 1); i++) {
160
          extentsNext[i] = extentsNext[i + 1];
161
        }
162
        extentsNext[numNext - 1] = ext;
163
      }
164
    }
165
  }
166

    
167
  /**
168
   * <p>
169
   * Returns <code>true</code> if there are previous extents registered.
170
   * </p>
171
   *
172
   * @return <code>true</code> if there are previous extents registered;
173
   *         <code>false</code> otherwise
174
   * @see #put(Rectangle2D)
175
   * @see #removePrev()
176
   * @see #get()
177
   */
178
  public boolean hasPrevious() {
179
    return num > 0;
180
  }
181

    
182
  /**
183
   * <p>
184
   * Returns <code>true</code> if there are next extents registered.
185
   * </p>
186
   *
187
   * @return <code>true</code> if there are next extents registered;
188
   *         <code>false</code> otherwise
189
   * @see #putNext(Rectangle2D)
190
   * @see #removeNext()
191
   * @see #getNext()
192
   */
193
  public boolean hasNext() {
194
    return numNext > 0;
195
  }
196

    
197
  /**
198
   * <p>
199
   * Returns the last previous extent in the history.
200
   * </p>
201
   *
202
   * @return the last previous extent in the history
203
   * @see #put(Rectangle2D)
204
   * @see #getXMLEntity()
205
   */
206
  public Rectangle2D get() {
207
    if (num <= 0) {
208
      return null;
209
    }
210
    Rectangle2D ext = extents[num - 1];
211

    
212
    return ext;
213
  }
214

    
215
  /**
216
   * <p>
217
   * Returns the last next extent in the history.
218
   * </p>
219
   *
220
   * @return the last next extent in the history
221
   * @see #putNext(Rectangle2D)
222
   * @see #getXMLEntity()
223
   */
224
  public Rectangle2D getNext() {
225
    if (numNext <= 0) {
226
      return null;
227
    }
228

    
229
    Rectangle2D ext = extentsNext[numNext - 1];
230

    
231
    return ext;
232
  }
233

    
234
  /**
235
   * <p>
236
   * Extracts (removing) the last previous extent from the history.
237
   * </p>
238
   *
239
   * @return last previous extent in the history
240
   * @see #hasPrevious()
241
   */
242
  public Rectangle2D removePrev() {
243
    if (num <= 0) {
244
      return null;
245
    }
246

    
247
    Rectangle2D ext = extents[--num];
248
    return ext;
249
  }
250

    
251
  /**
252
   * <p>
253
   * Extracts (removing) the last next extent from the history.
254
   * </p>
255
   *
256
   * @return last next extent in the history
257
   * @see #hasNext()
258
   */
259
  public Rectangle2D removeNext() {
260
    if (numNext <= 0) {
261
      return null;
262
    }
263

    
264
    Rectangle2D ext = extentsNext[--numNext];
265
    return ext;
266
  }
267

    
268
  /**
269
   * <p>
270
   * Sets to zero the number of next extents from the history.
271
   * </p>
272
   *
273
   * @see #getNext()
274
   * @see #hasNext()
275
   */
276
  public void clear() {
277
    numNext = 0;
278
  }
279

    
280
  public void loadFromState(PersistentState state) throws PersistenceException {
281

    
282
    num = state.getInt("num");
283
    NUMREC = state.getInt("numrec");
284
    extents = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class);
285
    extentsNext = (Rectangle2D[]) state.getArray("extentsNext",
286
        Rectangle2D.class);
287
  }
288

    
289
  /**
290
   * <p>
291
   * Returns information of this object. All information is stored as
292
   * properties:<br>
293
   * </p>
294
   * <p>
295
   * <b>Properties:</b>
296
   * <ul>
297
   * <li><i>className</i>: name of this class.
298
   * <li><i>num</i>: number of extents registered.
299
   * <li><i>numrec</i>: maximum number of extents that can register.
300
   * <li><i>extents</i>: .
301
   * </ul>
302
   * </p>
303
   */
304
  public void saveToState(PersistentState state) throws PersistenceException {
305

    
306
    state.set("num", num);
307
    state.set("numrec", NUMREC);
308
    state.set("extents", extents);
309
    state.set("extentsNext", extentsNext);
310
  }
311

    
312
  public static class RegisterPersistence implements Callable {
313

    
314
    public Object call() {
315
      PersistenceManager manager = ToolsLocator.getPersistenceManager();
316
      DynStruct definition = manager.addDefinition(ExtentHistory.class,
317
          "ExtentHistory", "ExtentHistory Persistence definition", null, null);
318
      definition.addDynFieldInt("num").setMandatory(true);
319
      definition.addDynFieldInt("numrec").setMandatory(true);
320
      definition.addDynFieldArray("extents").setClassOfItems(Rectangle2D.class)
321
          .setMandatory(true);
322
      definition.addDynFieldArray("extentsNext")
323
          .setClassOfItems(Rectangle2D.class).setMandatory(true);
324
      return Boolean.TRUE;
325
    }
326
  }
327
}