1 package discRack; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.beans.*; 6 import java.net.URL ; 7 import java.util.*; 8 import java.io.*; 9 10 import javax.swing.*; 11 import javax.swing.undo.*; 12 import javax.swing.event.*; 13 import javax.swing.tree.*; 14 15 23 public class BarFactory extends JPanel { 24 25 public static final String imageSuffix = "Image"; 26 27 public static final String labelSuffix = "Label"; 28 29 public static final String actionSuffix = "Action"; 30 31 public static final String menuSuffix = "Menu"; 32 33 public static final String accelSuffix = "Accel"; 34 35 public static final String mnemonicSuffix = "Mnemonic"; 36 37 public static final String tipSuffix = "Tooltip"; 38 39 47 public static Component createToolBars(String toolbarToLoad,Map actions) { 48 String [] toolBars = Utils.tokenize(ResourceManager.getResourceString(toolbarToLoad)," "); 49 JPanel lastPanel = new JPanel(); 50 lastPanel.setLayout(new BorderLayout()); 51 JTabbedPane tabbedPane = new JTabbedPane(); 52 int i; 53 for (i = 0; i<toolBars.length; i++) { 54 if (!toolBars[i].equals("-")) { 56 String label = ResourceManager.getResourceString(toolBars[i]+labelSuffix); 57 final JPanel panel = new JPanel(); 58 panel.setLayout(new BorderLayout()); 59 final Component c = createToolbar(toolBars[i],actions); 60 panel.add(BorderLayout.WEST,c); 61 panel.add(c); 62 ImageIcon icon = null; 63 URL url = ResourceManager.getResource(toolBars[i]+imageSuffix); 64 if (url != null) { 65 icon = new ImageIcon(url); 66 } 67 tabbedPane.addTab(label,icon,panel,label); 68 } 69 else { 70 break; 71 } 72 } 73 tabbedPane.setPreferredSize(new Dimension(500,60)); 74 tabbedPane.setSelectedIndex(i-1); 75 76 lastPanel.add(BorderLayout.WEST,tabbedPane); 77 if (i<toolBars.length-1) { 78 Component c = createButton(toolBars[i+1],actions,false); 79 lastPanel.add(BorderLayout.EAST,c); 80 } 81 82 return lastPanel; 83 } 84 85 90 public static Component createToolbar(String key,Map actions) { 91 String label = ResourceManager.getResourceString(key+labelSuffix); 92 JToolBar toolbar = new JToolBar(label); 93 toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE); 94 toolbar.setFloatable(false); 95 String [] toolKeys = Utils.tokenize(ResourceManager.getResourceString(key)," "); 96 for (int i = 0; i < toolKeys.length; i++) { 97 if (toolKeys[i].equals("-")) { 98 toolbar.add(Box.createHorizontalStrut(10)); 99 } else { 100 toolbar.add(createButton(toolKeys[i],actions,false)); 101 } 102 } 103 toolbar.add(Box.createHorizontalGlue()); 104 return toolbar; 105 } 106 107 117 public static Component createButton(String key,Map actions,boolean setText) { 118 AbstractButton b = null; 119 URL url = ResourceManager.getResource(key + imageSuffix); 120 if (url!=null) { 121 b = new JButton(new ImageIcon(url)) { 122 public float getAlignmentY() { return 0.5f; } 123 }; 124 if (setText) { 125 b.setText(ResourceManager.getResourceString(key+labelSuffix)); 126 } 127 } else { 128 b = new JButton(ResourceManager.getResourceString(key+labelSuffix)) { 129 public float getAlignmentY() { return 0.5f; } 130 }; 131 } 132 133 b.setMargin(new Insets(1,1,1,1)); 134 b.setRequestFocusEnabled(false); 135 136 String astr = ResourceManager.getResourceString(key + actionSuffix); 137 if (astr == null) { 138 astr = key; 139 } 140 Action a = (Action)actions.get(astr); 141 if (a != null) { 142 b.setActionCommand(astr); 143 b.addActionListener(a); 144 a.addPropertyChangeListener(createActionChangeListener(b)); 145 b.setEnabled(a.isEnabled()); 146 } else { 147 b.setEnabled(false); 148 } 149 150 151 String tip = ResourceManager.getResourceString(key + tipSuffix); 152 if (tip != null) { 153 b.setToolTipText(tip); 154 } 155 return b; 156 157 } 158 159 164 public static JMenuBar createMenubar(String menubarToLoad,Map actions) { 165 JMenuItem mi; 166 JMenuBar mb = new JMenuBar(); 167 168 String [] menuKeys = Utils.tokenize(ResourceManager.getResourceString(menubarToLoad)," "); 169 for (int i = 0; i < menuKeys.length; i++) { 170 String [] itemKeys = Utils.tokenize(ResourceManager.getResourceString(menuKeys[i])," "); 171 JMenu m = createMenu(menuKeys[i],itemKeys,actions); 172 if (m != null) { 173 mb.add(m); 174 } 175 } 176 177 return mb; 178 } 179 180 183 public static JMenu createMenu(String key,String [] itemKeys,Map actions) { 184 JMenu menu = new JMenu(ResourceManager.getResourceString(key + labelSuffix)); 185 for (int i = 0; i < itemKeys.length; i++) { 186 if (itemKeys[i].equals("-")) { 187 menu.addSeparator(); 188 } else { 189 JMenuItem mi = createMenuItem(itemKeys[i],actions); 190 menu.add(mi); 191 } 192 } 193 URL url = ResourceManager.getResource(key + imageSuffix); 194 if (url != null) { 195 menu.setHorizontalTextPosition(JButton.RIGHT); 196 menu.setIcon(new ImageIcon(url)); 197 } 198 199 setMnemonic(menu,ResourceManager.getResourceString(key+mnemonicSuffix)); 200 201 menu.setActionCommand(key); 202 return menu; 203 } 204 205 209 public static JMenuItem createMenuItem(String cmd,Map actions) { 210 String subMenu = ResourceManager.getResourceString(cmd + menuSuffix); 211 if (subMenu != null) { 212 String [] itemKeys = Utils.tokenize(subMenu," "); 213 JMenu mn=createMenu(cmd,itemKeys,actions); 214 return mn; 215 } else { 216 JMenuItem mi; 217 mi = new JMenuItem(ResourceManager.getResourceString(cmd + labelSuffix)); 218 URL url = ResourceManager.getResource(cmd + imageSuffix); 219 if (url != null) { 220 mi.setHorizontalTextPosition(JButton.RIGHT); 221 mi.setIcon(new ImageIcon(url)); 222 } 223 setAccelerator(mi,ResourceManager.getResourceString(cmd+accelSuffix)); 224 setMnemonic(mi,ResourceManager.getResourceString(cmd+mnemonicSuffix)); 225 226 String astr = ResourceManager.getResourceString(cmd + actionSuffix); 227 if (astr == null) { 228 astr = cmd; 229 } 230 mi.setActionCommand(astr); 231 Action a = (Action)actions.get(astr); 232 if (a != null) { 233 mi.addActionListener(a); 234 a.addPropertyChangeListener(createActionChangeListener(mi)); 235 mi.setEnabled(a.isEnabled()); 236 } 237 return mi; 238 } 239 } 240 241 public static void setMnemonic (JMenuItem mi,String mnemonic) { 242 if (mnemonic != null && mnemonic.length() > 0) { 243 mi.setMnemonic(mnemonic.charAt(0)); 244 } 245 } 246 247 public static void setAccelerator (JMenuItem mi,String accel) { 248 if (accel != null) { 249 try { 250 int mask = 0; 251 if (accel.startsWith("CTRL")) { 252 mask += ActionEvent.CTRL_MASK; 253 accel = accel.substring(5); 254 } 255 if (accel.startsWith("SHIFT")) { 256 mask += ActionEvent.SHIFT_MASK; 257 accel = accel.substring(6); 258 } 259 if (accel.startsWith("ALT")) { 260 mask += ActionEvent.ALT_MASK; 261 accel = accel.substring(4); 262 } 263 int key = KeyEvent.class.getField("VK_"+accel).getInt(null); 264 mi.setAccelerator(KeyStroke.getKeyStroke(key, mask)); 265 } catch (Exception e) { 266 System.err.println("Error while assigning accelerator !!!"); 267 } 268 } 269 } 270 271 276 public static Component createButtonPanel(String key,Map actions) { 277 String label = ResourceManager.getResourceString(key+labelSuffix); 278 JPanel p = new JPanel(); 279 String [] buttonKeys = Utils.tokenize(ResourceManager.getResourceString(key)," "); 280 for (int i = 0; i < buttonKeys.length; i++) { 281 if (buttonKeys[i].equals("-")) { 282 p.add(Box.createHorizontalStrut(5)); 283 } else { 284 p.add(createButton(buttonKeys[i],actions,true)); 285 } 286 } 287 return p; 288 } 289 290 291 296 private static class ActionChangedListener implements PropertyChangeListener { 297 AbstractButton button; 298 299 ActionChangedListener(AbstractButton b) { 300 super(); 301 button = b; 302 } 303 public void propertyChange(PropertyChangeEvent e) { 304 String propertyName = e.getPropertyName(); 305 if (e.getPropertyName().equals(Action.NAME) && 306 button instanceof JMenuItem) { 307 String text = (String )e.getNewValue(); 308 button.setText(text); 309 } else { 310 if (propertyName.equals("enabled")) { 311 Boolean enabledState = (Boolean )e.getNewValue(); 312 button.setEnabled(enabledState.booleanValue()); 313 } 314 } 315 } 316 } 317 321 private static PropertyChangeListener createActionChangeListener(AbstractButton b) { 322 return new BarFactory.ActionChangedListener(b); 323 } 324 325 } 326 | Popular Tags |