Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutFunctions.java @ 1822

History | View | Annotate | Download (7.98 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 com.iver.cit.gvsig.fmap.drivers.DriverIOException;
48
import com.iver.cit.gvsig.gui.layout.fframes.FFrameGroup;
49
import com.iver.cit.gvsig.gui.layout.fframes.FFrameLegend;
50
import com.iver.cit.gvsig.gui.layout.fframes.FFramePicture;
51
import com.iver.cit.gvsig.gui.layout.fframes.FFrameScaleBar;
52
import com.iver.cit.gvsig.gui.layout.fframes.FFrameText;
53
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
54
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
55

    
56
import java.awt.Point;
57
import java.awt.geom.AffineTransform;
58
import java.awt.geom.NoninvertibleTransformException;
59
import java.awt.geom.Point2D;
60
import java.awt.geom.Rectangle2D;
61

    
62
import java.io.IOException;
63

    
64

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

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

    
82
        /**
83
         * Gestiona la herramienta de selecci?n sobre el Mapa.
84
         */
85
        public void setSelect() {
86
                IFFrame fframe = null;
87
                boolean isUpdate = false;
88
                IFFrame[] frames = layout.getFFrames1();
89

    
90
                for (int i = 0; i < frames.length; i++) {
91
                        fframe = frames[i];
92

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

    
96
                        if (((Math.abs(difx) > 3) || (Math.abs(dify) > 3)) &&
97
                                        (fframe.getSelected() != IFFrame.NOSELECT)) {
98
                                Rectangle2D rectangle = fframe.getMovieRect(difx, dify);
99

    
100
                                if (layout.isCuadricula()) {
101
                                        //cuadrar con la cuadr?cula dibujada y despu?s en el mouseReleased guardar ese rect?ngulo de la misma forma.
102
                                        FLayoutUtilities.setRectGrid(rectangle,
103
                                                layout.getAtributes().getUnitInPixelsX(),
104
                                                layout.getAtributes().getUnitInPixelsY(), layout.getAT());
105
                                }
106

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

    
111
                                //fframe.setBoundBox(FLayoutUtilities.toSheetRect(rectangle,
112
                                //                layout.getAT()));
113
                                IFFrame frame = fframe.cloneFFrame(layout);
114
                                frame.setBoundBox(FLayoutUtilities.toSheetRect(rectangle,
115
                                                layout.getAT()));
116

    
117
                                try {
118
                                        ((LayoutEditableFeature) layout.getCadToolAdapter()
119
                                                                                                   .getEditableFeatureSource()).modifyFFrame(frame,
120
                                                layout.getAllFFrames().indexOf(fframe));
121
                                } catch (IOException e) {
122
                                        e.printStackTrace();
123
                                } catch (DriverIOException e) {
124
                                        e.printStackTrace();
125
                                }
126

    
127
                                isUpdate = true;
128
                        }
129

    
130
                        if (layout.isReSel() &&
131
                                        (layout.getReSel().contains(fframe.getBoundingBox(
132
                                                        layout.getAT())))) {
133
                                fframe.setSelected(true);
134
                        }
135

    
136
                        if (isUpdate) {
137
                                layout.setStatus(Layout.DESACTUALIZADO);
138
                        } else {
139
                                layout.setStatus(Layout.ACTUALIZADO);
140
                        }
141
                }
142

    
143
                layout.setIsReSel(true);
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
         */
150
        public void setFFrame() {
151
                IFFrame fframe = null;
152
                boolean isadd = false;
153

    
154
                if (layout.getTool() == Layout.RECTANGLEVIEW) {
155
                        fframe = new FFrameView();
156
                        isadd = true;
157
                } else if (layout.getTool() == Layout.RECTANGLEPICTURE) {
158
                        fframe = new FFramePicture();
159
                        isadd = true;
160
                } else if (layout.getTool() == Layout.RECTANGLESCALEBAR) {
161
                        fframe = new FFrameScaleBar();
162
                        isadd = true;
163
                } else if (layout.getTool() == Layout.RECTANGLELEGEND) {
164
                        fframe = new FFrameLegend();
165
                        isadd = true;
166
                } else if (layout.getTool() == Layout.RECTANGLETEXT) {
167
                        fframe = new FFrameText();
168
                        isadd = true;
169
                }
170

    
171
                if (isadd) {
172
                        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);
173

    
174
                        int tolerance = 20;
175

    
176
                        if (isCorrectSize(tolerance, layout.getFirstPoint(),
177
                                                layout.getLastPoint())) {
178
                                r.setFrameFromDiagonal(layout.getFirstPoint(),
179
                                        layout.getLastPoint());
180
                        } else {
181
                                Point2D p1 = layout.getFirstPoint();
182
                                p1 = new Point2D.Double(p1.getX() + tolerance,
183
                                                p1.getY() + tolerance);
184
                                r.setFrameFromDiagonal(layout.getFirstPoint(), p1);
185
                        }
186

    
187
                        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

    
194
                        fframe.setBoundBox(FLayoutUtilities.toSheetRect(r, layout.getAT()));
195

    
196
                        if (layout.openFFrameDialog(fframe)) {
197
                                try {
198
                                        ///layout.addFFrame(fframe, true);
199
                                        ((LayoutEditableFeature) layout.getCadToolAdapter()
200
                                                                                                   .getEditableFeatureSource()).addFFrame(fframe);
201
                                } catch (DriverIOException e) {
202
                                        e.printStackTrace();
203
                                } catch (IOException e) {
204
                                        e.printStackTrace();
205
                                }
206
                        }
207
                }
208
        }
209

    
210
        /**
211
         * Pan sobre la vista del FFrameView.
212
         *
213
         * @param p1 Punto inicial del desplazamiento.
214
         * @param p2 Punto final del desplazamiento.
215
         */
216
        public void setViewPan(Point p1, Point p2) {
217
                IFFrame[] frames = layout.getFFrames1();
218

    
219
                for (int i = 0; i < frames.length; i++) {
220
                        if (frames[i] instanceof FFrameView) {
221
                                FFrameView fframe = (FFrameView) frames[i]; //layout.getFFrames().get(i);
222

    
223
                                if (fframe.getSelected() != IFFrame.NOSELECT) {
224
                                        Rectangle2D.Double r = new Rectangle2D.Double();
225
                                        Rectangle2D extent = fframe.getFMap().getViewPort()
226
                                                                                           .getExtent();
227
                                        Point2D mp1 = toMapPoint(p1, fframe.getAt());
228
                                        Point2D mp2 = toMapPoint(p2, fframe.getAt());
229
                                        r.x = extent.getX() - (mp2.getX() - mp1.getX());
230
                                        r.y = extent.getY() - (mp2.getY() - mp1.getY());
231
                                        r.width = extent.getWidth();
232
                                        r.height = extent.getHeight();
233
                                        fframe.getFMap().getViewPort().setExtent(r);
234
                                }
235
                        }
236
                }
237
        }
238

    
239
        /**
240
         * Devuelve un punto real a partir de un punto en pixels sobre la vista.
241
         *
242
         * @param pScreen Punto en pixels.
243
         * @param at1 Matriz de transformaci?n.
244
         *
245
         * @return Punto real.
246
         */
247
        public Point2D.Double toMapPoint(Point2D pScreen, AffineTransform at1) {
248
                Point2D.Double pWorld = new Point2D.Double();
249

    
250
                AffineTransform at;
251

    
252
                try {
253
                        at = at1.createInverse();
254
                        at.transform(pScreen, pWorld);
255
                } catch (NoninvertibleTransformException e) {
256
                        //                                 throw new RuntimeException(e);
257
                }
258

    
259
                return pWorld;
260
        }
261

    
262
        /**
263
         * Devuelve true si el rectangulo formado por los dos puntos que se pasan
264
         * como par?metro es superior a la tolerancia.
265
         *
266
         * @param tolerance Tolerancia
267
         * @param p1 Punto inicial del rect?ngulo.
268
         * @param p2 Punto final del rect?ngulo.
269
         *
270
         * @return True si el tama?o es correcto.
271
         */
272
        private boolean isCorrectSize(int tolerance, Point2D p1, Point2D p2) {
273
                if (Math.abs(p2.getX()) < (Math.abs(p1.getX()) + tolerance)) {
274
                        return false;
275
                } else if (Math.abs(p2.getY()) < (Math.abs(p1.getY()) + tolerance)) {
276
                        return false;
277
                }
278

    
279
                return true;
280
        }
281
}