Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1014 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ExtentHistory.java @ 13593

History | View | Annotate | Download (6.88 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 com.iver.cit.gvsig.fmap;
42

    
43
import com.iver.utiles.XMLEntity;
44

    
45
import java.awt.geom.Rectangle2D;
46

    
47

    
48
/**
49
 * <p>The class <code>ExtentHistory</code> is designed for managing a history of extents.</p>
50
 * <p><i>(An extent is a point of a corner and the dimensions of a rectangular area).</i></p>
51
 * <p><i>Note:</i> The class <code>ExtentHistory</code> only supports <i>get</i> and <i>remove</i> the last extent.</p>
52
 *
53
 * @author Vicente Caballero Navarro
54
 */
55
public class ExtentHistory {
56
        /**
57
         * <p>Maximum number of extents that can store.</p>
58
         */
59
        private int NUMREC;
60
        
61
        /**
62
         * <p>Array with the extents.</p>
63
         * 
64
         * @see #hasPrevious()
65
         * @see #put(Rectangle2D)
66
         * @see #get()
67
         * @see #removePrev()
68
         */
69
        private Rectangle2D[] extents;
70
        
71
        /**
72
         * <p>Number of extents stored.</p>
73
         * 
74
         * @see #hasPrevious()
75
         * @see #put(Rectangle2D)
76
         * @see #get()
77
         * @see #removePrev()
78
         */
79
        private int num = 0;
80

    
81
        /**
82
         * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of 10 extents.</p>
83
         */
84
        public ExtentHistory() {
85
                NUMREC = 10;
86
                extents = new Rectangle2D[NUMREC];
87
        }
88

    
89
        /**
90
         * <p>Creates a new instance of <code>ExtentsHistory</code> with an history of <code>numEntries</code> extents.</p>
91
         *
92
         * @param numEntries the maximum number of extents that will store the instance
93
         */
94
        public ExtentHistory(int numEntries) {
95
                NUMREC = numEntries;
96
        }
97

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

    
116
                                extents[num - 1] = ext;
117
                        }
118
                }
119
        }
120

    
121
        /**
122
         * <p>Returns <code>true</code> if there are extents registered.</p>
123
         *
124
         * @return <code>true</code> if there are extents registered; <code>false</code> otherwise
125
         * 
126
         * @see #put(Rectangle2D)
127
         * @see #removePrev()
128
         * @see #get()
129
         */
130
        public boolean hasPrevious() {
131
                return num > 0;
132
        }
133

    
134
        /**
135
         * <p>Returns the last extent in the history.</p>
136
         *
137
         * @return the last extent in the history?
138
         * 
139
         * @see #put(Rectangle2D)
140
         * @see #getXMLEntity()
141
         */
142
        public Rectangle2D get() {
143
                Rectangle2D ext = extents[num - 1];
144

    
145
                return ext;
146
        }
147

    
148
        /**
149
         * <p>Extracts the last extent from the history.</p>
150
         *
151
         * @return last extent in the history
152
         * 
153
         * @see #hasPrevious()
154
         */
155
        public Rectangle2D removePrev() {
156
                Rectangle2D ext = extents[--num];
157
                return ext;
158
        }
159

    
160
        /**
161
         * <p>Returns an XML entity with information of this object. All information is stored as properties:<br></p>
162
         * <p><b>Properties:</b>
163
         * <ul>
164
         *  <li><i>className</i>: name of this class.
165
         *  <li><i>num</i>: number of extents registered.
166
         *  <li><i>numrec</i>: maximum number of extents that can register.
167
         *  <li><i>extentiX</i>: X coordinate of the upper left corner of the rectangle that defines the area.
168
         *  <li><i>extentiY</i>: Y coordinate of the upper left corner of the rectangle that defines the area.
169
         *  <li><i>extentiW</i>: width of the rectangle that defines the area.
170
         *  <li><i>extentiH</i>: height of the rectangle that defines the area.
171
         * </ul>
172
         * </p>
173
         *
174
         * @return XML entity that represents this object
175
         * 
176
         * @see #createFromXML(XMLEntity)
177
         * @see #get()
178
         * @see #put(Rectangle2D)
179
         */
180
        public XMLEntity getXMLEntity() {
181
                XMLEntity xml = new XMLEntity();
182
                xml.putProperty("className",this.getClass().getName());
183
                xml.putProperty("num", num);
184
                xml.putProperty("numrec", NUMREC);
185

    
186
                for (int i = 0; i < NUMREC; i++) {
187
                        if (extents[i] != null) {
188
                                xml.putProperty("extent" + i + "X", extents[i].getX());
189
                                xml.putProperty("extent" + i + "Y", extents[i].getY());
190
                                xml.putProperty("extent" + i + "W", extents[i].getWidth());
191
                                xml.putProperty("extent" + i + "H", extents[i].getHeight());
192
                        }
193
                }
194

    
195
                return xml;
196
        }
197

    
198
        /**
199
         * @see #createFromXML(XMLEntity) 
200
         */
201
        public static ExtentHistory createFromXML03(XMLEntity xml) {
202
                ExtentHistory eh = new ExtentHistory();
203
                eh.num = xml.getIntProperty("num");
204
                eh.NUMREC = xml.getIntProperty("numrec");
205

    
206
                for (int i = 0; i < eh.NUMREC; i++) {
207
                        try {
208
                                eh.extents[i] = new Rectangle2D.Double(
209
                                                xml.getDoubleProperty("extent" + i + "X"),
210
                                                xml.getDoubleProperty("extent" + i + "Y"),
211
                                                xml.getDoubleProperty("extent" + i + "W"),
212
                                                xml.getDoubleProperty("extent" + i + "H"));
213
                        } catch (Exception e) {
214
                                ///System.out.println("En las ExtentHistory =" + e); //TODO o se captura de alguna forma o se mete un nuevo parametro en el xml para saber exactamente cuantos rect?gulos se han a?adido.
215
                        }
216
                }
217

    
218
                return eh;
219
        }
220

    
221
        /**
222
         * <p>Binds the information in the XML entity argument to create and return an <code>ExtentHistory</code>
223
         *  object.</p>
224
         *
225
         * @param xml an XML entity of a <code>ExtentHistory</code>
226
         *
227
         * @return an <code>ExtentHistory</code> object with the information of the <code>xml</code> argument
228
         * 
229
         * @see #getXMLEntity()
230
         */
231
        public static ExtentHistory createFromXML(XMLEntity xml) {
232
                ExtentHistory eh = new ExtentHistory();
233
                eh.num = xml.getIntProperty("num");
234
                eh.NUMREC = xml.getIntProperty("numrec");
235

    
236
                for (int i = 0; i < eh.NUMREC; i++) {
237
                        try {
238
                                eh.extents[i] = new Rectangle2D.Double(
239
                                                xml.getDoubleProperty("extent" + i + "X"),
240
                                                xml.getDoubleProperty("extent" + i + "Y"),
241
                                                xml.getDoubleProperty("extent" + i + "W"),
242
                                                xml.getDoubleProperty("extent" + i + "H"));
243
                        } catch (Exception e) {
244
                                ///System.out.println("En las ExtentHistory =" + e); //TODO o se captura de alguna forma o se mete un nuevo parametro en el xml para saber exactamente cuantos rect?gulos se han a?adido.
245
                        }
246
                }
247

    
248
                return eh;
249
        }
250
}