Statistics
| Revision:

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

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.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
                                        return;
99
//                                if (layout.isAdjustingToGrid()) {
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

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

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

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

    
133
                        if (isUpdate) {
134
                                layout.getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
135

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

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

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

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

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

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

    
293
                AffineTransform at;
294

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

    
302
                return pWorld;
303
        }
304

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