Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutFunctions.java @ 36648

History | View | Annotate | Download (13.4 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

    
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

    
34
import org.gvsig.app.project.documents.layout.commands.FrameCommandsRecord;
35
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
36
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
37
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
38
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
39
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
43

    
44
/**
45
 * Funciones utilizadas desde el Layout.
46
 * 
47
 * @author Vicente Caballero Navarro
48
 */
49
public class FLayoutFunctions {
50

    
51
    protected static final Logger LOG = LoggerFactory
52
        .getLogger(FLayoutFunctions.class);
53
    private static final GeometryManager geomManager = GeometryLocator
54
        .getGeometryManager();
55
    private static final Logger logger = LoggerFactory
56
        .getLogger(FLayoutFunctions.class);
57
    private LayoutPanel layout = null;
58

    
59
    /**
60
     * Crea un nuevo FLayoutFunctions.
61
     * 
62
     * @param l
63
     *            Referencia al Layout.
64
     */
65
    public FLayoutFunctions(LayoutPanel l) {
66
        layout = l;
67
    }
68

    
69
    /**
70
     * Gestiona la herramienta de selecci?n sobre el Mapa.
71
     */
72
    public void setSelect() {
73
        IFFrame fframe = null;
74
        boolean isUpdate = false;
75
        layout.getLayoutContext().updateFFrames();
76
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
77
        FrameCommandsRecord efs =
78
            layout.getLayoutContext().getFrameCommandsRecord();
79
        efs.startComplex("move");
80
        for (int i = 0; i < fframes.length; i++) {
81
            fframe = fframes[i];
82

    
83
            int difx =
84
                (layout.getLayoutControl().getLastPoint().x - layout
85
                    .getLayoutControl().getFirstPoint().x);
86
            int dify =
87
                (layout.getLayoutControl().getLastPoint().y - layout
88
                    .getLayoutControl().getFirstPoint().y);
89

    
90
            if (((Math.abs(difx) > 3) || (Math.abs(dify) > 3))
91
                && (fframe.getSelected() != IFFrame.NOSELECT)) {
92
                Rectangle2D rectangle = fframe.getLastMoveRect();
93
                if (rectangle == null) {
94
                    efs.endComplex();
95
                    return;
96
                }
97
                // if (layout.isAdjustingToGrid()) {
98
                // //cuadrar con la cuadr?cula dibujada y despu?s en el
99
                // mouseReleased guardar ese rect?ngulo de la misma forma.
100
                // FLayoutUtilities.setRectGrid(rectangle,
101
                // layout.getAtributes().getUnitInPixelsX(),
102
                // layout.getAtributes().getUnitInPixelsY(), layout.getAT());
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
                    // if (fframeAux instanceof IFFrameUseFMap)
116
                    // ((IFFrameUseFMap)fframeAux).refresh();
117
                    efs.update(fframe, fframeAux);
118
                    fframeAux.getBoundingBox(layout.getLayoutControl().getAT());
119

    
120
                    isUpdate = true;
121
                } catch (CloneNotSupportedException e) {
122
                    LOG.error("It is not possible clonate the object", e);
123
                }
124

    
125
            }
126
            Rectangle rect = null;
127
            if (layout.getLayoutControl().getReSel() == null) {
128
                rect = new Rectangle();
129
                rect.setFrameFromDiagonal(layout.getLayoutControl()
130
                    .getFirstPoint(), layout.getLayoutControl().getLastPoint());
131
            } else {
132
                rect = layout.getLayoutControl().getReSel();
133
            }
134
            if (layout.getLayoutControl().isReSel()
135
                && (rect.contains(fframe.getBoundingBox(layout
136
                    .getLayoutControl().getAT())))) {
137
                fframe.setSelected(true);
138
            }
139

    
140
            if (isUpdate) {
141
                layout.getLayoutControl().setStatus(
142
                    LayoutControl.DESACTUALIZADO);
143

    
144
            } else {
145
                layout.getLayoutControl().setStatus(LayoutControl.ACTUALIZADO);
146
            }
147
        }
148
        efs.endComplex();
149
        layout.getLayoutContext().updateFFrames();
150
    }
151

    
152
    /**
153
     * A?ade un fframe al Layout del tipo preseleccionado y abre el di?logo
154
     * para configurar las caracter?sticas.
155
     * 
156
     * @return TODO
157
     */
158
    // public IFFrame addFFrame() {
159
    // IFFrame fframe = null;
160
    // boolean isadd = false;
161
    //
162
    // if (layout.getTool() == Layout.RECTANGLEVIEW) {
163
    // fframe = new FFrameView();
164
    // isadd = true;
165
    // } else if (layout.getTool() == Layout.RECTANGLEOVERVIEW) {
166
    // fframe = new FFrameOverView();
167
    // isadd = true;
168
    // } else if (layout.getTool() == Layout.RECTANGLEPICTURE) {
169
    // fframe = new FFramePicture();
170
    // isadd = true;
171
    // } else if (layout.getTool() == Layout.RECTANGLESCALEBAR) {
172
    // fframe = new FFrameScaleBar();
173
    // isadd = true;
174
    // } else if (layout.getTool() == Layout.RECTANGLELEGEND) {
175
    // fframe = new FFrameLegend();
176
    // isadd = true;
177
    // } else if (layout.getTool() == Layout.RECTANGLETEXT) {
178
    // fframe = new FFrameText();
179
    // isadd = true;
180
    // } else if (layout.getTool() == Layout.RECTANGLENORTH) {
181
    // fframe = new FFrameNorth();
182
    // isadd = true;
183
    // } else if (layout.getTool() == Layout.RECTANGLEBOX) {
184
    // fframe = new FFrameTable();
185
    // isadd = true;
186
    // } else if ((layout.getTool() == Layout.LINE) ||
187
    // (layout.getTool() == Layout.POLYLINE) ||
188
    // (layout.getTool() == Layout.POLYGON) ||
189
    // (layout.getTool() == Layout.CIRCLE) ||
190
    // (layout.getTool() == Layout.RECTANGLESIMPLE) ||
191
    // (layout.getTool() == Layout.POINT)) {
192
    // fframe = new FFrameGraphics();
193
    // ((FFrameGraphics)fframe).setGeometryAdapter(layout.getGeometryAdapter());
194
    // isadd = false;
195
    // ((FFrameGraphics) fframe).update(layout.getTool(), layout.getAT());
196
    // } /*else if ((layout.getTool() == Layout.POINT)){
197
    // fframe = new FFrameGraphics();
198
    // isadd = true;
199
    // ((FFrameGraphics) fframe).update(layout.getTool(), layout.getAT());
200
    // }*/
201
    // if (isadd) {
202
    // Rectangle2D r = new Rectangle2D.Double();
203
    // //rectOrigin.x+m_PointAnt.x,rectOrigin.y+m_PointAnt.y,m_LastPoint.x-m_PointAnt.x,m_LastPoint.y-m_PointAnt.y);
204
    // int tolerance=20;
205
    // r=getRectangle(tolerance);
206
    //
207
    // // if (layout.isAdjustingToGrid()) {
208
    // // //cuadrar con la cuadr?cula dibujada y despu?s en el mouseReleased
209
    // guardar ese rect?ngulo de la misma forma.
210
    // // FLayoutUtilities.setRectGrid(r,
211
    // // layout.getAtributes().getUnitInPixelsX(),
212
    // // layout.getAtributes().getUnitInPixelsY(), layout.getAT());
213
    // // }
214
    //
215
    // fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
216
    // IFFrame newFrame=layout.openFFrameDialog(fframe);
217
    // if (newFrame!=null) {
218
    // if (!layout.isEditGroup()){
219
    // layout.addFFrame(newFrame, true,true);
220
    // }
221
    // }
222
    // }else{
223
    // if (layout.getTool() == Layout.POINT){
224
    // Rectangle2D r=layout.getGeometryAdapter().getBounds2D();
225
    // double d=0.5;//FLayoutUtilities.toSheetDistance(50,layout.getAT());
226
    // r=new Rectangle2D.Double(r.getX()-(d/2),r.getY()-d,d*2,d*2);
227
    // fframe.setBoundBox(r);
228
    // }else{
229
    // fframe.setBoundBox(layout.getGeometryAdapter().getBounds2D());
230
    // }
231
    // layout.addFFrame(fframe, true,true);
232
    // ///fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
233
    // }
234
    // fframe.setLayout(layout);
235
    // return fframe;
236
    // }
237

    
238
    // private Rectangle2D getRectangle(int tolerance) {
239
    // Rectangle2D r=new Rectangle2D.Double();
240
    // if
241
    // (isCorrectSize(tolerance,layout.getFirstPoint(),layout.getLastPoint())){
242
    // Point2D fp=layout.getFirstPoint();
243
    // Point2D lp=layout.getLastPoint();
244
    // if (fp.getX()<lp.getX()){
245
    // if (fp.getY()<lp.getY()){
246
    // r.setRect(fp.getX(),fp.getY(),lp.getX()-fp.getX(),lp.getY()-fp.getY());
247
    // }else{
248
    // r.setRect(fp.getX(),lp.getY(),lp.getX()-fp.getX(),fp.getY()-lp.getY());
249
    // }
250
    // }else{
251
    // if (fp.getY()>lp.getY()){
252
    // r.setRect(lp.getX(),lp.getY(),fp.getX()-lp.getX(),fp.getY()-lp.getY());
253
    // }else{
254
    // r.setRect(lp.getX(),fp.getY(),lp.getX()-fp.getX(),lp.getY()-fp.getY());
255
    // }
256
    // }
257
    // }else{
258
    // Point2D p1=layout.getFirstPoint();
259
    // p1=new Point2D.Double(p1.getX()+tolerance,p1.getY()+tolerance);
260
    // r.setFrameFromDiagonal(layout.getFirstPoint(),p1);
261
    // }
262
    // return r;
263
    // }
264

    
265
    /**
266
     * Pan sobre la vista del FFrameView.
267
     * 
268
     * @param p1
269
     *            Punto inicial del desplazamiento.
270
     * @param p2
271
     *            Punto final del desplazamiento.
272
     */
273
    public void setViewPan(Point p1, Point p2) {
274
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
275
        for (int i = 0; i < fframes.length; i++) {
276
            if (fframes[i] instanceof IFFrameUseFMap) {
277
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
278

    
279
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
280
                    if (fframe.getATMap() != null) {
281
                        Rectangle2D.Double r = new Rectangle2D.Double();
282
                        Rectangle2D extent =
283
                            fframe.getMapContext().getViewPort().getExtent();
284
                        if (extent != null) {
285
                            Point2D mp1 = toMapPoint(p1, fframe.getATMap());
286
                            Point2D mp2 = toMapPoint(p2, fframe.getATMap());
287
                            double x =
288
                                extent.getX() - (mp2.getX() - mp1.getX());
289
                            double y =
290
                                extent.getY() - (mp2.getY() - mp1.getY());
291
                            double width = extent.getWidth();
292
                            double height = extent.getHeight();
293
                            try {
294
                                fframe
295
                                    .getMapContext()
296
                                    .getViewPort()
297
                                    .setEnvelope(
298
                                        geomManager.createEnvelope(x, y, x
299
                                            + width, y + height,
300
                                            SUBTYPES.GEOM2D));
301
                            } catch (CreateEnvelopeException e) {
302
                                logger.error("Error creating the envelope", e);
303
                            }
304
                            fframe.refresh();
305
                            fframe.refreshOriginalExtent();
306
                        }
307
                    }
308
                }
309
            }
310
        }
311
    }
