Statistics
| Revision:

svn-gvsig-desktop / tags / Root_v061 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutFunctions.java @ 4812

History | View | Annotate | Download (8.15 KB)

1 312 fernando
/*
2
 * Created on 16-sep-2004
3
 *
4
 */
5 1103 fjp
/* 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 312 fernando
package com.iver.cit.gvsig.gui.layout;
46
47 3115 caballero
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 312 fernando
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGraphics;
55
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGroup;
56
import com.iver.cit.gvsig.gui.layout.fframes.FFrameLegend;
57 2914 caballero
import com.iver.cit.gvsig.gui.layout.fframes.FFrameNorth;
58 312 fernando
import com.iver.cit.gvsig.gui.layout.fframes.FFramePicture;
59
import com.iver.cit.gvsig.gui.layout.fframes.FFrameScaleBar;
60
import com.iver.cit.gvsig.gui.layout.fframes.FFrameText;
61
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
62
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
63
64
65
/**
66
 * Funciones utilizadas desde la clase EventsHandler.
67
 *
68
 * @author Vicente Caballero Navarro
69
 */
70
public class FLayoutFunctions {
71 1074 vcaballero
        private Layout layout = null;
72 312 fernando
73 1074 vcaballero
        /**
74
         * Crea un nuevo FLayoutFunctions.
75
         *
76
         * @param l Referencia al Layout.
77
         */
78
        public FLayoutFunctions(Layout l) {
79
                layout = l;
80
        }
81 312 fernando
82 1074 vcaballero
        /**
83
         * Gestiona la herramienta de selecci?n sobre el Mapa.
84
         */
85
        public void setSelect() {
86
                IFFrame fframe = null;
87
                boolean isUpdate = false;
88 312 fernando
89 1074 vcaballero
                for (int i = 0; i < layout.getFFrames().size(); i++) {
90
                        fframe = (IFFrame) layout.getFFrames().get(i);
91 312 fernando
92 1074 vcaballero
                        int difx = (layout.getLastPoint().x - layout.getFirstPoint().x);
93
                        int dify = (layout.getLastPoint().y - layout.getFirstPoint().y);
94 312 fernando
95 1074 vcaballero
                        if (((Math.abs(difx) > 3) || (Math.abs(dify) > 3)) &&
96 2263 vcaballero
                                        (fframe.getSelected() != IFFrame.NOSELECT)) {
97 1074 vcaballero
                                Rectangle2D rectangle = fframe.getMovieRect(difx, dify);
98 312 fernando
99 1074 vcaballero
                                if (layout.isCuadricula()) {
100
                                        //cuadrar con la cuadr?cula dibujada y despu?s en el mouseReleased guardar ese rect?ngulo de la misma forma.
101
                                        FLayoutUtilities.setRectGrid(rectangle,
102
                                                layout.getAtributes().getUnitInPixelsX(),
103
                                                layout.getAtributes().getUnitInPixelsY(), layout.getAT());
104
                                }
105 312 fernando
106 1074 vcaballero
                                if (fframe instanceof FFrameGroup) {
107
                                        ((FFrameGroup) fframe).setAt(layout.getAT());
108
                                }
109 312 fernando
110 1074 vcaballero
                                fframe.setBoundBox(FLayoutUtilities.toSheetRect(rectangle,
111
                                                layout.getAT()));
112
                                isUpdate = true;
113
                        }
114 2772 caballero
                        Rectangle rect=null;
115
                        if (layout.getReSel()==null){
116
                                rect=new Rectangle();
117
                                rect.setFrameFromDiagonal(layout.getFirstPoint(), layout.getLastPoint());
118
                        }else{
119
                                rect=layout.getReSel();
120
                        }
121 1074 vcaballero
                        if (layout.isReSel() &&
122 2772 caballero
                                        (rect.contains(fframe.getBoundingBox(
123 1074 vcaballero
                                                        layout.getAT())))) {
124
                                fframe.setSelected(true);
125
                        }
126 312 fernando
127 1074 vcaballero
                        if (isUpdate) {
128
                                layout.setStatus(Layout.DESACTUALIZADO);
129
                        } else {
130
                                layout.setStatus(Layout.ACTUALIZADO);
131
                        }
132
                }
133 312 fernando
134 1074 vcaballero
                layout.setIsReSel(true);
135
        }
136 312 fernando
137 1074 vcaballero
        /**
138
         * A?ade un fframe al Layout del tipo preseleccionado  y abre el di?logo
139
         * para configurar las caracter?sticas.
140
         */
141
        public void setFFrame() {
142
                IFFrame fframe = null;
143
                boolean isadd = false;
144 312 fernando
145 1074 vcaballero
                if (layout.getTool() == Layout.RECTANGLEVIEW) {
146
                        fframe = new FFrameView();
147 2424 caballero
                        ((FFrameView)fframe).setLayout(layout);
148 1074 vcaballero
                        isadd = true;
149
                } else if (layout.getTool() == Layout.RECTANGLEPICTURE) {
150
                        fframe = new FFramePicture();
151
                        isadd = true;
152
                } else if (layout.getTool() == Layout.RECTANGLESCALEBAR) {
153
                        fframe = new FFrameScaleBar();
154
                        isadd = true;
155
                } else if (layout.getTool() == Layout.RECTANGLELEGEND) {
156
                        fframe = new FFrameLegend();
157
                        isadd = true;
158
                } else if (layout.getTool() == Layout.RECTANGLETEXT) {
159
                        fframe = new FFrameText();
160
                        isadd = true;
161 2914 caballero
                } else if (layout.getTool() == Layout.RECTANGLENORTH) {
162
                        fframe = new FFrameNorth();
163
                        isadd = true;
164 1074 vcaballero
                } else if ((layout.getTool() == Layout.RECTANGLESIMPLE) ||
165
                                (layout.getTool() == Layout.LINE) ||
166
                                (layout.getTool() == Layout.POLYLINE) ||
167
                                (layout.getTool() == Layout.POLYGON) ||
168
                                (layout.getTool() == Layout.CIRCLE) ||
169
                                (layout.getTool() == Layout.POINT)) {
170 3115 caballero
                        ////fframe = new FFrameGraphics(layout.getLine());
171 1074 vcaballero
                        fframe = new FFrameGraphics();
172
                        isadd = true;
173
                        ((FFrameGraphics) fframe).update(layout.getTool(), layout.getAT());
174
                }
175 312 fernando
176 1074 vcaballero
                if (isadd) {
177
                        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);
178 1846 vcaballero
                        int tolerance=20;
179
                        if (isCorrectSize(tolerance,layout.getFirstPoint(),layout.getLastPoint())){
180
                                r.setFrameFromDiagonal(layout.getFirstPoint(), layout.getLastPoint());
181
                        }else{
182
                                Point2D p1=layout.getFirstPoint();
183
                                p1=new Point2D.Double(p1.getX()+tolerance,p1.getY()+tolerance);
184
                                r.setFrameFromDiagonal(layout.getFirstPoint(),p1);
185
                        }
186 312 fernando
187 1074 vcaballero
                        if (layout.isCuadricula()) {
188
                                //cuadrar con la cuadr?cula dibujada y despu?s en el mouseReleased guardar ese rect?ngulo de la misma forma.
189
                                FLayoutUtilities.setRectGrid(r,
190
                                        layout.getAtributes().getUnitInPixelsX(),
191
                                        layout.getAtributes().getUnitInPixelsY(), layout.getAT());
192
                        }
193 312 fernando
194 1074 vcaballero
                        fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
195 312 fernando
196 1074 vcaballero
                        if (layout.openFFrameDialog(fframe)) {
197 2835 caballero
                                layout.addFFrame(fframe, true,true);
198 1074 vcaballero
                        }
199 3115 caballero
                }else{
200
                        ///fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
201 1074 vcaballero
                }
202
        }
