Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / gui / cad / CADToolAdapter.java @ 24500

History | View | Annotate | Download (49.8 KB)

1 3711 caballero
package com.iver.cit.gvsig.gui.cad;
2
3
import java.awt.Color;
4
import java.awt.Cursor;
5 5874 fjp
import java.awt.FontMetrics;
6 3711 caballero
import java.awt.Graphics;
7 5874 fjp
import java.awt.Graphics2D;
8 3883 caballero
import java.awt.Image;
9 3711 caballero
import java.awt.Point;
10 3883 caballero
import java.awt.Toolkit;
11 4313 fjp
import java.awt.event.InputEvent;
12 3711 caballero
import java.awt.event.MouseEvent;
13
import java.awt.event.MouseWheelEvent;
14
import java.awt.geom.Point2D;
15 3883 caballero
import java.awt.geom.Rectangle2D;
16 23646 vcaballero
import java.awt.image.BufferedImage;
17 3883 caballero
import java.awt.image.MemoryImageSource;
18 5261 fjp
import java.util.ArrayList;
19 6174 caballero
import java.util.HashMap;
20 22986 vcaballero
import java.util.Iterator;
21 3711 caballero
import java.util.Stack;
22 6713 caballero
import java.util.prefs.Preferences;
23 3711 caballero
24 5874 fjp
import org.cresques.cts.IProjection;
25 24500 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
26
import org.gvsig.fmap.dal.exception.ReadException;
27 24490 jmvivo
import org.gvsig.fmap.dal.feature.CommandsRecord;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureSet;
30
import org.gvsig.fmap.dal.feature.FeatureStore;
31 24500 jmvivo
import org.gvsig.fmap.dal.feature.exception.NeedEditingModeException;
32 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
33 22986 vcaballero
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
34 21668 vcaballero
import org.gvsig.fmap.geom.util.Converter;
35
import org.gvsig.fmap.geom.util.UtilFunctions;
36
import org.gvsig.fmap.mapcontext.MapContext;
37
import org.gvsig.fmap.mapcontext.ViewPort;
38
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
39
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
40
import org.gvsig.fmap.mapcontext.rendering.symbols.FSymbol;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
43
import org.gvsig.fmap.mapcontrol.MapControl;
44
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
45 21744 vcaballero
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
46 21668 vcaballero
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
47 5874 fjp
48 3711 caballero
import com.iver.andami.PluginServices;
49 12296 caballero
import com.iver.andami.messages.NotificationManager;
50 5874 fjp
import com.iver.andami.ui.mdiFrame.MainFrame;
51 12739 caballero
import com.iver.andami.ui.mdiManager.IWindow;
52 3883 caballero
import com.iver.cit.gvsig.CADExtension;
53 6174 caballero
import com.iver.cit.gvsig.EditionManager;
54 3847 caballero
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
55 5223 fjp
import com.iver.cit.gvsig.layers.ILayerEdited;
56 4366 caballero
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
57 7304 caballero
import com.iver.cit.gvsig.project.documents.view.gui.View;
58 9956 caballero
import com.iver.cit.gvsig.project.documents.view.toolListeners.StatusBarListener;
59 4892 caballero
import com.iver.utiles.console.JConsole;
60 4115 fjp
import com.vividsolutions.jts.geom.Envelope;
61 3711 caballero
62 20098 jmvivo
/**
63 24429 vcaballero
 * <p>
64
 * Allows user interact with different CAD tools, on a layer being edited.
65
 * </p>
66 21668 vcaballero
 *
67 24429 vcaballero
 * <p>
68
 * There are two ways of interacting:
69
 * <ul>
70
 * <li><b>With the mouse</b> : user selects any {@link CADTool CADTool} that
71
 * produces mouse events as consequence of the actions working with the layer
72
 * being edited. </li>
73
 * <li><b>Writing commands in the edition console</b> : most of the
74
 * {@link CADTool CADTool} mouse actions can also be called writing a command or
75
 * a command's parameter in the associated edition console, and pressing the key
76
 * <code>Enter</code>. If the command isn't valid, will notify it. </li>
77
 * </ul>
78 20098 jmvivo
 * </p>
79 21668 vcaballero
 *
80 24429 vcaballero
 * <p>
81
 * The edition has been implemented as a <i>finite machine</i>, with three kind
82
 * of transitions between states according the parameters introduced:
83
 * <ul>
84
 * <li><i>First transition type: <b>Point</i></b>: if <code>text</code>
85
 * matches with any pattern of parameters needed for any kind of point
86
 * coordinates.<br>
87
 * There are eight ways of introducing point 2D coordinates:
88
 * <ul>
89
 * <li><i>X,Y</i> : absolute cardinal 2D coordinate from the center <i>(0,0)</i>
90
 * of the CCS <i>Current Coordinate System</i>.</li>
91
 * <li><i>@X,Y</i> : relative cardinal 2D distances from the last point added
92
 * of the CCS. If it's the first point of the geometry, works like <i>X,Y</i>.</li>
93
 * <li><i>length< angle</i> : absolute polar 2D coordinate from the center
94
 * <i>(0,0)</i> of the CCS <i>Current Coordinate System</i>, using <i>angle</i>
95
 * from the <i>X</i> axis of CCS, and <i>length</i> far away.</li>
96
 * <li><i>@length< angle</i> : relative polar 2D coordinate from the last
97
 * point added of the CCS <i>Current Coordinate System</i>, using <i>angle</i>
98
 * from the <i>X</i> axis of CCS, and <i>length</i> far away. If it's the
99
 * first point of the geometry, works like <i>length< angle</i>.</li>
100
 * <li><i>*X,Y</i> : like <i>X,Y</i> but using UCS <i>Universal Coordinate
101
 * System</i> as reference.</li>
102
 * <li><i>@*X,Y</i> : like <i>@X,Y</i> but using UCS <i>Universal Coordinate
103
 * System</i> as reference. If it's the first point of the geometry, works like
104
 * <i>*X,Y</i>.</li>
105
 * <li><i>*length< angle</i> : like <i>length< angle</i> but using UCS
106
 * <i>Universal Coordinate System</i> as reference.</li>
107
 * <li><i>@*length< angle</i> : like <i>@length< angle</i> but using UCS
108
 * <i>Universal Coordinate System</i> as reference. If it's the first point of
109
 * the geometry, works like <i>*length< angle</i>.</li>
110
 * </ul>
111
 * </li>
112
 * <li><i>Second transition type: <b>Value</i></b>: if recognizes it as a
113
 * single number.</li>
114
 * <li><i>Third transition type: <b>Option</i></b>: by default, if can't
115
 * classify the information as a single number neither as a point. This
116
 * information will be an <code>String</code> and dealt as an option of the
117
 * current tool state. Ultimately, if isn't valid, <code>text</code> will be
118
 * rewritten in the console notifying the user that isn't correct.</li>
119
 * </ul>
120 20098 jmvivo
 * </p>
121 21668 vcaballero
 *
122 20098 jmvivo
 * @see Behavior
123
 * @see MapControl
124
 */
