f@0: /* f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool f@0: f@0: Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com) f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) f@0: f@0: This program is free software: you can redistribute it and/or modify f@0: it under the terms of the GNU General Public License as published by f@0: the Free Software Foundation, either version 3 of the License, or f@0: (at your option) any later version. f@0: f@0: This program is distributed in the hope that it will be useful, f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the f@0: GNU General Public License for more details. f@0: f@0: You should have received a copy of the GNU General Public License f@0: along with this program. If not, see . f@0: */ f@0: f@0: package uk.ac.qmul.eecs.ccmi.gui; f@0: f@0: import java.awt.event.ActionListener; f@0: import java.beans.EventHandler; f@0: import java.util.MissingResourceException; f@0: import java.util.ResourceBundle; f@0: f@0: import javax.swing.JMenu; f@0: import javax.swing.JMenuItem; f@0: import javax.swing.KeyStroke; f@0: f@0: /** f@0: * A factory class for swing components creation support. f@0: * f@0: * Components that are created via this class are, in turn created by {@code SpeechMenuFactory} methods. f@0: * This class handles the labelling and other menu properties (such as accelerators) f@0: * using the {@code ResourceBundle} passed as argument to the constructor. f@0: */ f@0: class ResourceFactory{ f@0: public ResourceFactory(ResourceBundle bundle){ f@0: this.bundle = bundle; f@0: } f@0: f@0: public JMenu createMenu(String prefix){ f@0: String text = bundle.getString(prefix + ".text"); f@0: JMenu menu = SpeechMenuFactory.getMenu(text); f@0: try{ f@0: String mnemonic = bundle.getString(prefix + ".mnemonic"); f@0: menu.setMnemonic(mnemonic.charAt(0)); f@0: }catch (MissingResourceException exception){ f@0: // ok not to set mnemonic f@0: } f@0: f@0: try{ f@0: String tooltip = bundle.getString(prefix + ".tooltip"); f@0: menu.setToolTipText(tooltip); f@0: } f@0: catch (MissingResourceException exception){ f@0: // ok not to set tooltip f@0: } f@0: return menu; f@0: } f@0: f@0: public JMenuItem createMenuItem(String prefix, f@0: Object target, String methodName){ f@0: return createMenuItem(prefix, f@0: (ActionListener) EventHandler.create( f@0: ActionListener.class, target, methodName)); f@0: } f@0: f@0: public JMenuItem createMenuItem(String prefix, f@0: ActionListener listener){ f@0: String text = bundle.getString(prefix + ".text"); f@0: JMenuItem menuItem = SpeechMenuFactory.getMenuItem(text); f@0: return configure(menuItem, prefix, listener); f@0: } f@0: f@0: public JMenuItem createCheckBoxMenuItem(String prefix, f@0: ActionListener listener){ f@0: String text = bundle.getString(prefix + ".text"); f@0: JMenuItem menuItem = SpeechMenuFactory.getJCheckBoxMenuItem(text); f@0: return configure(menuItem, prefix, listener); f@0: } f@0: f@0: private JMenuItem configure(JMenuItem menuItem, f@0: String prefix, ActionListener listener){ f@0: menuItem.addActionListener(listener); f@0: try{ f@0: String mnemonic = bundle.getString(prefix + ".mnemonic"); f@0: menuItem.setMnemonic(mnemonic.charAt(0)); f@0: }catch (MissingResourceException exception){ f@0: // ok not to set mnemonic f@0: } f@0: f@0: try{ f@0: String accelerator = bundle.getString(prefix + ".accelerator"); f@0: menuItem.setAccelerator(KeyStroke.getKeyStroke(accelerator)); f@0: }catch (MissingResourceException exception){ f@0: // ok not to set accelerator f@0: } f@0: f@0: try{ f@0: String tooltip = bundle.getString(prefix + ".tooltip"); f@0: menuItem.setToolTipText(tooltip); f@0: }catch (MissingResourceException exception){ f@0: // ok not to set tooltip f@0: } f@0: return menuItem; f@0: } f@0: f@0: private ResourceBundle bundle; f@0: }