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 @ 1714

History | View | Annotate | Download (11.9 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
import org.gvsig.app.project.documents.layout.commands.FrameCommandsRecord;
32
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
33
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
34
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
35
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
36
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
37
import org.gvsig.fmap.geom.GeometryLocator;
38
import org.gvsig.fmap.geom.GeometryManager;
39
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
40
import org.gvsig.fmap.geom.primitive.Envelope;
41
import org.gvsig.tools.observer.Observable;
42
import org.gvsig.tools.observer.ObservableHelper;
43
import org.gvsig.tools.observer.Observer;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

    
47
/**
48
 * Funciones utilizadas desde el Layout.
49
 * 
50
 * @author Vicente Caballero Navarro
51
 */
52
public class FLayoutFunctions implements Observable {
53

    
54
    protected static final Logger LOG = LoggerFactory
55
        .getLogger(FLayoutFunctions.class);
56
    private static final GeometryManager geomManager = GeometryLocator
57
        .getGeometryManager();
58
    private static final Logger logger = LoggerFactory
59
        .getLogger(FLayoutFunctions.class);
60
    
61
    private LayoutPanel layout = null;
62
    private final ObservableHelper observers;
63
    /**
64
     * Crea un nuevo FLayoutFunctions.
65
     * 
66
     * @param l
67
     *            Referencia al Layout.
68
     */
69
    public FLayoutFunctions(LayoutPanel layoutPanel) {
70
        layout = layoutPanel;
71
        observers = new ObservableHelper();
72
        observers.addObserver(layoutPanel.getLayoutControl());
73
    }
74

    
75
    /**
76
     * Gestiona la herramienta de selecci?n sobre el Mapa.
77
     */
78
    public void setSelect() {
79
        IFFrame fframe;
80
        boolean isUpdate = false;
81
        layout.getLayoutContext().updateFFrames();
82
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
83
        FrameCommandsRecord efs =
84
            layout.getLayoutContext().getFrameCommandsRecord();
85
        efs.startComplex("move");
86
        for (IFFrame fframe1 : fframes) {
87
            fframe = fframe1;
88
            int difx =
89
                (layout.getLayoutControl().getLastPoint().x - layout
90
                    .getLayoutControl().getFirstPoint().x);
91
            int dify =
92
                (layout.getLayoutControl().getLastPoint().y - layout
93
                    .getLayoutControl().getFirstPoint().y);
94
            if (((Math.abs(difx) > 3) || (Math.abs(dify) > 3))
95
                && (fframe.getSelected() != IFFrame.NOSELECT)) {
96
                Rectangle2D rectangle = fframe.getLastMoveRect();
97
                if (rectangle == null) {
98
                    efs.endComplex();
99
                    return;
100
                }
101
  
102
                if (fframe instanceof FFrameGroup) {
103
                    ((FFrameGroup) fframe).setAt(layout.getLayoutControl()
104
                        .getAT());
105
                }
106

    
107
                IFFrame fframeAux;
108
                try {
109
                    fframeAux = (IFFrame) fframe.clone();
110
                    fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(
111
                        rectangle, layout.getLayoutControl().getAT()));
112
                    efs.update(fframe, fframeAux);
113
                    fframeAux.getBoundingBox(layout.getLayoutControl().getAT()); //?????????????????
114

    
115
                    isUpdate = true;
116
                } catch (CloneNotSupportedException e) {
117
                    LOG.error("It is not possible clonate the object", e);
118
                }
119

    
120
            }
121
            Rectangle rect;
122
            if (layout.getLayoutControl().getReSel() == null) {
123
                rect = new Rectangle();
124
                rect.setFrameFromDiagonal(layout.getLayoutControl()
125
                    .getFirstPoint(), layout.getLayoutControl().getLastPoint());
126
            } else {
127
                rect = layout.getLayoutControl().getReSel();
128
            }
129
            if (layout.getLayoutControl().isReSel()
130
                && (rect.contains(fframe.getBoundingBox(layout
131
                    .getLayoutControl().getAT())))) {
132
                fframe.setSelected(true);
133
            }
134
            if (isUpdate) {
135
                observers.notifyObservers(this, 
136
                    new DefaultLayoutNotification(LayoutNotification.LAYOUT_INVALIDATED));
137
            } else {
138
                observers.notifyObservers(this, 
139
                    new DefaultLayoutNotification(LayoutNotification.LAYOUT_VALIDATED));
140
            }
141
        }
142
        efs.endComplex();
143
        layout.getLayoutContext().updateFFrames();
144
    }
145

    
146
    /**
147
     * Pan sobre la vista del FFrameView.
148
     * 
149
     * @param p1
150
     *            Punto inicial del desplazamiento.
151
     * @param p2
152
     *            Punto final del desplazamiento.
153
     */
154
    public void setViewPan(Point p1, Point p2) {
155
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
156
        for (IFFrame fframe1 : fframes) {
157
            if (fframe1 instanceof IFFrameUseFMap) {
158
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframe1;
159
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
160
                    if (fframe.getATMap() != null) {
161
                        Envelope envel =
162
                            fframe.getMapContext().getViewPort().getEnvelope();
163
                        
164
                        if (envel != null) {
165
                            double visible_factor = screenPixelToViewPortPixelRatio(
166
                                layout,
167
                                ((IFFrame) fframe).getBoundBox().getHeight(),
168
                                fframe.getMapContext().getViewPort().getImageHeight());
169

    
170
                            Point p1scaled = new Point(
171
                                (int) (visible_factor * p1.getX()),
172
                                (int) (visible_factor * p1.getY()));
173
                            Point p2scaled = new Point(
174
                                (int) (visible_factor * p2.getX()),
175
                                (int) (visible_factor * p2.getY()));
176
                            
177
                            Point2D mp1 = toMapPoint(p1scaled, fframe.getATMap());
178
                            Point2D mp2 = toMapPoint(p2scaled, fframe.getATMap());
179
                            double x =
180
                                envel.getMinimum(0) - (mp2.getX() - mp1.getX());
181
                            double y =
182
                                envel.getMinimum(1) - (mp2.getY() - mp1.getY());
183
                            double width = envel.getLength(0);
184
                            double height = envel.getLength(1);
185
                            try {
186
                                fframe
187
                                    .getMapContext()
188
                                    .getViewPort()
189
                                    .setEnvelope(
190
                                        geomManager.createEnvelope(x, y, x
191
                                            + width, y + height,
192
                                            SUBTYPES.GEOM2D));
193
                            } catch (CreateEnvelopeException e) {
194
                                logger.error("Error creating the envelope", e);
195
                            }
196
                            fframe.refresh();
197
                            if (fframe.getExtentSynced()) {
198
                                fframe.refreshOriginalExtent();
199
                            }
200
                        }
201
                    }
202
                }
203
            }
204
        }
205
    }