203 312 fernando
204 1074 vcaballero
        /**
205
         * Pan sobre la vista del FFrameView.
206
         *
207
         * @param p1 Punto inicial del desplazamiento.
208
         * @param p2 Punto final del desplazamiento.
209
         */
210
        public void setViewPan(Point p1, Point p2) {
211
                for (int i = 0; i < layout.getFFrames().size(); i++) {
212
                        if (layout.getFFrames().get(i) instanceof FFrameView) {
213
                                FFrameView fframe = (FFrameView) layout.getFFrames().get(i);
214 312 fernando
215 2263 vcaballero
                                if (fframe.getSelected() != IFFrame.NOSELECT) {
216 1074 vcaballero
                                        Rectangle2D.Double r = new Rectangle2D.Double();
217
                                        Rectangle2D extent = fframe.getFMap().getViewPort()
218
                                                                                           .getExtent();
219 4452 caballero
                                        Point2D mp1 = toMapPoint(p1, fframe.getAt());
220
                                        Point2D mp2 = toMapPoint(p2, fframe.getAt());
221 1074 vcaballero
                                        r.x = extent.getX() - (mp2.getX() - mp1.getX());
222
                                        r.y = extent.getY() - (mp2.getY() - mp1.getY());
223
                                        r.width = extent.getWidth();
224
                                        r.height = extent.getHeight();
225
                                        fframe.getFMap().getViewPort().setExtent(r);
226
                                }
227
                        }
228
                }
229
        }
230 312 fernando
231 1074 vcaballero
        /**
232
         * Devuelve un punto real a partir de un punto en pixels sobre la vista.
233
         *
234
         * @param pScreen Punto en pixels.
235
         * @param at1 Matriz de transformaci?n.
236
         *
237
         * @return Punto real.
238
         */
239 4452 caballero
        public Point2D.Double toMapPoint(Point2D pScreen, AffineTransform at1) {
240 1074 vcaballero
                Point2D.Double pWorld = new Point2D.Double();
241 312 fernando
242 1074 vcaballero
                AffineTransform at;
243 312 fernando
244 1074 vcaballero
                try {
245
                        at = at1.createInverse();
246
                        at.transform(pScreen, pWorld);
247
                } catch (NoninvertibleTransformException e) {
248
                        //                                 throw new RuntimeException(e);
249
                }
250
251
                return pWorld;
252
        }
253 1846 vcaballero
254
        /**
255
         * Devuelve true si el rectangulo formado por los dos puntos que se pasan
256
         * como par?metro es superior a la tolerancia.
257
         *
258
         * @param tolerance Tolerancia
259
         * @param p1 Punto inicial del rect?ngulo.
260
         * @param p2 Punto final del rect?ngulo.
261
         *
262
         * @return True si el tama?o es correcto.
263
         */
264
        private boolean isCorrectSize(int tolerance, Point2D p1, Point2D p2) {
265
                if (Math.abs(p2.getX()) < (Math.abs(p1.getX()) + tolerance)) {
266
                        return false;
267
                } else if (Math.abs(p2.getY()) < (Math.abs(p1.getY()) + tolerance)) {
268
                        return false;
269
                }
270
                return true;
271
        }
272 312 fernando
}