1 7 package org.jdesktop.swing.actions; 8 9 import java.util.Arrays ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 import javax.swing.AbstractButton ; 16 import javax.swing.Action ; 17 import javax.swing.ButtonGroup ; 18 import javax.swing.Icon ; 19 import javax.swing.JButton ; 20 import javax.swing.JCheckBoxMenuItem ; 21 import javax.swing.JComponent ; 22 import javax.swing.JMenu ; 23 import javax.swing.JMenuBar ; 24 import javax.swing.JMenuItem ; 25 import javax.swing.JPopupMenu ; 26 import javax.swing.JRadioButtonMenuItem ; 27 import javax.swing.JToggleButton ; 28 import javax.swing.JToolBar ; 29 30 56 public class ActionContainerFactory { 57 58 private ActionManager manager; 59 60 private Map groupMap; 62 63 69 public ActionContainerFactory(ActionManager manager) { 70 setActionManager(manager); 71 } 72 73 80 public ActionManager getActionManager() { 81 if (manager == null) { 82 manager = ActionManager.getInstance(); 83 } 84 return manager; 85 } 86 87 91 public void setActionManager(ActionManager manager) { 92 ActionManager oldManager = this.manager; 93 if (oldManager != null) { 94 oldManager.setFactory(null); 95 } 96 this.manager = manager; 97 98 if (manager != null) { 99 manager.setFactory(this); 100 } 101 } 102 103 110 private JToolBar createToolBar(Object [] list) { 111 return createToolBar(Arrays.asList(list)); 112 } 113 114 121 public JToolBar createToolBar(List list) { 122 JToolBar toolbar = new JToolBar (); 123 Iterator iter = list.iterator(); 124 while(iter.hasNext()) { 125 Object element = iter.next(); 126 127 if (element == null) { 128 toolbar.addSeparator(); 129 } else { 130 AbstractButton button = createButton(element, toolbar); 131 button.setFocusable(false); 133 toolbar.add(button); 134 } 135 } 136 return toolbar; 137 } 138 139 140 146 private JPopupMenu createPopup(Object [] list) { 147 return createPopup(Arrays.asList(list)); 148 } 149 150 156 public JPopupMenu createPopup(List list) { 157 JPopupMenu popup = new JPopupMenu (); 158 Iterator iter = list.iterator(); 159 while(iter.hasNext()) { 160 Object element = iter.next(); 161 162 if (element == null) { 163 popup.addSeparator(); 164 } else if (element instanceof List ) { 165 JMenu newMenu= createMenu((List )element); 166 if (newMenu!= null) { 167 popup.add(newMenu); 168 } 169 } else { 170 popup.add(createMenuItem(element, popup)); 171 } 172 } 173 return popup; 174 } 175 176 182 public JMenuBar createMenuBar(List list) { 183 JMenuBar menubar = new JMenuBar (); 184 JMenu menu = null; 185 186 Iterator iter = list.iterator(); 187 while(iter.hasNext()) { 188 Object element = iter.next(); 189 190 if (element == null) { 191 if (menu != null) { 192 menu.addSeparator(); 193 } 194 } else if (element instanceof List ) { 195 menu = createMenu((List )element); 196 if (menu != null) { 197 menubar.add(menu); 198 } 199 } else { 200 if (menu != null) { 201 menu.add(createMenuItem(element, menu)); 202 } 203 } 204 } 205 return menubar; 206 } 207 208 209 219 public JMenu createMenu(List list) { 220 Action action = getAction(list.get(0)); 222 if (action == null) { 223 return null; 224 } 225 JMenu menu = new JMenu (action); 226 227 Iterator iter = list.listIterator(1); 229 while(iter.hasNext()) { 230 Object element = iter.next(); 231 if (element == null) { 232 menu.addSeparator(); 233 } else if (element instanceof List ) { 234 JMenu newMenu = createMenu((List )element); 235 if (newMenu != null) { 236 menu.add(newMenu); 237 } 238 } else { 239 menu.add(createMenuItem(element, menu)); 240 } 241 } 242 return menu; 243 } 244 245 246 249 private Action getAction(Object id) { 250 Action action = getActionManager().getAction(id); 251 if (action == null) { 252 throw new RuntimeException ("ERROR: No Action for " + id); 253 } 254 return action; 255 } 256 257 263 private ButtonGroup getGroup(String groupid, JComponent container) { 264 if (groupMap == null) { 265 groupMap = new HashMap (); 266 } 267 int intCode = groupid.hashCode(); 268 if (container != null) { 269 intCode ^= container.hashCode(); 270 } 271 Integer hashCode = new Integer (intCode); 272 273 ButtonGroup group = (ButtonGroup )groupMap.get(hashCode); 274 if (group == null) { 275 group = new ButtonGroup (); 276 groupMap.put(hashCode, group); 277 } 278 return group; 279 } 280 281 288 private JMenuItem createMenuItem(Object id, JComponent container) { 289 return createMenuItem(getAction(id), container); 290 } 291 292 293 302 public JMenuItem createMenuItem(Action action, JComponent container) { 303 JMenuItem menuItem = null; 304 if (action instanceof AbstractActionExt) { 305 AbstractActionExt ta = (AbstractActionExt)action; 306 307 if (ta.isStateAction()) { 308 String groupid = (String )ta.getGroup(); 309 if (groupid != null) { 310 menuItem = createRadioButtonMenuItem(getGroup(groupid, container), 313 (AbstractActionExt)action); 314 } else { 315 menuItem = createCheckBoxMenuItem((AbstractActionExt)action); 316 } 317 } 318 } 319 320 if (menuItem == null) { 321 menuItem= new JMenuItem (action); 322 configureMenuItem(menuItem, action); 323 } 324 return menuItem; 325 } 326 327 335 public JMenuItem createMenuItem(Action action) { 336 return createMenuItem(action, null); 337 } 338 339 340 344 private AbstractButton createButton(Object id, JComponent container) { 345 return createButton(getAction(id), container); 346 } 347 348 359 public AbstractButton createButton(Action action, JComponent container) { 360 if (action == null) { 361 return null; 362 } 363 364 AbstractButton button = null; 365 if (action instanceof AbstractActionExt) { 366 AbstractActionExt ta = (AbstractActionExt)action; 368 369 if (ta.isStateAction()) { 370 String groupid = (String )ta.getGroup(); 373 if (groupid == null) { 374 button = createToggleButton(ta); 375 } else { 376 button = createToggleButton(ta, getGroup(groupid, container)); 377 } 378 } 379 } 380 381 if (button == null) { 382 button = new JButton (action); 384 configureButton(button, action); 385 } 386 return button; 387 } 388 389 395 public AbstractButton createButton(Action action) { 396 return createButton(action, null); 397 } 398 399 403 private JToggleButton createToggleButton(AbstractActionExt a) { 404 return createToggleButton(a, null); 405 } 406 407 412 private JToggleButton createToggleButton(AbstractActionExt a, ButtonGroup group) { 413 JToggleButton button = new JToggleButton (a); 414 button.addItemListener(a); 415 button.setSelected(a.isSelected()); 416 if (group != null) { 417 group.add(button); 418 } 419 configureToggleButton(button, a); 420 return button; 421 } 422 423 431 protected void configureToggleButton(JToggleButton button, Action action) { 432 configureButton(button, action); 433 434 action.addPropertyChangeListener(new ToggleActionPropertyChangeListener(button)); 439 } 440 441 442 449 protected void configureButton(AbstractButton button, Action action) { 450 if (action.getValue(Action.SHORT_DESCRIPTION) == null) { 451 button.setToolTipText((String )action.getValue(Action.NAME)); 452 } 453 if (action.getValue(AbstractActionExt.LARGE_ICON) != null) { 455 button.setIcon((Icon )action.getValue(AbstractActionExt.LARGE_ICON)); 456 } 457 if (button.getIcon() != null) { 459 button.setText(""); 460 } 461 } 462 463 472 protected void configureToggleMenuItem(JMenuItem menuItem, Action action) { 473 configureMenuItem(menuItem, action); 474 475 action.addPropertyChangeListener(new ToggleActionPropertyChangeListener(menuItem)); 480 } 481 482 483 490 protected void configureMenuItem(JMenuItem menuItem, Action action) { 491 } 492 493 496 private JCheckBoxMenuItem createCheckBoxMenuItem(AbstractActionExt a) { 497 JCheckBoxMenuItem mi = new JCheckBoxMenuItem (a); 498 mi.addItemListener(a); 499 mi.setSelected(a.isSelected()); 500 501 configureToggleMenuItem(mi, a); 502 return mi; 503 } 504 505 508 private JRadioButtonMenuItem createRadioButtonMenuItem(ButtonGroup group, 509 AbstractActionExt a) { 510 JRadioButtonMenuItem mi = new JRadioButtonMenuItem (a); 511 mi.addItemListener(a); 512 mi.setSelected(a.isSelected()); 513 if (group != null) { 514 group.add(mi); 515 } 516 configureToggleMenuItem(mi, a); 517 return mi; 518 } 519 } 520 | Popular Tags |