Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / CADExtension.java @ 3847

History | View | Annotate | Download (10.7 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;
42

    
43
import java.awt.event.ActionEvent;
44
import java.awt.event.KeyEvent;
45
import java.util.HashMap;
46

    
47
import javax.swing.AbstractAction;
48
import javax.swing.FocusManager;
49
import javax.swing.JComponent;
50
import javax.swing.KeyStroke;
51

    
52
import com.iver.andami.PluginServices;
53
import com.iver.andami.plugins.Extension;
54
import com.iver.cit.gvsig.fmap.MapControl;
55
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
56
import com.iver.cit.gvsig.fmap.layers.FBitSet;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
import com.iver.cit.gvsig.fmap.layers.FLayers;
59
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
60
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
61
import com.iver.cit.gvsig.gui.View;
62
import com.iver.cit.gvsig.gui.cad.CADTool;
63
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
64
import com.iver.cit.gvsig.gui.cad.tools.ArcCADTool;
65
import com.iver.cit.gvsig.gui.cad.tools.CircleCADTool;
66
import com.iver.cit.gvsig.gui.cad.tools.EllipseCADTool;
67
import com.iver.cit.gvsig.gui.cad.tools.LineCADTool;
68
import com.iver.cit.gvsig.gui.cad.tools.PointCADTool;
69
import com.iver.cit.gvsig.gui.cad.tools.PolygonCADTool;
70
import com.iver.cit.gvsig.gui.cad.tools.PolylineCADTool;
71
import com.iver.cit.gvsig.gui.cad.tools.RectangleCADTool;
72
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
73
import com.iver.utiles.console.ResponseListener;
74
/**
75
 * Extensi?n dedicada a controlar las diferentes operaciones sobre el editado
76
 * de una capa.
77
 *
78
 * @author Vicente Caballero Navarro
79
 */
80
public class CADExtension implements Extension {
81
   private static CADToolAdapter adapter=new CADToolAdapter();
82
   private static HashMap namesCadTools = new HashMap();
83
   private boolean isLoad =false;
84
   private MapControl mapControl;
85
   private View view;
86
   public static CADToolAdapter getCADToolAdapter(){
87
           return adapter;
88
   }
89
   /**
90
     * @see com.iver.andami.plugins.Extension#inicializar()
91
     */
92
    public void inicializar() {
93

    
94

    
95

    
96

    
97
        SelectionCADTool selection=new SelectionCADTool();
98
            LineCADTool line = new LineCADTool();
99
        PointCADTool point = new PointCADTool();
100
        CircleCADTool circle=new CircleCADTool();
101
        RectangleCADTool rectangle=new RectangleCADTool();
102
        PolylineCADTool polyline=new PolylineCADTool();
103
        EllipseCADTool ellipse=new EllipseCADTool();
104
        ArcCADTool arc=new ArcCADTool();
105
        PolygonCADTool polygon=new PolygonCADTool();
106
        addCADTool("selection", selection);
107
        addCADTool("line", line);
108
        addCADTool("point", point);
109
        addCADTool("circle",circle);
110
        addCADTool("rectangle", rectangle);
111
        addCADTool("polyline", polyline);
112
        addCADTool("ellipse", ellipse);
113
        addCADTool("arc", arc);
114
        addCADTool("polygon", polygon);
115
    }
116

    
117
    /**
118
     * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
119
     */
120
    public void execute(String s) {
121
        view = (View) PluginServices.getMDIManager().getActiveView();
122
        mapControl = (MapControl) view.getMapControl();
123
        if (!isLoad){
124
                mapControl.addMapTool("cadtooladapter", new Behavior[]{adapter});
125
                isLoad=true;
126
        }
127
                view.addConsoleListener("cad", new ResponseListener() {
128
                             public void acceptResponse(String response) {
129
                                     adapter.textEntered(response);
130
                                     FocusManager fm=FocusManager.getCurrentManager();
131
                                     fm.focusPreviousComponent(mapControl);
132
                                     /*if (popup.isShowing()){
133
                                         popup.setVisible(false);
134
                                     }*/
135

    
136
                             }
137
                     });
138
                registerKeyStrokes();
139
                view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "elimina");
140
            view.getActionMap().put("elimina", new MyAction("eliminar"));
141
            view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
142
            view.getActionMap().put("escape", new MyAction("escape"));
143

    
144
        FLayers layers=mapControl.getMapContext().getLayers();
145
                for (int i=0;i<layers.getLayersCount();i++){
146
                        if (layers.getLayer(i).isEditing() && layers.getLayer(i) instanceof FLyrVect){
147
                                adapter.setVectorialAdapter((VectorialEditableAdapter)((FLyrVect)layers.getLayer(i)).getSource());
148
                                adapter.setMapControl(mapControl);
149
                        }
150
                }
151

    
152
        view.getMapControl().setTool("cadtooladapter");
153
        if (s.compareTo("SPLINE") == 0) {
154
                setCADTool("spline");
155
        } else if (s.compareTo("COPY") == 0) {
156
                setCADTool("copy");
157
        } else if (s.compareTo("EQUIDISTANCE") == 0) {
158
                setCADTool("equidistance");
159
        } else if (s.compareTo("MATRIZ") == 0) {
160
                setCADTool("matriz");
161
        } else if (s.compareTo("SYMMETRY") == 0) {
162
                setCADTool("symmetry");
163
        } else if (s.compareTo("ROTATION") == 0) {
164
                setCADTool("rotation");
165
        } else if (s.compareTo("STRETCHING") == 0) {
166
                setCADTool("stretching");
167
        } else if (s.compareTo("SCALE") == 0) {
168
                setCADTool("scale");
169
        } else if (s.compareTo("EXTEND") == 0) {
170
                setCADTool("extend");
171
        } else if (s.compareTo("TRIM") == 0) {
172
                setCADTool("trim");
173
        } else if (s.compareTo("UNIT") == 0) {
174
                setCADTool("unit");
175
        } else if (s.compareTo("EXPLOIT") == 0) {
176
                setCADTool("exploit");
177
        } else if (s.compareTo("CHAFLAN") == 0) {
178
                setCADTool("chaflan");
179
        } else if (s.compareTo("JOIN") == 0) {
180
                setCADTool("join");
181
        } else if (s.compareTo("SELECT") == 0) {
182
                setCADTool("selection");
183
        } else if (s.compareTo("POINT") == 0) {
184
                setCADTool("point");
185
        } else if (s.compareTo("LINE") == 0) {
186
                setCADTool("line");
187
        } else if (s.compareTo("POLYLINE") == 0) {
188
                setCADTool("polyline");
189
        } else if (s.compareTo("CIRCLE") == 0) {
190
           setCADTool("circle");
191
        } else if (s.compareTo("ARC") == 0) {
192
                setCADTool("arc");
193
        } else if (s.compareTo("ELLIPSE") == 0) {
194
                setCADTool("ellipse");
195
        } else if (s.compareTo("RECTANGLE") == 0) {
196
                setCADTool("rectangle");
197
        } else if (s.compareTo("POLYGON") == 0) {
198
                setCADTool("polygon");
199
        }
200

    
201
        //ViewControls.CANCELED=false;
202
    }
203
    public void addCADTool(String name, CADTool c){
204
                namesCadTools.put(name, c);
205
        }
206
    public static void setCADTool(String text){
207
                CADTool ct = (CADTool) namesCadTools.get(text);
208
                if (ct == null) throw new RuntimeException("No such cad tool");
209
                ct.init();
210
                adapter.setCadTool(ct);
211
        }
212
    /**
213
     * @see com.iver.andami.plugins.Extension#isEnabled()
214
     */
215
    public boolean isEnabled() {
216
        com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
217
                                                             .getActiveView();
218

    
219
        if (f == null) {
220
            return false;
221
        }
222

    
223
        if (f.getClass() == View.class) {
224
            FLayer[] l = ((View) f).getModel().getMapContext().getLayers()
225
                          .getActives();
226

    
227
            for (int i = 0; i < l.length; i++) {
228
                if (l[i] instanceof FLyrVect && ((FLyrVect) l[i]).isEditing()) {
229
                    return true;
230
                }
231
            }
232
        }
233

    
234
        return false;
235
    }
236

    
237
    /**
238
     * @see com.iver.andami.plugins.Extension#isVisible()
239
     */
240
    public boolean isVisible() {
241
        com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
242
                                                             .getActiveView();
243

    
244
        if (f == null) {
245
            return false;
246
        }
247

    
248
        if (f.getClass() == View.class) {
249
            return true;
250
        } else {
251
            return false;
252
        }
253
    }
254
        public MapControl getMapControl() {
255
                return this.mapControl;
256
        }
257
        class KeyAction extends AbstractAction{
258

    
259
                private String key;
260

    
261
                public KeyAction(String key){
262
                        this.key = key;
263
                }
264

    
265
                /* (non-Javadoc)
266
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
267
                 */
268
                public void actionPerformed(ActionEvent e) {
269
                        view.focusConsole(key);
270
                }
271

    
272
        }
273

    
274
        class MyAction extends AbstractAction{
275
                private String actionCommand;
276

    
277
                public MyAction(String command){
278
                        actionCommand = command;
279
                }
280

    
281
                /**
282
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
283
                 */
284
                public void actionPerformed(ActionEvent e) {
285
                        adapter.keyPressed(actionCommand);
286
                }
287

    
288
        }
289

    
290
        private void registerKeyStrokes(){
291
                for (char key = '0'; key <= '9'; key++){
292
                        Character keyChar = new Character(key);
293
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
294
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
295
                }
296
                for (char key = 'a'; key <= 'z'; key++){
297
                        Character keyChar = new Character(key);
298
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
299
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
300
                }
301
                for (char key = 'A'; key <= 'Z'; key++){
302
                        Character keyChar = new Character(key);
303
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
304
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
305
                }
306
                //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
307
        //this.getActionMap().put("enter", new MyAction("enter"));
308
                Character keyChar = new Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
309
                mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),keyChar);
310
                mapControl.getActionMap().put(keyChar, new KeyAction(""));
311
        }
312
}