125 3711 caballero
public class CADToolAdapter extends Behavior {
126 23646 vcaballero
127 20098 jmvivo
        /**
128 24429 vcaballero
         * Stores the CAD tools to edit the layers of the associated
129
         * <code>MapControl</code>.
130 21668 vcaballero
         *
131 20098 jmvivo
         * @see #addCADTool(String, CADTool)
132
         * @see #getCadTool()
133 21668 vcaballero
         * @see #getCADTool(String)
134 20098 jmvivo
         */
135 6174 caballero
        private static HashMap namesCadTools = new HashMap();
136 4366 caballero
137 20098 jmvivo
        /**
138 24429 vcaballero
         * Reference to the object used to manage the edition of the layers of the
139
         * associated <code>MapControl</code>.
140 21668 vcaballero
         *
141 20098 jmvivo
         * @see EditionManager
142
         * @see #getEditionManager()
143
         */
144 6174 caballero
        private EditionManager editionManager = new EditionManager();
145
146 20098 jmvivo
        /**
147 24429 vcaballero
         * Identifies that the data are absolute coordinates of the new point from
148
         * the (0, 0) position.
149 20098 jmvivo
         */
150 5165 fjp
        public static final int ABSOLUTE = 0;
151
152 20098 jmvivo
        /**
153
         * Equivalent to {@link CADToolAdapter#ABSOLUTE CADToolAdapter#ABSOLUTE}.
154
         */
155 5165 fjp
        public static final int RELATIVE_SCP = 1;
156
157 20098 jmvivo
        /**
158 24429 vcaballero
         * Identifies that the data are relative distances of the new point from the
159
         * previous introduced.
160 20098 jmvivo
         */
161 5165 fjp
        public static final int RELATIVE_SCU = 2;
162
163 20098 jmvivo
        /**
164 24429 vcaballero
         * Identifies that the data are relative polar distances (longitude of the
165
         * line and angle given in degrees) of the new point from the previous
166
         * introduced.
167 20098 jmvivo
         */
168 5165 fjp
        public static final int POLAR_SCP = 3;
169
170 20098 jmvivo
        /**
171 24429 vcaballero
         * Identifies that the data are relative polar distances (longitude of the
172
         * line and angle given in radians) of the new point from the previous
173
         * introduced.
174 20098 jmvivo
         */
175 5165 fjp
        public static final int POLAR_SCU = 4;
176
177 20098 jmvivo
        /**
178
         * Stores the 2D map coordinates of the last point added.
179
         */
180 5165 fjp
        private double[] previousPoint = null;
181
182 20098 jmvivo
        /**
183
         * <i>Stack with CAD tools.</i>
184 21668 vcaballero
         *
185 20098 jmvivo
         * <i>For each CAD tool we use, the last item added in this stack will
186 24429 vcaballero
         * display a different icon according to the current operation and its
187
         * status.</i>
188 20098 jmvivo
         */
189 3711 caballero
        private Stack cadToolStack = new Stack();
190
191 20098 jmvivo
        /**
192
         * X coordinate of the last dragging or moving mouse event.
193
         */
194 3711 caballero
        private int lastX;
195 4115 fjp
196 20098 jmvivo
        /**
197
         * Y coordinate of the last dragging or moving mouse event.
198
         */
199 3711 caballero
        private int lastY;
200 4115 fjp
201 20098 jmvivo
        /**
202
         * Unused attribute.
203
         */
204 24429 vcaballero
        private ISymbol symbol = SymbologyFactory.createDefaultSymbolByShapeType(
205
                        Geometry.TYPES.POINT, Color.RED);
206 4115 fjp
207 20098 jmvivo
        /**
208
         * Represents the cursor's point selected in <i>map coordinates</i>.
209 21668 vcaballero
         *
210 20098 jmvivo
         * @see MapControl#toMapPoint
211
         */
212 3711 caballero
        private Point2D mapAdjustedPoint;
213 5383 caballero
214 20098 jmvivo
        /**
215 24429 vcaballero
         * Kind of geometry drawn to identify the kind of control point selected by
216
         * the cursor's mouse.
217 20098 jmvivo
         */
218 24429 vcaballero
        // private ISnapper usedSnap = null;
219 20098 jmvivo
        /**
220 24429 vcaballero
         * Determines if has displayed at the edition console, the question for the
221
         * operations that can do the user with the current CAD tool, in its current
222
         * state.
223 20098 jmvivo
         */
224 3711 caballero
        private boolean questionAsked = false;
225 4115 fjp
226 20098 jmvivo
        /**
227
         * Represents the cursor's point selected in <i>screen coordinates</i>.
228
         *
229
         * @see ViewPort#fromMapPoint(Point2D)
230
         */
231 3711 caballero
        private Point2D adjustedPoint;
232 4115 fjp
233 20098 jmvivo
        /**
234
         * Determines if the snap tools are enabled or disabled.
235 21668 vcaballero
         *
236 20098 jmvivo
         * @see #isRefentEnabled()
237
         * @see #setRefentEnabled(boolean)
238
         */
239 24429 vcaballero
        // private boolean bRefent = true;
240 20098 jmvivo
        /**
241 24429 vcaballero
         * <p>
242
         * Determines if the position of the snap of the mouse's cursor on the
243
         * <code>MapControl</code> is within the area around a control point of a
244
         * geometry.
245
         * </p>
246 21668 vcaballero
         *
247 24429 vcaballero
         * <p>
248
         * The area is calculated as a circle centered at the control point and with
249
         * radius the pixels tolerance defined in the preferences.
250
         * </p>
251 20098 jmvivo
         */
252 24429 vcaballero
        // private boolean bForceCoord = false;
253 20098 jmvivo
        /**
254 24429 vcaballero
         * Optional grid that could be applied on the <code>MapControl</code>'s
255
         * view port.
256 21668 vcaballero
         *
257 20098 jmvivo
         * @see #getGrid()
258
         * @see #setAdjustGrid(boolean)
259
         */
260 24429 vcaballero
        // private CADGrid cadgrid = new CADGrid();
261 20098 jmvivo
        /**
262
         * Determines is is enabled or not the <i>Orto</i> mode.
263
         */
264 5874 fjp
        private boolean bOrtoMode;
265
266 20098 jmvivo
        /**
267 24429 vcaballero
         * A light yellow color for the tool tip text box associated to the point
268
         * indicated by the mouse's cursor.
269 20098 jmvivo
         */
270 5874 fjp
        private Color theTipColor = new Color(255, 255, 155);
271 7072 caballero
272 20098 jmvivo
        /**
273
         * Last question asked to the user in the CAD console.
274
         */
275 9121 caballero
        private Object lastQuestion;
276
277 20098 jmvivo
        /**
278
         * Maximum tolerance in the approximation of a curved line by a polyline.
279 21668 vcaballero
         *
280 20098 jmvivo
         * @see #initializeFlatness()
281
         */
282 24429 vcaballero
        private static boolean flatnessInitialized = false;
283 20098 jmvivo
284
        /**
285
         * Edition preferences.
286
         */
287 24429 vcaballero
        private static Preferences prefs = Preferences.userRoot().node(
288
                        "cadtooladapter");
289 21668 vcaballero
290 20098 jmvivo
        /**
291 24429 vcaballero
         * Listener to display the coordinates in the current application's status
292
         * bar.
293 20098 jmvivo
         */
294 24429 vcaballero
        private StatusBarListener sbl = null;
295 20098 jmvivo
296
        /*
297
         * (non-Javadoc)
298 24429 vcaballero
         *
299 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
300 9956 caballero
         */
301
        public void setMapControl(MapControl mc) {
302
                super.setMapControl(mc);
303 24429 vcaballero
                sbl = new StatusBarListener(getMapControl());
304 9956 caballero
        }
305 24429 vcaballero
306
        private static final Image imageCursor = new BufferedImage(32, 32,
307
                        BufferedImage.TYPE_INT_ARGB);
308 23646 vcaballero
        static {
309 24429 vcaballero
                Graphics g = imageCursor.getGraphics();
310 23646 vcaballero
                int size1 = 15;
311
                int size2 = 3;
312 24429 vcaballero
                int x = 16;
313
                int y = 16;
314 23646 vcaballero
                g.setColor(Color.MAGENTA);
315 24429 vcaballero
                g.drawLine((int) (x - size1), (int) (y), (int) (x + size1), (int) (y));
316
                g.drawLine((int) (x), (int) (y - size1), (int) (x), (int) (y + size1));
317
                // g.setColor(Color.MAGENTA);
318
                g.drawRect((int) (x - 6), (int) (y - 6), 12, 12);
319
                g.drawRect((int) (x - 3), (int) (y - 3), 6, 6);
320 20098 jmvivo
321 24429 vcaballero
                // // getMapControl().setToolTipText(null);
322
                // if (adjustedPoint != null) {
323
                // if (bForceCoord) {
324
                // /* g.setColor(Color.ORANGE);
325
                // g.drawRect((int) (adjustedPoint.getX() - 6),
326
                // (int) (adjustedPoint.getY() - 6), 12, 12);
327
                // g.drawRect((int) (adjustedPoint.getX() - 3),
328
                // (int) (adjustedPoint.getY() - 3), 6, 6);
329
                // g.setColor(Color.MAGENTA);
330
                // g.drawRect((int) (adjustedPoint.getX() - 4),
331
                // (int) (adjustedPoint.getY() - 4), 8, 8); */
332
                // if (usedSnap != null)
333
                // {
334
                // usedSnap.draw(g, adjustedPoint);
335
                //
336
                // Graphics2D g2 = (Graphics2D) g;
337
                // FontMetrics metrics = g2.getFontMetrics();
338
                // int w = metrics.stringWidth(usedSnap.getToolTipText()) + 5;
339
                // int h = metrics.getMaxAscent() + 5;
340
                // int x = (int)p.getX()+9;
341
                // int y = (int)p.getY()- 7;
342
                //
343
                // g2.setColor(theTipColor );
344
                // g2.fillRect(x, y-h, w, h);
345
                // g2.setColor(Color.BLACK);
346
                // g2.drawRect(x, y-h, w, h);
347
                // g2.drawString(usedSnap.getToolTipText(), x+3, y-3);
348
                //
349
                //
350
                // // getMapControl().setToolTipText(usedSnap.getToolTipText());
351
                // }
352
                //
353
                // bForceCoord = false;
354
                // } else {
355
                // g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
356
                // (int) (size2 * 2), (int) (size2 * 2));
357
                // }
358
                // }
359 23646 vcaballero
360
        }
361
362 3711 caballero
        /**
363 24429 vcaballero
         * <p>
364
         * Draws the selected geometries to edit. And, if the <i>snapping</i> is
365
         * enabled, draws also its effect over them.
366
         * </p>
367 5383 caballero
         *
368 3711 caballero
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
369
         */
370
        public void paintComponent(Graphics g) {
371
                super.paintComponent(g);
372 24429 vcaballero
                if (CADExtension.getCADToolAdapter() != this)
373 6174 caballero
                        return;
374 12888 caballero
375 3711 caballero
                if (adjustedPoint != null) {
376 4115 fjp
                        Point2D p = null;
377 3711 caballero
                        if (mapAdjustedPoint != null) {
378
                                p = mapAdjustedPoint;
379
                        } else {
380
                                p = getMapControl().getViewPort().toMapPoint(adjustedPoint);
381
                        }
382 6174 caballero
383 4115 fjp
                        ((CADTool) cadToolStack.peek())
384
                                        .drawOperation(g, p.getX(), p.getY());
385 3711 caballero
                }
386 24429 vcaballero
                // drawCursor(g);
387
                // getGrid().drawGrid(g);
388 3711 caballero
        }
389
390
        /**
391 24429 vcaballero
         * <p>
392
         * Responds two kind of mouse click events:
393
         * <ul>
394
         * <li><b><i>One click of the third mouse's button</i></b>: displays a
395
         * popup with edition options.</li>
396
         * <li><b><i>Two clicks of the first mouse's button</i></b>: ends the
397
         * last cad tool setting as end transition point the event's one.</li>
398
         * </ul>
399 20098 jmvivo
         * </p>
400 21668 vcaballero
         *
401
         *
402 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseClicked(java.awt.event.MouseEvent)
403
         * @see CADExtension#showPopup(MouseEvent)
404 3711 caballero
         */
405
        public void mouseClicked(MouseEvent e) throws BehaviorException {
406 4115 fjp
                if (e.getButton() == MouseEvent.BUTTON3) {
407 3883 caballero
                        CADExtension.showPopup(e);
408 24429 vcaballero
                } else if (e.getButton() == MouseEvent.BUTTON1
409
                                && e.getClickCount() == 2) {
410 17984 vcaballero
                        questionAsked = true;
411
                        if (!cadToolStack.isEmpty()) {
412
                                CADTool ct = (CADTool) cadToolStack.peek();
413
                                ViewPort vp = getMapControl().getMapContext().getViewPort();
414
                                Point2D p;
415
416
                                if (mapAdjustedPoint != null) {
417
                                        p = mapAdjustedPoint;
418
                                } else {
419
                                        p = vp.toMapPoint(adjustedPoint);
420
                                }
421
                                ct.endTransition(p.getX(), p.getY(), e);
422 24429 vcaballero
                                previousPoint = new double[] { p.getX(), p.getY() };
423 17984 vcaballero
                        }
424 3883 caballero
                }
425 3711 caballero
        }
426
427 20098 jmvivo
        /*
428
         * (non-Javadoc)
429 24429 vcaballero
         *
430 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseEntered(java.awt.event.MouseEvent)
431 3711 caballero
         */
432
        public void mouseEntered(MouseEvent e) throws BehaviorException {
433 3952 fjp
                clearMouseImage();
434 3711 caballero
        }
435
436 20098 jmvivo
        /*
437
         * (non-Javadoc)
438 24429 vcaballero
         *
439 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseExited(java.awt.event.MouseEvent)
440 3711 caballero
         */
441
        public void mouseExited(MouseEvent e) throws BehaviorException {
442
        }
443
444
        /**
445 24429 vcaballero
         * Selects the vertex of a geometry at the point selected on the
446
         * <code>MapControl</code> by pressing the first mouse's button.
447 21668 vcaballero
         *
448 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
449 3711 caballero
         */
450
        public void mousePressed(MouseEvent e) throws BehaviorException {
451
                if (e.getButton() == MouseEvent.BUTTON1) {
452
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
453
                        Point2D p;
454
455 4115 fjp
                        if (mapAdjustedPoint != null) {
456 3711 caballero
                                p = mapAdjustedPoint;
457
                        } else {
458
                                p = vp.toMapPoint(adjustedPoint);
459
                        }
460 5165 fjp
                        transition(new double[] { p.getX(), p.getY() }, e, ABSOLUTE);
461 3711 caballero
                }
462
        }
463
464 24429 vcaballero
        // /**
465
        // * <p>Adjusts the <code>point</code> to the grid if its enabled, and
466
        // * sets <code>mapHandlerAdjustedPoint</code> with that new value.</p>
467
        // *
468
        // * <p>The value returned is the distance between those points: the
469
        // original and
470
        // * the adjusted one.</p>
471
        // *
472
        // * @param point point to adjust
473
        // * @param mapHandlerAdjustedPoint <code>point</code> adjusted
474
        // *
475
        // * @return distance from <code>point</code> to the adjusted one. If there
476
        // is no
477
        // * adjustment, returns <code>Double.MAX_VALUE</code>.
478
        // */
479
        // private double adjustToHandler(Point2D point,
480
        // Point2D mapHandlerAdjustedPoint) {
481
        //
482
        // if (!isRefentEnabled())
483
        // return Double.MAX_VALUE;
484
        //
485
        // ILayerEdited aux =
486
        // CADExtension.getEditionManager().getActiveLayerEdited();
487
        // if (!(aux instanceof VectorialLayerEdited))
488
        // return Double.MAX_VALUE;
489
        // VectorialLayerEdited vle = (VectorialLayerEdited) aux;
490
        //
491
        // ArrayList snappers = vle.getSnappers();
492
        // ArrayList layersToSnap = vle.getLayersToSnap();
493
        //
494
        //
495
        // ViewPort vp = getMapControl().getViewPort();
496
        //
497
        // snappers=SnapConfigPage.getActivesSnappers();
498
        //
499
        // double mapTolerance = vp.toMapDistance(SelectionCADTool.tolerance);
500
        // double minDist = mapTolerance;
501
        // // double rw = getMapControl().getViewPort().toMapDistance(5);
502
        // Point2D mapPoint = point;
503
        // double middleTol=mapTolerance * 0.5;
504
        // org.gvsig.fmap.geom.primitive.Envelope r = new
505
        // DefaultEnvelope(mapPoint.getX() - middleTol,
506
        // mapPoint.getY() - middleTol,
507
        // mapPoint.getX() + middleTol,
508
        // mapPoint.getY() + middleTol);
509
        //
510
        // Envelope e = Converter.convertEnvelopeToJTS(r);
511
        //
512
        // usedSnap = null;
513
        // Point2D lastPoint = null;
514
        // if (previousPoint != null)
515
        // {
516
        // lastPoint = new Point2D.Double(previousPoint[0], previousPoint[1]);
517
        // }
518
        // for (int j = 0; j < layersToSnap.size(); j++)
519
        // {
520
        // FLyrVect lyrVect = (FLyrVect) layersToSnap.get(j);
521
        // SpatialCache cache = lyrVect.getSpatialCache();
522
        // if (lyrVect.isVisible())
523
        // {
524
        // // La lista de snappers est? siempre ordenada por prioridad. Los de mayor
525
        // // prioridad est?n primero.
526
        // for (int i = 0; i < snappers.size(); i++)
527
        // {
528
        // ISnapper theSnapper = (ISnapper) snappers.get(i);
529
        //
530
        // if (usedSnap != null)
531
        // {
532
        // // Si ya tenemos un snap y es de alta prioridad, cogemos ese. (A no ser
533
        // que en otra capa encontremos un snapper mejor)
534
        // if (theSnapper.getPriority() < usedSnap.getPriority())
535
        // break;
536
        // }
537
        // SnappingVisitor snapVisitor = null;
538
        // Point2D theSnappedPoint = null;
539
        //
540
        // if (theSnapper instanceof ISnapperVectorial)
541
        // {
542
        // if (theSnapper instanceof ISnapperGeometriesVectorial) {
543
        // snapVisitor=new GeometriesSnappingVisitor((ISnapperGeometriesVectorial)
544
        // theSnapper,point,mapTolerance,lastPoint);
545
        // }else {
546
        // snapVisitor = new SnappingVisitor((ISnapperVectorial) theSnapper, point,
547
        // mapTolerance, lastPoint);
548
        // }
549
        // // System.out.println("Cache size = " + cache.size());
550
        // cache.query(e, snapVisitor);
551
        // theSnappedPoint = snapVisitor.getSnapPoint();
552
        //
553
        // }
554
        // if (theSnapper instanceof ISnapperRaster)
555
        // {
556
        // ISnapperRaster snapRaster = (ISnapperRaster) theSnapper;
557
        // theSnappedPoint = snapRaster.getSnapPoint(getMapControl(), point,
558
        // mapTolerance, lastPoint);
559
        // }
560
        //
561
        //
562
        // if (theSnappedPoint != null) {
563
        // double distAux = theSnappedPoint.distance(point);
564
        // if (minDist > distAux)
565
        // {
566
        // minDist = distAux;
567
        // usedSnap = theSnapper;
568
        // mapHandlerAdjustedPoint.setLocation(theSnappedPoint);
569
        // }
570
        // }
571
        // }
572
        // } // visible
573
        // }
574
        // if (usedSnap != null)
575
        // return minDist;
576
        // return Double.MAX_VALUE;
577
        //
578
        // }
579 5985 caballero
580 20098 jmvivo
        /*
581
         * (non-Javadoc)
582 24429 vcaballero
         *
583 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
584 3711 caballero
         */
585
        public void mouseReleased(MouseEvent e) throws BehaviorException {
586 3883 caballero
                getMapControl().repaint();
587 3711 caballero
        }
588
589 20098 jmvivo
        /*
590
         * (non-Javadoc)
591 24429 vcaballero
         *
592 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
593 3711 caballero
         */
594
        public void mouseDragged(MouseEvent e) throws BehaviorException {
595
                lastX = e.getX();
596
                lastY = e.getY();
597 24429 vcaballero
                adjustedPoint = e.getPoint();
598
                // calculateSnapPoint(e.getPoint());
599 3711 caballero
        }
600
601 20098 jmvivo
        /*
602
         * (non-Javadoc)
603 24429 vcaballero
         *
604 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
605 3711 caballero
         */
606
        public void mouseMoved(MouseEvent e) throws BehaviorException {
607 3883 caballero
608 3711 caballero
                lastX = e.getX();
609
                lastY = e.getY();
610 24429 vcaballero
                adjustedPoint = e.getPoint();
611
                // calculateSnapPoint(e.getPoint());
612 3711 caballero
613 5874 fjp
                showCoords(e.getPoint());
614 5985 caballero
615 3711 caballero
                getMapControl().repaint();
616
        }
617 4115 fjp
618 20098 jmvivo
        /**
619 24429 vcaballero
         * Displays the current coordinates of the mouse's cursor on the associated
620
         * <code>MapControl</code> object, at the status bar of the application's
621
         * main frame.
622 21668 vcaballero
         *
623 24429 vcaballero
         * @param pPix
624
         *            current 2D mouse's cursor coordinates on the
625
         *            <code>MapControl</code>
626 20098 jmvivo
         */
627 24429 vcaballero
        private void showCoords(Point2D pPix) {
628 5874 fjp
                String[] axisText = new String[2];
629 9956 caballero
                axisText[0] = "X = ";
630
                axisText[1] = "Y = ";
631 24429 vcaballero
                // NumberFormat nf = NumberFormat.getInstance();
632 5874 fjp
                MapControl mapControl = getMapControl();
633
                ViewPort vp = mapControl.getMapContext().getViewPort();
634
                IProjection iProj = vp.getProjection();
635 9956 caballero
636 24429 vcaballero
                // if (iProj.getAbrev().equals("EPSG:4326") ||
637
                // iProj.getAbrev().equals("EPSG:4230")) {
638
                // axisText[0] = "Lon = ";
639
                // axisText[1] = "Lat = ";
640
                // nf.setMaximumFractionDigits(8);
641
                // } else {
642
                // axisText[0] = "X = ";
643
                // axisText[1] = "Y = ";
644
                // nf.setMaximumFractionDigits(2);
645
                // }
646 5874 fjp
                Point2D p;
647 24429 vcaballero
                if (mapAdjustedPoint == null) {
648 5874 fjp
                        p = vp.toMapPoint(pPix);
649 24429 vcaballero
                } else {
650 5874 fjp
                        p = mapAdjustedPoint;
651
                }
652 9956 caballero
                sbl.setFractionDigits(p);
653
                axisText = sbl.setCoorDisplayText(axisText);
654 5874 fjp
                MainFrame mF = PluginServices.getMainFrame();
655
656 24429 vcaballero
                if (mF != null) {
657
                        mF.getStatusBar().setMessage(
658
                                        "units",
659
                                        PluginServices
660
                                                        .getText(this, MapContext.getDistanceNames()[vp
661
                                                                        .getDistanceUnits()]));
662
                        mF.getStatusBar().setControlValue("scale",
663
                                        String.valueOf(mapControl.getMapContext().getScaleView()));
664 6630 caballero
                        mF.getStatusBar().setMessage("projection", iProj.getAbrev());
665 5874 fjp
666 24429 vcaballero
                        String[] coords = sbl.getCoords(p);
667
                        mF.getStatusBar().setMessage("x", axisText[0] + coords[0]);
668
                        mF.getStatusBar().setMessage("y", axisText[1] + coords[1]);
669 5874 fjp
                }
670
        }
671
672 20098 jmvivo
        /**
673
         * Hides the mouse's cursor.
674
         */
675 4115 fjp
        private void clearMouseImage() {
676 3883 caballero
                int[] pixels = new int[16 * 16];
677
                Image image = Toolkit.getDefaultToolkit().createImage(
678 4115 fjp
                                new MemoryImageSource(16, 16, pixels, 0, 16));
679
                Cursor transparentCursor = Toolkit.getDefaultToolkit()
680
                                .createCustomCursor(image, new Point(0, 0), "invisiblecursor");
681 3996 caballero
682 3952 fjp
                getMapControl().setCursor(transparentCursor);
683 3883 caballero
        }
684 4115 fjp
685 3711 caballero
        /**
686 24429 vcaballero
         * <p>
687
         * Draws a 31x31 pixels cross round the mouse's cursor with an small
688
         * geometry centered:
689
         * <ul>
690
         * <li><i>an square centered</i>: if isn't over a <i>control point</i>.
691
         * <li><i>an small geometry centered according to the kind of control point</i>:
692
         * if it's over a control point. In this case, the small geometry is drawn
693
         * by a {@link ISnapper ISnapper} type object.<br>
694
         * On the other hand, a light-yellowed background tool tip text with the
695
         * type of <i>control point</i> will be displayed.</li>
696 21668 vcaballero
         * </p>
697 5383 caballero
         *
698 24429 vcaballero
         * @param g
699
         *            <code>MapControl</code>'s graphics where the data will be
700
         *            drawn
701 3711 caballero
         */
702 24429 vcaballero
        // private void drawCursor(Graphics g) {
703
        // g.setColor(Color.black);
704
        // Point2D p = adjustedPoint;
705
        //
706
        // if (p == null) {
707
        // getGrid().setViewPort(getMapControl().getViewPort());
708
        //
709
        // return;
710
        // }
711
        //
712
        // int size1 = 15;
713
        // int size2 = 3;
714
        // g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
715
        // (int) (p.getX() + size1), (int) (p.getY()));
716
        // g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
717
        // (int) (p.getX()), (int) (p.getY() + size1));
718
        //
719
        // // getMapControl().setToolTipText(null);
720
        // if (adjustedPoint != null) {
721
        // if (bForceCoord) {
722
        // /* g.setColor(Color.ORANGE);
723
        // g.drawRect((int) (adjustedPoint.getX() - 6),
724
        // (int) (adjustedPoint.getY() - 6), 12, 12);
725
        // g.drawRect((int) (adjustedPoint.getX() - 3),
726
        // (int) (adjustedPoint.getY() - 3), 6, 6);
727
        // g.setColor(Color.MAGENTA);
728
        // g.drawRect((int) (adjustedPoint.getX() - 4),
729
        // (int) (adjustedPoint.getY() - 4), 8, 8); */
730
        // if (usedSnap != null)
731
        // {
732
        // usedSnap.draw(g, adjustedPoint);
733
        //
734
        // Graphics2D g2 = (Graphics2D) g;
735
        // FontMetrics metrics = g2.getFontMetrics();
736
        // int w = metrics.stringWidth(usedSnap.getToolTipText()) + 5;
737
        // int h = metrics.getMaxAscent() + 5;
738
        // int x = (int)p.getX()+9;
739
        // int y = (int)p.getY()- 7;
740
        //
741
        // g2.setColor(theTipColor );
742
        // g2.fillRect(x, y-h, w, h);
743
        // g2.setColor(Color.BLACK);
744
        // g2.drawRect(x, y-h, w, h);
745
        // g2.drawString(usedSnap.getToolTipText(), x+3, y-3);
746
        //
747
        //
748
        // // getMapControl().setToolTipText(usedSnap.getToolTipText());
749
        // }
750
        //
751
        // bForceCoord = false;
752
        // } else {
753
        // g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
754
        // (int) (size2 * 2), (int) (size2 * 2));
755
        // }
756
        // }
757
        // }
758 3711 caballero
        /**
759 24429 vcaballero
         * <p>
760
         * Tries to find the nearest geometry or grid control point by the position
761
         * of the current snap tool.
762
         * </p>
763 21668 vcaballero
         *
764 24429 vcaballero
         * <p>
765
         * Prioritizes the grid control points than the geometries ones.
766
         * </p>
767 21668 vcaballero
         *
768 24429 vcaballero
         * <p>
769
         * If finds any near, stores the <i>map</i> and <i>pixel</i> coordinates
770
         * for the snap, and enables the <code>bForceCoord</code> attribute for
771
         * the next draw of the mouse's cursor.
772
         * </p>
773 5383 caballero
         *
774 24429 vcaballero
         * @param point
775
         *            current mouse 2D position
776 3711 caballero
         */
777 24429 vcaballero
        // private void calculateSnapPoint(Point point) {
778
        // // Se comprueba el ajuste a rejilla
779
        //
780
        // Point2D gridAdjustedPoint = getMapControl().getViewPort().toMapPoint(
781
        // point);
782
        // double minDistance = Double.MAX_VALUE;
783
        // CADTool ct = (CADTool) cadToolStack.peek();
784
        // if (ct instanceof SelectionCADTool
785
        // && ((SelectionCADTool) ct).getStatus().equals(
786
        // "Selection.FirstPoint")) {
787
        // mapAdjustedPoint = gridAdjustedPoint;
788
        // adjustedPoint = (Point2D) point.clone();
789
        // } else {
790
        //
791
        // minDistance = getGrid().adjustToGrid(gridAdjustedPoint);
792
        // if (minDistance < Double.MAX_VALUE) {
793
        // adjustedPoint = getMapControl().getViewPort().fromMapPoint(
794
        // gridAdjustedPoint);
795
        // mapAdjustedPoint = gridAdjustedPoint;
796
        // } else {
797
        // mapAdjustedPoint = null;
798
        // }
799
        // }
800
        // Point2D handlerAdjustedPoint = null;
801
        //
802
        // // Se comprueba el ajuste a los handlers
803
        // if (mapAdjustedPoint != null) {
804
        // handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); //
805
        // getMapControl().getViewPort().toMapPoint(point);
806
        // } else {
807
        // handlerAdjustedPoint = getMapControl().getViewPort().toMapPoint(
808
        // point);
809
        // }
810
        //
811
        // Point2D mapPoint = new Point2D.Double();
812
        // double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
813
        //
814
        // if (distance < minDistance) {
815
        // bForceCoord = true;
816
        // adjustedPoint = getMapControl().getViewPort().fromMapPoint(mapPoint);
817
        // mapAdjustedPoint = mapPoint;
818
        // minDistance = distance;
819
        // }
820
        //
821
        // // Si no hay ajuste
822
        // if (minDistance == Double.MAX_VALUE) {
823
        // adjustedPoint = point;
824
        // mapAdjustedPoint = null;
825
        // }
826
        //
827
        // }
828 20098 jmvivo
        /*
829
         * (non-Javadoc)
830 24429 vcaballero
         *
831 20098 jmvivo
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseWheelMoved(java.awt.event.MouseWheelEvent)
832 3711 caballero
         */
833
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
834
        }
