Statistics
| Revision:

root / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / ViewCADToolAdapter.java @ 1822

History | View | Annotate | Download (7.46 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.cad;
42

    
43
import com.iver.andami.PluginServices;
44
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
45
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
46
import com.iver.cit.gvsig.fmap.layers.FBitSet;
47
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
48
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
49
import com.iver.fsac.Automaton;
50

    
51
import java.awt.Graphics;
52
import java.awt.event.MouseEvent;
53
import java.awt.geom.AffineTransform;
54
import java.awt.geom.Point2D;
55
import java.awt.geom.Rectangle2D;
56
import java.io.IOException;
57

    
58

    
59
/**
60
 * CADToolAdapter para gestionar la interactividad de la parte CAD con el MapControl.
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class ViewCADToolAdapter extends CADToolAdapter {
65
        private boolean questionAsked = false;
66
        /**
67
         * Pinta de alguna manera especial las geometrias seleccionadas para la
68
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto
69
         * del mismo.
70
         *
71
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
72
         */
73
        public void paintComponent(Graphics g) {
74
                super.paintComponent(g);
75
                //grid.drawGrid(g);
76
                drawCursor(g);
77

    
78
                if (adjustedPoint != null) {
79
                        Point2D p = getMapControl().getViewPort().toMapPoint((int) adjustedPoint.getX(),
80
                                        (int) adjustedPoint.getY());
81
                        CadTool cadtool = (CadTool) cadToolStack.peek();
82
                        cadtool.setAT(getMapControl().getViewPort().getAffineTransform());
83
                        cadtool.drawOperation(g, editableFeatureSource, selection,
84
                                p.getX(), p.getY());
85
                }
86
        }
87

    
88
        /**
89
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
90
         */
91
        public void mouseMoved(MouseEvent e) throws BehaviorException {
92
                calculateSnapPoint(e.getPoint());
93

    
94
                getMapControl().repaint();
95
        }
96

    
97
        /**
98
         * Inserta true o false seg?n se quiera visualizar el grid.
99
         *
100
         * @param value True si se quiere visualizar el grid.
101
         */
102
        public void setGrid(boolean value) {
103
                grid.setUseGrid(value);
104
                grid.setAT(getAT());
105
                setGridSizeX(toDistance(20));
106
                setGridSizeY(toDistance(20));
107
                getMapControl().setGrid(grid);
108
        }
109

    
110
        /**
111
         * Devuelve la matriz de transformaci?n del viewport de la vista.
112
         *
113
         * @return MAtriz de transformaci?n.
114
         */
115
        public AffineTransform getAT() {
116
                return getMapControl().getViewPort().getAffineTransform();
117
        }
118

    
119
        /**
120
         * Devuelve la distancia real de la cartograf?a a partir de los pixels.
121
         *
122
         * @param i Pixels.
123
         *
124
         * @return Distancia real.
125
         */
126
        public double toDistance(int i) {
127
                return getMapControl().getViewPort().toMapDistance(i);
128
        }
129

    
130
        
131
        /**
132
         * Repinta la vista.
133
         */
134
        public void refresh() {
135
                getMapControl().drawMap(false);
136
        }
137

    
138
        /**
139
         * Devuelve un punto real a partir de uno en pixels.
140
         *
141
         * @param p Punto de pantalla.
142
         *
143
         * @return Punto real de la cartograf?a.
144
         */
145
        public Point2D toPoint(Point2D p) {
146
                return getMapControl().getViewPort().toMapPoint(p);
147
        }
148

    
149
        /**
150
         * Devuelve el extent real de la cartograf?a.
151
         *
152
         * @return Extent real.
153
         */
154
        public Rectangle2D getExtent() {
155
                return getMapControl().getViewPort().getAdjustedExtent();
156
        }
157

    
158
        /**
159
         * Devuelve la distancia en pixels a partir de una distancia real.
160
         *
161
         * @param d distancia real.
162
         *
163
         * @return Distancia en pixels.
164
         */
165
        public double fromDistance(double d) {
166
                return getMapControl().getViewPort().fromMapDistance(d);
167
        }
168

    
169
        /**
170
         * Devuelve un punto en pixels a partir de un punto real.
171
         *
172
         * @param p Punto real.
173
         *
174
         * @return Punto en pixels.
175
         */
176
        public Point2D fromPoint(Point2D p) {
177
                return getMapControl().getViewPort().fromMapPoint(p);
178
        }
179
        /**
180
         * Configura el men? con las opciones adecuadas.
181
         */
182
        public void configureMenu() {
183
                String[] desc = ((CadTool) cadToolStack.peek()).getAutomaton()
184
                                                 .getCurrentTransitionDescriptions();
185
                String[] labels = ((CadTool) cadToolStack.peek()).getAutomaton()
186
                                                   .getCurrentTransitions();
187
                if (getCadMapControl()!=null){
188
                getCadMapControl().clearMenu();
189

    
190
                for (int i = 0; i < desc.length; i++) {
191
                        if (desc[i] != null) {
192
                                getCadMapControl().addMenuEntry(desc[i], labels[i]);
193
                        }
194
                }
195
                }
196
        }
197
        /**
198
         * DOCUMENT ME!
199
         *
200
         * @param text DOCUMENT ME!
201
         * @param source DOCUMENT ME!
202
         * @param sel DOCUMENT ME!
203
         * @param values DOCUMENT ME!
204
         */
205
        protected void transition(String text, EditableFeatureSource source,
206
                FBitSet sel, double[] values) {
207
                questionAsked = false;
208
                //logger.debug(text);
209

    
210
                int ret = ((CadTool) cadToolStack.peek()).transition(text, source, sel,
211
                                values);
212

    
213
                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
214
                        popCadTool();
215

    
216
                        if (cadToolStack.isEmpty()) {
217
                                pushCadTool(new SelectionCadTool());
218
                                PluginServices.getMainFrame().selectTool("selection");
219
                        }
220

    
221
                        askQuestion();
222
                        if (getMapControl()!=null)
223
                        getMapControl().drawMap(false);
224
                } else {
225
                        if (((CadTool) cadToolStack.peek()).getAutomaton().checkState('c')) {
226
                                refresh();
227
                        }
228

    
229
                        if (!questionAsked) {
230
                                askQuestion();
231
                        }
232
                }
233
                        configureMenu();
234
                PluginServices.getMainFrame().enableControls();
235
        }
