KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > service > ToolBarServiceProvider


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.service;
8
9 import java.awt.Container JavaDoc;
10 import java.beans.beancontext.BeanContextServices JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Vector JavaDoc;
14
15 import javax.swing.Action JavaDoc;
16 import javax.swing.Icon JavaDoc;
17 import javax.swing.JButton JavaDoc;
18 import javax.swing.JToolBar JavaDoc;
19
20 import org.apache.log4j.Logger;
21 import org.ejtools.adwt.action.CommandAction;
22 import org.ejtools.beans.beancontext.CustomBeanContextServiceProvider;
23
24 /**
25  * Description of the Class
26  *
27  * @author Laurent Etiemble
28  * @version $Revision: 1.5 $
29  * @todo Javadoc to complete
30  */

31 public class ToolBarServiceProvider extends CustomBeanContextServiceProvider implements ToolBarService
32 {
33    /** Description of the Field */
34    private Hashtable JavaDoc bars = new Hashtable JavaDoc();
35    /** Description of the Field */
36    private boolean bigIcons = false;
37    /** Description of the Field */
38    private ToolBarService service = null;
39    /** Description of the Field */
40    private JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
41    /** Description of the Field */
42    private static Logger logger = Logger.getLogger(ToolBarServiceProvider.class);
43
44
45    /** Constructor for the ToolBarServiceProvider object */
46    public ToolBarServiceProvider()
47    {
48       this.service = this;
49    }
50
51
52    /**
53     * Gets the container attribute of the ToolBarServiceProvider object
54     *
55     * @return The container value
56     */

57    public Container JavaDoc getContainer()
58    {
59       return this.toolBar;
60    }
61
62
63    /**
64     * Gets the currentServiceSelectors attribute of the ApplicationServiceProvider object
65     *
66     * @param bcs Description of Parameter
67     * @param serviceClass Description of Parameter
68     * @return The currentServiceSelectors value
69     */

70    public Iterator JavaDoc getCurrentServiceSelectors(BeanContextServices JavaDoc bcs, Class JavaDoc serviceClass)
71    {
72       return new Vector JavaDoc().iterator();
73    }
74
75
76    /**
77     * Gets the service attribute of the ApplicationServiceProvider object
78     *
79     * @param bcs Description of Parameter
80     * @param requestor Description of Parameter
81     * @param serviceClass Description of Parameter
82     * @param serviceSelector Description of Parameter
83     * @return The service value
84     */

85    public Object JavaDoc getService(BeanContextServices JavaDoc bcs, Object JavaDoc requestor, Class JavaDoc serviceClass, Object JavaDoc serviceSelector)
86    {
87       return service;
88    }
89
90
91    /**
92     * Description of the Method
93     *
94     * @param action Description of Parameter
95     */

96    public void register(CommandAction action)
97    {
98       String JavaDoc menu = action.getMenu();
99       JToolBarWrapper bar = (JToolBarWrapper) this.bars.get(menu);
100
101       if (bar == null)
102       {
103          bar = new JToolBarWrapper();
104          bar.setFloatable(false);
105
106          this.bars.put(menu, bar);
107
108          if (this.toolBar.getComponentCount() > 0)
109          {
110             this.toolBar.addSeparator();
111          }
112
113          this.toolBar.add(bar);
114       }
115
116       JButton JavaDoc button = bar.add(action);
117
118       Object JavaDoc o = null;
119       if (this.bigIcons)
120       {
121          o = action.getValue(CommandAction.ICON);
122       }
123       else
124       {
125          o = action.getValue(Action.SMALL_ICON);
126       }
127       if (o != null)
128       {
129          button.setIcon((Icon JavaDoc) o);
130       }
131       if (action.getValue(CommandAction.TOOLTIP) != null)
132       {
133          button.setToolTipText((String JavaDoc) action.getValue(CommandAction.TOOLTIP));
134       }
135
136       toolBar.validate();
137       logger.debug("Button registered");
138    }
139
140
141    /**
142     * Description of the Method
143     *
144     * @param bcs Description of Parameter
145     * @param requestor Description of Parameter
146     * @param service Description of Parameter
147     */

148    public void releaseService(BeanContextServices JavaDoc bcs, Object JavaDoc requestor, Object JavaDoc service) { }
149
150
151    /**
152     * Sets the bigIcons.
153     *
154     * @param bigIcons The bigIcons to set
155     */

156    public void setBigIcons(boolean bigIcons)
157    {
158       if (this.bars.size() > 0)
159       {
160          throw new IllegalStateException JavaDoc("Cannot change icon size if buttons are registered");
161       }
162       this.bigIcons = bigIcons;
163    }
164
165
166    /**
167     * Description of the Method
168     *
169     * @param action Description of Parameter
170     */

171    public void unregister(CommandAction action)
172    {
173       String JavaDoc menu = action.getMenu();
174       JToolBarWrapper bar = (JToolBarWrapper) this.bars.get(menu);
175
176       if (bar == null)
177       {
178          return;
179       }
180
181       bar.remove(action);
182       toolBar.validate();
183       logger.debug("Button unregistered");
184    }
185
186
187    /**
188     * @return The serviceClass value
189     */

190    protected Class JavaDoc[] getServiceClass()
191    {
192       return new Class JavaDoc[]{ToolBarService.class};
193    }
194
195
196    /**
197     * Description of the Class
198     *
199     * @author laurent
200     * @version $Revision: 1.5 $
201     */

202    protected class JToolBarWrapper extends JToolBar JavaDoc
203    {
204       /** Description of the Field */
205       protected Hashtable JavaDoc buttons = new Hashtable JavaDoc();
206
207
208       /** Constructor for JToolBarWrapper. */
209       public JToolBarWrapper()
210       {
211          super();
212          this.setBorder(null);
213       }
214
215
216       /**
217        * Constructor for JToolBarWrapper.
218        *
219        * @param orientation
220        */

221       public JToolBarWrapper(int orientation)
222       {
223          super(orientation);
224       }
225
226
227       /**
228        * Constructor for JToolBarWrapper.
229        *
230        * @param name
231        */

232       public JToolBarWrapper(String JavaDoc name)
233       {
234          super(name);
235       }
236
237
238       /**
239        * Constructor for JToolBarWrapper.
240        *
241        * @param name
242        * @param orientation
243        */

244       public JToolBarWrapper(String JavaDoc name, int orientation)
245       {
246          super(name, orientation);
247       }
248
249
250       /**
251        * Description of the Method
252        *
253        * @param action Description of Parameter
254        * @return Description of the Returned Value
255        */

256       public JButton JavaDoc add(Action JavaDoc action)
257       {
258          JButton JavaDoc button = super.add(action);
259          this.buttons.put(action, button);
260          return button;
261       }
262
263
264       /**
265        * Description of the Method
266        *
267        * @param action Description of Parameter
268        */

269       public void remove(Action JavaDoc action)
270       {
271          JButton JavaDoc button = (JButton JavaDoc) this.buttons.get(action);
272          this.remove(button);
273       }
274    }
275 }
276
277
Popular Tags