835
836
        /**
837 24429 vcaballero
         * <p>
838
         * Process the information written by the user about the next point
839
         * coordinate, determining the kind of <i>transition</i> according the
840
         * parameters written.
841
         * </p>
842 21668 vcaballero
         *
843 24429 vcaballero
         * <p>
844
         * After, invokes one of the three possible <i>transition</i> methods of
845
         * the <i>finite machine</i> of edition:
846
         * <ul>
847
         * <li><i>First transition type: <b>Point</i></b>: if <code>text</code>
848
         * matches with any pattern of parameters needed for any kind of point
849
         * coordinates.<br>
850
         * There are eight ways of introducing point 2D coordinates:
851
         * <ul>
852
         * <li><i>X,Y</i> : absolute cardinal 2D coordinate from the center
853
         * <i>(0,0)</i> of the CCS <i>Current Coordinate System</i>.</li>
854
         * <li><i>@X,Y</i> : relative cardinal 2D distances from the last point
855
         * added of the CCS. If it's the first point of the geometry, works like
856
         * <i>X,Y</i>.</li>
857
         * <li><i>length< angle</i> : absolute polar 2D coordinate from the
858
         * center <i>(0,0)</i> of the CCS <i>Current Coordinate System</i>, using
859
         * <i>angle</i> from the <i>X</i> axis of CCS, and <i>length</i> far
860
         * away.</li>
861
         * <li><i>@length< angle</i> : relative polar 2D coordinate from the last
862
         * point added of the CCS <i>Current Coordinate System</i>, using <i>angle</i>
863
         * from the <i>X</i> axis of CCS, and <i>length</i> far away. If it's the
864
         * first point of the geometry, works like <i>length< angle</i>.</li>
865
         * <li><i>*X,Y</i> : like <i>X,Y</i> but using UCS <i>Universal
866
         * Coordinate System</i> as reference.</li>
867
         * <li><i>@*X,Y</i> : like <i>@X,Y</i> but using UCS <i>Universal
868
         * Coordinate System</i> as reference. If it's the first point of the
869
         * geometry, works like <i>*X,Y</i>.</li>
870
         * <li><i>*length< angle</i> : like <i>length< angle</i> but using UCS
871
         * <i>Universal Coordinate System</i> as reference.</li>
872
         * <li><i>@*length< angle</i> : like <i>@length< angle</i> but using
873
         * UCS <i>Universal Coordinate System</i> as reference. If it's the first
874
         * point of the geometry, works like <i>*length< angle</i>.</li>
875
         * </ul>
876
         * </li>
877
         * <li><i>Second transition type: <b>Value</i></b>: if recognizes it as a
878
         * single number.</li>
879
         * <li><i>Third transition type: <b>Option</i></b>: by default, if can't
880
         * classify the information as a single number neither as a point. This
881
         * information will be an <code>String</code> and dealt as an option of
882
         * the current tool state. Ultimately, if isn't valid, <code>text</code>
883
         * will be rewritten in the console notifying the user that isn't correct.</li>
884
         * </ul>
885 20098 jmvivo
         * </p>
886 5383 caballero
         *
887 24429 vcaballero
         * @param text
888
         *            command written by user in the edition's console
889 3711 caballero
         */
