KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > gui > resource > ButtonFactory


1 /*
2
3    Copyright 2000,2003-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util.gui.resource;
19
20 import java.net.URL JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 import javax.swing.AbstractButton JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JCheckBox JavaDoc;
29 import javax.swing.JRadioButton JavaDoc;
30
31 /**
32  * This class represents a button factory which builds
33  * buttons from the content of a resource bundle. <br>
34  *
35  * The resource entries format is (for a button named 'Button'):<br>
36  * <pre>
37  * Button.text = text
38  * Button.icon = icon_name
39  * Button.mnemonic = mnemonic
40  * Button.action = action_name
41  * Button.selected = true | false
42  * Button.tooltip = tool tip text
43  * where
44  * text, icon_name and action_name are strings
45  * mnemonic is a character
46  * </pre>
47  *
48  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
49  * @version $Id: ButtonFactory.java,v 1.8 2005/03/27 08:58:37 cam Exp $
50  */

51 public class ButtonFactory extends ResourceManager {
52     // Constants
53
//
54
private final static String JavaDoc ICON_SUFFIX = ".icon";
55     private final static String JavaDoc TEXT_SUFFIX = ".text";
56     private final static String JavaDoc MNEMONIC_SUFFIX = ".mnemonic";
57     private final static String JavaDoc ACTION_SUFFIX = ".action";
58     private final static String JavaDoc SELECTED_SUFFIX = ".selected";
59     private final static String JavaDoc TOOLTIP_SUFFIX = ".tooltip";
60
61     /** The table which contains the actions */
62     private ActionMap actions;
63
64     /**
65      * Creates a new button factory
66      * @param rb the resource bundle that contains the buttons
67      * description.
68      * @param am the actions to bind to the button
69      */

70     public ButtonFactory(ResourceBundle JavaDoc rb, ActionMap am) {
71         super(rb);
72         actions = am;
73     }
74
75     /**
76      * Creates and returns a new swing button
77      * @param name the name of the button in the resource bundle
78      * @throws MissingResourceException if key is not the name of a button.
79      * It is not thrown if the mnemonic and the action keys are missing
80      * @throws ResourceFormatException if the mnemonic is not a single
81      * character
82      * @throws MissingListenerException if the button action is not found in
83      * the action map
84      */

85     public JButton JavaDoc createJButton(String JavaDoc name)
86     throws MissingResourceException JavaDoc,
87            ResourceFormatException,
88            MissingListenerException {
89         JButton JavaDoc result;
90     try {
91         result = new JButton JavaDoc(getString(name+TEXT_SUFFIX));
92     } catch (MissingResourceException JavaDoc e) {
93         result = new JButton JavaDoc();
94     }
95     initializeButton(result, name);
96         return result;
97     }
98
99     /**
100      * Creates and returns a new swing button initialised
101      * to be used as a toolbar button
102      * @param name the name of the button in the resource bundle
103      * @throws MissingResourceException if key is not the name of a button.
104      * It is not thrown if the mnemonic and the action keys are missing
105      * @throws ResourceFormatException if the mnemonic is not a single
106      * character
107      * @throws MissingListenerException if the button action is not found in
108      * the action map
109      */

110     public JButton JavaDoc createJToolbarButton(String JavaDoc name)
111     throws MissingResourceException JavaDoc,
112            ResourceFormatException,
113            MissingListenerException {
114         JButton JavaDoc result;
115     try {
116         result = new JToolbarButton(getString(name+TEXT_SUFFIX));
117     } catch (MissingResourceException JavaDoc e) {
118         result = new JToolbarButton();
119     }
120     initializeButton(result, name);
121         return result;
122     }
123
124     /**
125      * Creates and returns a new swing radio button
126      * @param name the name of the button in the resource bundle
127      * @throws MissingResourceException if key is not the name of a button.
128      * It is not thrown if the mnemonic and the action keys are
129      * missing.
130      * @throws ResourceFormatException if the mnemonic is not a single
131      * character.
132      * @throws MissingListenerException if the button action is not found in
133      * the action map.
134      */

135     public JRadioButton JavaDoc createJRadioButton(String JavaDoc name)
136     throws MissingResourceException JavaDoc,
137            ResourceFormatException,
138            MissingListenerException {
139         JRadioButton JavaDoc result = new JRadioButton JavaDoc(getString(name+TEXT_SUFFIX));
140     initializeButton(result, name);
141
142         // is the button selected?
143
try {
144         result.setSelected(getBoolean(name+SELECTED_SUFFIX));
145     } catch (MissingResourceException JavaDoc e) {
146     }
147     
148         return result;
149     }
150
151     /**
152      * Creates and returns a new swing check box
153      * @param name the name of the button in the resource bundle
154      * @throws MissingResourceException if key is not the name of a button.
155      * It is not thrown if the mnemonic and the action keys are missing
156      * @throws ResourceFormatException if the mnemonic is not a single
157      * character.
158      * @throws MissingListenerException if the button action is not found in
159      * the action map.
160      */

161     public JCheckBox JavaDoc createJCheckBox(String JavaDoc name)
162     throws MissingResourceException JavaDoc,
163            ResourceFormatException,
164            MissingListenerException {
165         JCheckBox JavaDoc result = new JCheckBox JavaDoc(getString(name+TEXT_SUFFIX));
166     initializeButton(result, name);
167
168         // is the button selected?
169
try {
170         result.setSelected(getBoolean(name+SELECTED_SUFFIX));
171     } catch (MissingResourceException JavaDoc e) {
172     }
173     
174         return result;
175     }
176
177     /**
178      * Initializes a button
179      * @param b the button to initialize
180      * @param name the button's name
181      * @throws ResourceFormatException if the mnemonic is not a single
182      * character.
183      * @throws MissingListenerException if the button action is not found
184      * in the action map.
185      */

186     private void initializeButton(AbstractButton JavaDoc b, String JavaDoc name)
187     throws ResourceFormatException, MissingListenerException {
188         // Action
189
try {
190         Action JavaDoc a = actions.getAction(getString(name+ACTION_SUFFIX));
191         if (a == null) {
192         throw new MissingListenerException("", "Action",
193                                                    name+ACTION_SUFFIX);
194         }
195         b.setAction(a);
196             try {
197                 b.setText(getString(name+TEXT_SUFFIX));
198             } catch (MissingResourceException JavaDoc mre) {
199                 // not all buttons have text defined so just
200
// ignore this exception.
201
}
202         if (a instanceof JComponentModifier) {
203         ((JComponentModifier)a).addJComponent(b);
204         }
205     } catch (MissingResourceException JavaDoc e) {
206     }
207
208     // Icon
209
try {
210         String JavaDoc s = getString(name+ICON_SUFFIX);
211         URL JavaDoc url = actions.getClass().getResource(s);
212         if (url != null) {
213         b.setIcon(new ImageIcon JavaDoc(url));
214         }
215     } catch (MissingResourceException JavaDoc e) {
216     }
217
218         // Mnemonic
219
try {
220         String JavaDoc str = getString(name+MNEMONIC_SUFFIX);
221         if (str.length() == 1) {
222         b.setMnemonic(str.charAt(0));
223         } else {
224         throw new ResourceFormatException("Malformed mnemonic",
225                           bundle.getClass().getName(),
226                           name+MNEMONIC_SUFFIX);
227         }
228     } catch (MissingResourceException JavaDoc e) {
229     }
230
231     // ToolTip
232
try {
233         String JavaDoc s = getString(name+TOOLTIP_SUFFIX);
234         if (s != null) {
235         b.setToolTipText(s);
236         }
237     } catch (MissingResourceException JavaDoc e) {
238     }
239     }
240 }
241
Popular Tags