Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutFunctions.java @ 9192

History | View | Annotate | Download (10.5 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.gui.layout.Layout;
55
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGraphics;
56
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGroup;
57
import com.iver.cit.gvsig.gui.layout.fframes.FFrameLegend;
58
import com.iver.cit.gvsig.gui.layout.fframes.FFrameNorth;
59
import com.iver.cit.gvsig.gui.layout.fframes.FFrameOverView;
60
import com.iver.cit.gvsig.gui.layout.fframes.FFramePicture;
61
import com.iver.cit.gvsig.gui.layout.fframes.FFrameScaleBar;
62
import com.iver.cit.gvsig.gui.layout.fframes.FFrameTable;
63
import com.iver.cit.gvsig.gui.layout.fframes.FFrameText;
64
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
65
import com.iver.cit.gvsig.project.documents.layout.commands.EditableFeatureSource;
66
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
67
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameLayoutDependence;
68
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameUseFMap;
69

    
70

    
71
/**
72
 * Funciones utilizadas desde la clase EventsHandler.
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class FLayoutFunctions {
77
        private Layout layout = null;
78

    
79
        /**
80
         * Crea un nuevo FLayoutFunctions.
81
         *
82
         * @param l Referencia al Layout.
83
         */
84
        public FLayoutFunctions(Layout l) {
85
                layout = l;
86
        }
87

    
88
        /**
89
         * Gestiona la herramienta de selecci?n sobre el Mapa.
90
         */
91
        public void setSelect() {
92
                IFFrame fframe = null;
93
                boolean isUpdate = false;
94
                IFFrame[] fframes=layout.getFFrames();
95
                EditableFeatureSource efs=layout.getEFS();
96
                efs.startComplexCommand();
97
                for (int i = 0; i < fframes.length; i++) {
98
                        fframe = fframes[i];
99

    
100
                        int difx = (layout.getLastPoint().x - layout.getFirstPoint().x);
101
                        int dify = (layout.getLastPoint().y - layout.getFirstPoint().y);
102

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

    
115
                                if (fframe instanceof FFrameGroup) {
116
                                        ((FFrameGroup) fframe).setAt(layout.getAT());
117
                                }
118

    
119
                                IFFrame fframeAux=fframe.cloneFFrame(layout);
120
                                fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(rectangle,
121
                                                layout.getAT()));
122
                                if (fframeAux instanceof IFFrameUseFMap)
123
                                        ((IFFrameUseFMap)fframeAux).refresh();
124
                                efs.modifyFFrame(fframe,fframeAux);
125
                                fframeAux.getBoundingBox(layout.getAT());
126

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

    
142
                        if (isUpdate) {
143
                                layout.setStatus(Layout.DESACTUALIZADO);
144

    
145
                        } else {
146
                                layout.setStatus(Layout.ACTUALIZADO);
147
                        }
148
                }
149
                efs.endComplexCommand("move");
150
                layout.updateFFrames();
151
                layout.setIsReSel(true);
152
        }
153

    
154
        /**
155
         * A?ade un fframe al Layout del tipo preseleccionado  y abre el di?logo
156
         * para configurar las caracter?sticas.
157
         * @return TODO
158
         */
