Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / EventsHandler.java @ 6735

History | View | Annotate | Download (18.3 KB)

1 312 fernando
/*
2
 * Created on 27-jul-2004
3
 *
4
 */
5 1103 fjp
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45 312 fernando
package com.iver.cit.gvsig.gui.layout;
46
47
import java.awt.Cursor;
48
import java.awt.Point;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.awt.event.ComponentEvent;
52
import java.awt.event.ComponentListener;
53
import java.awt.event.MouseEvent;
54
import java.awt.event.MouseListener;
55
import java.awt.event.MouseMotionListener;
56
import java.awt.geom.Point2D;
57
import java.util.ArrayList;
58
59 2423 caballero
import com.iver.andami.PluginServices;
60 2263 vcaballero
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
61 4845 caballero
import com.iver.cit.gvsig.gui.layout.fframes.IFFrameEditableVertex;
62 4774 caballero
import com.iver.cit.gvsig.gui.layout.fframes.IFFrameGroupSelectable;
63 312 fernando
64 2263 vcaballero
65 312 fernando
/**
66
 * Eventos que se realizan sobre el Layout.
67
 *
68
 * @author Vicente Caballero Navarro
69
 */
70
public class EventsHandler implements ActionListener, ComponentListener,
71 6276 caballero
    MouseMotionListener, MouseListener{
72 3550 caballero
    private Layout layout = null;
73
    private Point2D.Double m_pointSelected = null;
74
    private int index = 0;
75
    private ArrayList lastSelect = new ArrayList();
76
    private FLayoutFunctions events = null;
77
    private FLayoutZooms zooms = null;
78
    /**
79
     * Crea un nuevo EventsHandler.
80
     *
81
     * @param l Referencia al Layout.
82
     */
83
    public EventsHandler(Layout l) {
84
        layout = l;
85
        events = new FLayoutFunctions(layout);
86
        zooms = new FLayoutZooms(layout);
87
    }
88 394 vcaballero
89 3550 caballero
    /**
90
     * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
91
     */
92
    public void componentHidden(ComponentEvent arg0) {
93
    }
94 312 fernando
95 3550 caballero
    /**
96
     * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
97
     */
98
    public void componentMoved(ComponentEvent arg0) {
99
    }
100 312 fernando
101 3550 caballero
    /**
102
     * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
103
     */
104
    public void componentResized(ComponentEvent arg0) {
105
        layout.fullRect();
106
    }
107 312 fernando
108 3550 caballero
    /**
109
     * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
110
     */
111
    public void componentShown(ComponentEvent arg0) {
112
    }
113 312 fernando
114 3550 caballero
    /**
115
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
116
     */
117
    public void actionPerformed(ActionEvent arg0) {
118
        layout.repaint();
119
    }
120 312 fernando
121 3550 caballero
    /**
122
     * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
123
     */
124
    public void mouseDragged(MouseEvent e) {
125
        if (e.getButton() != MouseEvent.BUTTON3) {
126
            layout.setLastPoint(e.getPoint());
127
            layout.repaint();
128 4774 caballero
        }
129 3550 caballero
        if (layout.getTool() == Layout.EDIT){
130 4774 caballero
                IFFrame[] fframes=layout.getFFrames();
131
                for (int i=0;i<fframes.length;i++){
132
                        IFFrame frame=fframes[i];
133 4845 caballero
                        if (frame instanceof IFFrameEditableVertex){
134
                                ((IFFrameEditableVertex)frame).pointDragged(FLayoutUtilities.toSheetPoint(e.getPoint(),layout.getAT()));
135 3550 caballero
                                layout.setStatus(Layout.GRAPHICS);
136 4774 caballero
137 3550 caballero
                        }
138
                }
139 4774 caballero
140 3550 caballero
        }
141
    }
142 312 fernando
143 3550 caballero
    /**
144
     * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
145
     */
146
    public void mouseMoved(MouseEvent E) {
147
       if ((layout.getTool() == Layout.POLYLINE) ||
148
                (layout.getTool() == Layout.POLYGON) ||
149
                (layout.getTool() == Layout.LINE) ||
150
                (layout.getTool() == Layout.CIRCLE) ||
151
                (layout.getTool() == Layout.RECTANGLESIMPLE)) {
152
            layout.getGeometryAdapter().pointPosition(FLayoutUtilities.toSheetPoint(
153
                    E.getPoint(), layout.getAT()));
154
            layout.setStatus(Layout.GRAPHICS);
155
            layout.repaint();
156
        }
157 312 fernando
158 3550 caballero
        if (layout.getTool() == Layout.SELECT) {
159
            Cursor cursor = null;
160
            Point2D.Double p = new Point2D.Double(E.getX(), E.getY());
161
            IFFrame[] fframes=layout.getFFrameSelected();
162
            for (int i = 0; i < fframes.length; i++) {
163
                if (fframes[i].getContains(p)!=IFFrame.NOSELECT){
164
                        cursor=fframes[i].getMapCursor(p);
165
                }
166
                if (cursor != null) {
167
                    layout.setMapCursor(cursor);
168
                } else {
169
                    layout.setMapCursor(Layout.icrux);
170
                }
171
            }
172
            ///layout.setStatus(Layout.SELECT);
173
        }
174
    }
175 312 fernando
176 3550 caballero
    /**
177
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
178
     */
179
    public void mouseClicked(MouseEvent E) {
180 3575 caballero
            if ((E.getClickCount() == 2) &&
181 3550 caballero
                ((layout.getTool() == Layout.POLYLINE) ||
182
                (layout.getTool() == Layout.POLYGON))) {
183 6601 caballero
                    layout.delLastPoint();
184
            layout.endGraphic();
185 3550 caballero
        } else {
186
            if (E.getButton() == MouseEvent.BUTTON1) {
187
                if (layout.getTool() == Layout.SELECT) {
188
                    m_pointSelected = new Point2D.Double(E.getX(), E.getY());
189 4774 caballero
                    IFFrame[] fframes=layout.getFFrames();
190
                    if (fframes.length > 0) {
191 3550 caballero
                        ArrayList listSelect = new ArrayList();
192 4774 caballero
                        for (int j = 0; j < fframes.length; j++) {
193
                            if (fframes[j].getContains(
194 3550 caballero
                                        m_pointSelected) != IFFrame.NOSELECT) {
195 4774 caballero
                                listSelect.add(fframes[j]);
196 3550 caballero
                            }
197
                        }
198 312 fernando
199 3550 caballero
                        if (listSelect.size() > 0) {
200
                            for (int k = 0; k < listSelect.size(); k++) {
201
                                if (((IFFrame) listSelect.get(k)).getSelected() != IFFrame.NOSELECT) {
202
                                    index = listSelect.size() - k;
203 312 fernando
204 3550 caballero
                                    break;
205
                                }
206
                            }
207 312 fernando
208 3550 caballero
                            if (!FLayoutUtilities.isEqualList(listSelect,
209
                                        lastSelect) ||
210
                                    (index > (listSelect.size() - 1))) {
211
                                index = 0;
212
                            }
213 4774 caballero
                            for (int j = 0; j < fframes.length;
214 3550 caballero
                                    j++) {
215 4774 caballero
                                IFFrame fframe = fframes[j];
216 312 fernando
217 3550 caballero
                                if (!E.isShiftDown()) {
218
                                    fframe.setSelected(false);
219
                                } else {
220
                                    if (fframe.getSelected() != IFFrame.NOSELECT) {
221
                                        if (fframe.getContains(m_pointSelected) != IFFrame.NOSELECT) {
222
                                            fframe.setSelected(false);
223
                                        }
224
                                    }
225
                                }
226
                            }
227 312 fernando
228 3550 caballero
                            ((IFFrame) listSelect.get((listSelect.size() - 1 -
229
                                index))).setSelected(true);
230
                            index++;
231
                            lastSelect = listSelect;
232
                        }
233 312 fernando
234 3550 caballero
                        layout.setStatus(Layout.SELECT);
235
                        layout.repaint();
236 312 fernando
237 3550 caballero
                        if (E.getClickCount() > 1) {
238
                            FLayoutGraphics flg = new FLayoutGraphics(layout);
239
                            flg.openFFrameDialog();
240
                            layout.setStatus(Layout.DESACTUALIZADO);
241
                            layout.repaint();
242 312 fernando
243 3550 caballero
                            //layout.setStatus(Layout.SELECT);
244
                        }
245
                    }
246 312 fernando
247 3550 caballero
                    PluginServices.getMainFrame().enableControls();
248
                }
249
            } else if (E.getButton() == MouseEvent.BUTTON2) {
250
            }
251
        }
252
    }
253 312 fernando
254 3550 caballero
    /**
255
     * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
256
     */
257
    public void mouseEntered(MouseEvent arg0) {
258
        /* TODO        PluginServices.getMainFrame().getStatusBar().setMessage("0",
259
           layout.getAtributes().getNameUnit());
260 4774 caballero

261 3550 caballero
        if (layout.getTool() == Layout.PAN) {
262
            layout.setMapCursor(Layout.iLayoutpan);
263
        } else if (layout.getTool() == Layout.ZOOM_MAS) {
264
            layout.setMapCursor(Layout.iLayoutzoomin);
265
        } else if (layout.getTool() == Layout.ZOOM_MENOS) {
266
            layout.setMapCursor(Layout.iLayoutzoomout);
267
        } else if (layout.getTool() == Layout.VIEW_PAN) {
268
            layout.setMapCursor(Layout.ipan);
269
        } else if (layout.getTool() == Layout.VIEW_ZOOMIN) {
270
            layout.setMapCursor(Layout.izoomin);
271
        } else if (layout.getTool() == Layout.VIEW_ZOOMOUT) {
272
            layout.setMapCursor(Layout.izoomout);
273
        }
274
        */
275
    }
276 312 fernando
277 3550 caballero
    /**
278
     * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
279
     */
280
    public void mouseExited(MouseEvent arg0) {
281
        ///TODO        PluginServices.getMainFrame().getStatusBar().setMessage("0", "");
282
    }
283 312 fernando
284 3550 caballero
    /**
285
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
286
     */
287
    public void mousePressed(MouseEvent E) {
288
        if (E.getButton() == MouseEvent.BUTTON1) {
289
            Point pScreen = new Point(E.getX(), E.getY());
290
            layout.setPointAnt(pScreen);
291
            layout.setFirstPoint(layout.getPointAnt());
292 312 fernando
293 3550 caballero
            if ((layout.getTool() == Layout.ZOOM_MAS) ||
294
                    (layout.getTool() == Layout.VIEW_ZOOMIN)) {
295
                layout.setStatus(Layout.ZOOM_MAS);
296
            } else if ((layout.getTool() == Layout.RECTANGLEVIEW) ||
297
                    (layout.getTool() == Layout.RECTANGLEPICTURE) ||
298
                    (layout.getTool() == Layout.RECTANGLESCALEBAR) ||
299
                    (layout.getTool() == Layout.RECTANGLELEGEND) ||
300
                    (layout.getTool() == Layout.RECTANGLENORTH) ||
301 3610 caballero
                    (layout.getTool() == Layout.RECTANGLETEXT)||
302
                    (layout.getTool() == Layout.RECTANGLEBOX)) {
303 3550 caballero
                layout.setStatus(Layout.RECTANGLE);
304
            } else if ((layout.getTool() == Layout.POINT) ) {
305
                    layout.getGeometryAdapter().addPoint(FLayoutUtilities.toSheetPoint(
306
                        E.getPoint(), layout.getAT()));
307 6601 caballero
                    layout.endGraphic();
308 3550 caballero
                //layout.setStatus(Layout.GRAPHICS);
309
            } else if ((layout.getTool() == Layout.POLYLINE) ||
310
                    (layout.getTool() == Layout.POLYGON)) {
311
                layout.getGeometryAdapter().addPoint(FLayoutUtilities.toSheetPoint(
312
                        E.getPoint(), layout.getAT()));
313
                layout.setStatus(Layout.GRAPHICS);
314
            } else if ((layout.getTool() == Layout.LINE) ||
315
                    (layout.getTool() == Layout.CIRCLE) ||
316
                    (layout.getTool() == Layout.RECTANGLESIMPLE)) {
317
                if (layout.getGeometryAdapter().addPoint(FLayoutUtilities.toSheetPoint(
318
                                E.getPoint(), layout.getAT())) == 2) {
319 6601 caballero
                    layout.endGraphic();
320 3550 caballero
                    layout.setStatus(Layout.DESACTUALIZADO);
321
                } else {
322
                    layout.setStatus(Layout.GRAPHICS);
323
                }
324
            } else if (layout.getTool() == Layout.PAN) {
325
                layout.getRectOrigin().setLocation(layout.getRect().x,
326
                    layout.getRect().y);
327
                layout.setStatus(Layout.PAN);
328
            } else if (layout.getTool() == Layout.VIEW_PAN) {
329
                /*        layout.getRectOrigin().setLocation(layout.getRect().x,
330
                   layout.getRect().y);
331
                 */
332
                layout.setStatus(Layout.VIEW_PAN);
333
            } else if (layout.getTool() == Layout.ZOOM_MENOS) {
334
                layout.setCancelDrawing(true);
335
            } else if ((layout.getTool() == Layout.ZOOM_MAS) ||
336
                    (layout.getTool() == Layout.PAN)) {
337
                layout.setCancelDrawing(true);
338
            } else if (layout.getTool() == Layout.SELECT) {
339
                m_pointSelected = new Point2D.Double(E.getX(), E.getY());
340 4774 caballero
                IFFrame[] fframes=layout.getFFrames();
341
                for (int i = 0; i < fframes.length; i++) {
342
                    IFFrame fframe = fframes[i];
343 312 fernando
344 3550 caballero
                    if (m_pointSelected != null) {
345
                        if (!E.isShiftDown()) {
346
                            if ((fframe.getSelected() != IFFrame.NOSELECT)) {
347 4774 caballero
                                fframe.setSelected(m_pointSelected,E);
348 3550 caballero
                            }
349 4774 caballero
                        }else if (fframe instanceof IFFrameGroupSelectable){
350
                                    fframe.setSelected(m_pointSelected,E);
351 3550 caballero
                        }
352
                    }
353 312 fernando
354 3550 caballero
                    if (fframe.getSelected() != IFFrame.NOSELECT) {
355
                        layout.setIsReSel(false);
356
                    }
357
                }
358 312 fernando
359 3550 caballero
                if ((layout.getLastPoint() != null) &&
360
                        (layout.getFirstPoint() != null)) {
361
                    layout.getLastPoint().setLocation(layout.getFirstPoint());
362
                }
363 312 fernando
364 3550 caballero
                if (E.getClickCount() < 2) {
365
                    layout.setStatus(Layout.SELECT);
366
                    layout.repaint();
367
                }
368
            } else if (layout.getTool() == Layout.SET_TAG) {
369
                m_pointSelected = new Point2D.Double(E.getX(), E.getY());
370 4774 caballero
                IFFrame[] fframes=layout.getFFrames();
371
                for (int i = 0; i < fframes.length; i++) {
372
                    IFFrame fframe = fframes[i];
373 312 fernando
374 3550 caballero
                    if (m_pointSelected != null) {
375
                        if (fframe.contains(m_pointSelected)) {
376
                            fframe.openTag();
377
                        }
378
                    }
379
                }
380
            } else if (layout.getTool() == Layout.EDIT){
381 4774 caballero
                    IFFrame[] fframes=layout.getFFrames();
382
                    for (int i=0;i<fframes.length;i++){
383
                            IFFrame frame=fframes[i];
384 4845 caballero
                            if (frame instanceof IFFrameEditableVertex){
385
                                    ((IFFrameEditableVertex)frame).pointPressed(FLayoutUtilities.toSheetPoint(E.getPoint(),layout.getAT()));
386 3550 caballero
                            }
387
                    }
388
            }
389
        } else if (E.getButton() == MouseEvent.BUTTON3) {
390
            new Popupmenu(layout, E.getPoint());
391
        }
392
    }
393 394 vcaballero
394 3550 caballero
    /**
395
     * @see java.awt.event.MouseListener#mouseReleassed(java.awt.event.MouseEvent)
396
     */
397
    public void mouseReleased(MouseEvent E) {
398
        if (E.getButton() != MouseEvent.BUTTON3) {
399
            layout.setLastPoint(E.getPoint());
400
        }
401 394 vcaballero
402 3550 caballero
        if (E.getButton() == MouseEvent.BUTTON1) {
403
            Point p1 = layout.getFirstPoint();
404
            Point p2 = new Point(E.getX(), E.getY());
405
406
            if (layout.getTool() == Layout.ZOOM_MAS) {
407
                zooms.setZoomIn(p1, p2);
408
                layout.refresh();
409
            } else if ((layout.getTool() == Layout.ZOOM_MENOS) ||
410
                    (E.getButton() == MouseEvent.BUTTON3)) {
411
                zooms.setZoomOut(p2);
412
                layout.refresh();
413
            } else if (layout.getTool() == Layout.SELECT) {
414
                events.setSelect();
415
                layout.refresh();
416
            } else if ((layout.getTool() == Layout.RECTANGLEVIEW) ||
417
                    (layout.getTool() == Layout.RECTANGLEPICTURE) ||
418
                    (layout.getTool() == Layout.RECTANGLESCALEBAR) ||
419
                    (layout.getTool() == Layout.RECTANGLELEGEND) ||
420
                    (layout.getTool() == Layout.RECTANGLETEXT) ||
421 3610 caballero
                    (layout.getTool() == Layout.RECTANGLENORTH)||
422
                    (layout.getTool() == Layout.RECTANGLEBOX)) {
423 4774 caballero
                events.addFFrame();
424 3550 caballero
                PluginServices.getMainFrame().enableControls();
425
                layout.refresh();
426
            } else if (layout.getTool() == Layout.VIEW_ZOOMIN) {
427
                zooms.setViewZoomIn(p1, p2);
428
                layout.refresh();
429
            } else if (layout.getTool() == Layout.VIEW_ZOOMOUT) {
430
                zooms.setViewZoomOut(p2);
431
                layout.refresh();
432
            } else if (layout.getTool() == Layout.VIEW_PAN) {
433
                events.setViewPan(p1, p2);
434
                layout.refresh();
435
            } else if ((layout.getTool() == Layout.POLYLINE) ||
436
                    (layout.getTool() == Layout.POLYGON)) {
437
                layout.setStatus(Layout.GRAPHICS);
438
                layout.repaint();
439
            } else if (layout.getTool() == Layout.EDIT){
440 4774 caballero
                    IFFrame[] fframes=layout.getFFrames();
441
                    for (int i=0;i<fframes.length;i++){
442
                            IFFrame frame=fframes[i];
443 4845 caballero
                            if (frame instanceof IFFrameEditableVertex){
444
                                    if (frame.getSelected()!=IFFrame.NOSELECT && ((IFFrameEditableVertex)frame).isEditing()){
445 3550 caballero
                                            IFFrame fframeAux=frame.cloneFFrame(layout);
446 4845 caballero
                                            ((IFFrameEditableVertex)fframeAux).startEditing();
447
                                            ((IFFrameEditableVertex)fframeAux).pointReleased(FLayoutUtilities.toSheetPoint(E.getPoint(),layout.getAT()),((IFFrameEditableVertex)frame).getGeometry());
448 3550 caballero
                                            layout.getEFS().modifyFFrame(frame,fframeAux);
449
                                            fframeAux.getBoundingBox(layout.getAT());
450
                                            layout.updateFFrames();
451
                                    }
452
                            }
453
                    }
454 3575 caballero
                    layout.refresh();
455
            } else if (layout.getTool() == Layout.PAN) {
456 5729 caballero
                zooms.setPan(p1,p2);
457
                    layout.refresh();
458 3550 caballero
            }
459
            layout.setCancelDrawing(false);
460
        } else if (E.getButton() == MouseEvent.BUTTON3) {
461
        }
462
463 4774 caballero
464 3550 caballero
    }
465 312 fernando
}