1/*
2
3   Licensed to the Apache Software Foundation (ASF) under one or more
4   contributor license agreements.  See the NOTICE file distributed with
5   this work for additional information regarding copyright ownership.
6   The ASF licenses this file to You under the Apache License, Version 2.0
7   (the "License"); you may not use this file except in compliance with
8   the License.  You may obtain a copy of the License at
9
10       http://www.apache.org/licenses/LICENSE-2.0
11
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17
18 */
19package components;
20
21import java.net.URL;
22import java.util.MissingResourceException;
23import java.util.ResourceBundle;
24
25import javax.swing.AbstractButton;
26import javax.swing.Action;
27import javax.swing.ImageIcon;
28import javax.swing.JButton;
29import javax.swing.JCheckBox;
30import javax.swing.JRadioButton;
31import javax.swing.JToggleButton;
32
33import org.apache.batik.util.resources.ResourceFormatException;
34import org.apache.batik.util.resources.ResourceManager;
35
36/**
37 * This class represents a button factory which builds
38 * buttons from the content of a resource bundle. <br>
39 *
40 * The resource entries format is (for a button named 'Button'):<br>
41 * <pre>
42 *   Button.text      = text
43 *   Button.icon      = icon_name
44 *   Button.mnemonic  = mnemonic
45 *   Button.action    = action_name
46 *   Button.selected  = true | false
47 *   Button.tooltip   = tool tip text
48 * where
49 *   text, icon_name and action_name are strings
50 *   mnemonic is a character
51 * </pre>
52 *
53 * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
54 * @version $Id: ButtonFactory.java,v 1.1 2013/02/21 11:24:35 jschimpf Exp $
55 */
56public class ButtonFactory extends ResourceManager {
57    // Constants
58    //
59    private static final String ICON_SUFFIX        = ".icon";
60    private static final String TEXT_SUFFIX        = ".text";
61    private static final String MNEMONIC_SUFFIX    = ".mnemonic";
62    private static final String ACTION_SUFFIX      = ".action";
63    private static final String SELECTED_SUFFIX    = ".selected";
64    private static final String TOOLTIP_SUFFIX     = ".tooltip";
65
66    /** The table which contains the actions */
67    private ActionMap actions;
68
69    /**
70     * Creates a new button factory
71     * @param rb the resource bundle that contains the buttons
72     *           description.
73     * @param am the actions to bind to the button
74     */
75    public ButtonFactory(ResourceBundle rb, ActionMap am) {
76        super(rb);
77        actions = am;
78    }
79
80    /**
81     * Creates and returns a new swing button
82     * @param name the name of the button in the resource bundle
83     * @throws MissingResourceException if key is not the name of a button.
84     *         It is not thrown if the mnemonic and the action keys are missing
85     * @throws ResourceFormatException if the mnemonic is not a single
86     *         character
87     * @throws MissingListenerException if the button action is not found in
88     *         the action map
89     */
90    public JButton createJButton(String name)
91        throws MissingResourceException,
92               ResourceFormatException,
93               MissingListenerException {
94        JButton result;
95        try {
96            result = new JButton(getString(name+TEXT_SUFFIX));
97        } catch (MissingResourceException e) {
98            result = new JButton();
99        }
100        initializeButton(result, name);
101        return result;
102    }
103
104    /**
105     * Creates and returns a new swing button initialised
106     * to be used as a toolbar button
107     * @param name the name of the button in the resource bundle
108     * @throws MissingResourceException if key is not the name of a button.
109     *         It is not thrown if the mnemonic and the action keys are missing
110     * @throws ResourceFormatException if the mnemonic is not a single
111     *         character
112     * @throws MissingListenerException if the button action is not found in
113     *         the action map
114     */
115    public JButton createJToolbarButton(String name)
116        throws MissingResourceException,
117               ResourceFormatException,
118               MissingListenerException {
119        JButton result;
120        try {
121        	String s = name+TEXT_SUFFIX;
122        	s = getString(name+TEXT_SUFFIX);
123        	result = new JToolbarButton(s);
124        } catch (MissingResourceException e) {
125            result = new JToolbarButton();
126        }
127        initializeButton(result, name);
128        return result;
129    }
130
131    /**
132     * Creates and returns a new swing button initialised
133     * to be used as a toolbar toggle button
134     * @param name the name of the button in the resource bundle
135     * @throws MissingResourceException if key is not the name of a button.
136     *         It is not thrown if the mnemonic and the action keys are missing
137     * @throws ResourceFormatException if the mnemonic is not a single
138     *         character
139     * @throws MissingListenerException if the button action is not found in
140     *         the action map
141     */
142    public JToggleButton createJToolbarToggleButton(String name)
143        throws MissingResourceException,
144               ResourceFormatException,
145               MissingListenerException {
146        JToggleButton result;
147        try {
148            result = new JToolbarToggleButton(getString(name+TEXT_SUFFIX));
149        } catch (MissingResourceException e) {
150            result = new JToolbarToggleButton();
151        }
152        initializeButton(result, name);
153        return result;
154    }
155
156    /**
157     * Creates and returns a new swing radio button
158     * @param name the name of the button in the resource bundle
159     * @throws MissingResourceException if key is not the name of a button.
160     *         It is not thrown if the mnemonic and the action keys are
161     *         missing.
162     * @throws ResourceFormatException if the mnemonic is not a single
163     *         character.
164     * @throws MissingListenerException if the button action is not found in
165     *         the action map.
166     */
167    public JRadioButton createJRadioButton(String name)
168        throws MissingResourceException,
169               ResourceFormatException,
170               MissingListenerException {
171        JRadioButton result = new JRadioButton(getString(name+TEXT_SUFFIX));
172        initializeButton(result, name);
173
174        // is the button selected?
175        try {
176            result.setSelected(getBoolean(name+SELECTED_SUFFIX));
177        } catch (MissingResourceException e) {
178        }
179
180        return result;
181    }
182
183    /**
184     * Creates and returns a new swing check box
185     * @param name the name of the button in the resource bundle
186     * @throws MissingResourceException if key is not the name of a button.
187     *         It is not thrown if the mnemonic and the action keys are missing
188     * @throws ResourceFormatException if the mnemonic is not a single
189     *         character.
190     * @throws MissingListenerException if the button action is not found in
191     *         the action map.
192     */
193    public JCheckBox createJCheckBox(String name)
194        throws MissingResourceException,
195               ResourceFormatException,
196               MissingListenerException {
197        JCheckBox result = new JCheckBox(getString(name+TEXT_SUFFIX));
198        initializeButton(result, name);
199
200        // is the button selected?
201        try {
202            result.setSelected(getBoolean(name+SELECTED_SUFFIX));
203        } catch (MissingResourceException e) {
204        }
205
206        return result;
207    }
208
209    /**
210     * Initializes a button
211     * @param b    the button to initialize
212     * @param name the button's name
213     * @throws ResourceFormatException if the mnemonic is not a single
214     *         character.
215     * @throws MissingListenerException if the button action is not found
216     *         in the action map.
217     */
218    private void initializeButton(AbstractButton b, String name)
219        throws ResourceFormatException, MissingListenerException {
220        // Action
221        try {
222            Action a = actions.getAction(getString(name+ACTION_SUFFIX));
223            if (a == null) {
224                throw new MissingListenerException("", "Action",
225                                                   name+ACTION_SUFFIX);
226            }
227            b.setAction(a);
228            try {
229                b.setText(getString(name+TEXT_SUFFIX));
230            } catch (MissingResourceException mre) {
231                // not all buttons have text defined so just
232                // ignore this exception.
233            }
234            if (a instanceof JComponentModifier) {
235                ((JComponentModifier)a).addJComponent(b);
236            }
237        } catch (MissingResourceException e) {
238        }
239
240        // Icon
241        try {
242            String s = getString(name+ICON_SUFFIX);
243            //URL url  = actions.getClass().getResource(s);
244            URL url  = actions.getClass().getResource(s);
245            if (url != null) {
246                b.setIcon(new ImageIcon(url));
247            }
248        } catch (MissingResourceException e) {
249        }
250
251        // Mnemonic
252        try {
253            String str = getString(name+MNEMONIC_SUFFIX);
254            if (str.length() == 1) {
255                b.setMnemonic(str.charAt(0));
256            } else {
257                throw new ResourceFormatException("Malformed mnemonic",
258                                                  bundle.getClass().getName(),
259                                                  name+MNEMONIC_SUFFIX);
260            }
261        } catch (MissingResourceException e) {
262        }
263
264        // ToolTip
265        try {
266            String s = getString(name+TOOLTIP_SUFFIX);
267            if (s != null) {
268                b.setToolTipText(s);
269            }
270        } catch (MissingResourceException e) {
271        }
272    }
273}
274