Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutFunctions.java @ 219

History | View | Annotate | Download (12 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.getExtentSynced()) {
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
                    fframe.setScale(scale);
219
                    // FIXME CMI: needed?? fframe.refresh();
220
            }
221
    }
222

    
223
    public void addLayer() {
224
            
225
    }
226
    
227
    /**
228
     * Devuelve un punto real a partir de un punto en pixels sobre la vista.
229
     * 
230
     * @param pScreen
231
     *            Punto en pixels.
232
     * @param at1
233
     *            Matriz de transformaci?n.
234
     * 
235
     * @return Punto real.
236
     */
237
    public static Point2D.Double toMapPoint(Point2D pScreen, AffineTransform at1) {
238
        Point2D.Double pWorld = new Point2D.Double();
239

    
240
        AffineTransform at;
241

    
242
        try {
243
            at = at1.createInverse();
244
            at.transform(pScreen, pWorld);
245
        } catch (NoninvertibleTransformException e) {
246
            // throw new RuntimeException(e);
247
        }
248

    
249
        return pWorld;
250
    }
251

    
252
    public void addObserver(Observer o) {
253
        observers.addObserver(o);        
254
    }
255

    
256
    public void deleteObserver(Observer o) {
257
      observers.deleteObserver(o);        
258
    }
259

    
260
    public void deleteObservers() {
261
       observers.deleteObservers();        
262
    }
263
    
264
    /**
265
     * Gets the proportion between the number of pixels used
266
     * on the screen (physical pixels) and the "logical" pixels
267
     * used in the viewport of a frame (imageHeight, etc).
268
     * 
269
     * For example if viewport image height is 500
270
     * and the frame actually needs 100 pixels in height
271
     * (due to zooming) then this method returns 5.
272
     *  
273
     * @param lyt_panel
274
     * @param frame_height_cm frame height in cm
275
     * @param frame_vp_img_height frame viewport image height (pixels)
276
     * @return
277
     */
278
    public static double screenPixelToViewPortPixelRatio(
279
        LayoutPanel lyt_panel, double frame_height_cm, int frame_vp_img_height) {
280
        
281
        double sheet_height_screen_pix = lyt_panel.getLayoutControl().getRect().getHeight();
282
        int ori = lyt_panel.getLayoutContext().getAttributes().getPageFormat().getOrientation();
283
        double paper_h_i72 = 0;
284
        
285
        /*
286
         * Javadoc says getPageFormat().getWidth()(getHeight()
287
         * take into account orientation
288
         * but apparently it's not like that
289
         */
290
        if (ori == PageFormat.LANDSCAPE || ori == PageFormat.REVERSE_LANDSCAPE) {
291
            paper_h_i72 = lyt_panel.getLayoutContext().getAttributes().getPageFormat().getWidth();
292
        } else {
293
            paper_h_i72 = lyt_panel.getLayoutContext().getAttributes().getPageFormat().getHeight();
294
        }
295
        
296
        /*
297
         * page format uses old unit (inch/72)
298
         */
299
        double paper_h_cm = (2.54 / 72.0) * paper_h_i72;
300
        double frame_part = frame_height_cm / paper_h_cm;
301
        double pixels_fview_screen_h = sheet_height_screen_pix * frame_part;   
302
        double pixels_fview_vp_h = 1.0 * frame_vp_img_height;
303
        return pixels_fview_vp_h / pixels_fview_screen_h;
304
    }
305
    
306

    
307
    public static String getLastMessage(Throwable ex) {
308
        
309
        if (ex == null) {
310
            return "[null]";
311
        }
312
        
313
        Throwable p = ex;
314
        while (p.getCause() != null && p.getCause() != p) {
315
            p = p.getCause();
316
        }
317
        return p.getMessage();
318
    }
319
}