001: /*
002:
003: ============================================================================
004: The Apache Software License, Version 1.1
005: ============================================================================
006:
007: Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modifica-
010: tion, are permitted provided that the following conditions are met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright notice,
016: this list of conditions and the following disclaimer in the documentation
017: and/or other materials provided with the distribution.
018:
019: 3. The end-user documentation included with the redistribution, if any, must
020: include the following acknowledgment: "This product includes software
021: developed by the Apache Software Foundation (http://www.apache.org/)."
022: Alternately, this acknowledgment may appear in the software itself, if
023: and wherever such third-party acknowledgments normally appear.
024:
025: 4. The names "Batik" and "Apache Software Foundation" must not be
026: used to endorse or promote products derived from this software without
027: prior written permission. For written permission, please contact
028: apache@apache.org.
029:
030: 5. Products derived from this software may not be called "Apache", nor may
031: "Apache" appear in their name, without prior written permission of the
032: Apache Software Foundation.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: This software consists of voluntary contributions made by many individuals
046: on behalf of the Apache Software Foundation. For more information on the
047: Apache Software Foundation, please see <http://www.apache.org/>.
048:
049: */
050:
051: package org.apache.batik.util.gui.resource;
052:
053: import java.awt.Point;
054: import java.awt.event.MouseEvent;
055: import java.net.URL;
056: import java.util.MissingResourceException;
057: import java.util.ResourceBundle;
058:
059: import javax.swing.AbstractButton;
060: import javax.swing.Action;
061: import javax.swing.ImageIcon;
062: import javax.swing.JButton;
063: import javax.swing.JCheckBox;
064: import javax.swing.JRadioButton;
065: import javax.swing.JToolTip;
066: import javax.swing.ToolTipManager;
067:
068: import com.projity.menu.HyperLinkToolTip;
069: import com.projity.util.ClassLoaderUtils;
070:
071: /**
072: * This class represents a button factory which builds buttons from the content
073: * of a resource bundle. <br>
074: *
075: * The resource entries format is (for a button named 'Button'): <br>
076: *
077: * <pre>
078: *
079: * Button.text = text
080: * Button.icon = icon_name
081: * Button.mnemonic = mnemonic
082: * Button.action = action_name
083: * Button.selected = true | false
084: * Button.tooltip = tool tip text
085: * where
086: * text, icon_name and action_name are strings
087: * mnemonic is a character
088: *
089: * </pre>
090: *
091: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion </a>
092: * @version $Id: ButtonFactory.java,v 1.2 2007/09/19 03:17:43 nekosej Exp $
093: */
094: public class ButtonFactory extends ResourceManager {
095: // Constants
096: //
097: private final static String ICON_SUFFIX = ".icon";
098:
099: private final static String TEXT_SUFFIX = ".text";
100:
101: private final static String MNEMONIC_SUFFIX = ".mnemonic";
102:
103: private final static String ACTION_SUFFIX = ".action";
104:
105: private final static String SELECTED_SUFFIX = ".selected";
106:
107: public final static String TOOLTIP_SUFFIX = ".tooltip";
108: public final static String HELP_SUFFIX = ".help";
109: public final static String DEMO_SUFFIX = ".demo";
110: public final static String DOC_SUFFIX = ".doc";
111:
112: /** The table which contains the actions */
113: private ActionMap actions;
114: protected ClassLoader classLoader; //LC MODIF
115:
116: /**
117: * Creates a new button factory
118: *
119: * @param rb
120: * the resource bundle that contains the buttons description.
121: * @param am
122: * the actions to bind to the button
123: */
124: public ButtonFactory(ResourceBundle rb, ActionMap am) {
125: super (rb);
126: actions = am;
127: classLoader = ClassLoaderUtils.getLocalClassLoader(); //LC MODIF
128: }
129:
130: /**
131: * Creates and returns a new swing button
132: *
133: * @param name
134: * the name of the button in the resource bundle
135: * @throws MissingResourceException
136: * if key is not the name of a button. It is not thrown if the
137: * mnemonic and the action keys are missing
138: * @throws ResourceFormatException
139: * if the mnemonic is not a single character
140: * @throws MissingListenerException
141: * if the button action is not found in the action map
142: */
143: public JButton createJButton(String name)
144: throws MissingResourceException, ResourceFormatException,
145: MissingListenerException {
146: JButton result;
147: try {
148: result = new JButton(getString(name + TEXT_SUFFIX));
149: } catch (MissingResourceException e) {
150: result = new JButton();
151: }
152: initializeButton(result, name);
153: return result;
154: }
155:
156: /**
157: * Creates and returns a new swing button initialised to be used as a
158: * toolbar button
159: *
160: * @param name
161: * the name of the button in the resource bundle
162: * @throws MissingResourceException
163: * if key is not the name of a button. It is not thrown if the
164: * mnemonic and the action keys are missing
165: * @throws ResourceFormatException
166: * if the mnemonic is not a single character
167: * @throws MissingListenerException
168: * if the button action is not found in the action map
169: */
170: public AbstractButton createJToolbarButton(final String name)
171: throws MissingResourceException, ResourceFormatException,
172: MissingListenerException {
173: // ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
174: // result = new JToolbarButton(getString(name + TEXT_SUFFIX)) Don't want this - don't know why it is here
175:
176: JButton result = new JToolbarButton() {
177: public Point getToolTipLocation(MouseEvent event) { // the tip MUST be touching the button if html because you can click on links
178: if (getToolTipText().startsWith("<html>"))
179: return new Point(0, getHeight() - 2);
180: else
181: return super .getToolTipLocation(event);
182: }
183:
184: public JToolTip createToolTip() {
185: if (getToolTipText().startsWith("<html>")) {
186: JToolTip tip = new HyperLinkToolTip();
187: tip.setComponent(this );
188: return tip;
189: } else {
190: return super .createToolTip();
191: }
192: }
193: };
194: initializeButton(result, name);
195: return result;
196: }
197:
198: /**
199: * Creates and returns a new swing radio button
200: *
201: * @param name
202: * the name of the button in the resource bundle
203: * @throws MissingResourceException
204: * if key is not the name of a button. It is not thrown if the
205: * mnemonic and the action keys are missing.
206: * @throws ResourceFormatException
207: * if the mnemonic is not a single character.
208: * @throws MissingListenerException
209: * if the button action is not found in the action map.
210: */
211: public JRadioButton createJRadioButton(String name)
212: throws MissingResourceException, ResourceFormatException,
213: MissingListenerException {
214: JRadioButton result = new JRadioButton(getString(name
215: + TEXT_SUFFIX));
216: initializeButton(result, name);
217:
218: // is the button selected?
219: try {
220: result.setSelected(getBoolean(name + SELECTED_SUFFIX));
221: } catch (MissingResourceException e) {
222: }
223:
224: return result;
225: }
226:
227: /**
228: * Creates and returns a new swing check box
229: *
230: * @param name
231: * the name of the button in the resource bundle
232: * @throws MissingResourceException
233: * if key is not the name of a button. It is not thrown if the
234: * mnemonic and the action keys are missing
235: * @throws ResourceFormatException
236: * if the mnemonic is not a single character.
237: * @throws MissingListenerException
238: * if the button action is not found in the action map.
239: */
240: public JCheckBox createJCheckBox(String name)
241: throws MissingResourceException, ResourceFormatException,
242: MissingListenerException {
243: JCheckBox result = new JCheckBox(getString(name + TEXT_SUFFIX));
244: initializeButton(result, name);
245:
246: // is the button selected?
247: try {
248: result.setSelected(getBoolean(name + SELECTED_SUFFIX));
249: } catch (MissingResourceException e) {
250: }
251:
252: return result;
253: }
254:
255: /**
256: * Initializes a button
257: *
258: * @param b
259: * the button to initialize
260: * @param name
261: * the button's name
262: * @throws ResourceFormatException
263: * if the mnemonic is not a single character.
264: * @throws MissingListenerException
265: * if the button action is not found in the action map.
266: */
267: protected void initializeButton(AbstractButton b, String name)
268: throws ResourceFormatException, MissingListenerException {
269: // Action
270: try {
271: Action a = actions
272: .getAction(getString(name + ACTION_SUFFIX));
273: if (a == null) {
274: throw new MissingListenerException("", "Action", name
275: + ACTION_SUFFIX);
276: }
277: b.setAction(a);
278: b.setText(getString(name + TEXT_SUFFIX));
279: if (a instanceof JComponentModifier) {
280: ((JComponentModifier) a).addJComponent(b);
281: }
282: } catch (MissingResourceException e) {
283: }
284:
285: // Icon
286: try {
287: String s = getString(name + ICON_SUFFIX);
288: URL url = classLoader.getResource(s); //LC MODIF
289: //URL url = actions.getClass().getResource(s);
290: if (url != null) {
291: b.setIcon(new ImageIcon(url));
292: }
293: } catch (MissingResourceException e) {
294: }
295:
296: // Mnemonic
297: try {
298: String str = getString(name + MNEMONIC_SUFFIX);
299: if (str.length() == 1) {
300: b.setMnemonic(str.charAt(0));
301: } else {
302: throw new ResourceFormatException("Malformed mnemonic",
303: bundle.getClass().getName(), name
304: + MNEMONIC_SUFFIX);
305: }
306: } catch (MissingResourceException e) {
307: }
308:
309: // ToolTip
310: try {
311: String s = getStringOrNull(name + TOOLTIP_SUFFIX);
312: if (s != null) {
313: String help = getStringOrNull(name + HELP_SUFFIX);
314: String demo = getStringOrNull(name + DEMO_SUFFIX);
315: String doc = getStringOrNull(name + DOC_SUFFIX);
316:
317: if (doc != null)
318: s = HyperLinkToolTip
319: .helpTipText(s, help, demo, doc);
320: b.setToolTipText(s);
321: }
322: } catch (MissingResourceException e) {
323: }
324: }
325:
326: }
|