svn-document-layout / trunk / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutFunctions.java @ 110
History | View | Annotate | Download (11.6 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.Rectangle; |
26 |
import java.awt.geom.AffineTransform; |
27 |
import java.awt.geom.NoninvertibleTransformException; |
28 |
import java.awt.geom.Point2D; |
29 |
import java.awt.geom.Rectangle2D; |
30 |
import java.awt.print.PageFormat; |
31 |
|
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
import org.gvsig.app.project.documents.layout.commands.FrameCommandsRecord; |
36 |
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup; |
37 |
import org.gvsig.app.project.documents.layout.fframes.IFFrame; |
38 |
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap; |
39 |
import org.gvsig.app.project.documents.layout.gui.LayoutPanel; |
40 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
41 |
import org.gvsig.fmap.geom.GeometryLocator; |
42 |
import org.gvsig.fmap.geom.GeometryManager; |
43 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
44 |
import org.gvsig.fmap.geom.primitive.Envelope; |
45 |
import org.gvsig.tools.observer.Observable; |
46 |
import org.gvsig.tools.observer.ObservableHelper; |
47 |
import org.gvsig.tools.observer.Observer; |
48 |
|
49 |
/**
|
50 |
* Funciones utilizadas desde el Layout.
|
51 |
*
|
52 |
* @author Vicente Caballero Navarro
|
53 |
*/
|
54 |
public class FLayoutFunctions implements Observable { |
55 |
|
56 |
protected static final Logger LOG = LoggerFactory |
57 |
.getLogger(FLayoutFunctions.class); |
58 |
private static final GeometryManager geomManager = GeometryLocator |
59 |
.getGeometryManager(); |
60 |
private static final Logger logger = LoggerFactory |
61 |
.getLogger(FLayoutFunctions.class); |
62 |
|
63 |
private LayoutPanel layout = null; |
64 |
private ObservableHelper observers;
|
65 |
/**
|
66 |
* Crea un nuevo FLayoutFunctions.
|
67 |
*
|
68 |
* @param l
|
69 |
* Referencia al Layout.
|
70 |
*/
|
71 |
public FLayoutFunctions(LayoutPanel layoutPanel) {
|
72 |
layout = layoutPanel; |
73 |
observers = new ObservableHelper();
|
74 |
observers.addObserver(layoutPanel.getLayoutControl()); |
75 |
} |
76 |
|
77 |
/**
|
78 |
* Gestiona la herramienta de selecci?n sobre el Mapa.
|
79 |
*/
|
80 |
public void setSelect() { |
81 |
IFFrame fframe = null;
|
82 |
boolean isUpdate = false; |
83 |
layout.getLayoutContext().updateFFrames(); |
84 |
IFFrame[] fframes = layout.getLayoutContext().getFFrames();
|
85 |
FrameCommandsRecord efs = |
86 |
layout.getLayoutContext().getFrameCommandsRecord(); |
87 |
efs.startComplex("move");
|
88 |
for (int i = 0; i < fframes.length; i++) { |
89 |
fframe = fframes[i]; |
90 |
|
91 |
int difx =
|
92 |
(layout.getLayoutControl().getLastPoint().x - layout |
93 |
.getLayoutControl().getFirstPoint().x); |
94 |
int dify =
|
95 |
(layout.getLayoutControl().getLastPoint().y - layout |
96 |
.getLayoutControl().getFirstPoint().y); |
97 |
|
98 |
if (((Math.abs(difx) > 3) || (Math.abs(dify) > 3)) |
99 |
&& (fframe.getSelected() != IFFrame.NOSELECT)) { |
100 |
Rectangle2D rectangle = fframe.getLastMoveRect();
|
101 |
if (rectangle == null) { |
102 |
efs.endComplex(); |
103 |
return;
|
104 |
} |
105 |
|
106 |
if (fframe instanceof FFrameGroup) { |
107 |
((FFrameGroup) fframe).setAt(layout.getLayoutControl() |
108 |
.getAT()); |
109 |
} |
110 |
|
111 |
IFFrame fframeAux; |
112 |
try {
|
113 |
fframeAux = (IFFrame) fframe.clone(); |
114 |
fframeAux.setBoundBox(FLayoutUtilities.toSheetRect( |
115 |
rectangle, layout.getLayoutControl().getAT())); |
116 |
efs.update(fframe, fframeAux); |
117 |
fframeAux.getBoundingBox(layout.getLayoutControl().getAT()); |
118 |
|
119 |
isUpdate = true;
|
120 |
} catch (CloneNotSupportedException e) { |
121 |
LOG.error("It is not possible clonate the object", e);
|
122 |
} |
123 |
|
124 |
} |
125 |
Rectangle rect = null; |
126 |
if (layout.getLayoutControl().getReSel() == null) { |
127 |
rect = new Rectangle(); |
128 |
rect.setFrameFromDiagonal(layout.getLayoutControl() |
129 |
.getFirstPoint(), layout.getLayoutControl().getLastPoint()); |
130 |
} else {
|
131 |
rect = layout.getLayoutControl().getReSel(); |
132 |
} |
133 |
if (layout.getLayoutControl().isReSel()
|
134 |
&& (rect.contains(fframe.getBoundingBox(layout |
135 |
.getLayoutControl().getAT())))) { |
136 |
fframe.setSelected(true);
|
137 |
} |
138 |
|
139 |
if (isUpdate) {
|
140 |
observers.notifyObservers(this,
|
141 |
new DefaultLayoutNotification(LayoutNotification.LAYOUT_INVALIDATED));
|
142 |
} else {
|
143 |
observers.notifyObservers(this,
|
144 |
new DefaultLayoutNotification(LayoutNotification.LAYOUT_VALIDATED));
|
145 |
} |
146 |
} |
147 |
efs.endComplex(); |
148 |
layout.getLayoutContext().updateFFrames(); |
149 |
} |
150 |
|
151 |
/**
|
152 |
* Pan sobre la vista del FFrameView.
|
153 |
*
|
154 |
* @param p1
|
155 |
* Punto inicial del desplazamiento.
|
156 |
* @param p2
|
157 |
* Punto final del desplazamiento.
|
158 |
*/
|
159 |
public void setViewPan(Point p1, Point p2) { |
160 |
IFFrame[] fframes = layout.getLayoutContext().getFFrames();
|
161 |
for (int i = 0; i < fframes.length; i++) { |
162 |
if (fframes[i] instanceof IFFrameUseFMap) { |
163 |
IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i]; |
164 |
|
165 |
if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
|
166 |
if (fframe.getATMap() != null) { |
167 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
168 |
Envelope envel = |
169 |
// fframe.getMapContext().getViewPort().getExtent();
|
170 |
fframe.getMapContext().getViewPort().getEnvelope(); |
171 |
|
172 |
if (envel != null) { |
173 |
double visible_factor = screenPixelToViewPortPixelRatio(
|
174 |
layout, |
175 |
((IFFrame) fframe).getBoundBox().getHeight(), |
176 |
fframe.getMapContext().getViewPort().getImageHeight()); |
177 |
|
178 |
Point p1scaled = new Point( |
179 |
(int) (visible_factor * p1.getX()),
|
180 |
(int) (visible_factor * p1.getY()));
|
181 |
Point p2scaled = new Point( |
182 |
(int) (visible_factor * p2.getX()),
|
183 |
(int) (visible_factor * p2.getY()));
|
184 |
|
185 |
Point2D mp1 = toMapPoint(p1scaled, fframe.getATMap());
|
186 |
Point2D mp2 = toMapPoint(p2scaled, fframe.getATMap());
|
187 |
double x =
|
188 |
envel.getMinimum(0) - (mp2.getX() - mp1.getX());
|
189 |
double y =
|
190 |
envel.getMinimum(1) - (mp2.getY() - mp1.getY());
|
191 |
double width = envel.getLength(0); |
192 |
double height = envel.getLength(1); |
193 |
try {
|
194 |
fframe |
195 |
.getMapContext() |
196 |
.getViewPort() |
197 |
.setEnvelope( |
198 |
geomManager.createEnvelope(x, y, x |
199 |
+ width, y + height, |
200 |
SUBTYPES.GEOM2D)); |
201 |
} catch (CreateEnvelopeException e) {
|
202 |
logger.error("Error creating the envelope", e);
|
203 |
} |
204 |
fframe.refresh(); |
205 |
if (fframe.getLinked()) {
|
206 |
fframe.refreshOriginalExtent(); |
207 |
} |
208 |
} |
209 |
} |
210 |
} |
211 |
} |
212 |
} |
213 |
} |
214 |
|
215 |
/**
|
216 |
* Devuelve un punto real a partir de un punto en pixels sobre la vista.
|
217 |
*
|
218 |
* @param pScreen
|
219 |
* Punto en pixels.
|
220 |
* @param at1
|
221 |
* Matriz de transformaci?n.
|
222 |
*
|
223 |
* @return Punto real.
|
224 |
*/
|
225 |
public static Point2D.Double toMapPoint(Point2D pScreen, AffineTransform at1) { |
226 |
Point2D.Double pWorld = new Point2D.Double(); |
227 |
|
228 |
AffineTransform at;
|
229 |
|
230 |
try {
|
231 |
at = at1.createInverse(); |
232 |
at.transform(pScreen, pWorld); |
233 |
} catch (NoninvertibleTransformException e) { |
234 |
// throw new RuntimeException(e);
|
235 |
} |
236 |
|
237 |
return pWorld;
|
238 |
} |
239 |
|
240 |
public void addObserver(Observer o) { |
241 |
observers.addObserver(o); |
242 |
} |
243 |
|
244 |
public void deleteObserver(Observer o) { |
245 |
observers.deleteObserver(o); |
246 |
} |
247 |
|
248 |
public void deleteObservers() { |
249 |
observers.deleteObservers(); |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Gets the proportion between the number of pixels used
|
254 |
* on the screen (physical pixels) and the "logical" pixels
|
255 |
* used in the viewport of a frame (imageHeight, etc).
|
256 |
*
|
257 |
* For example if viewport image height is 500
|
258 |
* and the frame actually needs 100 pixels in height
|
259 |
* (due to zooming) then this method returns 5.
|
260 |
*
|
261 |
* @param lyt_panel
|
262 |
* @param frame_height_cm frame height in cm
|
263 |
* @param frame_vp_img_height frame viewport image height (pixels)
|
264 |
* @return
|
265 |
*/
|
266 |
public static double screenPixelToViewPortPixelRatio( |
267 |
LayoutPanel lyt_panel, double frame_height_cm, int frame_vp_img_height) { |
268 |
|
269 |
double sheet_height_screen_pix = lyt_panel.getLayoutControl().getRect().getHeight();
|
270 |
int ori = lyt_panel.getLayoutContext().getAttributes().getPageFormat().getOrientation();
|
271 |
double paper_h_i72 = 0; |
272 |
|
273 |
/*
|
274 |
* Javadoc says getPageFormat().getWidth()(getHeight()
|
275 |
* take into account orientation
|
276 |
* but apparently it's not like that
|
277 |
*/
|
278 |
if (ori == PageFormat.LANDSCAPE || ori == PageFormat.REVERSE_LANDSCAPE) { |
279 |
paper_h_i72 = lyt_panel.getLayoutContext().getAttributes().getPageFormat().getWidth(); |
280 |
} else {
|
281 |
paper_h_i72 = lyt_panel.getLayoutContext().getAttributes().getPageFormat().getHeight(); |
282 |
} |
283 |
|
284 |
/*
|
285 |
* page format uses old unit (inch/72)
|
286 |
*/
|
287 |
double paper_h_cm = (2.54 / 72.0) * paper_h_i72; |
288 |
double frame_part = frame_height_cm / paper_h_cm;
|
289 |
double pixels_fview_screen_h = sheet_height_screen_pix * frame_part;
|
290 |
double pixels_fview_vp_h = 1.0 * frame_vp_img_height; |
291 |
return pixels_fview_vp_h / pixels_fview_screen_h;
|
292 |
} |
293 |
|
294 |
|
295 |
public static String getLastMessage(Throwable ex) { |
296 |
|
297 |
if (ex == null) { |
298 |
return "[null]"; |
299 |
} |
300 |
|
301 |
Throwable p = ex;
|
302 |
while (p.getCause() != null && p.getCause() != p) { |
303 |
p = p.getCause(); |
304 |
} |
305 |
return p.getMessage();
|
306 |
} |
307 |
} |