890
        public void textEntered(String text) {
891
                if (text == null) {
892 24429 vcaballero
                        transition(PluginServices.getText(this, "cancel"));
893 3711 caballero
                } else {
894 4115 fjp
                        /*
895
                         * if ("".equals(text)) { transition("aceptar"); } else {
896
                         */
897
                        text = text.trim();
898 5223 fjp
                        int type = ABSOLUTE;
899
                        String[] numbers = new String[1];
900
                        numbers[0] = text;
901
                        if (text.indexOf(",") != -1) {
902 3711 caballero
903 5015 caballero
                                numbers = text.split(",");
904 5165 fjp
                                if (numbers[0].substring(0, 1).equals("@")) {
905
                                        numbers[0] = numbers[0].substring(1, numbers[0].length());
906
                                        type = RELATIVE_SCU;
907
                                        if (numbers[0].substring(0, 1).equals("*")) {
908
                                                type = RELATIVE_SCP;
909
                                                numbers[0] = numbers[0].substring(1, numbers[0]
910
                                                                .length());
911 5015 caballero
                                        }
912
                                }
913 5165 fjp
                        } else if (text.indexOf("<") != -1) {
914
                                type = POLAR_SCP;
915 5015 caballero
                                numbers = text.split("<");
916 5165 fjp
                                if (numbers[0].substring(0, 1).equals("@")) {
917
                                        numbers[0] = numbers[0].substring(1, numbers[0].length());
918
                                        type = POLAR_SCU;
919
                                        if (numbers[0].substring(0, 1).equals("*")) {
920
                                                type = POLAR_SCP;
921
                                                numbers[0] = numbers[0].substring(1, numbers[0]
922
                                                                .length());
923 5015 caballero
                                        }
924
                                }
925
                        }
926
927 4115 fjp
                        double[] values = null;
928 3711 caballero
929 4115 fjp
                        try {
930
                                if (numbers.length == 2) {
931
                                        // punto
932
                                        values = new double[] { Double.parseDouble(numbers[0]),
933
                                                        Double.parseDouble(numbers[1]) };
934 5165 fjp
                                        transition(values, null, type);
935 4115 fjp
                                } else if (numbers.length == 1) {
936
                                        // valor
937
                                        values = new double[] { Double.parseDouble(numbers[0]) };
938 4147 fjp
                                        transition(values[0]);
939 3711 caballero
                                }
940 4115 fjp
                        } catch (NumberFormatException e) {
941 4147 fjp
                                transition(text);
942 5070 caballero
                        } catch (NullPointerException e) {
943
                                transition(text);
944 4115 fjp
                        }
945
                        // }
946 3711 caballero
                }
947 4002 fjp
                getMapControl().repaint();
948 3711 caballero
        }
