Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutFunctions.java @ 24962

History | View | Annotate | Download (10.8 KB)

1
/*
2
 * Created on 16-sep-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.project.documents.layout;
46

    
47
import java.awt.Point;
48
import java.awt.Rectangle;
49
import java.awt.geom.AffineTransform;
50
import java.awt.geom.NoninvertibleTransformException;
51
import java.awt.geom.Point2D;
52
import java.awt.geom.Rectangle2D;
53

    
54
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
55

    
56
import com.iver.cit.gvsig.project.documents.layout.commands.FrameCommandsRecord;
57
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameGroup;
58
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
59
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameUseFMap;
60
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
61

    
62

    
63
/**
64
 * Funciones utilizadas desde el Layout.
65
 *
66
 * @author Vicente Caballero Navarro
67
 */
68
public class FLayoutFunctions {
69
        private Layout layout = null;
70

    
71
        /**
72
         * Crea un nuevo FLayoutFunctions.
73
         *
74
         * @param l Referencia al Layout.
75
         */
76
        public FLayoutFunctions(Layout l) {
77
                layout = l;
78
        }
79

    
80
        /**
81
         * Gestiona la herramienta de selecci?n sobre el Mapa.
82
         */
83
        public void setSelect() {
84
                IFFrame fframe = null;
85
                boolean isUpdate = false;
86
                layout.getLayoutContext().updateFFrames();
87
                IFFrame[] fframes=layout.getLayoutContext().getFFrames();
88
                FrameCommandsRecord efs=layout.getLayoutContext().getFrameCommandsRecord();
89
                efs.startComplex("move");
90
                for (int i = 0; i < fframes.length; i++) {
91
                        fframe = fframes[i];
92

    
93
                        int difx = (layout.getLayoutControl().getLastPoint().x - layout.getLayoutControl().getFirstPoint().x);
94
                        int dify = (layout.getLayoutControl().getLastPoint().y - layout.getLayoutControl().getFirstPoint().y);
95

    
96
                        if (((Math.abs(difx) > 3) || (Math.abs(dify) > 3)) &&
97
                                        (fframe.getSelected() != IFFrame.NOSELECT)) {
98
                                Rectangle2D rectangle = fframe.getLastMoveRect();
99
                                if (rectangle==null) {
100
                                        efs.endComplex();
101
                                        return;
102
                                }
103
//                                if (layout.isAdjustingToGrid()) {
104
//                                        //cuadrar con la cuadr?cula dibujada y despu?s en el mouseReleased guardar ese rect?ngulo de la misma forma.
105
//                                        FLayoutUtilities.setRectGrid(rectangle,
106
//                                                layout.getAtributes().getUnitInPixelsX(),
107
//                                                layout.getAtributes().getUnitInPixelsY(), layout.getAT());
108
//                                }
109

    
110
                                if (fframe instanceof FFrameGroup) {
111
                                        ((FFrameGroup) fframe).setAt(layout.getLayoutControl().getAT());
112
                                }
113

    
114
                                IFFrame fframeAux=fframe.cloneFFrame(layout);
115
                                fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(rectangle,
116
                                                layout.getLayoutControl().getAT()));
117
//                                if (fframeAux instanceof IFFrameUseFMap)
118
//                                        ((IFFrameUseFMap)fframeAux).refresh();
119
                                efs.update(fframe,fframeAux);
120
                                fframeAux.getBoundingBox(layout.getLayoutControl().getAT());
121

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

    
137
                        if (isUpdate) {
138
                                layout.getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
139

    
140
                        } else {
141
                                layout.getLayoutControl().setStatus(LayoutControl.ACTUALIZADO);
142
                        }
143
                }
144
                efs.endComplex();
145
                layout.getLayoutContext().updateFFrames();
146
        }
147

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

    
231
//        private Rectangle2D getRectangle(int tolerance) {
232
//                Rectangle2D r=new Rectangle2D.Double();
233
//                if (isCorrectSize(tolerance,layout.getFirstPoint(),layout.getLastPoint())){
234
//                        Point2D fp=layout.getFirstPoint();
235
//                        Point2D lp=layout.getLastPoint();
236
//                        if (fp.getX()<lp.getX()){
237
//                                if (fp.getY()<lp.getY()){
238
//                                        r.setRect(fp.getX(),fp.getY(),lp.getX()-fp.getX(),lp.getY()-fp.getY());
239
//                                }else{
240
//                                        r.setRect(fp.getX(),lp.getY(),lp.getX()-fp.getX(),fp.getY()-lp.getY());
241
//                                }
242
//                        }else{
243
//                                if (fp.getY()>lp.getY()){
244
//                                        r.setRect(lp.getX(),lp.getY(),fp.getX()-lp.getX(),fp.getY()-lp.getY());
245
//                                }else{
246
//                                        r.setRect(lp.getX(),fp.getY(),lp.getX()-fp.getX(),lp.getY()-fp.getY());
247
//                                }
248
//                        }
249
//                }else{
250
//                        Point2D p1=layout.getFirstPoint();
251
//                        p1=new Point2D.Double(p1.getX()+tolerance,p1.getY()+tolerance);
252
//                        r.setFrameFromDiagonal(layout.getFirstPoint(),p1);
253
//                }
254
//                return r;
255
//        }
256

    
257
        /**
258
         * Pan sobre la vista del FFrameView.
259
         *
260
         * @param p1 Punto inicial del desplazamiento.
261
         * @param p2 Punto final del desplazamiento.
262
         */
263
        public void setViewPan(Point p1, Point p2) {
264
                IFFrame[] fframes=layout.getLayoutContext().getFFrames();
265
                for (int i = 0; i < fframes.length; i++) {
266
                        if (fframes[i] instanceof IFFrameUseFMap) {
267
                                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
268

    
269
                                if (((IFFrame)fframe).getSelected() != IFFrame.NOSELECT) {
270
                                         if (fframe.getATMap()!=null) {
271
                                                Rectangle2D.Double r = new Rectangle2D.Double();
272
                                                Rectangle2D extent = fframe.getMapContext().getViewPort()
273
                                                                                                   .getExtent();
274
                                                if (extent != null) {
275
                                                        Point2D mp1 = toMapPoint(p1, fframe.getATMap());
276
                                                        Point2D mp2 = toMapPoint(p2, fframe.getATMap());
277
                                                        double x = extent.getX() - (mp2.getX() - mp1.getX());
278
                                                        double y = extent.getY() - (mp2.getY() - mp1.getY());
279
                                                        double width = extent.getWidth();
280
                                                        double height = extent.getHeight();
281
                                                        fframe.getMapContext().getViewPort().setEnvelope(new DefaultEnvelope(2,new double[]{x,y},new double[]{x+width,y+height}));
282
                                                        fframe.refresh();
283
                                                }
284
                                         }
285
                                }
286
                        }
287
                }
288
        }
289

    
290
        /**
291
         * Devuelve un punto real a partir de un punto en pixels sobre la vista.
292
         *
293
         * @param pScreen Punto en pixels.
294
         * @param at1 Matriz de transformaci?n.
295
         *
296
         * @return Punto real.
297
         */
298
        public static Point2D.Double toMapPoint(Point2D pScreen, AffineTransform at1) {
299
                Point2D.Double pWorld = new Point2D.Double();
300

    
301
                AffineTransform at;
302

    
303
                try {
304
                        at = at1.createInverse();
305
                        at.transform(pScreen, pWorld);
306
                } catch (NoninvertibleTransformException e) {
307
                        //                                 throw new RuntimeException(e);
308
                }
309

    
310
                return pWorld;
311
        }
312

    
313
        /**
314
         * Devuelve true si el rectangulo formado por los dos puntos que se pasan
315
         * como par?metro es superior a la tolerancia.
316
         *
317
         * @param tolerance Tolerancia
318
         * @param p1 Punto inicial del rect?ngulo.
319
         * @param p2 Punto final del rect?ngulo.
320
         *
321
         * @return True si el tama?o es correcto.
322
         */
323
//        private boolean isCorrectSize(int tolerance, Point2D p1, Point2D p2) {
324
//                if (Math.abs(p2.getX()-p1.getX())<tolerance) {
325
//                        return false;
326
//                } else if (Math.abs(p2.getY()-p1.getY())<tolerance) {
327
//                        return false;
328
//                }
329
//                return true;
330
//        }
331
}