Statistics
| Revision:

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
/* 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
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.persistence.PersistenceManager;
48
import org.gvsig.tools.persistence.Persistent;
49
import org.gvsig.tools.persistence.PersistentState;
50
import org.gvsig.tools.persistence.exception.PersistenceException;
51

    
52
/**
53
 * <p><code>ExtentHistory</code> is designed for managing a history of extents.</p>
54
 *
55
 * <p>Note: An <i>extent</i> is a re.setMandatory(true)ctangular area, with information of its top-left 2D corner.</p>
56
 *
57
 * @author Vicente Caballero Navarro
58
 */
59
public class ExtentHistory implements Persistent {
60
        
61
        /**
62
         * <p>Maximum number of extents that can store.</p>
63
         */
64
        private int NUMREC;
65

    
66
        /**
67
         * <p>Array with the extents.</p>
68
         *
69
         * @see #hasPrevious()
70
         * @see #put(Rectangle2D)
71
         * @see #get()
72
         * @see #removePrev()
73
         */
74
        private Rectangle2D[] extents;
75

    
76
        /**
77
         * <p>Number of extents stored.</p>
78
         *
79
         * @see #hasPrevious()
80
         * @see #put(Rectangle2D)
81
         * @see #get()
82
         * @see #removePrev()
83
         */
84
        private int num = 0;
85

    
86
        
87
        /**
88
         * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of 10 extents.</p>
89
         */
90
        public ExtentHistory() {
91
                this(10);
92
        }
93

    
94
        
95
        /**
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
                extents = new Rectangle2D[NUMREC];
103
        }
104

    
105
        /**
106
         * <p>Appends the specified extent to the end of this history.</p>
107
         *
108
         * @param ext the new extent
109
         *
110
         * @see #get()
111
         * @see #hasPrevious()
112
         */
113
        public void put(Rectangle2D ext) {
114
                if ((ext != null) && ((num < 1) || (ext != extents[num - 1]))) {
115
                        if (num < (NUMREC)) {
116
                                extents[num] = ext;
117
                                num = num + 1;
118
                        } else {
119
                                for (int i = 0; i < (NUMREC - 1); i++) {
120
                                        extents[i] = extents[i + 1];
121
                                }
122

    
123
                                extents[num - 1] = ext;
124
                        }
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
         *
133
         * @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
         *
146
         * @see #put(Rectangle2D)
147
         * @see #getXMLEntity()
148
         */
149
        public Rectangle2D get() {
150
                if (num <= 0) {
151
                        return null;
152
                }
153
                Rectangle2D ext = extents[num - 1];
154

    
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
         *
163
         * @see #hasPrevious()
164
         */
165
        public Rectangle2D removePrev() {
166
                if (num <= 0) {
167
                        return null;
168
                }
169
                Rectangle2D ext = extents[--num];
170
                return ext;
171
        }
172

    
173
        public void loadFromState(PersistentState state)
174
                        throws PersistenceException {
175
                
176
                num = state.getInt("num");
177
                NUMREC = state.getInt("numrec");
178
                extents = (Rectangle2D[]) state.getArray("extents", Rectangle2D.class);
179
        }
180

    
181
        /**
182
         * <p>
183
         * Returns information of this object. All information is stored as
184
         * properties:<br>
185
         * </p>
186
         * <p>
187
         * <b>Properties:</b>
188
         * <ul>
189
         * <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
         * </ul>
194
         * </p>
195
         * 
196
         */
197
        public void saveToState(PersistentState state) throws PersistenceException {
198
                
199
                state.set("num", num);
200
                state.set("numrec", NUMREC);
201
                state.set("extents", extents);
202
        }
203

    
204

    
205
        public static void registerPersistent() {
206
                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
                definition.addDynFieldArray("extents").setClassOfItems(Rectangle2D.class).setMandatory(true);
217
        }
218
}