236
        /**
237
         * DOCUMENT ME!
238
         */
239
        private void askQuestion() {
240
                CadTool cadtool=(CadTool) cadToolStack.peek();
241
                if (cadtool.getAutomaton().getStatus()==0){
242
                        PluginServices.getMainFrame().addTextToConsole("\n" +cadtool.getName());
243
                }
244
                PluginServices.getMainFrame().addTextToConsole("\n" + cadtool.getQuestion());
245
                questionAsked = true;
246
                
247
        }
248
        /**
249
         * @param actionCommand
250
         */
251
        public void keyPressed(String actionCommand) {
252
                if (actionCommand.equals("eliminar")){
253
                        delete();
254
                }else if (actionCommand.equals("escape")) {
255
                                cadToolStack.clear();
256
                                pushCadTool(new SelectionCadTool());
257
                                selection.clear();
258
                                getMapControl().drawMap(false);
259
                                PluginServices.getMainFrame().selectTool("selection");
260
                                askQuestion();
261
                } 
262
                PluginServices.getMainFrame().enableControls();
263
        }
264
        /**
265
         * Elimina las geometr?as seleccionadas actualmente
266
         */
267
        private void delete() {
268
                editableFeatureSource.startComplexGeometry();
269
                try {
270
                for (int i = selection.nextSetBit(0); i >= 0;
271
                                i = selection.nextSetBit(i + 1)) {
272
                                editableFeatureSource.removeGeometry(i);
273
                }
274
                } catch (DriverIOException e) {
275
                        e.printStackTrace();
276
                } catch (IOException e) {
277
                        e.printStackTrace();
278
                } finally {
279
                try {
280
                        editableFeatureSource.endComplexGeometry();
281
                } catch (IOException e1) {
282
                        e1.printStackTrace();
283
                } catch (DriverIOException e1) {
284
                        e1.printStackTrace();
285
                }
286
                }
287
                selection.clear();
288
                refresh();
289
        }
290

    
291
}