206

    
207
    public void setScale(long scale) {
208
            IFFrameUseFMap[] selectedFrames = layout.getLayoutContext().getSelectedFFrames(IFFrameUseFMap.class);
209
            if (selectedFrames.length==1) {
210
                    IFFrameUseFMap fframe = (IFFrameUseFMap) selectedFrames[0];
211
                    fframe.setScale(scale);
212
                    // FIXME CMI: needed?? fframe.refresh();
213
            }
214
    }
215

    
216
    public void addLayer() {
217
            
218
    }
219
    
220
    /**
221
     * Devuelve un punto real a partir de un punto en pixels sobre la vista.
222
     * 
223
     * @param pScreen
224
     *            Punto en pixels.
225
     * @param at1
226
     *            Matriz de transformaci?n.
227
     * 
228
     * @return Punto real.
229
     */
230
    public static Point2D.Double toMapPoint(Point2D pScreen, AffineTransform at1) {
231
        Point2D.Double pWorld = new Point2D.Double();
232

    
233
        AffineTransform at;
234

    
235
        try {
236
            at = at1.createInverse();
237
            at.transform(pScreen, pWorld);
238
        } catch (NoninvertibleTransformException e) {
239
            // throw new RuntimeException(e);
240
        }
241

    
242
        return pWorld;
243
    }
244

    
245
    @Override
246
    public void addObserver(Observer o) {
247
        observers.addObserver(o);        
248
    }
249

    
250
    @Override
251
    public void deleteObserver(Observer o) {
252
      observers.deleteObserver(o);        
253
    }
254

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

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