Statistics
| Revision:

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

History | View | Annotate | Download (7.55 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.layout;
42

    
43
import java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.Point;
46
import java.awt.event.MouseEvent;
47
import java.awt.geom.AffineTransform;
48
import java.awt.geom.Point2D;
49
import java.awt.geom.Rectangle2D;
50
import java.io.IOException;
51

    
52
import com.iver.cit.gvsig.fmap.core.FGeometry;
53
import com.iver.cit.gvsig.fmap.core.Handler;
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
56
import com.iver.cit.gvsig.fmap.layers.SelectionSupport;
57
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
58
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
59
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
60

    
61

    
62
/**
63
 * DOCUMENT ME!
64
 *
65
 * @author Vicente Caballero Navarro
66
 */
67
public class LayoutCADToolAdapter extends CADToolAdapter {
68
        private Layout layout;
69
        //private Point adjustedPoint;
70
        //private boolean adjustSnapping=false;
71
        //private Point2D mapAdjustedPoint;
72
        /**
73
         *
74
         */
75
        public LayoutCADToolAdapter(Layout l) {
76
                layout = l;
77
        }
78

    
79
        /**
80
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
81
         */
82
/*        public void mousePressed(MouseEvent e) throws BehaviorException {
83
                if (e.getButton() == MouseEvent.BUTTON1) {
84
                        Point2D.Double aux = new Point2D.Double(e.getX(), e.getY());
85
                        Point2D p = FLayoutUtilities.toSheetPoint(aux, layout.getAT());
86
                        
87
                        this.transition("punto", getEditableFeatureSource(),getSelection(),
88
                                new double[] { p.getX(), p.getY() });
89
                }
90
        }
91
        */
92
        public AffineTransform getAT(){
93
                return layout.getAT();
94
        }
95
        public double toDistance(int i){
96
                return FLayoutUtilities.toSheetDistance(i,getAT());
97
        }
98
        public void refresh(){
99
                layout.refresh();
100
        }
101
        public void paintComponent(Graphics g){
102
                if (layout.getTool()==Layout.GRAPHICS) {
103
                        getCadTool().setAT(getAT());
104
                        Point2D.Double paux=new Point2D.Double(adjustedPoint.getX(),adjustedPoint.getY());
105
                        paux=FLayoutUtilities.toSheetPoint(paux,getAT());
106
                        getCadTool().drawOperation(g, null, null,
107
                                paux.getX(),paux.getY());
108
                        
109
                        //drawCursor(g);
110
                }
111
                //grid.drawGrid(g);
112
        }
113
        /**
114
         * DOCUMENT ME!
115
         *
116
         * @param point
117
         */
118
/*        private void calculateSnapPoint(Point point) {
119
                //Se comprueba el ajuste a rejilla
120
                Point gridAdjustedPoint = (Point) point.clone();
121
                double minDistance = grid.adjustToGrid(gridAdjustedPoint);
122
                if (minDistance < Double.MAX_VALUE) {
123
                        adjustedPoint = gridAdjustedPoint;
124
                        mapAdjustedPoint = toPoint(adjustedPoint);
125
                }
126
                
127
                //Se comprueba el ajuste a los handlers
128
                Point handlerAdjustedPoint = (Point) point.clone();
129
                Point2D mapPoint = new Point2D.Double();
130
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
131
                if (distance < minDistance) {
132
                        adjustSnapping=true;
133
                        adjustedPoint = handlerAdjustedPoint;
134
                        mapAdjustedPoint = mapPoint;
135
                        minDistance = distance;
136
                }
137

138
                //Si no hay ajuste
139
                if (minDistance == Double.MAX_VALUE) {
140
                        adjustedPoint = point;
141
                        mapAdjustedPoint = null;
142
                }
143
        }
144
*/
145
        /**
146
         * Ajusta un punto de la imagen que se pasa como  par?metro al grid si ?ste
147
         * est? activo y devuelve la distancia de un punto al punto ajustado
148
         *
149
         * @param point
150
         *
151
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
152
         */
153
/*        private double adjustToHandler(Point point, Point2D mapHandlerAdjustedPoint) {
154
                //if (selection.cardinality() > 0) {
155
                        try {
156
                                Point2D mapPoint = toPoint(point);
157

158
                                Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - 1,
159
                                                mapPoint.getY() - 1, 2, 2);
160

161
                                int[] indexes = getEditableFeatureSource().getGeometriesIndexes(r);
162
                                
163
                                double min = Double.MAX_VALUE;
164
                                Point2D argmin = null;
165
                                Point2D mapArgmin = null;
166

167
                                for (int i = 0; i < indexes.length; i++) {
168
                                        //if (!selection.get(indexes[i])) continue;
169
                                        IGeometry geom;
170
                                        geom = getEditableFeatureSource().getGeometry(indexes[i]);
171

172
                                        Handler[] handlers = geom.getHandlers(FGeometry.STRETCHINGHANDLER);
173

174
                                        for (int j = 0; j < handlers.length; j++) {
175
                                                Point2D handlerPoint = handlers[j].getPoint();
176
                                                Point2D handlerImagePoint = fromPoint(handlerPoint);
177
                                                double dist = handlerImagePoint.distance(point);
178

179
                                                if ((dist < SelectionCadTool.tolerance) &&
180
                                                                (dist < min)) {
181
                                                        min = dist;
182
                                                        argmin = handlerImagePoint;
183
                                                        mapArgmin = handlerPoint;
184
                                                }
185
                                        }
186
                                }
187

188
                                if (argmin != null) {
189
                                        point.setLocation(argmin);
190

191
                                        //Se hace el casting porque no se quiere redondeo
192
                                        point.setLocation((int)argmin.getX(),
193
                                                        (int) argmin.getY());
194
                                        
195
                                        mapHandlerAdjustedPoint.setLocation(mapArgmin);
196
                                        return min;
197
                                }
198
                        } catch (IOException e) {
199
                                e.printStackTrace();
200
                        } catch (DriverIOException e) {
201
                                e.printStackTrace();
202
                        }
203
                //}
204

205
                return Double.MAX_VALUE;
206
        }
207
*/
208
        /**
209
         * DOCUMENT ME!
210
         *
211
         * @param g DOCUMENT ME!
212
         */
213
/*        private void drawCursor(Graphics g) {
214
                Point p = adjustedPoint;
215
                if (p==null) grid.setAT(getAT());
216
                
217
                int size1 = 15;
218
                int size2 = 3;
219
                g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
220
                        (int) (p.getX() + size1), (int) (p.getY()));
221
                g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
222
                        (int) (p.getX()), (int) (p.getY() + size1));
223
                if (adjustedPoint!=null){
224
                        if (adjustSnapping){
225
                                g.setColor(Color.ORANGE);
226
                                g.drawRect((int)(adjustedPoint.getX()-5),(int)(adjustedPoint.getY()-5),10,10);
227
                                g.drawRect((int)(adjustedPoint.getX()-3),(int)(adjustedPoint.getY()-3),6,6);
228
                                g.setColor(Color.MAGENTA);
229
                                g.drawRect((int)(adjustedPoint.getX()-4),(int)(adjustedPoint.getY()-4),8,8);
230
                                
231
                                adjustSnapping=false;
232
                        }else{
233
                                g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
234
                                                (int) (size2 * 2), (int) (size2 * 2));
235
                        }
236
                }
237
        }
238
        */
239
        public Point2D toPoint(Point2D p){
240
                return FLayoutUtilities.toSheetPoint(p,getAT());
241
        }
242
        public Rectangle2D getExtent(){
243
                return FLayoutUtilities.toSheetRect(layout.getRect(),getAT());
244
        }
245
        public double fromDistance(double d){
246
                return FLayoutUtilities.fromSheetDistance(d,getAT());
247
        }
248
        public Point2D fromPoint(Point2D p){
249
                return FLayoutUtilities.fromSheetPoint(p,getAT());
250
        }
251
        /**
252
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
253
         */
254
        public void mouseMoved(MouseEvent e) throws BehaviorException {
255
                calculateSnapPoint(e.getPoint());
256
        }
257
        public Point2D getAdjustedPoint(){
258
                return adjustedPoint;
259
        }
260
        public void drawGrid(Graphics g){
261
                grid.drawGrid(g);
262
        }
263
        /**
264
         * DOCUMENT ME!
265
         *
266
         * @param value DOCUMENT ME!
267
         */
268
        public void setGrid(boolean value) {
269
                grid.setUseGrid(value);
270
                grid.setAT(getAT());
271
        }
272
}