949
950
        /**
951 24429 vcaballero
         * If there are options related with the <code>CADTool</code> at the peek
952
         * of the CAD tool stack, displays them as a popup.
953 3711 caballero
         */
954 3883 caballero
        public void configureMenu() {
955
                String[] desc = ((CADTool) cadToolStack.peek()).getDescriptions();
956 4115 fjp
                // String[] labels = ((CADTool)
957
                // cadToolStack.peek()).getCurrentTransitions();
958 3883 caballero
                CADExtension.clearMenu();
959 3711 caballero
960
                for (int i = 0; i < desc.length; i++) {
961
                        if (desc[i] != null) {
962 5165 fjp
                                CADExtension
963
                                                .addMenuEntry(PluginServices.getText(this, desc[i]));// ,
964 5223 fjp
                                // labels[i]);
965 3711 caballero
                        }
966
                }
967 3883 caballero
968 3711 caballero
        }
969
970
        /**
971 24429 vcaballero
         * <p>
972
         * One of the three kind of transaction methods of the <i>finite machine</i>
973
         * of edition.
974
         * </p>
975 21668 vcaballero
         *
976 24429 vcaballero
         * <p>
977
         * This one deals <code>values</code> as two numbers that, according
978
         * <code>type</code> calculate a new point 2D in the current layer edited
979
         * in the associated <code>MapControl</code>.
980
         * </p>
981 5383 caballero
         *
982 24429 vcaballero
         * <p>
983
         * There are different ways of calculating the new point 2D coordinates,
984
         * according the value of <code>type</code>, see
985
         * {@link #textEntered(String) #textEntered(String)}.
986
         * </p>
987 21668 vcaballero
         *
988 24429 vcaballero
         * <p>
989
         * After applying the changes, updates the controls available for managing
990
         * the current data.
991
         * </p>
992 21668 vcaballero
         *
993 24429 vcaballero
         * @param values
994
         *            numbers needed to calculate the new point coordinates
995
         *            according <code>type</code>
996
         * @param event
997
         *            event which generated this invocation (a
998
         *            <code>MouseEvent</code> or a <code>KeyEvent</code>)
999
         * @param type
1000
         *            kind of information that is <code>values</code>. According
1001
         *            this parameter, will calculate the new point in a different
1002
         *            way
1003 21668 vcaballero
         *
1004 20098 jmvivo
         * @see CADTool#transition(double, double, InputEvent)
1005
         * @see #transition(double)
1006
         * @see #transition(String)
1007 3711 caballero
         */
1008 5165 fjp
        private void transition(double[] values, InputEvent event, int type) {
1009 3711 caballero
                questionAsked = true;
1010 4115 fjp
                if (!cadToolStack.isEmpty()) {
1011
                        CADTool ct = (CADTool) cadToolStack.peek();
1012 5015 caballero
1013
                        switch (type) {
1014
                        case ABSOLUTE:
1015 4313 fjp
                                ct.transition(values[0], values[1], event);
1016 5165 fjp
                                previousPoint = values;
1017 5015 caballero
                                break;
1018
                        case RELATIVE_SCU:
1019 5165 fjp
                                // Comprobar que tenemos almacenado el punto anterior
1020
                                // y crear nuevo con coordenadas relativas a ?l.
1021
                                double[] auxSCU = values;
1022
                                if (previousPoint != null) {
1023
                                        auxSCU[0] = previousPoint[0] + values[0];
1024
                                        auxSCU[1] = previousPoint[1] + values[1];
1025 5015 caballero
                                }
1026
                                ct.transition(auxSCU[0], auxSCU[1], event);
1027 3744 caballero
1028 5165 fjp
                                previousPoint = auxSCU;
1029 5015 caballero
                                break;
1030
                        case RELATIVE_SCP:
1031 5165 fjp
                                // TODO de momento no implementado.
1032 5015 caballero
                                ct.transition(values[0], values[1], event);
1033 5165 fjp
                                previousPoint = values;
1034 5015 caballero
                                break;
1035 24429 vcaballero
                        case POLAR_SCU:// Relativo
1036 5165 fjp
                                // Comprobar que tenemos almacenado el punto anterior
1037
                                // y crear nuevo con coordenadas relativas a ?l.
1038
                                double[] auxPolarSCU = values;
1039
                                if (previousPoint != null) {
1040
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
1041
                                                        previousPoint[0], previousPoint[1]), Math
1042
                                                        .toRadians(values[1]), values[0]);
1043
                                        auxPolarSCU[0] = point.getX();
1044
                                        auxPolarSCU[1] = point.getY();
1045 5015 caballero
                                        ct.transition(auxPolarSCU[0], auxPolarSCU[1], event);
1046 5165 fjp
                                } else {
1047
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
1048
                                                        0, 0), Math.toRadians(values[1]), values[0]);
1049
                                        auxPolarSCU[0] = point.getX();
