Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ExtentHistory.java @ 2183

History | View | Annotate | Download (5.25 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.Rectangle;
46
import java.awt.geom.Rectangle2D;
47

    
48

    
49
/**
50
 * Clase que gestiona los diferentes extent por los que ha pasado la vista.
51
 *
52
 * @author Vicente Caballero Navarro
53
 */
54
public class ExtentHistory {
55
        private int NUMREC;
56
        private Rectangle2D[] extents;
57
        private int num = 0;
58

    
59
        /**
60
         * Creates a new ExtentsHistory object.
61
         */
62
        public ExtentHistory() {
63
                NUMREC = 10;
64
                extents = new Rectangle2D[NUMREC];
65
        }
66

    
67
        /**
68
         * Creates a new ExtentsHistory object.
69
         *
70
         * @param numEntries Numero de entradas que se guardan en el historico de
71
         *                   rect?ngulos, por defecto 20
72
         */
73
        public ExtentHistory(int numEntries) {
74
                NUMREC = numEntries;
75
        }
76

    
77
        /**
78
         * Pone un nuevo rect?ngulo al final del array
79
         *
80
         * @param ext Rect?ngulo que se a?ade al hist?rico
81
         */
82
        public void put(Rectangle2D ext) {
83
                if ((ext != null) && ((num < 1) || (ext != extents[num - 1]))) {
84
                        if (num < (NUMREC)) {
85
                                extents[num] = ext;
86
                                num = num + 1;
87
                        } else {
88
                                for (int i = 0; i < (NUMREC - 1); i++) {
89
                                        extents[i] = extents[i + 1];
90
                                }
91

    
92
                                extents[num - 1] = ext;
93
                        }
94
                }
95
        }
96

    
97
        /**
98
         * Devuelve true si hay alg?n rect?ngulo en el hist?rico
99
         *
100
         * @return true o false en caso de que haya o no haya rect?ngulos
101
         */
102
        public boolean hasPrevious() {
103
                return num > 0;
104
        }
105

    
106
        /**
107
         * Obtiene el ?ltimo rect?ngulo que se a?adi? al hist?rico
108
         *
109
         * @return Ultimo rect?ngulo a?adido
110
         */
111
        public Rectangle2D get() {
112
                Rectangle2D ext = extents[num - 1];
113

    
114
                return ext;
115
        }
116

    
117
        /**
118
         * Devuelve el ?ltimo rect?ngulo del hist?rico y lo elimina del mismo
119
         *
120
         * @return Ultimo rect?ngulo a?adido
121
         */
122
        public Rectangle2D removePrev() {
123
                Rectangle2D ext = extents[num - 1];
124
                num = num - 1;
125

    
126
                return ext;
127
        }
128

    
129
        /**
130
         * Devuelve el XMLEntity con la informaci?n para poder reproducir de nuevo
131
         * el objeto actual.
132
         *
133
         * @return XMLEntity.
134
         */
135
        public XMLEntity getXMLEntity() {
136
                XMLEntity xml = new XMLEntity();
137
                xml.putProperty("className",this.getClass().getName());
138
                xml.putProperty("num", num);
139
                xml.putProperty("numrec", NUMREC);
140

    
141
                for (int i = 0; i < NUMREC; i++) {
142
                        if (extents[i] != null) {
143
                                xml.putProperty("extent" + i + "X", extents[i].getX());
144
                                xml.putProperty("extent" + i + "Y", extents[i].getY());
145
                                xml.putProperty("extent" + i + "W", extents[i].getWidth());
146
                                xml.putProperty("extent" + i + "H", extents[i].getHeight());
147
                        }
148
                }
149

    
150
                return xml;
151
        }
152

    
153
        /**
154
         * Crea un nuevo ExtentHistory a partir del XMLEntity.
155
         *
156
         * @param xml XMLEntity
157
         *
158
         * @return Nuevo ExtentHistory.
159
         */
160
        public static ExtentHistory createFromXML03(XMLEntity xml) {
161
                ExtentHistory eh = new ExtentHistory();
162
                eh.num = xml.getIntProperty("num");
163
                eh.NUMREC = xml.getIntProperty("numrec");
164

    
165
                for (int i = 0; i < eh.NUMREC; i++) {
166
                        try {
167
                                eh.extents[i] = new Rectangle2D.Double(xml.getDoubleProperty(
168
                                                        "extent" + i + "X"),
169
                                                xml.getDoubleProperty("extent" + i + "Y"),
170
                                                xml.getDoubleProperty("extent" + i + "W"),
171
                                                xml.getDoubleProperty("extent" + i + "H"));
172
                        } catch (Exception e) {
173
                                ///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.
174
                        }
175
                }
176

    
177
                return eh;
178
        }
179

    
180
        /**
181
         * Crea un nuevo ExtentHistory a partir del XMLEntity.
182
         *
183
         * @param xml XMLEntity
184
         *
185
         * @return Nuevo ExtentHistory.
186
         */
187
        public static ExtentHistory createFromXML(XMLEntity xml) {
188
                ExtentHistory eh = new ExtentHistory();
189
                eh.num = xml.getIntProperty("num");
190
                eh.NUMREC = xml.getIntProperty("numrec");
191

    
192
                for (int i = 0; i < eh.NUMREC; i++) {
193
                        try {
194
                                eh.extents[i] = new Rectangle2D.Double(xml.getDoubleProperty(
195
                                                        "extent" + i + "X"),
196
                                                xml.getDoubleProperty("extent" + i + "Y"),
197
                                                xml.getDoubleProperty("extent" + i + "W"),
198
                                                xml.getDoubleProperty("extent" + i + "H"));
199
                        } catch (Exception e) {
200
                                ///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.
201
                        }
202
                }
203

    
204
                return eh;
205
        }
206
}