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