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 @ 42170
History | View | Annotate | Download (7.12 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
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 | 41840 | jjdelcerro | import org.gvsig.tools.util.Callable; |
35 | 40435 | jjdelcerro | |
36 | /**
|
||
37 | 42170 | mcompany | * <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 | 40435 | jjdelcerro | *
|
45 | * @author Vicente Caballero Navarro
|
||
46 | */
|
||
47 | public class ExtentHistory implements Persistent { |
||
48 | |||
49 | 42170 | mcompany | /**
|
50 | * <p>
|
||
51 | * Maximum number of extents that can store.
|
||
52 | * </p>
|
||
53 | */
|
||
54 | private int NUMREC; |
||
55 | 40435 | jjdelcerro | |
56 | 42170 | mcompany | /**
|
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 | 40435 | jjdelcerro | |
68 | 42170 | mcompany | private Rectangle2D[] extentsNext; |
69 | 40435 | jjdelcerro | |
70 | 42170 | mcompany | /**
|
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 | 40435 | jjdelcerro | |
82 | 42170 | mcompany | private int numNext = 0; |
83 | 40435 | jjdelcerro | |
84 | 42170 | mcompany | /**
|
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 | 40435 | jjdelcerro | |
94 | 42170 | mcompany | /**
|
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 | 40435 | jjdelcerro | |
109 | 42170 | mcompany | /**
|
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 | 40435 | jjdelcerro | |
133 | 42170 | mcompany | /**
|
134 | * <p>
|
||
135 | * Returns <code>true</code> if there are extents registered.
|
||
136 | * </p>
|
||
137 | *
|
||
138 | * @return <code>true</code> if there are extents registered;
|
||
139 | * <code>false</code> otherwise
|
||
140 | * @see #put(Rectangle2D)
|
||
141 | * @see #removePrev()
|
||
142 | * @see #get()
|
||
143 | */
|
||
144 | public boolean hasPrevious() { |
||
145 | return num > 0; |
||
146 | } |
||
147 | 40435 | jjdelcerro | |
148 | 42170 | mcompany | public boolean hasNext() { |
149 | return numNext > 0; |
||
150 | } |
||
151 | 40435 | jjdelcerro | |
152 | 42170 | mcompany | /**
|
153 | * <p>
|
||
154 | * Returns the last extent in the history.
|
||
155 | * </p>
|
||
156 | *
|
||
157 | * @return the last extent in the history
|
||
158 | * @see #put(Rectangle2D)
|
||
159 | * @see #getXMLEntity()
|
||
160 | */
|
||
161 | public Rectangle2D get() { |
||
162 | if (num <= 0) { |
||
163 | return null; |
||
164 | } |
||
165 | Rectangle2D ext = extents[num - 1]; |
||
166 | 40435 | jjdelcerro | |
167 | 42170 | mcompany | return ext;
|
168 | } |
||
169 | |||
170 | public Rectangle2D getNext() { |
||
171 | if (numNext <= 0) { |
||
172 | return null; |
||
173 | } |
||
174 | |||
175 | Rectangle2D ext = extentsNext[numNext - 1]; |
||
176 | |||
177 | return ext;
|
||
178 | } |
||
179 | 40435 | jjdelcerro | |
180 | 42170 | mcompany | /**
|
181 | * <p>
|
||
182 | * Extracts (removing) the last extent from the history.
|
||
183 | * </p>
|
||
184 | *
|
||
185 | * @return last extent in the history
|
||
186 | * @see #hasPrevious()
|
||
187 | */
|
||
188 | public Rectangle2D removePrev() { |
||
189 | if (num <= 0) { |
||
190 | return null; |
||
191 | } |
||
192 | if (numNext < (NUMREC)) {
|
||
193 | extentsNext[numNext] = extents[num - 1];
|
||
194 | numNext++; |
||
195 | } |
||
196 | else {
|
||
197 | for (int i = 0; i < (NUMREC - 1); i++) { |
||
198 | extentsNext[i] = extentsNext[i + 1];
|
||
199 | } |
||
200 | extentsNext[numNext - 1] = extents[num - 1]; |
||
201 | } |
||
202 | 40435 | jjdelcerro | |
203 | 42170 | mcompany | Rectangle2D ext = extents[--num];
|
204 | return ext;
|
||
205 | } |
||
206 | 41840 | jjdelcerro | |
207 | 42170 | mcompany | /**
|
208 | * <p>
|
||
209 | * Extracts (removing) the last extent from the history.
|
||
210 | * </p>
|
||
211 | *
|
||
212 | * @return last extent in the history
|
||
213 | * @see #hasNext()
|
||
214 | */
|
||
215 | public Rectangle2D removeNext() { |
||
216 | if (numNext <= 0) { |
||
217 | return null; |
||
218 | 41840 | jjdelcerro | } |
219 | 42170 | mcompany | |
220 | if (num < (NUMREC)) {
|
||
221 | extents[num] = extentsNext[numNext - 1];
|
||
222 | } |
||
223 | else {
|
||
224 | for (int i = 0; i < (NUMREC - 1); i++) { |
||
225 | extents[i] = extents[i + 1];
|
||
226 | } |
||
227 | extents[num - 1] = extentsNext[numNext - 1]; |
||
228 | } |
||
229 | |||
230 | Rectangle2D ext = extentsNext[--numNext];
|
||
231 | return ext;
|
||
232 | } |
||
233 | |||
234 | |||
235 | public void clear() { |
||
236 | num = 0;
|
||
237 | numNext = 0;
|
||
238 | } |
||
239 | |||
240 | |||
241 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
242 | |||
243 | num = state.getInt("num");
|
||
244 | NUMREC = state.getInt("numrec");
|
||
245 | extents = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class); |
||
246 | extentsNext = (Rectangle2D[]) state.getArray("extentsNext", |
||
247 | Rectangle2D.class);
|
||
248 | } |
||
249 | |||
250 | /**
|
||
251 | * <p>
|
||
252 | * Returns information of this object. All information is stored as
|
||
253 | * properties:<br>
|
||
254 | * </p>
|
||
255 | * <p>
|
||
256 | * <b>Properties:</b>
|
||
257 | * <ul>
|
||
258 | * <li><i>className</i>: name of this class.
|
||
259 | * <li><i>num</i>: number of extents registered.
|
||
260 | * <li><i>numrec</i>: maximum number of extents that can register.
|
||
261 | * <li><i>extents</i>: .
|
||
262 | * </ul>
|
||
263 | * </p>
|
||
264 | */
|
||
265 | public void saveToState(PersistentState state) throws PersistenceException { |
||
266 | |||
267 | state.set("num", num);
|
||
268 | state.set("numrec", NUMREC);
|
||
269 | state.set("extents", extents);
|
||
270 | state.set("extentsNext", extentsNext);
|
||
271 | } |
||
272 | |||
273 | public static class RegisterPersistence implements Callable { |
||
274 | |||
275 | public Object call() { |
||
276 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
277 | DynStruct definition = manager.addDefinition(ExtentHistory.class, |
||
278 | "ExtentHistory", "ExtentHistory Persistence definition", null, null); |
||
279 | definition.addDynFieldInt("num").setMandatory(true); |
||
280 | definition.addDynFieldInt("numrec").setMandatory(true); |
||
281 | definition.addDynFieldArray("extents").setClassOfItems(Rectangle2D.class) |
||
282 | .setMandatory(true);
|
||
283 | definition.addDynFieldArray("extentsNext")
|
||
284 | .setClassOfItems(Rectangle2D.class).setMandatory(true); |
||
285 | return Boolean.TRUE; |
||
286 | } |
||
287 | } |
||
288 | 40435 | jjdelcerro | } |