Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutUtilities.java @ 28368

History | View | Annotate | Download (8.48 KB)

1
/*
2
 * Created on 16-jul-2004
3
 *
4
 */
5
/* 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
package com.iver.cit.gvsig.project.documents.layout;
46

    
47
import java.awt.Point;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.NoninvertibleTransformException;
50
import java.awt.geom.Point2D;
51
import java.awt.geom.Rectangle2D;
52
import java.util.ArrayList;
53

    
54
import org.cresques.cts.IProjection;
55

    
56
import com.iver.cit.gvsig.fmap.ViewPort;
57

    
58

    
59
/**
60
 * Clase que recoge m?todos est?ticos sobre el Layout.
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class FLayoutUtilities {
65
        private static double scale=0;
66
        private static Point2D.Double auxp;
67
        private static double distX=0;
68
        private static double distY=0;
69
        /**
70
         * Devuelve true si las dos ArrayList que se le pasan como parametro son
71
         * iguales.
72
         *
73
         * @param n lista anterior
74
         * @param l lista actual
75
         *
76
         * @return true si los ArrayList son iguales.
77
         */
78
        public static boolean isEqualList(ArrayList n, ArrayList l) {
79
                if (n.size() != l.size()) {
80
                        return false;
81
                }
82

    
83
                for (int i = 0; i < n.size(); i++) {
84
                        if (l.get(i) != n.get(i)) {
85
                                return false;
86
                        }
87
                }
88

    
89
                return true;
90
        }
91

    
92
        /**
93
         * Pasa una distancia en pixels a unidades del folio.
94
         *
95
         * @param d distancia en pixels.
96
         * @param at Matriz de transformaci?n.
97
         *
98
         * @return distancia en unidades de folio.
99
         */
100
        public static double toSheetDistance(double d, AffineTransform at) {
101
                double dist = d / at.getScaleX(); // pProv.x;
102

    
103
                return dist;
104
        }
105

    
106
        /**
107
         * Pasa una distancia de coordenadas del folio a pixels.
108
         *
109
         * @param d distancia en coordenadas de folio.
110
         * @param at Matriz de transformaci?n.
111
         *
112
         * @return double en pixels.
113
         */
114
        public static double fromSheetDistance(double d, AffineTransform at) {
115
                Point2D.Double pSheet1 = new Point2D.Double(0, 0);
116
                Point2D.Double pSheet2 = new Point2D.Double(1, 0);
117
                Point2D.Double pScreen1 = new Point2D.Double();
118
                Point2D.Double pScreen2 = new Point2D.Double();
119

    
120
                try {
121
                        at.transform(pSheet1, pScreen1);
122
                        at.transform(pSheet2, pScreen2);
123
                } catch (Exception e) {
124
                        System.err.print(e.getMessage());
125
                }
126

    
127
                return pScreen1.distance(pScreen2) * d;
128
        }
129

    
130
        /**
131
         * Pasa un punto en pixels a coordenadas del folio.
132
         *
133
         * @param pScreen pixels.
134
         * @param at Matriz de transformaci?n.
135
         *
136
         * @return Point2D en coordenadas de folio.
137
         */
138
        public static Point2D.Double toSheetPoint(Point2D pScreen,
139
                AffineTransform at) {
140
                Point2D.Double pWorld = new Point2D.Double();
141
                AffineTransform at1;
142

    
143
                try {
144
                        at1 = at.createInverse();
145
                        at1.transform(pScreen, pWorld);
146
                } catch (NoninvertibleTransformException e) {
147
                }
148

    
149
                return pWorld;
150
        }
151

    
152
        /**
153
         * Pasa un ret?ngulo de pixels a coordenadas del folio.
154
         *
155
         * @param r rect?ngulo en coordenadas de pixels a coordenadas de folio.
156
         * @param at Matriz de transformaci?n.
157
         *
158
         * @return Rectangle2D en coordenadas de folio.
159
         */
160
        public static Rectangle2D.Double toSheetRect(Rectangle2D r,
161
                AffineTransform at) {
162
                Point2D.Double pSheet = toSheetPoint(new Point2D.Double(r.getX(),
163
                                        r.getY()), at);
164
                Point2D.Double pSheetX = toSheetPoint(new Point2D.Double(r.getMaxX(),
165
                                        r.getMinY()), at);
166
                Point2D.Double pSheetY = toSheetPoint(new Point2D.Double(r.getMinX(),
167
                                        r.getMaxY()), at);
168
                Rectangle2D.Double res = new Rectangle2D.Double();
169
                res.setRect(pSheet.getX(), pSheet.getY(), pSheet.distance(pSheetX),
170
                        pSheet.distance(pSheetY));
171

    
172
                return res;
173
        }
174

    
175
        /**
176
         * Pasa de un punto en coordenadas del folio a pixels.
177
         *
178
         * @param pSheet punto en coordenadas de folio.
179
         * @param at Matriz de transformaci?n.
180
         *
181
         * @return Point2D en pixels.
182
         */
183
        public static Point2D.Double fromSheetPoint(Point2D pSheet,
184
                AffineTransform at) {
185
                Point2D.Double pScreen = new Point2D.Double();
186

    
187
                try {
188
                        at.transform(pSheet, pScreen);
189
                } catch (Exception e) {
190
                        System.err.print(e.getMessage());
191
                }
192

    
193
                return pScreen;
194
        }
