root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutUtilities.java @ 1103
History | View | Annotate | Download (7.75 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.gui.layout; |
46 |
|
47 |
import com.iver.cit.gvsig.fmap.FMap; |
48 |
|
49 |
import java.awt.geom.AffineTransform; |
50 |
import java.awt.geom.NoninvertibleTransformException; |
51 |
import java.awt.geom.Point2D; |
52 |
import java.awt.geom.Rectangle2D; |
53 |
|
54 |
import java.util.ArrayList; |
55 |
|
56 |
|
57 |
/**
|
58 |
* Clase que recoge m?todos est?ticos sobre el Layout.
|
59 |
*
|
60 |
* @author Vicente Caballero Navarro
|
61 |
*/
|
62 |
public class FLayoutUtilities { |
63 |
/**
|
64 |
* Devuelve true si las dos ArrayList que se le pasan como parametro son
|
65 |
* iguales.
|
66 |
*
|
67 |
* @param n lista anterior
|
68 |
* @param l lista actual
|
69 |
*
|
70 |
* @return true si los ArrayList son iguales.
|
71 |
*/
|
72 |
public static boolean isEqualList(ArrayList n, ArrayList l) { |
73 |
if (n.size() != l.size()) {
|
74 |
return false; |
75 |
} |
76 |
|
77 |
for (int i = 0; i < n.size(); i++) { |
78 |
if (l.get(i) != n.get(i)) {
|
79 |
return false; |
80 |
} |
81 |
} |
82 |
|
83 |
return true; |
84 |
} |
85 |
|
86 |
/**
|
87 |
* Pasa una distancia en pixels a unidades del folio.
|
88 |
*
|
89 |
* @param d distancia en pixels.
|
90 |
* @param at Matriz de transformaci?n.
|
91 |
*
|
92 |
* @return distancia en unidades de folio.
|
93 |
*/
|
94 |
public static double toSheetDistance(double d, AffineTransform at) { |
95 |
double dist = d / at.getScaleX(); // pProv.x; |
96 |
|
97 |
return dist;
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* Pasa una distancia de coordenadas del folio a pixels.
|
102 |
*
|
103 |
* @param d distancia en coordenadas de folio.
|
104 |
* @param at Matriz de transformaci?n.
|
105 |
*
|
106 |
* @return double en pixels.
|
107 |
*/
|
108 |
public static double fromSheetDistance(double d, AffineTransform at) { |
109 |
Point2D.Double pSheet1 = new Point2D.Double(0, 0); |
110 |
Point2D.Double pSheet2 = new Point2D.Double(1, 0); |
111 |
Point2D.Double pScreen1 = new Point2D.Double(); |
112 |
Point2D.Double pScreen2 = new Point2D.Double(); |
113 |
|
114 |
try {
|
115 |
at.transform(pSheet1, pScreen1); |
116 |
at.transform(pSheet2, pScreen2); |
117 |
} catch (Exception e) { |
118 |
System.err.print(e.getMessage());
|
119 |
} |
120 |
|
121 |
return pScreen1.distance(pScreen2) * d;
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* Pasa un punto en pixels a coordenadas del folio.
|
126 |
*
|
127 |
* @param pScreen pixels.
|
128 |
* @param at Matriz de transformaci?n.
|
129 |
*
|
130 |
* @return Point2D en coordenadas de folio.
|
131 |
*/
|
132 |
public static Point2D.Double toSheetPoint(Point2D.Double pScreen, |
133 |
AffineTransform at) {
|
134 |
Point2D.Double pWorld = new Point2D.Double(); |
135 |
AffineTransform at1;
|
136 |
|
137 |
try {
|
138 |
at1 = at.createInverse(); |
139 |
at1.transform(pScreen, pWorld); |
140 |
} catch (NoninvertibleTransformException e) { |
141 |
} |
142 |
|
143 |
return pWorld;
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* Pasa un ret?ngulo de pixels a coordenadas del folio.
|
148 |
*
|
149 |
* @param r rect?ngulo en coordenadas de pixels a coordenadas de folio.
|
150 |
* @param at Matriz de transformaci?n.
|
151 |
*
|
152 |
* @return Rectangle2D en coordenadas de folio.
|
153 |
*/
|
154 |
public static Rectangle2D.Double toSheetRect(Rectangle2D r, |
155 |
AffineTransform at) {
|
156 |
Point2D.Double pSheet = toSheetPoint(new Point2D.Double(r.getX(), |
157 |
r.getY()), at); |
158 |
Point2D.Double pSheetX = toSheetPoint(new Point2D.Double(r.getMaxX(), |
159 |
r.getMinY()), at); |
160 |
Point2D.Double pSheetY = toSheetPoint(new Point2D.Double(r.getMinX(), |
161 |
r.getMaxY()), at); |
162 |
Rectangle2D.Double res = new Rectangle2D.Double(); |
163 |
res.setRect(pSheet.getX(), pSheet.getY(), pSheet.distance(pSheetX), |
164 |
pSheet.distance(pSheetY)); |
165 |
|
166 |
return res;
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Pasa de un punto en coordenadas del folio a pixels.
|
171 |
*
|
172 |
* @param pSheet punto en coordenadas de folio.
|
173 |
* @param at Matriz de transformaci?n.
|
174 |
*
|
175 |
* @return Point2D en pixels.
|
176 |
*/
|
177 |
public static Point2D.Double fromSheetPoint(Point2D.Double pSheet, |
178 |
AffineTransform at) {
|
179 |
Point2D.Double pScreen = new Point2D.Double(); |
180 |
|
181 |
try {
|
182 |
at.transform(pSheet, pScreen); |
183 |
} catch (Exception e) { |
184 |
System.err.print(e.getMessage());
|
185 |
} |
186 |
|
187 |
return pScreen;
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* Pasa un rect?ngulo en coordenadas del folio a pixels.
|
192 |
*
|
193 |
* @param r rect?ngulo en coordenadas de folio.
|
194 |
* @param at Matriz de transformaci?n.
|
195 |
*
|
196 |
* @return Rectangle2D en pixels.
|
197 |
*/
|
198 |
public static Rectangle2D.Double fromSheetRect(Rectangle2D.Double r, |
199 |
AffineTransform at) {
|
200 |
Point2D.Double pSheet = new Point2D.Double(r.getX(), r.getY()); |
201 |
Point2D.Double pSX = new Point2D.Double(r.getMaxX(), r.getMinY()); |
202 |
Point2D.Double pSY = new Point2D.Double(r.getMinX(), r.getMaxY()); |
203 |
Point2D.Double pScreen = new Point2D.Double(); |
204 |
Point2D.Double pScreenX = new Point2D.Double(); |
205 |
Point2D.Double pScreenY = new Point2D.Double(); |
206 |
|
207 |
try {
|
208 |
at.transform(pSheet, pScreen); |
209 |
at.transform(pSX, pScreenX); |
210 |
at.transform(pSY, pScreenY); |
211 |
} catch (Exception e) { |
212 |
System.err.print(e.getMessage());
|
213 |
} |
214 |
|
215 |
Rectangle2D.Double res = new Rectangle2D.Double(); |
216 |
res.setRect(pScreen.getX(), pScreen.getY(), pScreen.distance(pScreenX), |
217 |
pScreen.distance(pScreenY)); |
218 |
|
219 |
return res;
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Obtiene el rect?ngulo ajustado al grid del layout.
|
224 |
*
|
225 |
* @param re Rect?ngulo a ajustar.
|
226 |
* @param distX Distancia m?nima en pixels de X.
|
227 |
* @param distY Distancia m?nima en pixels de Y.
|
228 |
* @param at Matriz de transformaci?n.
|
229 |
*/
|
230 |
public static void setRectGrid(Rectangle2D re, double distX, double distY, |
231 |
AffineTransform at) {
|
232 |
Point2D.Double auxp = FLayoutUtilities.fromSheetPoint(new Point2D.Double( |
233 |
0, 0), at); |
234 |
double x = ((re.getMinX() - distX) % distX) - ((auxp.x) % distX);
|
235 |
double y = ((re.getMinY() - distY) % distY) - ((auxp.y) % distY);
|
236 |
double w = (((re.getWidth() + (distX / 2)) % distX)); |
237 |
double h = (((re.getHeight() + (distY / 2)) % distY)); |
238 |
re.setRect(re.getMinX() - (x), re.getMinY() - (y), |
239 |
(re.getWidth() + (distX / 2)) - (w),
|
240 |
(re.getHeight() + (distY / 2)) - (h));
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* Cuando se dibuja sobre el graphics todo se tiene que situar en enteros y
|
245 |
* aqu? lo que se comprueba es que si los valores que contiene el
|
246 |
* Rectangle2D, que toma como par?metro, supera los valores soportados por
|
247 |
* un entero.
|
248 |
*
|
249 |
* @param r Rectangle2D a comprobar si los valores que contiene no superan
|
250 |
* a los que puede tener un entero.
|
251 |
*
|
252 |
* @return true si no se han superado los l?mites.
|
253 |
*/
|
254 |
public static boolean isPosible(Rectangle2D.Double r) { |
255 |
if ((r.getMaxX() > Integer.MAX_VALUE) || |
256 |
(r.getMaxY() > Integer.MAX_VALUE) ||
|
257 |
(r.getMinX() < Integer.MIN_VALUE) ||
|
258 |
(r.getMinY() < Integer.MIN_VALUE) ||
|
259 |
(r.getWidth() > Integer.MAX_VALUE) ||
|
260 |
(r.getHeight() > Integer.MAX_VALUE)) {
|
261 |
return false; |
262 |
} |
263 |
|
264 |
return true; |
265 |
} |
266 |
|
267 |
/**
|
268 |
* Devuelve un long representando a la escala en funci?n de que unidad de
|
269 |
* medida se pase como par?metro.
|
270 |
*
|
271 |
* @param map FMap
|
272 |
* @param h Rect?ngulo.
|
273 |
*
|
274 |
* @return escala.
|
275 |
*/
|
276 |
public static long getScaleView(FMap map, double h) { |
277 |
if (map == null) { |
278 |
return 0; |
279 |
} |
280 |
|
281 |
long scaleView = 1; |
282 |
double hextent = map.getViewPort().getExtent().getHeight();
|
283 |
double hview = h;
|
284 |
scaleView = (long) (Attributes.CHANGE[map.getViewPort().getMapUnits()] * (hextent / hview)); |
285 |
|
286 |
return scaleView;
|
287 |
} |
288 |
} |