1050
                                        auxPolarSCU[1] = point.getY();
1051 5015 caballero
                                        ct.transition(auxPolarSCU[0], auxPolarSCU[1], event);
1052
                                }
1053 5165 fjp
                                previousPoint = auxPolarSCU;
1054 5015 caballero
                                break;
1055 24429 vcaballero
                        case POLAR_SCP:// Absoluto
1056 5165 fjp
                                double[] auxPolarSCP = values;
1057
                                if (previousPoint != null) {
1058
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
1059 24429 vcaballero
                                                        0, 0), Math.toRadians(values[1]), values[0]);
1060 5165 fjp
                                        auxPolarSCP[0] = point.getX();
1061
                                        auxPolarSCP[1] = point.getY();
1062 5015 caballero
                                        ct.transition(auxPolarSCP[0], auxPolarSCP[1], event);
1063 5165 fjp
                                } else {
1064
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
1065
                                                        0, 0), values[1], values[0]);
1066
                                        auxPolarSCP[0] = point.getX();
1067
                                        auxPolarSCP[1] = point.getY();
1068 5015 caballero
                                        ct.transition(auxPolarSCP[0], auxPolarSCP[1], event);
1069
                                }
1070 5165 fjp
                                previousPoint = auxPolarSCP;
1071 5015 caballero
                                break;
1072
                        default:
1073
                                break;
1074 4115 fjp
                        }
1075 5015 caballero
                        askQuestion();
1076 3711 caballero
                }
1077 3883 caballero
                configureMenu();
1078 4024 caballero
                PluginServices.getMainFrame().enableControls();
1079 3711 caballero
        }
1080 4115 fjp
1081 3744 caballero
        /**
1082 24429 vcaballero
         * <p>
1083
         * One of the three kind of transaction methods of the <i>finite machine</i>
1084
         * of edition.
1085
         * </p>
1086 21668 vcaballero
         *
1087 24429 vcaballero
         * <p>
1088
         * This one deals <code>value</code> as a single number used as a
1089
         * parameter for the current tool state. Ultimately, if isn't valid,
1090
         * <code>number</code> will be rewritten in the console notifying the user
1091
         * that isn't correct.
1092
         * </p>
1093 21668 vcaballero
         *
1094 24429 vcaballero
         * <p>
1095
         * After applying the changes, updates the controls available for managing
1096
         * the current data.
1097
         * </p>
1098 21668 vcaballero
         *
1099 24429 vcaballero
         * @param value
1100
         *            value for the current tool state
1101 21668 vcaballero
         *
1102 20098 jmvivo
         * @see CADTool#transition(double)
1103
         * @see #transition(double[], InputEvent, int)
1104
         * @see #transition(String)
1105 3744 caballero
         */
1106 4147 fjp
        private void transition(double value) {
1107 3744 caballero
                questionAsked = true;
1108 4115 fjp
                if (!cadToolStack.isEmpty()) {
1109
                        CADTool ct = (CADTool) cadToolStack.peek();
1110
                        ct.transition(value);
1111 3744 caballero
                        askQuestion();
1112 4115 fjp
                }
1113 3883 caballero
                configureMenu();
1114 3744 caballero
                PluginServices.getMainFrame().enableControls();
1115
        }
1116 4115 fjp
1117 20098 jmvivo
        /**
1118 24429 vcaballero
         * <p>
1119
         * One of the three kind of transaction methods of the <i>finite machine</i>
1120
         * of edition.
1121
         * </p>
1122 21668 vcaballero
         *
1123 24429 vcaballero
         * <p>
1124
         * This one deals <code>option</code> as an option of the current tool
1125
         * state. Ultimately, if isn't valid, <code>option</code> will be
1126
         * rewritten in the console notifying the user that isn't correct.
1127
         * </p>
1128 21668 vcaballero
         *
1129 24429 vcaballero
         * @param option
1130
         *            option for the current tool state
1131 21668 vcaballero
         *
1132 20098 jmvivo
         * @see CADTool#transition(String)
1133
         * @see #transition(double[], InputEvent, int)
1134
         * @see #transition(double)
1135
         */
1136 4147 fjp
        public void transition(String option) {
1137 4115 fjp
                questionAsked = true;
1138
                if (!cadToolStack.isEmpty()) {
1139
                        CADTool ct = (CADTool) cadToolStack.peek();
1140 5165 fjp
                        try {
1141
                                ct.transition(option);
1142
                        } catch (Exception e) {
1143 24429 vcaballero
                                IWindow window = PluginServices.getMDIManager()
1144
                                                .getActiveWindow();
1145 12739 caballero
1146
                                if (window instanceof View) {
1147 24429 vcaballero
                                        ((View) window).getConsolePanel().addText(
1148
                                                        "\n"
1149
                                                                        + PluginServices.getText(this,
1150
                                                                                        "incorrect_option") + " : "
1151
                                                                        + option, JConsole.ERROR);
1152 12739 caballero
                                }
1153 4892 caballero
                        }
1154 4115 fjp
                        askQuestion();
1155
                }
1156
                configureMenu();
1157
                PluginServices.getMainFrame().enableControls();
1158 3744 caballero
        }
1159 4115 fjp
1160 3711 caballero
        /**
1161 24429 vcaballero
         * Shows or hides a grid on the <code>ViewPort</code> of the associated
1162
         * <code>MapControl</code>.
1163 5383 caballero
         *
1164 24429 vcaballero
         * @param value
1165
         *            <code>true</code> to make the grid visible;
1166
         *            <code>false</code> to make it invisible
1167 3711 caballero
         */
1168 24429 vcaballero
        // public void setGridVisibility(boolean value) {
1169
        // getGrid().setShowGrid(value);
1170
        // getGrid().setViewPort(getMapControl().getViewPort());
1171
        // getMapControl().repaint();
1172
        // }
1173
        // /**
1174
        // * Sets the snap tools enabled or disabled.
1175
        // *
1176
        // * @param activated <code>true</code> to enable the snap tools;
1177
        // <code>false</code> to disable them
1178
        // *
1179
        // * @see #isRefentEnabled()
1180
        // */
1181
        // public void setRefentEnabled(boolean activated) {
1182
        // bRefent = activated;
1183
        // }
1184
        //
1185
        // /**
1186
        // * Determines if snap tools are enabled or disabled.
1187
        // *
1188
        // * @return <code>true</code> to enable the snap tools; <code>false</code>
1189
        // to disable them
1190
        // *
1191
        // * @see #setRefentEnabled(boolean)
1192
        // */
1193
        // public boolean isRefentEnabled()
1194
        // {
1195
        // return bRefent;
1196
        // }
1197 20098 jmvivo
        /*
1198
         * (non-Javadoc)
1199 24429 vcaballero
         *
1200 3711 caballero
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
1201
         */
1202
        public ToolListener getListener() {
1203
                return new ToolListener() {
1204 4115 fjp
                        /**
1205
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
1206
                         */
1207 23646 vcaballero
                        public Image getImageCursor() {
1208
                                return imageCursor;
1209 4115 fjp
                        }
1210 3711 caballero
1211 4115 fjp
                        /**
1212
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
1213
                         */
1214
                        public boolean cancelDrawing() {
1215
                                return false;
1216
                        }
1217
                };
1218 3711 caballero
        }
1219
1220
        /**
1221 24429 vcaballero
         * Returns the {@link CADTool CADTool} at the top of the stack without
1222
         * removing it from the CAD tool stack.
1223 21668 vcaballero
         *
1224 20098 jmvivo
         * @return the {@link CADTool CADTool} at the top of the stack
1225 21668 vcaballero
         *
1226 20098 jmvivo
         * @see #pushCadTool(CADTool)
1227
         * @see #popCadTool()
1228
         * @see #setCadTool(CADTool)
1229 3711 caballero
         */
1230
        public CADTool getCadTool() {
1231
                return (CADTool) cadToolStack.peek();
1232
        }
1233
1234
        /**
1235 24429 vcaballero
         * <p>
1236
         * Pushes a {@link CADTool CADTool} onto the top of the CAD tool stack, and
1237
         * sets it as current.
1238
         * </p>
1239 21668 vcaballero
         *
1240 24429 vcaballero
         * @param cadTool
1241
         *            CAD tool to enable as current
1242 21668 vcaballero
         *
1243 20098 jmvivo
         * @see #getCadTool()
1244
         * @see #popCadTool()
1245
         * @see #setCadTool(CADTool)
1246 3711 caballero
         */
1247
        public void pushCadTool(CADTool cadTool) {
1248
                cadToolStack.push(cadTool);
1249
                cadTool.setCadToolAdapter(this);
1250 4115 fjp
                // cadTool.initializeStatus();
1251
                // cadTool.setVectorialAdapter(vea);
1252
                /*
1253
                 * int ret = cadTool.transition(null, editableFeatureSource, selection,
1254
                 * new double[0]);
1255 5383 caballero
                 *
1256 4115 fjp
                 * if ((ret & Automaton.AUTOMATON_FINISHED) ==
1257
                 * Automaton.AUTOMATON_FINISHED) { popCadTool();
1258 5383 caballero
                 *
1259 4115 fjp
                 * if (cadToolStack.isEmpty()) { pushCadTool(new
1260
                 * com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new
1261
                 * SelectionCadTool());
1262
                 * PluginServices.getMainFrame().setSelectedTool("selection"); }
1263 5383 caballero
                 *
1264 4115 fjp
                 * askQuestion();
1265 5383 caballero
                 *
1266 4115 fjp
                 * getMapControl().drawMap(false); }
1267
                 */
1268 3711 caballero
        }
1269
1270
        /**
1271 20098 jmvivo
         * Removes the peek of the CAD tool stack.
1272 21668 vcaballero
         *
1273 20098 jmvivo
         * @see #pushCadTool(CADTool)
1274
         * @see #getCadTool()
1275
         * @see #setCadTool(CADTool)
1276 3711 caballero
         */
