Statistics
| Revision:

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

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 com.iver.andami.PluginServices;
44

    
45
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
46
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
47
import com.iver.cit.gvsig.fmap.layers.FBitSet;
48
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
49
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
50
import com.iver.cit.gvsig.gui.cad.CadTool;
51
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
52
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
53

    
54
import com.iver.fsac.Automaton;
55

    
56
import java.awt.Color;
57
import java.awt.Graphics;
58
import java.awt.event.MouseEvent;
59
import java.awt.geom.AffineTransform;
60
import java.awt.geom.Point2D;
61
import java.awt.geom.Rectangle2D;
62

    
63
import java.io.IOException;
64

    
65

    
66
/**
67
 * CADToolAdapter ajustado a las necesidades del Layout.
68
 *
69
 * @author Vicente Caballero Navarro
70
 */
71
public class LayoutCADToolAdapter extends CADToolAdapter {
72
        private Layout layout;
73

    
74
        /**
75
         *
76
         */
77
        public LayoutCADToolAdapter(Layout l) {
78
                layout = l;
79
        }
80

    
81
        /**
82
         * Devuelve la matriz de transformaci?n del layout.
83
         *
84
         * @return Matriz de transformaci?n.
85
         */
86
        public AffineTransform getAT() {
87
                return layout.getAT();
88
        }
89

    
90
        /**
91
         * Devuelve la distancia real del Layout a partir de los pixels.
92
         *
93
         * @param i Pixels.
94
         *
95
         * @return Distancia real.
96
         */
97
        public double toDistance(int i) {
98
                return FLayoutUtilities.toSheetDistance(i, getAT());
99
        }
100

    
101
        /**
102
         * Repinta el Layout.
103
         */
104
        public void refresh() {
105
                //        layout.refresh();
106
        }
107

    
108
        /**
109
         * Pinta de alguna manera especial las geometrias seleccionadas para la
110
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto
111
         * del mismo.
112
         *
113
         * @param g DOCUMENT ME!
114
         */
115
        public void paintComponent(Graphics g) {
116
                getCadTool().setAT(getAT());
117

    
118
                Point2D.Double paux = new Point2D.Double(adjustedPoint.getX(),
119
                                adjustedPoint.getY());
120
                paux = FLayoutUtilities.toSheetPoint(paux, getAT());
121
                getCadTool().drawOperation(g, null, null, paux.getX(), paux.getY());
122
        }
123

    
124
        /**
125
         * Devuelve un punto real a partir de uno en pixels.
126
         *
127
         * @param p Punto de pantalla.
128
         *
129
         * @return Punto real del Layout.
130
         */
131
        public Point2D toPoint(Point2D p) {
132
                return FLayoutUtilities.toSheetPoint(p, getAT());
133
        }
134

    
135
        /**
136
         * Devuelve el extent real del Layout.
137
         *
138
         * @return Extent real.
139
         */
140
        public Rectangle2D getExtent() {
141
                return FLayoutUtilities.toSheetRect(layout.getRect(), getAT());
142
        }
143

    
144
        /**
145
         * Devuelve la distancia en pixels a partir de una distancia real.
146
         *
147
         * @param d distancia real.
148
         *
149
         * @return Distancia en pixels.
150
         */
151
        public double fromDistance(double d) {
152
                return FLayoutUtilities.fromSheetDistance(d, getAT());
153
        }
154

    
155
        /**
156
         * Devuelve un punto en pixels a partir de un punto real.
157
         *
158
         * @param p Punto real.
159
         *
160
         * @return Punto en pixels.
161
         */
162
        public Point2D fromPoint(Point2D p) {
163
                return FLayoutUtilities.fromSheetPoint(p, getAT());
164
        }
165

    
166
        /**
167
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
168
         */
169
        public void mouseMoved(MouseEvent e) throws BehaviorException {
170
                calculateSnapPoint(e.getPoint());
171
        }
172

    
173
        /**
174
         * Dibuja el grid sobre el graphics que se pasa como par?metro.
175
         *
176
         * @param g Graphics sobre el que se dibuja el grid.
177
         */
178
        public void drawGrid(Graphics g) {
179
                g.setColor(Color.blue);
180
                grid.drawGrid(g);
181
        }
182

    
183
        /**
184
         * Inserta true o false seg?n se quiera visualizar el grid.
185
         *
186
         * @param value True si se quiere visualizar el grid.
187
         */
188
        public void setGrid(boolean value) {
189
                grid.setUseGrid(value);
190
                grid.setAT(getAT());
191
        }
192

    
193
        /**
194
         * Configura las opciones de men?.
195
         */
196
        public void configureMenu() {
197
                String[] desc = ((CadTool) cadToolStack.peek()).getAutomaton()
198
                                                 .getCurrentTransitionDescriptions();
199
                String[] labels = ((CadTool) cadToolStack.peek()).getAutomaton()
200
                                                   .getCurrentTransitions();
201
                layout.clearMenu();
202

    
203
                for (int i = 0; i < desc.length; i++) {
204
                        if (desc[i] != null) {
205
                                layout.addMenuEntry(desc[i], labels[i]);
206
                        }
207
                }
208
        }
209

    
210
        /**
211
         * Realiza la transici?n que corresponde con el String que se pasa como
212
         * par?metro.
213
         *
214
         * @param text Texto de la transici?n.
215
         * @param source EditableFeatureSource.
216
         * @param sel FBitset con la selecci?n.
217
         * @param values Valores que se pasan a la transici?n.
218
         */
219
        protected void transition(String text, EditableFeatureSource source,
220
                FBitSet sel, double[] values) {
221
                int ret = ((CadTool) cadToolStack.peek()).transition(text, source, sel,
222
                                values);
223

    
224
                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
225
                        popCadTool();
226

    
227
                        if (cadToolStack.isEmpty()) {
228
                                pushCadTool(new SelectionCadTool());
229
                                PluginServices.getMainFrame().selectTool("selection");
230
                        }
231

    
232
                        if (((CadTool) cadToolStack.peek()).getAutomaton().checkState('c')) {
233
                                refresh();
234
                        }
235

    
236
                        //configureMenu();
237
                        PluginServices.getMainFrame().enableControls();
238
                }
239
        }
