Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutUtilities.java @ 36648

History | View | Annotate | Download (9.51 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout;
23

    
24
import java.awt.Point;
25
import java.awt.geom.AffineTransform;
26
import java.awt.geom.NoninvertibleTransformException;
27
import java.awt.geom.Point2D;
28
import java.awt.geom.Rectangle2D;
29
import java.util.ArrayList;
30

    
31
import org.cresques.cts.IProjection;
32

    
33
import org.gvsig.fmap.mapcontext.ViewPort;
34

    
35
/**
36
 * Clase que recoge m?todos est?ticos sobre el Layout.
37
 * 
38
 * @author Vicente Caballero Navarro
39
 */
40
public class FLayoutUtilities {
41

    
42
    /**
43
     * Devuelve true si las dos ArrayList que se le pasan como parametro son
44
     * iguales.
45
     * 
46
     * @param n
47
     *            lista anterior
48
     * @param l
49
     *            lista actual
50
     * 
51
     * @return true si los ArrayList son iguales.
52
     */
53
    public static boolean isEqualList(ArrayList n, ArrayList l) {
54
        if (n.size() != l.size()) {
55
            return false;
56
        }
57

    
58
        for (int i = 0; i < n.size(); i++) {
59
            if (l.get(i) != n.get(i)) {
60
                return false;
61
            }
62
        }
63

    
64
        return true;
65
    }
66

    
67
    /**
68
     * Pasa una distancia en pixels a unidades del folio.
69
     * 
70
     * @param d
71
     *            distancia en pixels.
72
     * @param at
73
     *            Matriz de transformaci?n.
74
     * 
75
     * @return distancia en unidades de folio.
76
     */
77
    public static double toSheetDistance(double d, AffineTransform at) {
78
        double dist = d / at.getScaleX(); // pProv.x;
79

    
80
        return dist;
81
    }
82

    
83
    /**
84
     * Pasa una distancia de coordenadas del folio a pixels.
85
     * 
86
     * @param d
87
     *            distancia en coordenadas de folio.
88
     * @param at
89
     *            Matriz de transformaci?n.
90
     * 
91
     * @return double en pixels.
92
     */
93
    public static double fromSheetDistance(double d, AffineTransform at) {
94
        Point2D.Double pSheet1 = new Point2D.Double(0, 0);
95
        Point2D.Double pSheet2 = new Point2D.Double(1, 0);
96
        Point2D.Double pScreen1 = new Point2D.Double();
97
        Point2D.Double pScreen2 = new Point2D.Double();
98

    
99
        try {
100
            at.transform(pSheet1, pScreen1);
101
            at.transform(pSheet2, pScreen2);
102
        } catch (Exception e) {
103
            System.err.print(e.getMessage());
104
        }
105

    
106
        return pScreen1.distance(pScreen2) * d;
107
    }
108

    
109
    /**
110
     * Pasa un punto en pixels a coordenadas del folio.
111
     * 
112
     * @param pScreen
113
     *            pixels.
114
     * @param at
115
     *            Matriz de transformaci?n.
116
     * 
117
     * @return Point2D en coordenadas de folio.
118
     */
119
    public static Point2D.Double toSheetPoint(Point2D pScreen,
120
        AffineTransform at) {
121
        Point2D.Double pWorld = new Point2D.Double();
122
        AffineTransform at1;
123

    
124
        try {
125
            at1 = at.createInverse();
126
            at1.transform(pScreen, pWorld);
127
        } catch (NoninvertibleTransformException e) {
128
        }
129

    
130
        return pWorld;
131
    }
132

    
133
    /**
134
     * Pasa un ret?ngulo de pixels a coordenadas del folio.
135
     * 
136
     * @param r
137
     *            rect?ngulo en coordenadas de pixels a coordenadas de folio.
138
     * @param at
139
     *            Matriz de transformaci?n.
140
     * 
141
     * @return Rectangle2D en coordenadas de folio.
142
     */
143
    public static Rectangle2D.Double toSheetRect(Rectangle2D r,
144
        AffineTransform at) {
145
        Point2D.Double pSheet =
146
            toSheetPoint(new Point2D.Double(r.getX(), r.getY()), at);
147
        Point2D.Double pSheetX =
148
            toSheetPoint(new Point2D.Double(r.getMaxX(), r.getMinY()), at);
149
        Point2D.Double pSheetY =
150
            toSheetPoint(new Point2D.Double(r.getMinX(), r.getMaxY()), at);
151
        Rectangle2D.Double res = new Rectangle2D.Double();
152
        res.setRect(pSheet.getX(), pSheet.getY(), pSheet.distance(pSheetX),
153
            pSheet.distance(pSheetY));
154

    
155
        return res;
156
    }
157

    
158
    /**
159
     * Pasa de un punto en coordenadas del folio a pixels.
160
     * 
161
     * @param pSheet
162
     *            punto en coordenadas de folio.
163
     * @param at
164
     *            Matriz de transformaci?n.
165
     * 
166
     * @return Point2D en pixels.
167
     */
168
    public static Point2D.Double fromSheetPoint(Point2D pSheet,
169
        AffineTransform at) {
170
        Point2D.Double pScreen = new Point2D.Double();
171

    
172
        try {
173
            at.transform(pSheet, pScreen);
174
        } catch (Exception e) {
175
            System.err.print(e.getMessage());
176
        }
177

    
178
        return pScreen;
179
    }
180

    
181
    /**
182
     * Pasa un rect?ngulo en coordenadas del folio a pixels.
183
     * 
184
     * @param r
185
     *            rect?ngulo en coordenadas de folio.
186
     * @param at
187
     *            Matriz de transformaci?n.
188
     * 
189
     * @return Rectangle2D en pixels.
190
     */
191
    public static Rectangle2D.Double fromSheetRect(Rectangle2D r,
192
        AffineTransform at) {
193
        Point2D.Double pSheet = new Point2D.Double(r.getX(), r.getY());
194
        Point2D.Double pSX = new Point2D.Double(r.getMaxX(), r.getMinY());
195
        Point2D.Double pSY = new Point2D.Double(r.getMinX(), r.getMaxY());
196
        Point2D.Double pScreen = new Point2D.Double();
197
        Point2D.Double pScreenX = new Point2D.Double();
198
        Point2D.Double pScreenY = new Point2D.Double();
199

    
200
        try {
201
            at.transform(pSheet, pScreen);
202
            at.transform(pSX, pScreenX);
203
            at.transform(pSY, pScreenY);
204
        } catch (Exception e) {
205
            System.err.print(e.getMessage());
206
        }
207

    
208
        Rectangle2D.Double res = new Rectangle2D.Double();
209
        res.setRect(pScreen.getX(), pScreen.getY(), pScreen.distance(pScreenX),
210
            pScreen.distance(pScreenY));
211

    
212
        return res;
213
    }
214

    
215
    /**
216
     * Obtiene el punto ajustado al grid del layout.
217
     * 
218
     * @param p
219
     *            Punto a ajustar.
220
     * @param distX
221
     *            Distancia m?nima en pixels de X.
222
     * @param distY
223
     *            Distancia m?nima en pixels de Y.
224
     * @param at
225
     *            Matriz de transformaci?n.
226
     */
227
    public static Point getPointGrid(Point p, double distX, double distY,
228
        AffineTransform at) {
229

    
230
        Point2D.Double auxp =
231
            FLayoutUtilities.fromSheetPoint(new Point2D.Double(0, 0), at);
232
        int x = (int) (((p.getX()) % distX) - ((auxp.x) % distX));
233
        int y = (int) (((p.getY()) % distY) - ((auxp.y) % distY));
234
        if (x < distX / 2) {
235
            x = -x;
236
        } else {
237
            x = (int) (distX - x);
238
        }
239
        if (y < distY / 2) {
240
            y = -y;
241
        } else {
242
            y = (int) (distY - y);
243
        }
244
        return new Point((int) (p.getX() + x), (int) (p.getY() + y));
245
    }
246

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

    
269
        return true;
270
    }
271

    
272
    /**
273
     * Devuelve un long representando a la escala en funci?n de que unidad de
274
     * medida se pase como par?metro.
275
     * 
276
     * @param map
277
     *            FMap
278
     * @param h
279
     *            Rect?ngulo.
280
     * 
281
     * @return escala.
282
     */
283
    /*
284
     * public static long getScaleView(FMap map, double h) {
285
     * if (map == null) {
286
     * return 0;
287
     * }
288
     * 
289
     * long scaleView = 1;
290
     * double hextent = map.getViewPort().getExtent().getHeight();
291
     * double hview = h;
292
     * scaleView = (long) (Attributes.CHANGE[map.getViewPort().getMapUnits()] *
293
     * (hextent / hview));
294
     * 
295
     * return scaleView;
296
     * }
297
     */
298
    public static long getScaleView(ViewPort viewPort, double wcm,
299
        double wpixels) {
300
        double dpi = wpixels / wcm * 2.54;
301
        IProjection proj = viewPort.getProjection();
302

    
303
        // if (viewPort.getImageSize() == null)
304
        // return -1;
305
        if (viewPort.getAdjustedExtent() == null) {
306
            return 0;
307
        }
308

    
309
        if (proj == null || viewPort.getImageSize() == null) {
310
            return (long) (viewPort.getAdjustedExtent().getLength(1) / wcm * Attributes.CHANGE[viewPort
311
                .getMapUnits()]);
312
        }
313

    
314
        return (long) proj.getScale(viewPort.getAdjustedExtent().getMinimum(0),
315
            viewPort.getAdjustedExtent().getMaximum(0), wpixels, dpi);
316
    }
317

    
318
}