1277
        public void popCadTool() {
1278
                cadToolStack.pop();
1279
        }
1280
1281
        /**
1282 24429 vcaballero
         * <p>
1283
         * Displays at the console associated to the current active view that's
1284
         * being edited, the question of the following operation that user can do
1285
         * with the current <code>CADTool</code>, only if it hasn't just
1286
         * answered.
1287
         * </p>
1288 21668 vcaballero
         *
1289 24429 vcaballero
         * <p>
1290
         * The format of the question will be according the following pattern:<br>
1291
         * "\n#"<i>{cadtool at CAD tool stack peek}</i>.getQuestion()">"
1292 20098 jmvivo
         * </p>
1293 3711 caballero
         */
1294
        public void askQuestion() {
1295 4115 fjp
                CADTool cadtool = (CADTool) cadToolStack.peek();
1296
                /*
1297
                 * if (cadtool..getStatus()==0){
1298
                 * PluginServices.getMainFrame().addTextToConsole("\n"
1299
                 * +cadtool.getName()); }
1300
                 */
1301 24429 vcaballero
                if (PluginServices.getMDIManager().getActiveWindow() instanceof View) {
1302
                        View vista = (View) PluginServices.getMDIManager()
1303
                                        .getActiveWindow();
1304
                        String question = cadtool.getQuestion();
1305
                        if (lastQuestion == null || !(lastQuestion.equals(question))
1306
                                        || questionAsked) {
1307
                                vista.getConsolePanel().addText("\n" + "#" + question + " > ",
1308
                                                JConsole.MESSAGE);
1309
                                // ***PluginServices.getMainFrame().addTextToConsole("\n" +
1310
                                // cadtool.getQuestion());
1311
                                questionAsked = false;
1312 9121 caballero
                        }
1313 24429 vcaballero
                        lastQuestion = question;
1314 5923 fjp
                }
1315 3711 caballero
1316
        }
1317
1318
        /**
1319 20098 jmvivo
         * Empties the CAD tools stack and pushes <code>cadTool</code> in it.
1320 21668 vcaballero
         *
1321 24429 vcaballero
         * @param cadTool
1322
         *            CAD tool to set at the peek of the stack
1323 21668 vcaballero
         *
1324 20098 jmvivo
         * @see #pushCadTool(CADTool)
1325
         * @see #popCadTool()
1326
         * @see #getCadTool()
1327 3711 caballero
         */
1328
        public void setCadTool(CADTool cadTool) {
1329
                cadToolStack.clear();
1330
                pushCadTool(cadTool);
1331 5165 fjp
                // askQuestion();
1332 3711 caballero
        }
1333
1334
        /**
1335 24429 vcaballero
         * <p>
1336
         * Removes all geometries selected in the associated <code>MapControl</code>.
1337 3711 caballero
         */
1338 18453 vcaballero
        public void delete() {
1339 24429 vcaballero
                ILayerEdited aux = CADExtension.getEditionManager()
1340
                                .getActiveLayerEdited();
1341 5383 caballero
                if (!(aux instanceof VectorialLayerEdited))
1342 5223 fjp
                        return;
1343 5383 caballero
                VectorialLayerEdited vle = (VectorialLayerEdited) aux;
1344 24429 vcaballero
                FeatureStore featureStore = null;
1345 22986 vcaballero
                try {
1346 24429 vcaballero
                        featureStore = ((FLyrVect) vle.getLayer()).getFeatureStore();
1347 22986 vcaballero
                } catch (ReadException e1) {
1348
                        // TODO Auto-generated catch block
1349
                        e1.printStackTrace();
1350
                }
1351 24429 vcaballero
                String description = PluginServices.getText(this, "remove_geometry");
1352
                try {
1353
                        featureStore.beginEditingGroup(description);
1354 5223 fjp
1355 24429 vcaballero
                        FeatureSet selection = (FeatureSet) featureStore.getSelection();
1356
                        Iterator iterator = selection.iterator();
1357 22986 vcaballero
                        while (iterator.hasNext()) {
1358
                                Feature feature = (Feature) iterator.next();
1359
                                featureStore.delete(feature);
1360 4584 caballero
                        }
1361 24429 vcaballero
                        // int[] indexesToDel = new int[selection.cardinality()];
1362
                        // int j = 0;
1363
                        // for (int i = selection.nextSetBit(0); i >= 0; i = selection
1364
                        // .nextSetBit(i + 1)) {
1365
                        // indexesToDel[j++] = i;
1366
                        // // /vea.removeRow(i);
1367
                        // }
1368 5985 caballero
1369 24429 vcaballero
                        // ArrayList selectedRow = vle.getSelectedRow();
1370
                        //
1371
                        // int[] indexesToDel = new int[selectedRow.size()];
1372
                        // for (int i = 0;i < selectedRow.size(); i++) {
1373
                        // IRowEdited edRow = (IRowEdited) selectedRow.get(i);
1374
                        // indexesToDel[i] = vea.getInversedIndex(edRow.getIndex());
1375
                        // }
1376
                        //
1377
                        // for (int i = indexesToDel.length - 1; i >= 0; i--) {
1378
                        // vea.removeRow(indexesToDel[i], PluginServices.getText(this,
1379
                        // "deleted_feature"),EditionEvent.GRAPHIC);
1380
                        // }
1381 10626 caballero
                        System.out.println("clear Selection");
1382 24263 vcaballero
                        selection.dispose();
1383 10626 caballero
                        vle.clearSelection(VectorialLayerEdited.NOTSAVEPREVIOUS);
1384 21668 vcaballero
                } catch (ReadException e) {
1385 24429 vcaballero
                        NotificationManager.addError(e.getMessage(), e);
1386 22986 vcaballero
                } catch (DataException e) {
1387 24429 vcaballero
                        NotificationManager.addError(e.getMessage(), e);
1388 3711 caballero
                } finally {
1389 24429 vcaballero
                        try {
1390
                                featureStore.endEditingGroup();
1391
                        } catch (NeedEditingModeException e) {
1392
                                // TODO Auto-generated catch block
1393
                                e.printStackTrace();
1394
                        }
1395 3711 caballero
                }
1396 10626 caballero
1397 5165 fjp
                /*
1398
                 * if (getCadTool() instanceof SelectionCADTool) { SelectionCADTool
1399
                 * selTool = (SelectionCADTool) getCadTool(); selTool.clearSelection(); }
1400
                 */
1401 5923 fjp
                refreshEditedLayer();
1402 3711 caballero
        }
1403
1404
        /**
1405 20098 jmvivo
         * @see CADGrid#setAdjustGrid(boolean)
1406 3711 caballero
         */
1407 24429 vcaballero
        // public void setAdjustGrid(boolean b) {
1408
        // getGrid().setAdjustGrid(b);
1409
        // }
1410 3711 caballero
        /**
1411 24429 vcaballero
         * <p>
1412
         * Responds to actions of writing common key commands for all kind of CAD
1413
         * operations, enabling/disabling after the controls to manage the available
1414
         * information according the tool selected:
1415
         * <ul>
1416
         * <li><i>eliminar</i>: removes the geometries that are now selected.</li>
1417
         * <li><i>escape</i>: executes different actions according to the current
1418
         * CAD tool of the associated <code>MapControl</code>:
1419
         * <ul>
1420
         * <li>If the tool enabled is identified by <i>cadtooladapter</i>: empties
1421
         * the CAD tools stack, changing the current tool by a
1422
         * {@link SelectionCADTool SelectionCADTool}, which is identified by
1423
         * <i>_selection</i> and allows select features of the active vector layer
1424
         * of the associated <code>MapControl</code> instance. </li>
1425
         * <li>Otherwise, that means current associated <code>MapControl</code>
1426
         * instance isn't identified by "<i>cadtooladapter</i>", changes the
1427
         * enabled tool by the previous.</li>
1428
         * </ul>
1429
         * </li>
1430
         * </ul>
1431 20098 jmvivo
         * </p>
1432 5383 caballero
         *
1433 24429 vcaballero
         * @param actionCommand
1434
         *            identifier of the key action command executed by the user
1435 21668 vcaballero
         *
1436 20098 jmvivo
         * @see SelectionCADTool
1437
         * @see MapControl#setPrevTool()
1438 3711 caballero
         */
1439
        public void keyPressed(String actionCommand) {
1440 24429 vcaballero
                if (CADExtension.getEditionManager().getActiveLayerEdited() == null) {
1441 7834 jmvivo
                        return;
1442
                }
1443 3725 caballero
                if (actionCommand.equals("eliminar")) {
1444 3711 caballero
                        delete();
1445
                } else if (actionCommand.equals("escape")) {
1446 6385 fjp
                        if (getMapControl().getCurrentTool().equals("cadtooladapter")) {
1447 3847 caballero
                                CADTool ct = (CADTool) cadToolStack.peek();
1448
                                ct.end();
1449 3711 caballero
                                cadToolStack.clear();
1450 4210 fjp
                                SelectionCADTool selCad = new SelectionCADTool();
1451
                                selCad.init();
1452 5165 fjp
                                VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
1453
                                                .getEditionManager().getActiveLayerEdited();
1454 10626 caballero
                                try {
1455
                                        vle.clearSelection(VectorialLayerEdited.NOTSAVEPREVIOUS);
1456 24429 vcaballero
                                } catch (DataException e) {
1457
                                        NotificationManager.addError(e.getMessage(), e);
1458 10626 caballero
                                }
1459 4366 caballero
1460 4210 fjp
                                pushCadTool(selCad);
1461
                                // getVectorialAdapter().getSelection().clear();
1462 5985 caballero
1463 5923 fjp
                                refreshEditedLayer();
1464 5985 caballero
1465 5015 caballero
                                PluginServices.getMainFrame().setSelectedTool("_selection");
1466 5165 fjp
                                // askQuestion();
1467
                        } else {
1468 4892 caballero
                                getMapControl().setPrevTool();
1469 3711 caballero
                        }
1470
                }
1471 3725 caballero
1472 3711 caballero
                PluginServices.getMainFrame().enableControls();
1473
1474
        }
