KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > SelectionTool


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 */

18
19 package org.objectweb.jac.ide.diagrams;
20
21 import CH.ifa.draw.contrib.DragNDropTool;
22 import CH.ifa.draw.framework.DrawingEditor;
23 import CH.ifa.draw.framework.DrawingView;
24 import CH.ifa.draw.framework.Figure;
25 import CH.ifa.draw.framework.Handle;
26 import CH.ifa.draw.framework.Tool;
27 import CH.ifa.draw.standard.HandleTracker;
28 import CH.ifa.draw.standard.SelectAreaTracker;
29 import org.objectweb.jac.aspects.gui.DisplayContext;
30 import java.awt.event.MouseEvent JavaDoc;
31
32
33 public class SelectionTool extends AbstractTool {
34
35    protected Tool fChild = null;
36    protected DisplayContext context;
37
38    public SelectionTool(DrawingEditor newDrawingEditor, DisplayContext context) {
39       super(newDrawingEditor);
40       this.context = context;
41    }
42
43    /**
44     * Handles mouse down events and starts the corresponding tracker.
45     */

46    public void mouseDown(MouseEvent JavaDoc e, int x, int y) {
47       // on MS-Windows NT: AWT generates additional mouse down events
48
// when the left button is down && right button is clicked.
49
// To avoid dead locks we ignore such events
50
if (fChild != null) {
51          return;
52       }
53
54       view().freezeView();
55
56       Handle handle = view().findHandle(e.getX(), e.getY());
57       if (handle != null) {
58          fChild = createHandleTracker(view(), handle);
59       } else {
60          Figure figure = drawing().findFigureInside(e.getX(), e.getY());
61          if (figure instanceof Selectable) {
62             ((Selectable)figure).onSelect(context);
63          }
64          figure = drawing().findFigure(e.getX(), e.getY());
65          if (figure != null) {
66             fChild = createDragTracker(figure);
67          } else {
68             if (!e.isShiftDown()) {
69                view().clearSelection();
70             }
71             fChild = createAreaTracker();
72          }
73       }
74       fChild.mouseDown(e, x, y);
75       fChild.activate();
76       view().repairDamage();
77    }
78
79    /**
80     * Handles mouse moves (if the mouse button is up).
81     * Switches the cursors depending on whats under them.
82     */

83    public void mouseMove(MouseEvent JavaDoc evt, int x, int y) {
84       DragNDropTool.setCursor(evt.getX(), evt.getY(), view());
85       ((DiagramView)editor()).setCoord(x,y);
86       view().repairDamage();
87    }
88
89    /**
90     * Handles mouse drag events. The events are forwarded to the
91     * current tracker.
92     */

93    public void mouseDrag(MouseEvent JavaDoc e, int x, int y) {
94       if (fChild != null) { // JDK1.1 doesn't guarantee mouseDown, mouseDrag, mouseUp
95
fChild.mouseDrag(e, x, y);
96       }
97       view().repairDamage();
98    }
99
100    /**
101     * Handles mouse up events. The events are forwarded to the
102     * current tracker.
103     */

104    public void mouseUp(MouseEvent JavaDoc e, int x, int y) {
105       view().unfreezeView();
106       if (fChild != null) { // JDK1.1 doesn't guarantee mouseDown, mouseDrag, mouseUp
107
fChild.mouseUp(e, x, y);
108          fChild.deactivate();
109          fChild = null;
110       }
111       view().repairDamage();
112    }
113
114    /**
115     * Factory method to create a Handle tracker. It is used to track a handle.
116     */

117    protected Tool createHandleTracker(DrawingView view, Handle handle) {
118       return new HandleTracker(editor(), handle);
119    }
120
121    /**
122     * Factory method to create a Drag tracker. It is used to drag a figure.
123     */

124    protected Tool createDragTracker(Figure f) {
125       return new DragTracker(editor(), f);
126    }
127
128    /**
129     * Factory method to create an area tracker. It is used to select an
130     * area.
131     */

132    protected Tool createAreaTracker() {
133       return new SelectAreaTracker(editor());
134    }
135
136 }
137
Popular Tags