312

    
313
    /**
314
     * Devuelve un punto real a partir de un punto en pixels sobre la vista.
315
     * 
316
     * @param pScreen
317
     *            Punto en pixels.
318
     * @param at1
319
     *            Matriz de transformaci?n.
320
     * 
321
     * @return Punto real.
322
     */
323
    public static Point2D.Double toMapPoint(Point2D pScreen, AffineTransform at1) {
324
        Point2D.Double pWorld = new Point2D.Double();
325

    
326
        AffineTransform at;
327

    
328
        try {
329
            at = at1.createInverse();
330
            at.transform(pScreen, pWorld);
331
        } catch (NoninvertibleTransformException e) {
332
            // throw new RuntimeException(e);
333
        }
334

    
335
        return pWorld;
336
    }
337

    
338
    /**
339
     * Devuelve true si el rectangulo formado por los dos puntos que se pasan
340
     * como par?metro es superior a la tolerancia.
341
     * 
342
     * @param tolerance
343
     *            Tolerancia
344
     * @param p1
345
     *            Punto inicial del rect?ngulo.
346
     * @param p2
347
     *            Punto final del rect?ngulo.
348
     * 
349
     * @return True si el tama?o es correcto.
350
     */
351
    // private boolean isCorrectSize(int tolerance, Point2D p1, Point2D p2) {
352
    // if (Math.abs(p2.getX()-p1.getX())<tolerance) {
353
    // return false;
354
    // } else if (Math.abs(p2.getY()-p1.getY())<tolerance) {
355
    // return false;
356
    // }
357
    // return true;
358
    // }
359
}