Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutUtilities.java @ 5525

History | View | Annotate | Download (8.38 KB)

1 312 fernando
/*
2
 * Created on 16-jul-2004
3
 *
4
 */
5 1103 fjp
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45 312 fernando
package com.iver.cit.gvsig.gui.layout;
46
47
import java.awt.geom.AffineTransform;
48
import java.awt.geom.NoninvertibleTransformException;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51
import java.util.ArrayList;
52
53 1513 vcaballero
import org.cresques.cts.IProjection;
54 312 fernando
55 2209 vcaballero
import com.iver.cit.gvsig.fmap.ViewPort;
56 1513 vcaballero
57 2209 vcaballero
58 312 fernando
/**
59
 * Clase que recoge m?todos est?ticos sobre el Layout.
60
 *
61
 * @author Vicente Caballero Navarro
62
 */
63
public class FLayoutUtilities {
64 1074 vcaballero
        /**
65
         * Devuelve true si las dos ArrayList que se le pasan como parametro son
66
         * iguales.
67
         *
68
         * @param n lista anterior
69
         * @param l lista actual
70
         *
71
         * @return true si los ArrayList son iguales.
72
         */
73
        public static boolean isEqualList(ArrayList n, ArrayList l) {
74
                if (n.size() != l.size()) {
75
                        return false;
76
                }
77 312 fernando
78 1074 vcaballero
                for (int i = 0; i < n.size(); i++) {
79
                        if (l.get(i) != n.get(i)) {
80
                                return false;
81
                        }
82
                }
83 312 fernando
84 1074 vcaballero
                return true;
85
        }
86 312 fernando
87 1074 vcaballero
        /**
88
         * Pasa una distancia en pixels a unidades del folio.
89
         *
90
         * @param d distancia en pixels.
91
         * @param at Matriz de transformaci?n.
92
         *
93
         * @return distancia en unidades de folio.
94
         */
95
        public static double toSheetDistance(double d, AffineTransform at) {
96
                double dist = d / at.getScaleX(); // pProv.x;
97 312 fernando
98 1074 vcaballero
                return dist;
99
        }
100 312 fernando
101 1074 vcaballero
        /**
102
         * Pasa una distancia de coordenadas del folio a pixels.
103
         *
104
         * @param d distancia en coordenadas de folio.
105
         * @param at Matriz de transformaci?n.
106
         *
107
         * @return double en pixels.
108
         */
109
        public static double fromSheetDistance(double d, AffineTransform at) {
110
                Point2D.Double pSheet1 = new Point2D.Double(0, 0);
111
                Point2D.Double pSheet2 = new Point2D.Double(1, 0);
112
                Point2D.Double pScreen1 = new Point2D.Double();
113
                Point2D.Double pScreen2 = new Point2D.Double();
114 312 fernando
115 1074 vcaballero
                try {
116
                        at.transform(pSheet1, pScreen1);
117
                        at.transform(pSheet2, pScreen2);
118
                } catch (Exception e) {
119
                        System.err.print(e.getMessage());
120
                }
121 312 fernando
122 1074 vcaballero
                return pScreen1.distance(pScreen2) * d;
123
        }
124 312 fernando
125 1074 vcaballero
        /**
126
         * Pasa un punto en pixels a coordenadas del folio.
127
         *
128
         * @param pScreen pixels.
129
         * @param at Matriz de transformaci?n.
130
         *
131
         * @return Point2D en coordenadas de folio.
132
         */
133 3106 caballero
        public static Point2D.Double toSheetPoint(Point2D pScreen,
134 1074 vcaballero
                AffineTransform at) {
135
                Point2D.Double pWorld = new Point2D.Double();
136
                AffineTransform at1;
137 312 fernando
138 1074 vcaballero
                try {
139
                        at1 = at.createInverse();
140
                        at1.transform(pScreen, pWorld);
141
                } catch (NoninvertibleTransformException e) {
142
                }
143 312 fernando
144 1074 vcaballero
                return pWorld;
145
        }
146 312 fernando
147 1074 vcaballero
        /**
148
         * Pasa un ret?ngulo de pixels a coordenadas del folio.
149
         *
150
         * @param r rect?ngulo en coordenadas de pixels a coordenadas de folio.
151
         * @param at Matriz de transformaci?n.
152
         *
153
         * @return Rectangle2D en coordenadas de folio.
154
         */
155
        public static Rectangle2D.Double toSheetRect(Rectangle2D r,
156
                AffineTransform at) {
157
                Point2D.Double pSheet = toSheetPoint(new Point2D.Double(r.getX(),
158
                                        r.getY()), at);
159
                Point2D.Double pSheetX = toSheetPoint(new Point2D.Double(r.getMaxX(),
160
                                        r.getMinY()), at);
161
                Point2D.Double pSheetY = toSheetPoint(new Point2D.Double(r.getMinX(),
162
                                        r.getMaxY()), at);
163
                Rectangle2D.Double res = new Rectangle2D.Double();
164
                res.setRect(pSheet.getX(), pSheet.getY(), pSheet.distance(pSheetX),
165
                        pSheet.distance(pSheetY));
166 312 fernando
167 1074 vcaballero
                return res;
168
        }
169 312 fernando
170 1074 vcaballero
        /**
171
         * Pasa de un punto en coordenadas del folio a pixels.
172
         *
173
         * @param pSheet punto en coordenadas de folio.
174
         * @param at Matriz de transformaci?n.
175
         *
176
         * @return Point2D en pixels.
177
         */
178 3106 caballero
        public static Point2D.Double fromSheetPoint(Point2D pSheet,
179 1074 vcaballero
                AffineTransform at) {
180
                Point2D.Double pScreen = new Point2D.Double();
181 312 fernando
182 1074 vcaballero
                try {
183
                        at.transform(pSheet, pScreen);
184
                } catch (Exception e) {
185
                        System.err.print(e.getMessage());
186
                }
187 312 fernando
188 1074 vcaballero
                return pScreen;
189
        }
190 312 fernando
191 1074 vcaballero
        /**
192
         * Pasa un rect?ngulo en coordenadas del folio a pixels.
193
         *
194
         * @param r rect?ngulo en coordenadas de folio.
195
         * @param at Matriz de transformaci?n.
196
         *
197
         * @return Rectangle2D en pixels.
198
         */
199 2477 caballero
        public static Rectangle2D.Double fromSheetRect(Rectangle2D r,
200 1074 vcaballero
                AffineTransform at) {
201
                Point2D.Double pSheet = new Point2D.Double(r.getX(), r.getY());
202
                Point2D.Double pSX = new Point2D.Double(r.getMaxX(), r.getMinY());
203
                Point2D.Double pSY = new Point2D.Double(r.getMinX(), r.getMaxY());
204
                Point2D.Double pScreen = new Point2D.Double();
205
                Point2D.Double pScreenX = new Point2D.Double();
206
                Point2D.Double pScreenY = new Point2D.Double();
207 312 fernando
208 1074 vcaballero
                try {
209
                        at.transform(pSheet, pScreen);
210
                        at.transform(pSX, pScreenX);
211
                        at.transform(pSY, pScreenY);
212
                } catch (Exception e) {
213
                        System.err.print(e.getMessage());
214
                }
215 312 fernando
216 1074 vcaballero
                Rectangle2D.Double res = new Rectangle2D.Double();
217
                res.setRect(pScreen.getX(), pScreen.getY(), pScreen.distance(pScreenX),
218
                        pScreen.distance(pScreenY));
219 312 fernando
220 1074 vcaballero
                return res;
221
        }
222 312 fernando
223 1074 vcaballero
        /**
224
         * Obtiene el rect?ngulo ajustado al grid del layout.
225
         *
226
         * @param re Rect?ngulo a ajustar.
227
         * @param distX Distancia m?nima en pixels de X.
228
         * @param distY Distancia m?nima en pixels de Y.
229
         * @param at Matriz de transformaci?n.
230
         */
231
        public static void setRectGrid(Rectangle2D re, double distX, double distY,
232
                AffineTransform at) {
233
                Point2D.Double auxp = FLayoutUtilities.fromSheetPoint(new Point2D.Double(
234
                                        0, 0), at);
235
                double x = ((re.getMinX() - distX) % distX) - ((auxp.x) % distX);
236
                double y = ((re.getMinY() - distY) % distY) - ((auxp.y) % distY);
237
                double w = (((re.getWidth() + (distX / 2)) % distX));
238
                double h = (((re.getHeight() + (distY / 2)) % distY));
239
                re.setRect(re.getMinX() - (x), re.getMinY() - (y),
240
                        (re.getWidth() + (distX / 2)) - (w),
241
                        (re.getHeight() + (distY / 2)) - (h));
242
        }
243 312 fernando
244 1074 vcaballero
        /**
245
         * Cuando se dibuja sobre el graphics todo se tiene que situar en enteros y
246
         * aqu? lo que se comprueba es que si los valores que contiene el
247
         * Rectangle2D, que toma como par?metro, supera los valores soportados por
248
         * un entero.
249
         *
250
         * @param r Rectangle2D a comprobar si los valores que contiene no superan
251
         *                   a los que puede tener un entero.
252
         *
253
         * @return true si no se han superado los l?mites.
254
         */
255
        public static boolean isPosible(Rectangle2D.Double r) {
256
                if ((r.getMaxX() > Integer.MAX_VALUE) ||
257
                                (r.getMaxY() > Integer.MAX_VALUE) ||
258
                                (r.getMinX() < Integer.MIN_VALUE) ||
259
                                (r.getMinY() < Integer.MIN_VALUE) ||
260
                                (r.getWidth() > Integer.MAX_VALUE) ||
261
                                (r.getHeight() > Integer.MAX_VALUE)) {
262
                        return false;
263
                }
264 312 fernando
265 1074 vcaballero
                return true;
266
        }
267 312 fernando
268 1074 vcaballero
        /**
269
         * Devuelve un long representando a la escala en funci?n  de que unidad de
270
         * medida se pase como par?metro.
271
         *
272
         * @param map FMap
273
         * @param h Rect?ngulo.
274
         *
275
         * @return escala.
276
         */
277 1513 vcaballero
/*        public static long getScaleView(FMap map, double h) {
278 1074 vcaballero
                if (map == null) {
279
                        return 0;
280
                }
281 312 fernando

282 1074 vcaballero
                long scaleView = 1;
283
                double hextent = map.getViewPort().getExtent().getHeight();
284
                double hview = h;
285
                scaleView = (long) (Attributes.CHANGE[map.getViewPort().getMapUnits()] * (hextent / hview));
286 312 fernando

287 1074 vcaballero
                return scaleView;
288
        }
289 1513 vcaballero
        */
290 3468 caballero
        public static long getScaleView(ViewPort viewPort,double wcm,double wpixels) {
291
                double dpi = wpixels/wcm*2.54;
292 1513 vcaballero
                IProjection proj = viewPort.getProjection();
293
294 3238 caballero
                //if (viewPort.getImageSize() == null)
295
                //    return -1;
296 1513 vcaballero
                if (viewPort.getAdjustedExtent() == null) {
297
                        return 0;
298
                }
299
300 3238 caballero
                if (proj == null || viewPort.getImageSize() == null) {
301 3468 caballero
                        return (long) (viewPort.getAdjustedExtent().getHeight() / wcm * Attributes.CHANGE[viewPort
302 1513 vcaballero
                                                                                                                                                                        .getMapUnits()]);
303
                }
304
305
                return (long) proj.getScale(viewPort.getAdjustedExtent().getMinX(),
306
                        viewPort.getAdjustedExtent().getMaxX(),
307 3468 caballero
                        wpixels, dpi);
308 1513 vcaballero
        }
309
310 312 fernando
}