Statistics
| Revision:

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

History | View | Annotate | Download (12.4 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.KeyEventPostProcessor;
44
import java.awt.KeyboardFocusManager;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.awt.event.KeyEvent;
48
import java.awt.event.MouseEvent;
49
import java.util.HashMap;
50

    
51
import javax.swing.AbstractAction;
52
import javax.swing.JMenuItem;
53
import javax.swing.JPopupMenu;
54

    
55
import com.iver.andami.PluginServices;
56
import com.iver.andami.plugins.Extension;
57
import com.iver.cit.gvsig.fmap.MapControl;
58
import com.iver.cit.gvsig.gui.View;
59
import com.iver.cit.gvsig.gui.cad.CADTool;
60
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
61
import com.iver.cit.gvsig.gui.cad.tools.CopyCADTool;
62
import com.iver.cit.gvsig.gui.cad.tools.EditVertexCADTool;
63
import com.iver.cit.gvsig.gui.cad.tools.MoveCADTool;
64
import com.iver.cit.gvsig.gui.cad.tools.RotateCADTool;
65
import com.iver.cit.gvsig.gui.cad.tools.ScaleCADTool;
66
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
67
import com.iver.utiles.console.ResponseListener;
68
/**
69
 * Extensi?n dedicada a controlar las diferentes operaciones sobre el editado
70
 * de una capa.
71
 *
72
 * @author Vicente Caballero Navarro
73
 */
74
public class CADExtension implements Extension {
75
   private static CADToolAdapter adapter=new CADToolAdapter();
76
   private static EditionManager editionManager = new EditionManager();
77
   private static HashMap namesCadTools = new HashMap();
78
   private MapControl mapControl;
79
   private static View view;
80
   public static CADToolAdapter getCADToolAdapter(){
81
            return adapter;
82
   }
83
   /**
84
     * @see com.iver.andami.plugins.Extension#inicializar()
85
     */
86
    public void inicializar() {
87
        SelectionCADTool selection=new SelectionCADTool();
88
        CopyCADTool copy=new CopyCADTool();
89
        MoveCADTool move=new MoveCADTool();
90
        RotateCADTool rotate=new RotateCADTool();
91
        ScaleCADTool scale=new ScaleCADTool();
92
        EditVertexCADTool editvertex=new EditVertexCADTool();
93
        addCADTool("selection", selection);
94
        addCADTool("copy",copy);
95
        addCADTool("move",move);
96
        addCADTool("rotate",rotate);
97
        addCADTool("scale",scale);
98
        addCADTool("editvertex",editvertex);
99

    
100
        KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
101
        kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
102
    }
103

    
104
    /**
105
     * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
106
     */
107
    public void execute(String s) {
108
        view = (View) PluginServices.getMDIManager().getActiveView();
109
        mapControl = (MapControl) view.getMapControl();
110
        if (!mapControl.getNamesMapTools().containsKey("cadtooladapter"))
111
                mapControl.addMapTool("cadtooladapter",adapter);
112
                view.getMapControl().setTool("cadtooladapter");
113
                view.addConsoleListener("cad", new ResponseListener() {
114
                             public void acceptResponse(String response) {
115
                                     adapter.textEntered(response);
116
                                     // TODO:
117
                                     // FocusManager fm=FocusManager.getCurrentManager();
118
                                     // fm.focusPreviousComponent(mapControl);
119
                                     /*if (popup.isShowing()){
120
                                         popup.setVisible(false);
121
                                     }*/
122

    
123
                             }
124
                     });
125

    
126
        view.getMapControl().setTool("cadtooladapter");
127

    
128
        if (s.equals("SPLINE")) {
129
                setCADTool("spline");
130
        } else if (s.equals("COPY")) {
131
                setCADTool("copy");
132
        } else if (s.equals("MOVE")) {
133
                setCADTool("move");
134
        } else if (s.equals("EQUIDISTANCE")) {
135
                setCADTool("equidistance");
136
        } else if (s.equals("MATRIZ")) {
137
                setCADTool("matriz");
138
        } else if (s.equals("SYMMETRY")) {
139
                setCADTool("symmetry");
140
        } else if (s.equals("ROTATION")) {
141
                setCADTool("rotate");
142
        } else if (s.equals("STRETCHING")) {
143
                setCADTool("stretching");
144
        } else if (s.equals("SCALE")) {
145
                setCADTool("scale");
146
        } else if (s.equals("EXTEND")) {
147
                setCADTool("extend");
148
        } else if (s.equals("TRIM")) {
149
                setCADTool("trim");
150
        } else if (s.equals("UNIT")) {
151
                setCADTool("unit");
152
        } else if (s.equals("EXPLOIT")) {
153
                setCADTool("exploit");
154
        } else if (s.equals("CHAFLAN")) {
155
                setCADTool("chaflan");
156
        } else if (s.equals("JOIN")) {
157
                setCADTool("join");
158
        } else if (s.equals("SELCAD")) {
159
                setCADTool("selection");
160
        } else if (s.equals("EDITVERTEX")) {
161
                setCADTool("editvertex");
162
        }
163
        adapter.configureMenu();
164
        //ViewControls.CANCELED=false;
165
    }
166
    public static void addCADTool(String name, CADTool c){
167
                namesCadTools.put(name, c);
168
        }
169
    public static void setCADTool(String text){
170
                CADTool ct = (CADTool) namesCadTools.get(text);
171
                if (ct == null) throw new RuntimeException("No such cad tool");
172
                adapter.setCadTool(ct);
173
                ct.init();
174
                adapter.askQuestion();
175
                //PluginServices.getMainFrame().setSelectedTool("SELECT");
176
                //PluginServices.getMainFrame().enableControls();
177
        }
178
    public static CADTool getCADTool(){
179
            return adapter.getCadTool();
180
    }
181
    /**
182
     * @see com.iver.andami.plugins.Extension#isEnabled()
183
     */
184
    public boolean isEnabled() {
185
        /* com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
186
                                                             .getActiveView();
187

188
        if (f == null) {
189
            return false;
190
        }
191

192
        if (f.getClass() == View.class) {
193
            FLayer[] l = ((View) f).getModel().getMapContext().getLayers()
194
                          .getActives();
195

196
            for (int i = 0; i < l.length; i++) {
197
                if (l[i] instanceof FLyrVect && ((FLyrVect) l[i]).isEditing()) {
198
                    return true;
199
                }
200
            }
201
        }
202

203
        return false; */
204
                /*if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
205
                {
206
                        // Queremos que siempre que haya edici?n esto est? funcionando.
207
                editionManager.setMapControl(mapControl);
208
                        return true;
209
                }
210
                else
211
                        return false;
212

213
    */
214
            return true;
215
    }
216

    
217
    /**
218
     * @see com.iver.andami.plugins.Extension#isVisible()
219
     */
220
    public boolean isVisible() {
221
            if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
222
                {
223
                        // Queremos que siempre que haya edici?n esto est? funcionando.
224
                editionManager.setMapControl(mapControl);
225
                        return true;
226
                }
227
                else
228
                        return false;
229
//        com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
230
//                                                             .getActiveView();
231
//
232
//        if (f == null) {
233
//            return false;
234
//        }
235
//
236
//        if (f.getClass() == View.class) {
237
//                FLayer[] layers=getMapControl().getMapContext().getLayers().getActives();
238
//                if (layers[0] instanceof FLyrVect && ((FLyrVect)layers[0]).isEditing()){
239
//                        return true;
240
//                }
241
//        }
242
//        return false;
243
    }
244
        public MapControl getMapControl() {
245
                return this.mapControl;
246
        }
247
        class KeyAction extends AbstractAction{
248

    
249
                private String key;
250

    
251
                public KeyAction(String key){
252
                        this.key = key;
253
                }
254

    
255
                /* (non-Javadoc)
256
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
257
                 */
258
                public void actionPerformed(ActionEvent e) {
259
                        view.focusConsole(key);
260
                }
261

    
262
        }
263

    
264
        class MyAction extends AbstractAction{
265
                private String actionCommand;
266

    
267
                public MyAction(String command){
268
                        actionCommand = command;
269
                }
270

    
271
                /**
272
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
273
                 */
274
                public void actionPerformed(ActionEvent e) {
275
                        adapter.keyPressed(actionCommand);
276
                }
277

    
278
        }
279

    
280
        /**
281
         * @author fjp
282
         *
283
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo
284
         * a la consola de la vista para que salga por all?.
285
         */
286
        private class myKeyEventPostProcessor implements KeyEventPostProcessor
287
        {
288

    
289
                public boolean postProcessKeyEvent(KeyEvent e) {
290
                        // System.out.println("KeyEvent e = " + e);
291
                        if ((adapter==null) ||  (view == null)) return false;
292
                        if (e.getID() != KeyEvent.KEY_RELEASED) return false;
293
                if (e.getKeyCode() == KeyEvent.VK_DELETE)
294
                        adapter.keyPressed("eliminar");
295
                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
296
                        adapter.keyPressed("escape");
297
                else if (e.getKeyCode() == KeyEvent.VK_ENTER)
298
                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN JTEXTAREA
299
                        // QUE NO ES EL DE CONSOLA
300
                        view.focusConsole("");
301
                else
302
                {
303
                        if (e.getComponent().getName() != null)
304
                        {
305
                                System.out.println("Evento de teclado desde el componente " + e.getComponent().getName());
306
                                if (!e.getComponent().getName().equals("CADConsole"))
307
                                {
308
                                        if ((e.getID() == KeyEvent.KEY_TYPED) && (!e.isActionKey()))
309
                                        {
310
                                                    if (Character.isLetterOrDigit(e.getKeyChar()))
311
                                                    {
312
                                                            Character keyChar = new Character(e.getKeyChar());
313
                                                            view.focusConsole(keyChar+"");
314
                                                }
315
                                        }
316
                                }
317
                        }
318
                }
319
                        return false;
320
                }
321

    
322
        }
323

    
324
/*        private void registerKeyStrokes(){
325
                for (char key = '0'; key <= '9'; key++){
326
                        Character keyChar = new Character(key);
327
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
328
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
329
                }
330
                for (char key = 'a'; key <= 'z'; key++){
331
                        Character keyChar = new Character(key);
332
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
333
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
334
                }
335
                for (char key = 'A'; key <= 'Z'; key++){
336
                        Character keyChar = new Character(key);
337
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
338
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
339
                }
340
                //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
341
        //this.getActionMap().put("enter", new MyAction("enter"));
342
                Character keyChar = new Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
343
                mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),keyChar);
344
                mapControl.getActionMap().put(keyChar, new KeyAction(""));
345

346
                // El espacio como si fuera INTRO
347
                Character keyCharSpace = new Character(' ');
348
                mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(' '), keyCharSpace);
349
                mapControl.getActionMap().put(keyCharSpace, new KeyAction(""));
350

351

352
        }
353
*/
354
        private static JPopupMenu popup = new JPopupMenu();
355
        public static  void clearMenu(){
356
                popup.removeAll();
357
        }
358

    
359
        public static void addMenuEntry(String text){
360
                JMenuItem menu = new JMenuItem(text);
361
                menu.setActionCommand(text);
362
                menu.setEnabled(true);
363
                menu.setVisible(true);
364
                menu.addActionListener(new ActionListener() {
365
                        public void actionPerformed(ActionEvent e) {
366
                                adapter.transition(e.getActionCommand());
367
                        }
368
                });
369

    
370
                popup.add(menu);
371
        }
372
        public static void showPopup(MouseEvent e) {
373
                    popup.show(e.getComponent(),
374
                       e.getX(), e.getY());
375
    }
376
        public static View getView() {
377
                return view;
378
        }
379

    
380
        /**
381
         * @return Returns the editionManager.
382
         */
383
        public static EditionManager getEditionManager() {
384
                return editionManager;
385
        }
386
}