Statistics
| Revision:

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

History | View | Annotate | Download (12.5 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.fmap.layers.FLayer;
59
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
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.ResponseListener;
70
/**
71
 * Extensi?n dedicada a controlar las diferentes operaciones sobre el editado
72
 * de una capa.
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class CADExtension implements Extension {
77
   private static CADToolAdapter adapter=new CADToolAdapter();
78
   private static EditionManager editionManager = new EditionManager();
79
   private static HashMap namesCadTools = new HashMap();
80
   private MapControl mapControl;
81
   private static View view;
82
   public static CADToolAdapter getCADToolAdapter(){
83
            return adapter;
84
   }
85
   /**
86
     * @see com.iver.andami.plugins.Extension#inicializar()
87
     */
88
    public void inicializar() {
89
        SelectionCADTool selection=new SelectionCADTool();
90
        CopyCADTool copy=new CopyCADTool();
91
        MoveCADTool move=new MoveCADTool();
92
        RotateCADTool rotate=new RotateCADTool();
93
        ScaleCADTool scale=new ScaleCADTool();
94
        EditVertexCADTool editvertex=new EditVertexCADTool();
95
        addCADTool("selection", selection);
96
        addCADTool("copy",copy);
97
        addCADTool("move",move);
98
        addCADTool("rotate",rotate);
99
        addCADTool("scale",scale);
100
        addCADTool("editvertex",editvertex);
101

    
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
        view = (View) PluginServices.getMDIManager().getActiveView();
111
        mapControl = (MapControl) view.getMapControl();
112
        if (!mapControl.getNamesMapTools().containsKey("cadtooladapter"))
113
                mapControl.addMapTool("cadtooladapter",adapter);
114
                view.getMapControl().setTool("cadtooladapter");
115
                view.addConsoleListener("cad", new ResponseListener() {
116
                             public void acceptResponse(String response) {
117
                                     adapter.textEntered(response);
118
                                     // TODO:
119
                                     // FocusManager fm=FocusManager.getCurrentManager();
120
                                     // fm.focusPreviousComponent(mapControl);
121
                                     /*if (popup.isShowing()){
122
                                         popup.setVisible(false);
123
                                     }*/
124

    
125
                             }
126
                     });
127

    
128
        view.getMapControl().setTool("cadtooladapter");
129

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

190
        if (f == null) {
191
            return false;
192
        }
193

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

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

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

215
    */
216
            return true;
217
    }
218

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

    
251
                private String key;
252

    
253
                public KeyAction(String key){
254
                        this.key = key;
255
                }
256

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

    
264
        }
265

    
266
        class MyAction extends AbstractAction{
267
                private String actionCommand;
268

    
269
                public MyAction(String command){
270
                        actionCommand = command;
271
                }
272

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

    
280
        }
281

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

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

    
324
        }
325

    
326
/*        private void registerKeyStrokes(){
327
                for (char key = '0'; key <= '9'; 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
                for (char key = 'A'; key <= 'Z'; key++){
338
                        Character keyChar = new Character(key);
339
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
340
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
341
                }
342
                //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
343
        //this.getActionMap().put("enter", new MyAction("enter"));
344
                Character keyChar = new Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
345
                mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),keyChar);
346
                mapControl.getActionMap().put(keyChar, new KeyAction(""));
347

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

353

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

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

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

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