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 @ 40559
History | View | Annotate | Download (5.19 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 | |||
35 | /**
|
||
36 | * <p><code>ExtentHistory</code> is designed for managing a history of extents.</p>
|
||
37 | *
|
||
38 | * <p>Note: An <i>extent</i> is a re.setMandatory(true)ctangular area, with information of its top-left 2D corner.</p>
|
||
39 | *
|
||
40 | * @author Vicente Caballero Navarro
|
||
41 | */
|
||
42 | public class ExtentHistory implements Persistent { |
||
43 | |||
44 | /**
|
||
45 | * <p>Maximum number of extents that can store.</p>
|
||
46 | */
|
||
47 | private int NUMREC; |
||
48 | |||
49 | /**
|
||
50 | * <p>Array with the extents.</p>
|
||
51 | *
|
||
52 | * @see #hasPrevious()
|
||
53 | * @see #put(Rectangle2D)
|
||
54 | * @see #get()
|
||
55 | * @see #removePrev()
|
||
56 | */
|
||
57 | private Rectangle2D[] extents; |
||
58 | |||
59 | /**
|
||
60 | * <p>Number of extents stored.</p>
|
||
61 | *
|
||
62 | * @see #hasPrevious()
|
||
63 | * @see #put(Rectangle2D)
|
||
64 | * @see #get()
|
||
65 | * @see #removePrev()
|
||
66 | */
|
||
67 | private int num = 0; |
||
68 | |||
69 | |||
70 | /**
|
||
71 | * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of 10 extents.</p>
|
||
72 | */
|
||
73 | public ExtentHistory() {
|
||
74 | this(10); |
||
75 | } |
||
76 | |||
77 | |||
78 | /**
|
||
79 | * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of <code>numEntries</code> extents.</p>
|
||
80 | *
|
||
81 | * @param numEntries the maximum number of extents that will store the instance
|
||
82 | */
|
||
83 | public ExtentHistory(int numEntries) { |
||
84 | NUMREC = numEntries; |
||
85 | extents = new Rectangle2D[NUMREC]; |
||
86 | } |
||
87 | |||
88 | /**
|
||
89 | * <p>Appends the specified extent to the end of this history.</p>
|
||
90 | *
|
||
91 | * @param ext the new extent
|
||
92 | *
|
||
93 | * @see #get()
|
||
94 | * @see #hasPrevious()
|
||
95 | */
|
||
96 | public void put(Rectangle2D ext) { |
||
97 | if ((ext != null) && ((num < 1) || (ext != extents[num - 1]))) { |
||
98 | if (num < (NUMREC)) {
|
||
99 | extents[num] = ext; |
||
100 | num = num + 1;
|
||
101 | } else {
|
||
102 | for (int i = 0; i < (NUMREC - 1); i++) { |
||
103 | extents[i] = extents[i + 1];
|
||
104 | } |
||
105 | |||
106 | extents[num - 1] = ext;
|
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /**
|
||
112 | * <p>Returns <code>true</code> if there are extents registered.</p>
|
||
113 | *
|
||
114 | * @return <code>true</code> if there are extents registered; <code>false</code> otherwise
|
||
115 | *
|
||
116 | * @see #put(Rectangle2D)
|
||
117 | * @see #removePrev()
|
||
118 | * @see #get()
|
||
119 | */
|
||
120 | public boolean hasPrevious() { |
||
121 | return num > 0; |
||
122 | } |
||
123 | |||
124 | /**
|
||
125 | * <p>Returns the last extent in the history.</p>
|
||
126 | *
|
||
127 | * @return the last extent in the history
|
||
128 | *
|
||
129 | * @see #put(Rectangle2D)
|
||
130 | * @see #getXMLEntity()
|
||
131 | */
|
||
132 | public Rectangle2D get() { |
||
133 | if (num <= 0) { |
||
134 | return null; |
||
135 | } |
||
136 | Rectangle2D ext = extents[num - 1]; |
||
137 | |||
138 | return ext;
|
||
139 | } |
||
140 | |||
141 | /**
|
||
142 | * <p>Extracts (removing) the last extent from the history.</p>
|
||
143 | *
|
||
144 | * @return last extent in the history
|
||
145 | *
|
||
146 | * @see #hasPrevious()
|
||
147 | */
|
||
148 | public Rectangle2D removePrev() { |
||
149 | if (num <= 0) { |
||
150 | return null; |
||
151 | } |
||
152 | Rectangle2D ext = extents[--num];
|
||
153 | return ext;
|
||
154 | } |
||
155 | |||
156 | public void loadFromState(PersistentState state) |
||
157 | throws PersistenceException {
|
||
158 | |||
159 | num = state.getInt("num");
|
||
160 | NUMREC = state.getInt("numrec");
|
||
161 | extents = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class); |
||
162 | } |
||
163 | |||
164 | /**
|
||
165 | * <p>
|
||
166 | * Returns information of this object. All information is stored as
|
||
167 | * properties:<br>
|
||
168 | * </p>
|
||
169 | * <p>
|
||
170 | * <b>Properties:</b>
|
||
171 | * <ul>
|
||
172 | * <li><i>className</i>: name of this class.
|
||
173 | * <li><i>num</i>: number of extents registered.
|
||
174 | * <li><i>numrec</i>: maximum number of extents that can register.
|
||
175 | * <li><i>extents</i>: .
|
||
176 | * </ul>
|
||
177 | * </p>
|
||
178 | *
|
||
179 | */
|
||
180 | public void saveToState(PersistentState state) throws PersistenceException { |
||
181 | |||
182 | state.set("num", num);
|
||
183 | state.set("numrec", NUMREC);
|
||
184 | state.set("extents", extents);
|
||
185 | } |
||
186 | |||
187 | |||
188 | public static void registerPersistent() { |
||
189 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
190 | DynStruct definition = manager.addDefinition( |
||
191 | ExtentHistory.class, |
||
192 | "ExtentHistory",
|
||
193 | "ExtentHistory Persistence definition",
|
||
194 | null,
|
||
195 | null
|
||
196 | ); |
||
197 | definition.addDynFieldInt("num").setMandatory(true); |
||
198 | definition.addDynFieldInt("numrec").setMandatory(true); |
||
199 | definition.addDynFieldArray("extents").setClassOfItems(Rectangle2D.class).setMandatory(true); |
||
200 | } |
||
201 | } |