KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > graph > control > MoveTool


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.graph.control;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.graph.model.Display;
29 import org.objectweb.fractal.gui.graph.model.Rect;
30 import org.objectweb.fractal.gui.graph.model.Tools;
31 import org.objectweb.fractal.gui.graph.view.ComponentPart;
32
33 import java.awt.Cursor JavaDoc;
34 import java.awt.Rectangle JavaDoc;
35 import java.awt.event.MouseEvent JavaDoc;
36
37 import javax.swing.JComponent JavaDoc;
38
39 /**
40  * A controller component to scroll a display. This controller component
41  * modifies a display model, in reaction to events emited by a graph view.
42  */

43
44 public class MoveTool extends EmptyGraphViewListener
45   implements BindingController
46 {
47
48   /**
49    * A mandatory client interface bound to a {@link Tools tools} model. This
50    * model is used to know if this tool is the currently selected tool or not.
51    */

52
53   public final static String JavaDoc TOOLS_BINDING = "tools";
54
55   /**
56    * A mandatory client interface bound to a {@link Display display} model. This
57    * model is modified by this controller component when the mouse is dragged.
58    */

59
60   public final static String JavaDoc DISPLAY_BINDING = "display";
61
62   /**
63    * The tools client interface.
64    */

65
66   private Tools tools;
67
68   /**
69    * The display client interface.
70    */

71
72   private Display display;
73
74   /**
75    * x coordinate of the point from which the mouse is being dragged.
76    */

77
78   private int startX;
79
80   /**
81    * y coordinate of the point from which the mouse is being dragged.
82    */

83
84   private int startY;
85
86   /**
87    * {@link Rect#x0} coordinate of the displayed area, just before the mouse
88    * was dragged.
89    */

90
91   private double startX0;
92
93   /**
94    * {@link Rect#y0} coordinate of the displayed area, just before the mouse
95    * was dragged.
96    */

97
98   private double startY0;
99
100   /**
101    * {@link Rect#x1} coordinate of the displayed area, just before the mouse
102    * was dragged.
103    */

104
105   private double startX1;
106
107   /**
108    * {@link Rect#y1} coordinate of the displayed area, just before the mouse
109    * was dragged.
110    */

111
112   private double startY1;
113
114   /**
115    * If the display must be dragged when a mouse dragged event is received.
116    */

117
118   private boolean drag;
119
120   /**
121    * Constructs a new {@link MoveTool} component.
122    */

123
124   public MoveTool () {
125   }
126
127   // -------------------------------------------------------------------------
128
// Implementation of the BindingController interface
129
// -------------------------------------------------------------------------
130

131   public String JavaDoc[] listFc () {
132     return new String JavaDoc[] { DISPLAY_BINDING, TOOLS_BINDING };
133   }
134
135   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
136     if (DISPLAY_BINDING.equals(clientItfName)) {
137       return display;
138     } else if (TOOLS_BINDING.equals(clientItfName)) {
139       return tools;
140     }
141     return null;
142   }
143
144   public void bindFc (
145     final String JavaDoc clientItfName,
146     final Object JavaDoc serverItf)
147   {
148     if (DISPLAY_BINDING.equals(clientItfName)) {
149       display = (Display)serverItf;
150     } else if (TOOLS_BINDING.equals(clientItfName)) {
151       tools = (Tools)serverItf;
152     }
153   }
154
155   public void unbindFc (final String JavaDoc clientItfName) {
156     if (DISPLAY_BINDING.equals(clientItfName)) {
157       display = null;
158     } else if (TOOLS_BINDING.equals(clientItfName)) {
159       tools = null;
160     }
161   }
162
163   // -------------------------------------------------------------------------
164
// Implementation of the GraphViewListener interface
165
// -------------------------------------------------------------------------
166

167   public void viewChanged () {
168     drag = false;
169   }
170
171   public void mousePressed (final MouseEvent JavaDoc e, final ComponentPart p) {
172     if (tools.getTool() == Tools.MOVE) {
173       startX = e.getX();
174       startY = e.getY();
175       Rect position = display.getDisplayedArea();
176       startX0 = position.x0;
177       startY0 = position.y0;
178       startX1 = position.x1;
179       startY1 = position.y1;
180       drag = true;
181     } else {
182       drag = false;
183     }
184   }
185
186   public void mouseReleased (final MouseEvent JavaDoc e, final ComponentPart p) {
187     drag = false;
188   }
189
190   public void mouseClicked (final MouseEvent JavaDoc e, final ComponentPart p) {
191     drag = false;
192   }
193
194   public void mouseDragged (final MouseEvent JavaDoc e) {
195     if (drag) {
196       Rectangle JavaDoc r = display.getScreenSize();
197       double X0 = startX0 + ((double)(e.getX() - startX))/r.width;
198       double Y0 = startY0 + ((double)(e.getY() - startY))/r.height;
199       double X1 = startX1 + ((double)(e.getX() - startX))/r.width;
200       double Y1 = startY1 + ((double)(e.getY() - startY))/r.height;
201       display.setDisplayedArea(new Rect(X0, Y0, X1, Y1));
202     }
203   }
204
205   public void mouseMoved (final MouseEvent JavaDoc e, final ComponentPart p) {
206     // drag = false; // TODO bug JDK1.4? Windows?
207
if (tools.getTool() == Tools.MOVE) {
208       ((JComponent JavaDoc)e.getSource()).setCursor(
209         Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
210     }
211   }
212 }
213
Popular Tags