1475 5985 caballero
1476 5923 fjp
        /**
1477 24429 vcaballero
         * <p>
1478
         * Applies a lightweight repaint of the active layer being edited.
1479
         * </p>
1480 21668 vcaballero
         *
1481 24429 vcaballero
         * <p>
1482
         * All layers under it won't be drawn, only the upper one and whose are over
1483
         * that layer in the TOC.
1484
         * </p>
1485 21668 vcaballero
         *
1486 20098 jmvivo
         * @see MapControl#rePaintDirtyLayers()
1487 5923 fjp
         */
1488 24429 vcaballero
        public void refreshEditedLayer() {
1489
                ILayerEdited edLayer = CADExtension.getEditionManager()
1490
                                .getActiveLayerEdited();
1491
                if (edLayer != null) {
1492 5923 fjp
                        edLayer.getLayer().setDirty(true);
1493
                        getMapControl().rePaintDirtyLayers();
1494
                }
1495 5985 caballero
1496 5923 fjp
        }
1497 4115 fjp
1498 20098 jmvivo
        /**
1499 24429 vcaballero
         * Gets the {@link CADGrid CADGrid} that can be drawn on the
1500
         * <code>ViewPort</code> of the associated <code>MapControl</code>.
1501 21668 vcaballero
         *
1502 24429 vcaballero
         * @return reference to the <i>grid</i> that can be applied on the
1503
         *         <code>ViewPort</code>
1504 21668 vcaballero
         *
1505 20098 jmvivo
         * @see #setGridVisibility(boolean)
1506
         */
1507 24429 vcaballero
        // public CADGrid getGrid() {
1508
        // return cadgrid;
1509
        // }
1510 20098 jmvivo
        /**
1511
         * Determines if is enabled or not the <i>orto</i> mode.
1512 21668 vcaballero
         *
1513 24429 vcaballero
         * @return <code>true</code> if is enabled the <i>orto</i> mode;
1514
         *         otherwise <code>false</code>
1515 21668 vcaballero
         *
1516 20098 jmvivo
         * @see #setOrtoMode(boolean)
1517
         */
1518 5874 fjp
        public boolean isOrtoMode() {
1519
                return bOrtoMode;
1520
        }
1521 4115 fjp
1522 20098 jmvivo
        /**
1523
         * Enables / disables the <i>orto</i> mode.
1524 21668 vcaballero
         *
1525 24429 vcaballero
         * @param b
1526
         *            the desired value
1527 21668 vcaballero
         *
1528 20098 jmvivo
         * @see #isOrtoMode()
1529
         */
1530 5874 fjp
        public void setOrtoMode(boolean b) {
1531
                bOrtoMode = b;
1532
        }
1533
1534 20098 jmvivo
        /**
1535 24429 vcaballero
         * Associates and stores the specified name with the specified
1536
         * {@link CADTool CADTool}.
1537 21668 vcaballero
         *
1538 24429 vcaballero
         * @param name
1539
         *            name of the tool
1540
         * @param c
1541
         *            CAD tool to interactuate editing the layers
1542 21668 vcaballero
         *
1543 20098 jmvivo
         * @see #getCADTools()
1544
         * @see #getCADTool(String)
1545
         */
1546 6174 caballero
        public static void addCADTool(String name, CADTool c) {
1547
                namesCadTools.put(name, c);
1548 5874 fjp
1549 6174 caballero
        }
1550 20098 jmvivo
1551
        /**
1552
         * Gets all CAD tools available to edit layers with this tool listener.
1553 21668 vcaballero
         *
1554 20098 jmvivo
         * @return CAD tools available to edit layers with this tool listener
1555
         *
1556
         * @see #addCADTool(String, CADTool)
1557
         * @see #getCADTool(String)
1558
         */
1559 6174 caballero
        public static CADTool[] getCADTools() {
1560 24429 vcaballero
                return (CADTool[]) CADToolAdapter.namesCadTools.values().toArray(
1561
                                new CADTool[0]);
1562 6174 caballero
        }
1563 20098 jmvivo
1564
        /**
1565 24429 vcaballero
         * Returns the {@link CADTool CADTool} to which the specified name is
1566
         * mapped.
1567 21668 vcaballero
         *
1568 24429 vcaballero
         * @param text
1569
         *            name of the tool
1570 20098 jmvivo
         * @return the CAD tool whose associated name is to be returned
1571 21668 vcaballero
         *
1572 20098 jmvivo
         * @see #addCADTool(String, CADTool)
1573
         * @see #getCADTools()
1574
         */
1575 6174 caballero
        public CADTool getCADTool(String text) {
1576
                CADTool ct = (CADTool) namesCadTools.get(text);
1577
                return ct;
1578
        }
1579
1580 20098 jmvivo
        /**
1581 24429 vcaballero
         * Gets the object used to manage the edition of the layers of the
1582
         * associated <code>MapControl</code>.
1583 21668 vcaballero
         *
1584 20098 jmvivo
         * @see EditionManager
1585 21668 vcaballero
         *
1586 20098 jmvivo
         * @return object used to manage the edition of the layers
1587
         */
1588 6174 caballero
        public EditionManager getEditionManager() {
1589
                return editionManager;
1590
        }
1591
1592 20098 jmvivo
        /**
1593 24429 vcaballero
         * <p>
1594
         * Initializes the <i>flatness</i> with the defined in preferences.
1595
         * </p>
1596 20098 jmvivo
         *
1597 24429 vcaballero
         * <p>
1598
         * The <i>flatness</i> is the maximum tolerance used to approximate curved
1599
         * lines in a <i>shape</i> by polylines.
1600
         * </p>
1601
         * <p>
1602
         * The shapes doesn't support primitive like arcs neither other curved lines
1603
         * to draw their geometries, then for drawing any kind of this geometries
1604
         * the curved lines are drawn approximately by a polyline. And for doing
1605
         * more realistic that curves, is used the <i>flatness</i> parameter, that
1606
         * indicates that the difference between each arc and the straight segment
1607
         * that approximates it must be in the worse case, like the <i>flatness</i>.
1608
         * </p>
1609 21668 vcaballero
         *
1610 20098 jmvivo
         * @see FConverter#FLATNESS
1611
         */
1612 6713 caballero
        public void initializeFlatness() {
1613 24429 vcaballero
                if (!flatnessInitialized) {
1614
                        flatnessInitialized = true;
1615
                        Preferences prefs = Preferences.userRoot().node("cadtooladapter");
1616
                        double flatness = prefs.getDouble("flatness", Converter.FLATNESS);
1617
                        Converter.FLATNESS = flatness;
1618 7072 caballero
                }
1619 6713 caballero
        }
1620 20098 jmvivo
1621
        /**
1622 24429 vcaballero
         * <p>
1623
         * Updates the grid on the <code>ViewPort</code> of the associated
1624
         * <code>MapControl</code> object according the values in the
1625
         * {@link com.iver.cit.gvsig.gui.cad.CADToolAdapter.prefs.Preferences com.iver.cit.gvsig.gui.cad.CADToolAdapter.prefs.Preferences}.
1626
         * </p>
1627 21668 vcaballero
         *
1628 24429 vcaballero
         * <p>
1629
         * The preferences are:
1630
         * <ul>
1631
         * <li>Show/hide the grid.</li>
1632
         * <li>Adjust or not the grid.</li>
1633
         * <li>Horizontal ( X ) line separation.</li>
1634
         * <li>Vertical ( Y ) line separation.</li>
1635
         * </ul>
1636 20098 jmvivo
         * </p>
1637
         */
1638 24429 vcaballero
        // public void initializeGrid(){
1639
        // boolean showGrid =
1640
        // prefs.getBoolean("grid.showgrid",getGrid().isShowGrid());
1641
        // boolean adjustGrid =
1642
        // prefs.getBoolean("grid.adjustgrid",getGrid().isAdjustGrid());
1643
        //
1644
        // double dx = prefs.getDouble("grid.distancex",getGrid().getGridSizeX());
1645
        // double dy = prefs.getDouble("grid.distancey",getGrid().getGridSizeY());
1646
        //
1647
        // setGridVisibility(showGrid);
1648
        // setAdjustGrid(adjustGrid);
1649
        // getGrid().setGridSizeX(dx);
1650
        // getGrid().setGridSizeY(dy);
1651
        // }
1652 12296 caballero
        /**
1653 24429 vcaballero
         * <p>
1654
         * Returns the type of the shape that's the current active and vector layer
1655
         * being edited.
1656
         * </p>
1657 21668 vcaballero
         *
1658 20098 jmvivo
         * @see FLyrVect#getShapeType()
1659 21668 vcaballero
         *
1660 24429 vcaballero
         * @return type of the shape that's the current active and vector layer
1661
         *         being edited
1662 20098 jmvivo
         */
1663 12296 caballero
        public int getActiveLayerType() {
1664 24429 vcaballero
                int type = Geometry.TYPES.GEOMETRY;
1665 12296 caballero
                try {
1666 24429 vcaballero
                        type = ((FLyrVect) CADExtension.getEditionManager()
1667
                                        .getActiveLayerEdited().getLayer()).getShapeType();
1668 21668 vcaballero
                } catch (ReadException e) {
1669 12296 caballero
                        NotificationManager.addError(e);
1670
                }
1671
                return type;
1672
        }
1673 23646 vcaballero
1674 3711 caballero
}