KickJava   Java API By Example, From Geeks To Geeks.

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


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.Tools;
29 import org.objectweb.fractal.gui.graph.view.ComponentPart;
30 import org.objectweb.fractal.gui.model.ClientInterface;
31 import org.objectweb.fractal.gui.model.Interface;
32 import org.objectweb.fractal.gui.model.ServerInterface;
33
34 import java.awt.event.MouseEvent JavaDoc;
35
36 import javax.swing.JOptionPane JavaDoc;
37
38 /**
39  * A controller component to create or remove bindings. This controller
40  * component modifies a configuration model, in reaction to events emited by a
41  * graph view.
42  */

43
44 public class BindTool 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    * The tools client interface.
57    */

58
59   private Tools tools;
60
61   /**
62    * Indicates if the last mouve event was a mouse dragged event.
63    */

64
65   private boolean mouseDragged;
66
67   /**
68    * Constructs a new {@link BindTool} component.
69    */

70
71   public BindTool () {
72   }
73
74   // -------------------------------------------------------------------------
75
// Implementation of the BindingController interface
76
// -------------------------------------------------------------------------
77

78   public String JavaDoc[] listFc () {
79     return new String JavaDoc[] { TOOLS_BINDING };
80   }
81
82   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
83     if (TOOLS_BINDING.equals(clientItfName)) {
84       return tools;
85     }
86     return null;
87   }
88
89   public void bindFc (
90     final String JavaDoc clientItfName,
91     final Object JavaDoc serverItf)
92   {
93     if (TOOLS_BINDING.equals(clientItfName)) {
94       tools = (Tools)serverItf;
95     }
96   }
97
98   public void unbindFc (final String JavaDoc clientItfName) {
99     if (TOOLS_BINDING.equals(clientItfName)) {
100       tools = null;
101     }
102   }
103
104   // -------------------------------------------------------------------------
105
// Implementation of the GraphViewListener interface
106
// -------------------------------------------------------------------------
107

108   public void viewChanged () {
109     if (tools.getTool() == Tools.BIND) {
110       tools.setTool(Tools.SELECT);
111       mouseDragged = false;
112     }
113   }
114
115   public void mousePressed (final MouseEvent JavaDoc e, final ComponentPart p) {
116     if (tools.getTool() == Tools.BIND) {
117       if (!e.isConsumed()) {
118         tools.setTool(Tools.SELECT);
119       }
120       mouseDragged = false;
121     }
122   }
123
124   public void mouseReleased (final MouseEvent JavaDoc e, final ComponentPart p) {
125     if (tools.getTool() == Tools.BIND) {
126       tools.setTool(Tools.SELECT);
127       if (mouseDragged) {
128         ClientInterface citf = (ClientInterface)tools.getBindInterface();
129         Interface sitf = p.getInterface();
130         //try {
131
if (sitf != null) {
132             // bind citf
133
if (sitf instanceof ServerInterface) {
134               if (citf.getBinding() == null) {
135                 citf.getOwner().bind(citf, null, (ServerInterface)sitf);
136               } else {
137                 citf.getOwner().rebind(citf, (ServerInterface)sitf);
138               }
139             } else {
140               JOptionPane.showMessageDialog(
141                 null,
142                 "Cannot bind a client interface to another client interface",
143                 "Error",
144                 JOptionPane.ERROR_MESSAGE);
145             }
146           } else {
147             // unding citf
148
if (citf.getBinding() != null) {
149               citf.getOwner().unbind(citf);
150             }
151           }
152         /*} catch (IllegalOperationException ioe) {
153           JOptionPane.showMessageDialog(
154             null, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
155         }*/

156       }
157     }
158   }
159
160   public void mouseClicked (final MouseEvent JavaDoc e, final ComponentPart p) {
161     if (tools.getTool() == Tools.BIND) {
162       tools.setTool(Tools.SELECT);
163       mouseDragged = false;
164     }
165   }
166
167   public void mouseDragged (final MouseEvent JavaDoc e) {
168     if (tools.getTool() == Tools.BIND) {
169       mouseDragged = true;
170     }
171   }
172
173   public void mouseMoved (final MouseEvent JavaDoc e, final ComponentPart p) {
174     if (tools.getTool() == Tools.BIND) {
175       // tools.setTool(Tools.SELECT); // TODO bug JDK1.4? Windows?
176
// mouseDragged = false;
177
}
178   }
179 }
180
Popular Tags