KickJava   Java API By Example, From Geeks To Geeks.

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


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.Rectangle JavaDoc;
34 import java.awt.event.MouseEvent JavaDoc;
35 import java.awt.Cursor JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37
38 /**
39  * A controller component to zoom in or out a display. This controller component
40  * modifies a display model, in reaction to events emited by a graph view.
41  */

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

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

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

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

70
71   private Display display;
72
73   /**
74    * Constructs a new {@link ZoomTool} component.
75    */

76
77   public ZoomTool () {
78   }
79
80   // -------------------------------------------------------------------------
81
// Implementation of the BindingController interface
82
// -------------------------------------------------------------------------
83

84   public String JavaDoc[] listFc () {
85     return new String JavaDoc[] { DISPLAY_BINDING, TOOLS_BINDING };
86   }
87
88   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
89     if (DISPLAY_BINDING.equals(clientItfName)) {
90       return display;
91     } else if (TOOLS_BINDING.equals(clientItfName)) {
92       return tools;
93     }
94     return null;
95   }
96
97   public void bindFc (
98     final String JavaDoc clientItfName,
99     final Object JavaDoc serverItf)
100   {
101     if (DISPLAY_BINDING.equals(clientItfName)) {
102       display = (Display)serverItf;
103     } else if (TOOLS_BINDING.equals(clientItfName)) {
104       tools = (Tools)serverItf;
105     }
106   }
107
108   public void unbindFc (final String JavaDoc clientItfName) {
109     if (DISPLAY_BINDING.equals(clientItfName)) {
110       display = null;
111     } else if (TOOLS_BINDING.equals(clientItfName)) {
112       tools = null;
113     }
114   }
115
116   // -------------------------------------------------------------------------
117
// Implementation of the GraphViewListener interface
118
// -------------------------------------------------------------------------
119

120   public void mouseClicked (final MouseEvent JavaDoc e, final ComponentPart p) {
121     int tool = tools.getTool();
122     if (tool == Tools.ZOOM_IN || tool == Tools.ZOOM_OUT) {
123       Rect position = display.getDisplayedArea();
124       Rectangle JavaDoc r = display.getScreenSize();
125       double X0 = position.x0;
126       double Y0 = position.y0;
127       double X1 = position.x1;
128       double Y1 = position.y1;
129       X0 += ((double)(r.width/2 - e.getX()))/r.width;
130       Y0 += ((double)(r.height/2 - e.getY()))/r.height;
131       X1 += ((double)(r.width/2 - e.getX()))/r.width;
132       Y1 += ((double)(r.height/2 - e.getY()))/r.height;
133       if (tool == Tools.ZOOM_IN) {
134         display.setDisplayedArea(new Rect(
135           0.5 + (X0 - 0.5)*1.2,
136           0.5 + (Y0 - 0.5)*1.2,
137           0.5 + (X1 - 0.5)*1.2,
138           0.5 + (Y1 - 0.5)*1.2
139         ));
140       } else {
141         display.setDisplayedArea(new Rect(
142           0.5 + (X0 - 0.5)/1.2,
143           0.5 + (Y0 - 0.5)/1.2,
144           0.5 + (X1 - 0.5)/1.2,
145           0.5 + (Y1 - 0.5)/1.2
146         ));
147       }
148     }
149   }
150
151   public void mouseMoved (final MouseEvent JavaDoc e, final ComponentPart p) {
152     int tool = tools.getTool();
153     if (tool == Tools.ZOOM_IN) {
154       // TODO zoom in cursor
155
((JComponent JavaDoc)e.getSource()).setCursor(
156         Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
157     } else if (tool == Tools.ZOOM_OUT) {
158       // TODO zoom out cursor
159
((JComponent JavaDoc)e.getSource()).setCursor(
160         Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
161     }
162   }
163 }
164
Popular Tags