1 18 package org.apache.batik.util.gui.resource; 19 20 import java.net.URL ; 21 import java.util.MissingResourceException ; 22 import java.util.ResourceBundle ; 23 24 import javax.swing.AbstractButton ; 25 import javax.swing.Action ; 26 import javax.swing.ImageIcon ; 27 import javax.swing.JButton ; 28 import javax.swing.JCheckBox ; 29 import javax.swing.JRadioButton ; 30 31 51 public class ButtonFactory extends ResourceManager { 52 private final static String ICON_SUFFIX = ".icon"; 55 private final static String TEXT_SUFFIX = ".text"; 56 private final static String MNEMONIC_SUFFIX = ".mnemonic"; 57 private final static String ACTION_SUFFIX = ".action"; 58 private final static String SELECTED_SUFFIX = ".selected"; 59 private final static String TOOLTIP_SUFFIX = ".tooltip"; 60 61 62 private ActionMap actions; 63 64 70 public ButtonFactory(ResourceBundle rb, ActionMap am) { 71 super(rb); 72 actions = am; 73 } 74 75 85 public JButton createJButton(String name) 86 throws MissingResourceException , 87 ResourceFormatException, 88 MissingListenerException { 89 JButton result; 90 try { 91 result = new JButton (getString(name+TEXT_SUFFIX)); 92 } catch (MissingResourceException e) { 93 result = new JButton (); 94 } 95 initializeButton(result, name); 96 return result; 97 } 98 99 110 public JButton createJToolbarButton(String name) 111 throws MissingResourceException , 112 ResourceFormatException, 113 MissingListenerException { 114 JButton result; 115 try { 116 result = new JToolbarButton(getString(name+TEXT_SUFFIX)); 117 } catch (MissingResourceException e) { 118 result = new JToolbarButton(); 119 } 120 initializeButton(result, name); 121 return result; 122 } 123 124 135 public JRadioButton createJRadioButton(String name) 136 throws MissingResourceException , 137 ResourceFormatException, 138 MissingListenerException { 139 JRadioButton result = new JRadioButton (getString(name+TEXT_SUFFIX)); 140 initializeButton(result, name); 141 142 try { 144 result.setSelected(getBoolean(name+SELECTED_SUFFIX)); 145 } catch (MissingResourceException e) { 146 } 147 148 return result; 149 } 150 151 161 public JCheckBox createJCheckBox(String name) 162 throws MissingResourceException , 163 ResourceFormatException, 164 MissingListenerException { 165 JCheckBox result = new JCheckBox (getString(name+TEXT_SUFFIX)); 166 initializeButton(result, name); 167 168 try { 170 result.setSelected(getBoolean(name+SELECTED_SUFFIX)); 171 } catch (MissingResourceException e) { 172 } 173 174 return result; 175 } 176 177 186 private void initializeButton(AbstractButton b, String name) 187 throws ResourceFormatException, MissingListenerException { 188 try { 190 Action 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 mre) { 199 } 202 if (a instanceof JComponentModifier) { 203 ((JComponentModifier)a).addJComponent(b); 204 } 205 } catch (MissingResourceException e) { 206 } 207 208 try { 210 String s = getString(name+ICON_SUFFIX); 211 URL url = actions.getClass().getResource(s); 212 if (url != null) { 213 b.setIcon(new ImageIcon (url)); 214 } 215 } catch (MissingResourceException e) { 216 } 217 218 try { 220 String 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 e) { 229 } 230 231 try { 233 String s = getString(name+TOOLTIP_SUFFIX); 234 if (s != null) { 235 b.setToolTipText(s); 236 } 237 } catch (MissingResourceException e) { 238 } 239 } 240 } 241 | Popular Tags |