240

    
241
        /**
242
         * DOCUMENT ME!
243
         *
244
         * @param actionCommand
245
         */
246
        public void keyPressed(String actionCommand) {
247
                if (actionCommand.equals("eliminar")) {
248
                        delete();
249
                } else if (actionCommand.equals("escape")) {
250
                        cadToolStack.clear();
251
                        pushCadTool(new SelectionCadTool());
252
                        selection.clear();
253
                        getMapControl().drawMap(false);
254
                        PluginServices.getMainFrame().selectTool("selection");
255

    
256
                        //askQuestion();
257
                }
258

    
259
                PluginServices.getMainFrame().enableControls();
260
        }
261

    
262
        /**
263
         * Elimina las geometr?as seleccionadas actualmente
264
         */
265
        private void delete() {
266
                editableFeatureSource.startComplexGeometry();
267

    
268
                try {
269
                        for (int i = selection.nextSetBit(0); i >= 0;
270
                                        i = selection.nextSetBit(i + 1)) {
271
                                editableFeatureSource.removeGeometry(i);
272
                        }
273
                } catch (DriverIOException e) {
274
                        e.printStackTrace();
275
                } catch (IOException e) {
276
                        e.printStackTrace();
277
                } finally {
278
                        try {
279
                                editableFeatureSource.endComplexGeometry();
280
                        } catch (IOException e1) {
281
                                e1.printStackTrace();
282
                        } catch (DriverIOException e1) {
283
                                e1.printStackTrace();
284
                        }
285
                }
286

    
287
                selection.clear();
288
                refresh();
289
        }
290

    
291
        /**
292
         * Devuelve si est? seleccionada la geometr?a que contiene el FFrame que se pasa como par?metro.
293
         *
294
         * @param frame FFrame de tipo FFrameGraphic.
295
         *
296
         * @return True si la geometr?a est? seleccionada.
297
         */
298
        public boolean isSelected(IFFrame frame) {
299
                LayoutEditableFeature efs = (LayoutEditableFeature) getEditableFeatureSource();
300
                int index = layout.getAllFFrames().indexOf(frame);
301

    
302
                if (layout.isInvalidate(index)) {
303
                        return false;
304
                }
305

    
306
                try {
307
                        return selection.get(efs.getIndexGeom(index));
308
                } catch (NullPointerException e) {
309
                        return false;
310
                }
311
        }
312
}