Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / CADExtension.java @ 4901

History | View | Annotate | Download (13.3 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
import javax.swing.JTextArea;
55
import javax.swing.text.JTextComponent;
56

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

    
106
    /**
107
     * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
108
     */
109
    public void execute(String s) {
110
       initFocus();
111

    
112
        if (s.equals("SPLINE")) {
113
                setCADTool("spline",true);
114
        } else if (s.equals("COPY")) {
115
                setCADTool("copy",true);
116
        } else if (s.equals("MOVE")) {
117
                setCADTool("move",true);
118
        } else if (s.equals("EQUIDISTANCE")) {
119
                setCADTool("equidistance",true);
120
        } else if (s.equals("MATRIZ")) {
121
                setCADTool("matriz",true);
122
        } else if (s.equals("SYMMETRY")) {
123
                setCADTool("symmetry",true);
124
        } else if (s.equals("ROTATION")) {
125
                setCADTool("rotate",true);
126
        } else if (s.equals("STRETCHING")) {
127
                setCADTool("stretching",true);
128
        } else if (s.equals("SCALE")) {
129
                setCADTool("scale",true);
130
        } else if (s.equals("EXTEND")) {
131
                setCADTool("extend",true);
132
        } else if (s.equals("TRIM")) {
133
                setCADTool("trim",true);
134
        } else if (s.equals("UNIT")) {
135
                setCADTool("unit",true);
136
        } else if (s.equals("EXPLOIT")) {
137
                setCADTool("exploit",true);
138
        } else if (s.equals("CHAFLAN")) {
139
                setCADTool("chaflan",true);
140
        } else if (s.equals("JOIN")) {
141
                setCADTool("join",true);
142
        } else if (s.equals("SELCAD")) {
143
                setCADTool("selection",true);
144
        } else if (s.equals("EDITVERTEX")) {
145
                setCADTool("editvertex",true);
146
        }
147
        adapter.configureMenu();
148
        //ViewControls.CANCELED=false;
149
    }
150
    public static void addCADTool(String name, CADTool c){
151
                namesCadTools.put(name, c);
152
        }
153
    public static void setCADTool(String text,boolean showCommand){
154
                CADTool ct = (CADTool) namesCadTools.get(text);
155
                if (ct == null) throw new RuntimeException("No such cad tool");
156
                adapter.setCadTool(ct);
157
                ct.init();
158
                if (showCommand){
159
                        View vista = (View) PluginServices.getMDIManager().getActiveView();
160
                        vista.getConsolePanel().addText("\n" + ct.getName(),JConsole.COMMAND);
161
                        adapter.askQuestion();
162
                }
163
                //PluginServices.getMainFrame().setSelectedTool("SELECT");
164
                //PluginServices.getMainFrame().enableControls();
165
        }
166
    public static CADTool getCADTool(){
167
            return adapter.getCadTool();
168
    }
169
    /**
170
     * @see com.iver.andami.plugins.Extension#isEnabled()
171
     */
172
    public boolean isEnabled() {
173
        /* com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
174
                                                             .getActiveView();
175

176
        if (f == null) {
177
            return false;
178
        }
179

180
        if (f.getClass() == View.class) {
181
            FLayer[] l = ((View) f).getModel().getMapContext().getLayers()
182
                          .getActives();
183

184
            for (int i = 0; i < l.length; i++) {
185
                if (l[i] instanceof FLyrVect && ((FLyrVect) l[i]).isEditing()) {
186
                    return true;
187
                }
188
            }
189
        }
190

191
        return false; */
192
                /*if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
193
                {
194
                        // Queremos que siempre que haya edici?n esto est? funcionando.
195
                editionManager.setMapControl(mapControl);
196
                        return true;
197
                }
198
                else
199
                        return false;
200

201
    */
202
            return true;
203
    }
204

    
205
    /**
206
     * @see com.iver.andami.plugins.Extension#isVisible()
207
     */
208
    public boolean isVisible() {
209
            if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
210
                {
211
                        // Queremos que siempre que haya edici?n esto est? funcionando.
212
                ///editionManager.setMapControl(mapControl);
213
                        return true;
214
                }
215
                else
216
                        return false;
217
//        com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
218
//                                                             .getActiveView();
219
//
220
//        if (f == null) {
221
//            return false;
222
//        }
223
//
224
//        if (f.getClass() == View.class) {
225
//                FLayer[] layers=getMapControl().getMapContext().getLayers().getActives();
226
//                if (layers[0] instanceof FLyrVect && ((FLyrVect)layers[0]).isEditing()){
227
//                        return true;
228
//                }
229
//        }
230
//        return false;
231
    }
232
        public MapControl getMapControl() {
233
                return editionManager.getMapControl();
234
        }
235
        class KeyAction extends AbstractAction{
236

    
237
                private String key;
238

    
239
                public KeyAction(String key){
240
                        this.key = key;
241
                }
242

    
243
                /* (non-Javadoc)
244
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
245
                 */
246
                public void actionPerformed(ActionEvent e) {
247
                        view.focusConsole(key);
248
                }
249

    
250
        }
251

    
252
        class MyAction extends AbstractAction{
253
                private String actionCommand;
254

    
255
                public MyAction(String command){
256
                        actionCommand = command;
257
                }
258

    
259
                /**
260
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
261
                 */
262
                public void actionPerformed(ActionEvent e) {
263
                        adapter.keyPressed(actionCommand);
264
                }
265

    
266
        }
267

    
268
        /**
269
         * @author fjp
270
         *
271
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo
272
         * a la consola de la vista para que salga por all?.
273
         */
274
        private class myKeyEventPostProcessor implements KeyEventPostProcessor
275
        {
276

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

    
319
        }
320

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

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

348

349
        }
350
*/
351
        private static JPopupMenu popup = new JPopupMenu();
352
        public static  void clearMenu(){
353
                popup.removeAll();
354
        }
355

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

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

    
377
        /**
378
         * @return Returns the editionManager.
379
         */
380
        public static EditionManager getEditionManager() {
381
                return editionManager;
382
        }
383
        public static CADTool[] getCADTools(){
384
                return (CADTool[])namesCadTools.values().toArray(new CADTool[0]);
385
        }
386
        public static void initFocus(){
387
                 view = (View) PluginServices.getMDIManager().getActiveView();
388
             MapControl mapControl = (MapControl) view.getMapControl();
389
                if (!mapControl.getNamesMapTools().containsKey("cadtooladapter"))
390
                        mapControl.addMapTool("cadtooladapter",adapter);
391
                        view.getMapControl().setTool("cadtooladapter");
392
                        view.addConsoleListener("cad", new ResponseListener() {
393
                                     public void acceptResponse(String response) {
394
                                             adapter.textEntered(response);
395
                                             // TODO:
396
                                             // FocusManager fm=FocusManager.getCurrentManager();
397
                                             // fm.focusPreviousComponent(mapControl);
398
                                             /*if (popup.isShowing()){
399
                                                 popup.setVisible(false);
400
                                             }*/
401

    
402
                                     }
403
                             });
404
               editionManager.setMapControl(mapControl);
405
               view.getMapControl().setTool("cadtooladapter");
406
        }
407
}