KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > graph > view > BasicToolView


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.view;
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.model.ToolsListener;
30 import org.objectweb.fractal.swing.ActionAttributes;
31
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.Insets JavaDoc;
34
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JToggleButton JavaDoc;
37 import javax.swing.Action JavaDoc;
38
39 /**
40  * A {@link Tools tools} view component. This component displays one tool, as
41  * a toggle button which is selected iif the corresponding tool is selected in
42  * the model. This view component listens to the tools model, in order to select
43  * or deselect itself when the currently selected tool changes.
44  */

45
46 public class BasicToolView extends JToggleButton JavaDoc implements
47   Action JavaDoc,
48   ToolsListener,
49   BindingController,
50   ActionAttributes
51 {
52
53   /**
54    * A mandatory client interface bound to a {@link Tools tools} model. This
55    * is the model displayed by this view component.
56    */

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

63
64   private Tools tools;
65
66   /**
67    * URL of the icon of this action.
68    */

69
70   private String JavaDoc iconURL;
71
72   /**
73    * Accelerator key of this action.
74    */

75
76   private String JavaDoc acceleratorKey;
77
78   /**
79    * The tool represented by this view. This field is computed from the name of
80    * this action, in the {@link #setName setName} method.
81    */

82
83   private int tool;
84
85   /**
86    * Constructs a new {@link BasicToolView} component.
87    */

88
89   public BasicToolView () {
90     setMargin(new Insets JavaDoc(2, 2, 2, 2));
91   }
92
93   // -------------------------------------------------------------------------
94
// Implementation of the BindingController interface
95
// -------------------------------------------------------------------------
96

97   public String JavaDoc[] listFc () {
98     return new String JavaDoc[] { TOOLS_BINDING };
99   }
100
101   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
102     if (TOOLS_BINDING.equals(clientItfName)) {
103       return tools;
104     }
105     return null;
106   }
107
108   public void bindFc (
109     final String JavaDoc clientItfName,
110     final Object JavaDoc serverItf)
111   {
112     if (TOOLS_BINDING.equals(clientItfName)) {
113       tools = (Tools)serverItf;
114     }
115   }
116
117   public void unbindFc (final String JavaDoc clientItfName) {
118     if (TOOLS_BINDING.equals(clientItfName)) {
119       tools = null;
120     }
121   }
122
123   // -------------------------------------------------------------------------
124
// Implementation of the Action interface
125
// -------------------------------------------------------------------------
126

127   public Object JavaDoc getValue (final String JavaDoc key) {
128     return null;
129   }
130
131   public void putValue (final String JavaDoc key, final Object JavaDoc value) {
132   }
133
134   public void actionPerformed (final ActionEvent JavaDoc e) {
135   }
136
137   // -------------------------------------------------------------------------
138
// Implementation of the ActionAttributes interface
139
// -------------------------------------------------------------------------
140

141   public void setName (final String JavaDoc name) {
142     super.setName(name);
143     if (name.equals("Edit")) {
144       tool = Tools.SELECT;
145     } else if (name.equals("Move")) {
146       tool = Tools.MOVE;
147     } else if (name.equals("Zoomin")) {
148       tool = Tools.ZOOM_IN;
149     } else if (name.equals("Zoomout")) {
150       tool = Tools.ZOOM_OUT;
151     }
152   }
153
154   public String JavaDoc getIconURL () {
155     return iconURL;
156   }
157
158   public void setIconURL (final String JavaDoc iconURL) {
159     this.iconURL = iconURL;
160     setIcon(new ImageIcon JavaDoc(getClass().getResource(iconURL)));
161   }
162
163   public String JavaDoc getAcceleratorKey () {
164     return acceleratorKey;
165   }
166
167   public void setAcceleratorKey (final String JavaDoc acceleratorKey) {
168     this.acceleratorKey = acceleratorKey;
169   }
170
171   // -------------------------------------------------------------------------
172
// Implementation of the ToolsListener interface
173
// -------------------------------------------------------------------------
174

175   public void toolChanged () {
176     setSelected(tools.getTool() == tool);
177   }
178
179   // -------------------------------------------------------------------------
180
// Overriden JToggleButton methods
181
// -------------------------------------------------------------------------
182

183   protected void fireStateChanged () {
184     super.fireStateChanged();
185     if (isSelected()) {
186       tools.setTool(tool);
187     }
188   }
189 }
190
Popular Tags