root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / ExtentHistory.java @ 38557
History | View | Annotate | Download (5.46 KB)
1 | 21200 | vcaballero | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package org.gvsig.fmap.mapcontext; |
||
42 | |||
43 | import java.awt.geom.Rectangle2D; |
||
44 | |||
45 | 30173 | jldominguez | import org.gvsig.tools.ToolsLocator; |
46 | 32880 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct; |
47 | import org.gvsig.tools.persistence.PersistenceManager; |
||
48 | 30173 | jldominguez | import org.gvsig.tools.persistence.Persistent; |
49 | import org.gvsig.tools.persistence.PersistentState; |
||
50 | 32880 | jjdelcerro | import org.gvsig.tools.persistence.exception.PersistenceException; |
51 | 21200 | vcaballero | |
52 | /**
|
||
53 | * <p><code>ExtentHistory</code> is designed for managing a history of extents.</p>
|
||
54 | 22691 | vcaballero | *
|
55 | 31548 | jjdelcerro | * <p>Note: An <i>extent</i> is a re.setMandatory(true)ctangular area, with information of its top-left 2D corner.</p>
|
56 | 21200 | vcaballero | *
|
57 | * @author Vicente Caballero Navarro
|
||
58 | */
|
||
59 | 30173 | jldominguez | public class ExtentHistory implements Persistent { |
60 | |||
61 | 21200 | vcaballero | /**
|
62 | * <p>Maximum number of extents that can store.</p>
|
||
63 | */
|
||
64 | private int NUMREC; |
||
65 | 22691 | vcaballero | |
66 | 21200 | vcaballero | /**
|
67 | * <p>Array with the extents.</p>
|
||
68 | 22691 | vcaballero | *
|
69 | 21200 | vcaballero | * @see #hasPrevious()
|
70 | * @see #put(Rectangle2D)
|
||
71 | * @see #get()
|
||
72 | * @see #removePrev()
|
||
73 | */
|
||
74 | 31678 | cordinyana | private Rectangle2D[] extents; |
75 | 22691 | vcaballero | |
76 | 21200 | vcaballero | /**
|
77 | * <p>Number of extents stored.</p>
|
||
78 | 22691 | vcaballero | *
|
79 | 21200 | vcaballero | * @see #hasPrevious()
|
80 | * @see #put(Rectangle2D)
|
||
81 | * @see #get()
|
||
82 | * @see #removePrev()
|
||
83 | */
|
||
84 | private int num = 0; |
||
85 | |||
86 | 31548 | jjdelcerro | |
87 | 21200 | vcaballero | /**
|
88 | * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of 10 extents.</p>
|
||
89 | */
|
||
90 | public ExtentHistory() {
|
||
91 | 31678 | cordinyana | this(10); |
92 | 21200 | vcaballero | } |
93 | |||
94 | 31548 | jjdelcerro | |
95 | 21200 | vcaballero | /**
|
96 | * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of <code>numEntries</code> extents.</p>
|
||
97 | *
|
||
98 | * @param numEntries the maximum number of extents that will store the instance
|
||
99 | */
|
||
100 | public ExtentHistory(int numEntries) { |
||
101 | NUMREC = numEntries; |
||
102 | 31678 | cordinyana | extents = new Rectangle2D[NUMREC]; |
103 | 21200 | vcaballero | } |
104 | |||
105 | /**
|
||
106 | * <p>Appends the specified extent to the end of this history.</p>
|
||
107 | *
|
||
108 | * @param ext the new extent
|
||
109 | 22691 | vcaballero | *
|
110 | 21200 | vcaballero | * @see #get()
|
111 | * @see #hasPrevious()
|
||
112 | */
|
||
113 | public void put(Rectangle2D ext) { |
||
114 | 31678 | cordinyana | if ((ext != null) && ((num < 1) || (ext != extents[num - 1]))) { |
115 | 21200 | vcaballero | if (num < (NUMREC)) {
|
116 | 31678 | cordinyana | extents[num] = ext; |
117 | 21200 | vcaballero | num = num + 1;
|
118 | } else {
|
||
119 | for (int i = 0; i < (NUMREC - 1); i++) { |
||
120 | 31678 | cordinyana | extents[i] = extents[i + 1];
|
121 | 21200 | vcaballero | } |
122 | |||
123 | 31678 | cordinyana | extents[num - 1] = ext;
|
124 | 21200 | vcaballero | } |
125 | } |
||
126 | } |
||
127 | |||
128 | /**
|
||
129 | * <p>Returns <code>true</code> if there are extents registered.</p>
|
||
130 | *
|
||
131 | * @return <code>true</code> if there are extents registered; <code>false</code> otherwise
|
||
132 | 22691 | vcaballero | *
|
133 | 21200 | vcaballero | * @see #put(Rectangle2D)
|
134 | * @see #removePrev()
|
||
135 | * @see #get()
|
||
136 | */
|
||
137 | public boolean hasPrevious() { |
||
138 | return num > 0; |
||
139 | } |
||
140 | |||
141 | /**
|
||
142 | * <p>Returns the last extent in the history.</p>
|
||
143 | *
|
||
144 | * @return the last extent in the history
|
||
145 | 22691 | vcaballero | *
|
146 | 21200 | vcaballero | * @see #put(Rectangle2D)
|
147 | * @see #getXMLEntity()
|
||
148 | */
|
||
149 | public Rectangle2D get() { |
||
150 | 31698 | cordinyana | if (num <= 0) { |
151 | return null; |
||
152 | } |
||
153 | 31678 | cordinyana | Rectangle2D ext = extents[num - 1]; |
154 | 21200 | vcaballero | |
155 | return ext;
|
||
156 | } |
||
157 | |||
158 | /**
|
||
159 | * <p>Extracts (removing) the last extent from the history.</p>
|
||
160 | *
|
||
161 | * @return last extent in the history
|
||
162 | 22691 | vcaballero | *
|
163 | 21200 | vcaballero | * @see #hasPrevious()
|
164 | */
|
||
165 | public Rectangle2D removePrev() { |
||
166 | 31698 | cordinyana | if (num <= 0) { |
167 | return null; |
||
168 | } |
||
169 | 31678 | cordinyana | Rectangle2D ext = extents[--num];
|
170 | 21200 | vcaballero | return ext;
|
171 | } |
||
172 | |||
173 | 30173 | jldominguez | public void loadFromState(PersistentState state) |
174 | throws PersistenceException {
|
||
175 | |||
176 | num = state.getInt("num");
|
||
177 | NUMREC = state.getInt("numrec");
|
||
178 | 31678 | cordinyana | extents = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class); |
179 | 30173 | jldominguez | } |
180 | |||
181 | 21200 | vcaballero | /**
|
182 | 31678 | cordinyana | * <p>
|
183 | * Returns information of this object. All information is stored as
|
||
184 | * properties:<br>
|
||
185 | * </p>
|
||
186 | * <p>
|
||
187 | * <b>Properties:</b>
|
||
188 | 21200 | vcaballero | * <ul>
|
189 | 31678 | cordinyana | * <li><i>className</i>: name of this class.
|
190 | * <li><i>num</i>: number of extents registered.
|
||
191 | * <li><i>numrec</i>: maximum number of extents that can register.
|
||
192 | * <li><i>extents</i>: .
|
||
193 | 21200 | vcaballero | * </ul>
|
194 | * </p>
|
||
195 | 31678 | cordinyana | *
|
196 | 21200 | vcaballero | */
|
197 | 30173 | jldominguez | public void saveToState(PersistentState state) throws PersistenceException { |
198 | |||
199 | state.set("num", num);
|
||
200 | state.set("numrec", NUMREC);
|
||
201 | 31548 | jjdelcerro | state.set("extents", extents);
|
202 | 30173 | jldominguez | } |
203 | |||
204 | 21200 | vcaballero | |
205 | 30173 | jldominguez | public static void registerPersistent() { |
206 | 32880 | jjdelcerro | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
207 | DynStruct definition = manager.addDefinition( |
||
208 | ExtentHistory.class, |
||
209 | "ExtentHistory",
|
||
210 | "ExtentHistory Persistence definition",
|
||
211 | null,
|
||
212 | null
|
||
213 | ); |
||
214 | definition.addDynFieldInt("num").setMandatory(true); |
||
215 | definition.addDynFieldInt("numrec").setMandatory(true); |
||
216 | 33281 | jjdelcerro | definition.addDynFieldArray("extents").setClassOfItems(Rectangle2D.class).setMandatory(true); |
217 | 21200 | vcaballero | } |
218 | 31678 | cordinyana | } |