svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutUtilities.java @ 777
History | View | Annotate | Download (7.71 KB)
1 |
/*
|
---|---|
2 |
* Created on 16-jul-2004
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
package com.iver.cit.gvsig.gui.layout; |
8 |
|
9 |
import com.iver.cit.gvsig.fmap.FMap; |
10 |
|
11 |
import java.awt.geom.AffineTransform; |
12 |
import java.awt.geom.NoninvertibleTransformException; |
13 |
import java.awt.geom.Point2D; |
14 |
import java.awt.geom.Rectangle2D; |
15 |
|
16 |
import java.util.ArrayList; |
17 |
|
18 |
|
19 |
/**
|
20 |
* Clase que recoge m?todos est?ticos sobre el Layout.
|
21 |
*
|
22 |
* @author Vicente Caballero Navarro
|
23 |
*/
|
24 |
public class FLayoutUtilities { |
25 |
/**
|
26 |
* Devuelve true si las dos ArrayList que se le pasan como parametro son
|
27 |
* iguales.
|
28 |
*
|
29 |
* @param n lista anterior
|
30 |
* @param l lista actual
|
31 |
*
|
32 |
* @return true si los ArrayList son iguales.
|
33 |
*/
|
34 |
public static boolean isEqualList(ArrayList n, ArrayList l) { |
35 |
if (n.size() != l.size()) {
|
36 |
return false; |
37 |
} |
38 |
|
39 |
for (int i = 0; i < n.size(); i++) { |
40 |
if (l.get(i) != n.get(i)) {
|
41 |
return false; |
42 |
} |
43 |
} |
44 |
|
45 |
return true; |
46 |
} |
47 |
|
48 |
/**
|
49 |
* Pasa una distancia en pixels a unidades del folio.
|
50 |
*
|
51 |
* @param d distancia en pixels.
|
52 |
* @param at Matriz de transformaci?n.
|
53 |
*
|
54 |
* @return distancia en unidades de folio.
|
55 |
*/
|
56 |
public static double toSheetDistance(double d, AffineTransform at) { |
57 |
double dist = d / at.getScaleX(); // pProv.x; |
58 |
|
59 |
return dist;
|
60 |
} |
61 |
|
62 |
/**
|
63 |
* Pasa una distancia de coordenadas del folio a pixels.
|
64 |
*
|
65 |
* @param d distancia en coordenadas de folio.
|
66 |
* @param at Matriz de transformaci?n.
|
67 |
*
|
68 |
* @return double en pixels.
|
69 |
*/
|
70 |
public static double fromSheetDistance(double d, AffineTransform at) { |
71 |
Point2D.Double pSheet1 = new Point2D.Double(0, 0); |
72 |
Point2D.Double pSheet2 = new Point2D.Double(1, 0); |
73 |
Point2D.Double pScreen1 = new Point2D.Double(); |
74 |
Point2D.Double pScreen2 = new Point2D.Double(); |
75 |
|
76 |
try {
|
77 |
at.transform(pSheet1, pScreen1); |
78 |
at.transform(pSheet2, pScreen2); |
79 |
} catch (Exception e) { |
80 |
System.err.print(e.getMessage());
|
81 |
} |
82 |
|
83 |
return pScreen1.distance(pScreen2) * d;
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* Pasa un punto en pixels a coordenadas del folio.
|
88 |
*
|
89 |
* @param pScreen pixels.
|
90 |
* @param at Matriz de transformaci?n.
|
91 |
*
|
92 |
* @return Point2D en coordenadas de folio.
|
93 |
*/
|
94 |
public static Point2D.Double toSheetPoint(Point2D.Double pScreen, |
95 |
AffineTransform at) {
|
96 |
Point2D.Double pWorld = new Point2D.Double(); |
97 |
AffineTransform at1;
|
98 |
|
99 |
try {
|
100 |
at1 = at.createInverse(); |
101 |
at1.transform(pScreen, pWorld); |
102 |
} catch (NoninvertibleTransformException e) { |
103 |
} |
104 |
|
105 |
return pWorld;
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Pasa un ret?ngulo de pixels a coordenadas del folio.
|
110 |
*
|
111 |
* @param r rect?ngulo en coordenadas de pixels a coordenadas de folio.
|
112 |
* @param at Matriz de transformaci?n.
|
113 |
*
|
114 |
* @return Rectangle2D en coordenadas de folio.
|
115 |
*/
|
116 |
public static Rectangle2D.Double toSheetRect(Rectangle2D r, |
117 |
AffineTransform at) {
|
118 |
Point2D.Double pSheet = toSheetPoint(new Point2D.Double(r.getX(), |
119 |
r.getY()), at); |
120 |
Point2D.Double pSheetX = toSheetPoint(new Point2D.Double(r.getMaxX(), |
121 |
r.getMinY()), at); |
122 |
Point2D.Double pSheetY = toSheetPoint(new Point2D.Double(r.getMinX(), |
123 |
r.getMaxY()), at); |
124 |
Rectangle2D.Double res = new Rectangle2D.Double(); |
125 |
res.setRect(pSheet.getX(), pSheet.getY(), pSheet.distance(pSheetX), |
126 |
pSheet.distance(pSheetY)); |
127 |
|
128 |
return res;
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* Pasa de un punto en coordenadas del folio a pixels.
|
133 |
*
|
134 |
* @param pSheet punto en coordenadas de folio.
|
135 |
* @param at Matriz de transformaci?n.
|
136 |
*
|
137 |
* @return Point2D en pixels.
|
138 |
*/
|
139 |
public static Point2D.Double fromSheetPoint(Point2D.Double pSheet, |
140 |
AffineTransform at) {
|
141 |
Point2D.Double pScreen = new Point2D.Double(); |
142 |
|
143 |
try {
|
144 |
at.transform(pSheet, pScreen); |
145 |
} catch (Exception e) { |
146 |
System.err.print(e.getMessage());
|
147 |
} |
148 |
|
149 |
return pScreen;
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* Pasa un rect?ngulo en coordenadas del folio a pixels.
|
154 |
*
|
155 |
* @param r rect?ngulo en coordenadas de folio.
|
156 |
* @param at Matriz de transformaci?n.
|
157 |
*
|
158 |
* @return Rectangle2D en pixels.
|
159 |
*/
|
160 |
public static Rectangle2D.Double fromSheetRect(Rectangle2D.Double r, |
161 |
AffineTransform at) {
|
162 |
Point2D.Double pSheet = new Point2D.Double(r.getX(), r.getY()); |
163 |
Point2D.Double pSX = new Point2D.Double(r.getMaxX(), r.getMinY()); |
164 |
Point2D.Double pSY = new Point2D.Double(r.getMinX(), r.getMaxY()); |
165 |
Point2D.Double pScreen = new Point2D.Double(); |
166 |
Point2D.Double pScreenX = new Point2D.Double(); |
167 |
Point2D.Double pScreenY = new Point2D.Double(); |
168 |
|
169 |
try {
|
170 |
at.transform(pSheet, pScreen); |
171 |
at.transform(pSX, pScreenX); |
172 |
at.transform(pSY, pScreenY); |
173 |
} catch (Exception e) { |
174 |
System.err.print(e.getMessage());
|
175 |
} |
176 |
|
177 |
Rectangle2D.Double res = new Rectangle2D.Double(); |
178 |
res.setRect(pScreen.getX(), pScreen.getY(), pScreen.distance(pScreenX), |
179 |
pScreen.distance(pScreenY)); |
180 |
|
181 |
return res;
|
182 |
} |
183 |
|
184 |
/**
|
185 |
* Obtiene el rect?ngulo ajustado al grid del layout.
|
186 |
*
|
187 |
* @param re Rect?ngulo a ajustar.
|
188 |
* @param distX Distancia m?nima en pixels de X.
|
189 |
* @param distY Distancia m?nima en pixels de Y.
|
190 |
* @param at Matriz de transformaci?n.
|
191 |
*/
|
192 |
public static void setRectGrid(Rectangle2D re, double distX, double distY, |
193 |
AffineTransform at) {
|
194 |
Point2D.Double auxp = FLayoutUtilities.fromSheetPoint(new Point2D.Double( |
195 |
0, 0), at); |
196 |
double x = ((re.getMinX()-distX) % distX) - ((auxp.x) % distX);
|
197 |
double y = ((re.getMinY()-distY) % distY) - ((auxp.y) % distY);
|
198 |
double w = (((re.getWidth() + (distX / 2)) % distX)); |
199 |
double h = (((re.getHeight() + (distY / 2)) % distY)); |
200 |
re.setRect(re.getMinX() - (x), re.getMinY() - (y), |
201 |
(re.getWidth() + (distX / 2)) - (w),
|
202 |
(re.getHeight() + (distY / 2)) - (h));
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* Cuando se dibuja sobre el graphics todo se tiene que situar en enteros y
|
207 |
* aqu? lo que se comprueba es que si los valores que contiene el
|
208 |
* Rectangle2D, que toma como par?metro, supera los valores soportados por
|
209 |
* un entero.
|
210 |
*
|
211 |
* @param r Rectangle2D a comprobar si los valores que contiene no superan
|
212 |
* a los que puede tener un entero.
|
213 |
*
|
214 |
* @return true si no se han superado los l?mites.
|
215 |
*/
|
216 |
public static boolean isPosible(Rectangle2D.Double r) { |
217 |
if ((r.getMaxX() > Integer.MAX_VALUE) || |
218 |
(r.getMaxY() > Integer.MAX_VALUE) ||
|
219 |
(r.getMinX() < Integer.MIN_VALUE) ||
|
220 |
(r.getMinY() < Integer.MIN_VALUE) ||
|
221 |
(r.getWidth() > Integer.MAX_VALUE) ||
|
222 |
(r.getHeight() > Integer.MAX_VALUE)) {
|
223 |
return false; |
224 |
} |
225 |
|
226 |
return true; |
227 |
} |
228 |
|
229 |
/**
|
230 |
* Devuelve un long representando a la escala en funci?n de que unidad de
|
231 |
* medida se pase como par?metro.
|
232 |
*
|
233 |
* @param map FMap
|
234 |
* @param h Rect?ngulo.
|
235 |
* @param unit unidad de medida.
|
236 |
*
|
237 |
* @return escala.
|
238 |
*/
|
239 |
public static long getScaleView(FMap map, double h) { |
240 |
if (map == null) { |
241 |
return 0; |
242 |
} |
243 |
|
244 |
long scaleView = 1; |
245 |
double hextent = map.getViewPort().getExtent().getHeight();
|
246 |
double hview = h;
|
247 |
scaleView = (long) (Attributes.CHANGE[map.getViewPort().getMapUnits()] * (hextent / hview)); |
248 |
|
249 |
return scaleView;
|
250 |
} |
251 |
} |