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