159
        public IFFrame addFFrame() {
160
                IFFrame fframe = null;
161
                boolean isadd = false;
162

    
163
                if (layout.getTool() == Layout.RECTANGLEVIEW) {
164
                        fframe = new FFrameView();
165
                        ((IFFrameLayoutDependence)fframe).setLayout(layout);
166
                        isadd = true;
167
                } else if (layout.getTool() == Layout.RECTANGLEOVERVIEW) {
168
                        fframe = new FFrameOverView();
169
                        ((IFFrameLayoutDependence)fframe).setLayout(layout);
170
                        isadd = true;
171
                } else if (layout.getTool() == Layout.RECTANGLEPICTURE) {
172
                        fframe = new FFramePicture();
173
                        isadd = true;
174
                } else if (layout.getTool() == Layout.RECTANGLESCALEBAR) {
175
                        fframe = new FFrameScaleBar();
176
                        isadd = true;
177
                } else if (layout.getTool() == Layout.RECTANGLELEGEND) {
178
                        fframe = new FFrameLegend();
179
                        isadd = true;
180
                } else if (layout.getTool() == Layout.RECTANGLETEXT) {
181
                        fframe = new FFrameText();
182
                        isadd = true;
183
                } else if (layout.getTool() == Layout.RECTANGLENORTH) {
184
                        fframe = new FFrameNorth();
185
                        isadd = true;
186
                } else if (layout.getTool() == Layout.RECTANGLEBOX) {
187
                        fframe = new FFrameTable();
188
                        isadd = true;
189
                } else if ((layout.getTool() == Layout.LINE) ||
190
                                (layout.getTool() == Layout.POLYLINE) ||
191
                                (layout.getTool() == Layout.POLYGON) ||
192
                (layout.getTool() == Layout.CIRCLE) ||
193
                (layout.getTool() == Layout.RECTANGLESIMPLE) ||
194
                (layout.getTool() == Layout.POINT)) {
195
                        fframe = new FFrameGraphics();
196
                        ((FFrameGraphics)fframe).setGeometryAdapter(layout.getGeometryAdapter());
197
                        isadd = false;
198
                        ((FFrameGraphics) fframe).update(layout.getTool(), layout.getAT());
199
                } /*else if ((layout.getTool() == Layout.POINT)){
200
                        fframe = new FFrameGraphics();
201
                        isadd = true;
202
                        ((FFrameGraphics) fframe).update(layout.getTool(), layout.getAT());
203
                }*/
204
                if (isadd) {
205
                        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);
206
                        int tolerance=20;
207
                        r=getRectangle(tolerance);
208

    
209
                        if (layout.isAdjustingToGrid()) {
210
                                //cuadrar con la cuadr?cula dibujada y despu?s en el mouseReleased guardar ese rect?ngulo de la misma forma.
211
                                FLayoutUtilities.setRectGrid(r,
212
                                        layout.getAtributes().getUnitInPixelsX(),
213
                                        layout.getAtributes().getUnitInPixelsY(), layout.getAT());
214
                        }
215

    
216
                        fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
217

    
218
                        if (layout.openFFrameDialog(fframe)) {
219
                                if (!layout.isEditGroup()){
220
                                        layout.addFFrame(fframe, true,true);
221
                                }
222
                        }
223
                }else{
224
                        if (layout.getTool() == Layout.POINT){
225
                                Rectangle2D r=layout.getGeometryAdapter().getBounds2D();
226
                                double d=0.5;//FLayoutUtilities.toSheetDistance(50,layout.getAT());
227
                                r=new Rectangle2D.Double(r.getX()-(d/2),r.getY()-d,d*2,d*2);
228
                                fframe.setBoundBox(r);
229
                        }else{
230
                                fframe.setBoundBox(layout.getGeometryAdapter().getBounds2D());
231
                        }
232
                        layout.addFFrame(fframe, true,true);
233
                        ///fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
234
                }
235
                return fframe;
236
        }
237

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

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

    
276
                                if (((IFFrame)fframe).getSelected() != IFFrame.NOSELECT) {
277
                                        Rectangle2D.Double r = new Rectangle2D.Double();
278
                                        Rectangle2D extent = fframe.getMapContext().getViewPort()
279
                                                                                           .getExtent();
280
                                        Point2D mp1 = toMapPoint(p1, fframe.getATMap());
281
                                        Point2D mp2 = toMapPoint(p2, fframe.getATMap());
282
                                        r.x = extent.getX() - (mp2.getX() - mp1.getX());
283
                                        r.y = extent.getY() - (mp2.getY() - mp1.getY());
284
                                        r.width = extent.getWidth();
285
                                        r.height = extent.getHeight();
286
                                        fframe.getMapContext().getViewPort().setExtent(r);
287
                                }
288
                        }
289
                }
290
        }
291

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

    
303
                AffineTransform at;
304

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

    
312
                return pWorld;
313
        }
314

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