Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutFunctions.java @ 6117

History | View | Annotate | Download (10.1 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.gui.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.fframes.FFrameTable;
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.FFramePicture;
60
import com.iver.cit.gvsig.gui.layout.fframes.FFrameScaleBar;
61
import com.iver.cit.gvsig.gui.layout.fframes.FFrameText;
62
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
63
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
64
import com.iver.cit.gvsig.gui.layout.fframes.IFFrameLayoutDependence;
65
import com.iver.cit.gvsig.gui.layout.fframes.IFFrameUseFMap;
66

    
67

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

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

    
85
        /**
86
         * Gestiona la herramienta de selecci?n sobre el Mapa.
87
         */
88
        public void setSelect() {
89
                IFFrame fframe = null;
90
                boolean isUpdate = false;
91
                IFFrame[] fframes=layout.getFFrames();
92
                for (int i = 0; i < fframes.length; i++) {
93
                        fframe = fframes[i];
94

    
95
                        int difx = (layout.getLastPoint().x - layout.getFirstPoint().x);
96
                        int dify = (layout.getLastPoint().y - layout.getFirstPoint().y);
97

    
98
                        if (((Math.abs(difx) > 3) || (Math.abs(dify) > 3)) &&
99
                                        (fframe.getSelected() != IFFrame.NOSELECT)) {
100
                                Rectangle2D rectangle = fframe.getLastMoveRect();
101
                                if (rectangle==null)
102
                                        return;
103
                                if (layout.isCuadricula()) {
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.getAT());
112
                                }
113

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

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

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

    
140
                        } else {
141
                                layout.setStatus(Layout.ACTUALIZADO);
142
                        }
143
                }
144

    
145
                layout.updateFFrames();
146
                layout.setIsReSel(true);
147
        }
148

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

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

    
200
                        if (layout.isCuadricula()) {
201
                                //cuadrar con la cuadr?cula dibujada y despu?s en el mouseReleased guardar ese rect?ngulo de la misma forma.
202
                                FLayoutUtilities.setRectGrid(r,
203
                                        layout.getAtributes().getUnitInPixelsX(),
204
                                        layout.getAtributes().getUnitInPixelsY(), layout.getAT());
205
                        }
206

    
207
                        fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
208

    
209
                        if (layout.openFFrameDialog(fframe)) {
210
                                if (!layout.isEditGroup()){
211
                                        layout.addFFrame(fframe, true,true);
212
                                }
213
                        }
214
                }else{
215
                        if (layout.getTool() == Layout.POINT){
216
                                Rectangle2D r=layout.getGeometryAdapter().getBounds2D();
217
                                double d=0.5;//FLayoutUtilities.toSheetDistance(50,layout.getAT());
218
                                r=new Rectangle2D.Double(r.getX()-d,r.getY()-d,d*2,d*2);
219
                                fframe.setBoundBox(r);
220
                        }else{
221
                                fframe.setBoundBox(layout.getGeometryAdapter().getBounds2D());
222
                        }
223
                        layout.addFFrame(fframe, true,true);
224
                        ///fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
225
                }
226
                return fframe;
227
        }
228

    
229
        private Rectangle2D getRectangle(int tolerance,Point2D first,Point2D last) {
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.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
                                        Rectangle2D.Double r = new Rectangle2D.Double();
269
                                        Rectangle2D extent = fframe.getFMap().getViewPort()
270
                                                                                           .getExtent();
271
                                        Point2D mp1 = toMapPoint(p1, fframe.getATMap());
272
                                        Point2D mp2 = toMapPoint(p2, fframe.getATMap());
273
                                        r.x = extent.getX() - (mp2.getX() - mp1.getX());
274
                                        r.y = extent.getY() - (mp2.getY() - mp1.getY());
275
                                        r.width = extent.getWidth();
276
                                        r.height = extent.getHeight();
277
                                        fframe.getFMap().getViewPort().setExtent(r);
278
                                }
279
                        }
280
                }
281
        }
282

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

    
294
                AffineTransform at;
295

    
296
                try {
297
                        at = at1.createInverse();
298
                        at.transform(pScreen, pWorld);
299
                } catch (NoninvertibleTransformException e) {
300
                        //                                 throw new RuntimeException(e);
301
                }
302

    
303
                return pWorld;
304
        }
305

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