Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutFunctions.java @ 27352

History | View | Annotate | Download (10.7 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 com.iver.cit.gvsig.project.documents.layout.commands.EditableFeatureSource;
55
import com.iver.cit.gvsig.project.documents.layout.fframes.FFrameGroup;
56
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
57
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameUseFMap;
58
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
59

    
60

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

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

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

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

    
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.endComplexCommand("move");
99
                                        return;
100
                                }
101
//                                if (layout.isAdjustingToGrid()) {
102
//                                        //cuadrar con la cuadr?cula dibujada y despu?s en el mouseReleased guardar ese rect?ngulo de la misma forma.
103
//                                        FLayoutUtilities.setRectGrid(rectangle,
104
//                                                layout.getAtributes().getUnitInPixelsX(),
105
//                                                layout.getAtributes().getUnitInPixelsY(), layout.getAT());
106
//                                }
107

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

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

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

    
135
                        if (isUpdate) {
136
                                layout.getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
137

    
138
                        } else {
139
                                layout.getLayoutControl().setStatus(LayoutControl.ACTUALIZADO);
140
                        }
141
                }
142
                efs.endComplexCommand("move");
143
                layout.getLayoutContext().updateFFrames();
144
        }
145

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

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

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

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

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

    
299
                AffineTransform at;
300

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

    
308
                return pWorld;
309
        }
310

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