195

    
196
        /**
197
         * Pasa un rect?ngulo en coordenadas del folio a pixels.
198
         *
199
         * @param r rect?ngulo en coordenadas de folio.
200
         * @param at Matriz de transformaci?n.
201
         *
202
         * @return Rectangle2D en pixels.
203
         */
204
        public static Rectangle2D.Double fromSheetRect(Rectangle2D r,
205
                AffineTransform at) {
206
                Point2D.Double pSheet = new Point2D.Double(r.getX(), r.getY());
207
                Point2D.Double pSX = new Point2D.Double(r.getMaxX(), r.getMinY());
208
                Point2D.Double pSY = new Point2D.Double(r.getMinX(), r.getMaxY());
209
                Point2D.Double pScreen = new Point2D.Double();
210
                Point2D.Double pScreenX = new Point2D.Double();
211
                Point2D.Double pScreenY = new Point2D.Double();
212

    
213
                try {
214
                        at.transform(pSheet, pScreen);
215
                        at.transform(pSX, pScreenX);
216
                        at.transform(pSY, pScreenY);
217
                } catch (Exception e) {
218
                        System.err.print(e.getMessage());
219
                }
220

    
221
                Rectangle2D.Double res = new Rectangle2D.Double();
222
                res.setRect(pScreen.getX(), pScreen.getY(), pScreen.distance(pScreenX),
223
                        pScreen.distance(pScreenY));
224

    
225
                return res;
226
        }
227

    
228
        /**
229
         * Obtiene el punto ajustado al grid del layout.
230
         *
231
         * @param p Punto a ajustar.
232
         * @param distX Distancia m?nima en pixels de X.
233
         * @param distY Distancia m?nima en pixels de Y.
234
         * @param at Matriz de transformaci?n.
235
         */
236
        public static Point getPointGrid(Point p, double dX, double dY,
237
                        AffineTransform at) {
238
                if (scale!=at.getScaleX()){
239
                        scale = at.getScaleX();
240
                        auxp = FLayoutUtilities.fromSheetPoint(new Point2D.Double(
241
                                                0, 0), at);
242
                        distX=FLayoutUtilities.fromSheetDistance(dX, at);
243
                        distY=FLayoutUtilities.fromSheetDistance(dY, at);
244
                }
245
                int x = (int)(((p.getX() - distX) % distX) - ((auxp.x) % distX));
246
                int y = (int)(((p.getY() - distY) % distY) - ((auxp.y) % distY));
247
                return new Point((int)(p.getX()-x),(int)(p.getY()-y));
248
        }
249

    
250
        /**
251
         * Cuando se dibuja sobre el graphics todo se tiene que situar en enteros y
252
         * aqu? lo que se comprueba es que si los valores que contiene el
253
         * Rectangle2D, que toma como par?metro, supera los valores soportados por
254
         * un entero.
255
         *
256
         * @param r Rectangle2D a comprobar si los valores que contiene no superan
257
         *                   a los que puede tener un entero.
258
         *
259
         * @return true si no se han superado los l?mites.
260
         */
261
        public static boolean isPosible(Rectangle2D.Double r) {
262
                if ((r.getMaxX() > Integer.MAX_VALUE) ||
263
                                (r.getMaxY() > Integer.MAX_VALUE) ||
264
                                (r.getMinX() < Integer.MIN_VALUE) ||
265
                                (r.getMinY() < Integer.MIN_VALUE) ||
266
                                (r.getWidth() > Integer.MAX_VALUE) ||
267
                                (r.getHeight() > Integer.MAX_VALUE)) {
268
                        return false;
269
                }
270

    
271
                return true;
272
        }
273

    
274
        /**
275
         * Devuelve un long representando a la escala en funci?n  de que unidad de
276
         * medida se pase como par?metro.
277
         *
278
         * @param map FMap
279
         * @param h Rect?ngulo.
280
         *
281
         * @return escala.
282
         */
283
/*        public static long getScaleView(FMap map, double h) {
284
                if (map == null) {
285
                        return 0;
286
                }
287

288
                long scaleView = 1;
289
                double hextent = map.getViewPort().getExtent().getHeight();
290
                double hview = h;
291
                scaleView = (long) (Attributes.CHANGE[map.getViewPort().getMapUnits()] * (hextent / hview));
292

293
                return scaleView;
294
        }
295
        */
296
        public static long getScaleView(ViewPort viewPort,double wcm,double wpixels) {
297
                double dpi = wpixels/wcm*2.54;
298
                IProjection proj = viewPort.getProjection();
299

    
300
                //if (viewPort.getImageSize() == null)
301
                //    return -1;
302
                if (viewPort.getAdjustedExtent() == null) {
303
                        return 0;
304
                }
305

    
306
                if (proj == null || viewPort.getImageSize() == null) {
307
                        return (long) (viewPort.getAdjustedExtent().getHeight() / wcm * Attributes.CHANGE[viewPort
308
                                                                                                                                                                        .getMapUnits()]);
309
                }
310

    
311
                return (long) proj.getScale(viewPort.getAdjustedExtent().getMinX(),
312
                        viewPort.getAdjustedExtent().getMaxX(),
313
                        wpixels, dpi);
314
        }
315

    
316
}