Clover coverage report - groovy - 1.0-beta-6
Coverage timestamp: Thu Jul 15 2004 13:18:22 BST
file stats: LOC: 717   Methods: 34
NCLOC: 604   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
SwingBuilder.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  $Id: SwingBuilder.java,v 1.8 2004/07/14 19:12:39 glaforge Exp $
 3   
 
 4   
  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
 5   
 
 6   
  Redistribution and use of this software and associated documentation
 7   
  ("Software"), with or without modification, are permitted provided
 8   
  that the following conditions are met:
 9   
 
 10   
  1. Redistributions of source code must retain copyright
 11   
     statements and notices.  Redistributions must also contain a
 12   
     copy of this document.
 13   
 
 14   
  2. Redistributions in binary form must reproduce the
 15   
     above copyright notice, this list of conditions and the
 16   
     following disclaimer in the documentation and/or other
 17   
     materials provided with the distribution.
 18   
 
 19   
  3. The name "groovy" must not be used to endorse or promote
 20   
     products derived from this Software without prior written
 21   
     permission of The Codehaus.  For written permission,
 22   
     please contact info@codehaus.org.
 23   
 
 24   
  4. Products derived from this Software may not be called "groovy"
 25   
     nor may "groovy" appear in their names without prior written
 26   
     permission of The Codehaus. "groovy" is a registered
 27   
     trademark of The Codehaus.
 28   
 
 29   
  5. Due credit should be given to The Codehaus -
 30   
     http://groovy.codehaus.org/
 31   
 
 32   
  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
 33   
  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 34   
  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 35   
  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 36   
  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 37   
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 38   
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 39   
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 40   
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 41   
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 42   
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 43   
  OF THE POSSIBILITY OF SUCH DAMAGE.
 44   
 
 45   
  */
 46   
 package groovy.swing;
 47   
 
 48   
 import groovy.lang.Closure;
 49   
 import groovy.model.DefaultTableModel;
 50   
 import groovy.model.ValueHolder;
 51   
 import groovy.model.ValueModel;
 52   
 import groovy.swing.impl.ComponentFacade;
 53   
 import groovy.swing.impl.ContainerFacade;
 54   
 import groovy.swing.impl.DefaultAction;
 55   
 import groovy.swing.impl.Factory;
 56   
 import groovy.swing.impl.Startable;
 57   
 import groovy.swing.impl.TableLayout;
 58   
 import groovy.swing.impl.TableLayoutCell;
 59   
 import groovy.swing.impl.TableLayoutRow;
 60   
 import groovy.util.BuilderSupport;
 61   
 
 62   
 import java.awt.Component;
 63   
 import java.awt.Container;
 64   
 import java.awt.Dimension;
 65   
 import java.awt.Dialog;
 66   
 import java.awt.Frame;
 67   
 import java.awt.LayoutManager;
 68   
 import java.util.ArrayList;
 69   
 import java.util.Collections;
 70   
 import java.util.HashMap;
 71   
 import java.util.HashSet;
 72   
 import java.util.Iterator;
 73   
 import java.util.List;
 74   
 import java.util.Map;
 75   
 import java.util.Set;
 76   
 import java.util.Vector;
 77   
 import java.util.logging.Level;
 78   
 import java.util.logging.Logger;
 79   
 
 80   
 import javax.swing.AbstractButton;
 81   
 import javax.swing.Action;
 82   
 import javax.swing.Box;
 83   
 import javax.swing.BoxLayout;
 84   
 import javax.swing.ButtonGroup;
 85   
 import javax.swing.JButton;
 86   
 import javax.swing.JCheckBox;
 87   
 import javax.swing.JCheckBoxMenuItem;
 88   
 import javax.swing.JComboBox;
 89   
 import javax.swing.JComponent;
 90   
 import javax.swing.JDesktopPane;
 91   
 import javax.swing.JDialog;
 92   
 import javax.swing.JEditorPane;
 93   
 import javax.swing.JFileChooser;
 94   
 import javax.swing.JFrame;
 95   
 import javax.swing.JInternalFrame;
 96   
 import javax.swing.JLabel;
 97   
 import javax.swing.JList;
 98   
 import javax.swing.JMenu;
 99   
 import javax.swing.JMenuBar;
 100   
 import javax.swing.JMenuItem;
 101   
 import javax.swing.JOptionPane;
 102   
 import javax.swing.JPanel;
 103   
 import javax.swing.JPasswordField;
 104   
 import javax.swing.JPopupMenu;
 105   
 import javax.swing.JProgressBar;
 106   
 import javax.swing.JRadioButton;
 107   
 import javax.swing.JRadioButtonMenuItem;
 108   
 import javax.swing.JScrollPane;
 109   
 import javax.swing.JSeparator;
 110   
 import javax.swing.JSplitPane;
 111   
 import javax.swing.JTabbedPane;
 112   
 import javax.swing.JTable;
 113   
 import javax.swing.JTextArea;
 114   
 import javax.swing.JTextField;
 115   
 import javax.swing.JTextPane;
 116   
 import javax.swing.JToggleButton;
 117   
 import javax.swing.JToolBar;
 118   
 import javax.swing.JTree;
 119   
 import javax.swing.KeyStroke;
 120   
 import javax.swing.RootPaneContainer;
 121   
 import javax.swing.table.TableColumn;
 122   
 import javax.swing.table.TableModel;
 123   
 
 124   
 import org.codehaus.groovy.runtime.InvokerHelper;
 125   
 
 126   
 /**
 127   
  * A helper class for creating Swing widgets using GroovyMarkup
 128   
  * 
 129   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 130   
  * @version $Revision: 1.8 $
 131   
  */
 132   
 public class SwingBuilder extends BuilderSupport {
 133   
 
 134   
     private Logger log = Logger.getLogger(getClass().getName());
 135   
     private Map factories = new HashMap();
 136   
     private Object constraints;
 137   
     private Map passThroughNodes = new HashMap();
 138   
 
 139  0
     public SwingBuilder() {
 140  0
         registerWidgets();
 141   
     }
 142   
 
 143  0
     protected void setParent(Object parent, Object child) {
 144  0
         if (child instanceof Action) {
 145  0
             Action action = (Action) child;
 146  0
             try {
 147  0
                 InvokerHelper.setProperty(parent, "action", action);
 148   
             } catch (RuntimeException re) {
 149   
                 // must not have an action property...
 150   
                 // so we ignore it and go on
 151   
             }
 152  0
             Object keyStroke = action.getValue("KeyStroke");
 153   
             //System.out.println("keystroke: " + keyStroke + " for: " + action);
 154  0
             if (parent instanceof JComponent) {
 155  0
                 JComponent component = (JComponent) parent;
 156  0
                 KeyStroke stroke = null;
 157  0
                 if (keyStroke instanceof String) {
 158  0
                     stroke = KeyStroke.getKeyStroke((String) keyStroke);
 159   
                 }
 160  0
                 else if (keyStroke instanceof KeyStroke) {
 161  0
                     stroke = (KeyStroke) keyStroke;
 162   
                 }
 163  0
                 if (stroke != null) {
 164  0
                     String key = action.toString();
 165  0
                     component.getInputMap().put(stroke, key);
 166  0
                     component.getActionMap().put(key, action);
 167   
                 }
 168   
             }
 169   
         }
 170  0
         else if (child instanceof LayoutManager) {
 171  0
             if (parent instanceof RootPaneContainer) {
 172  0
                 RootPaneContainer rpc = (RootPaneContainer) parent;
 173  0
                 parent = rpc.getContentPane();
 174   
             }
 175  0
             InvokerHelper.setProperty(parent, "layout", child);
 176   
         }
 177  0
         else if (parent instanceof JTable && child instanceof TableColumn) {
 178  0
             JTable table = (JTable) parent;
 179  0
             TableColumn column = (TableColumn) child;
 180  0
             table.addColumn(column);
 181   
         }
 182  0
         else if (parent instanceof JTabbedPane && child instanceof Component) {
 183  0
             JTabbedPane tabbedPane = (JTabbedPane) parent;
 184  0
             tabbedPane.add((Component)child);
 185   
         }
 186   
         else {
 187  0
             Component component = null;
 188  0
             if (child instanceof Component) {
 189  0
                 component = (Component) child;
 190   
             }
 191  0
             else if (child instanceof ComponentFacade) {
 192  0
                 ComponentFacade facade = (ComponentFacade) child;
 193  0
                 component = facade.getComponent();
 194   
             }
 195  0
             if (component != null) {
 196  0
                 if (parent instanceof JFrame && component instanceof JMenuBar) {
 197  0
                     JFrame frame = (JFrame) parent;
 198  0
                     frame.setJMenuBar((JMenuBar) component);
 199   
                 }
 200  0
                 else if (parent instanceof RootPaneContainer) {
 201  0
                     RootPaneContainer rpc = (RootPaneContainer) parent;
 202  0
                     rpc.getContentPane().add(component);
 203   
                 }
 204  0
                 else if (parent instanceof JScrollPane) {
 205  0
                     JScrollPane scrollPane = (JScrollPane) parent;
 206  0
                     scrollPane.setViewportView(component);
 207   
                 }
 208  0
                 else if (parent instanceof JSplitPane) {
 209  0
                     JSplitPane splitPane = (JSplitPane) parent;
 210  0
                     if (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
 211  0
                         if (splitPane.getTopComponent() == null) {
 212  0
                             splitPane.setTopComponent(component);
 213   
                         }
 214   
                         else {
 215  0
                             splitPane.setBottomComponent(component);
 216   
                         }
 217   
                     }
 218   
                     else {
 219  0
                         if (splitPane.getLeftComponent() == null) {
 220  0
                             splitPane.setLeftComponent(component);
 221   
                         }
 222   
                         else {
 223  0
                             splitPane.setRightComponent(component);
 224   
                         }
 225   
                     }
 226   
                 }
 227  0
                 else if (parent instanceof JMenuBar && component instanceof JMenu) {
 228  0
                     JMenuBar menuBar = (JMenuBar) parent;
 229  0
                     menuBar.add((JMenu) component);
 230   
                 }
 231  0
                 else if (parent instanceof Container) {
 232  0
                     Container container = (Container) parent;
 233  0
                     if (constraints != null) {
 234  0
                         container.add(component, constraints);
 235   
                     }
 236   
                     else {
 237  0
                         container.add(component);
 238   
                     }
 239   
                 }
 240  0
                 else if (parent instanceof ContainerFacade) {
 241  0
                     ContainerFacade facade = (ContainerFacade) parent;
 242  0
                     facade.addComponent(component);
 243   
                 }
 244   
             }
 245   
         }
 246   
     }
 247   
 
 248  0
     protected void nodeCompleted(Object parent, Object node) {
 249   
         // set models after the node has been completed
 250  0
         if (node instanceof TableModel && parent instanceof JTable) {
 251  0
             JTable table = (JTable) parent;
 252  0
             TableModel model = (TableModel) node;
 253  0
             table.setModel(model);
 254   
         }
 255  0
         if (node instanceof Startable) {
 256  0
             Startable startable = (Startable) node;
 257  0
             startable.start();
 258   
         }
 259   
     }
 260   
 
 261  0
     protected Object createNode(Object name) {
 262  0
         return createNode(name, Collections.EMPTY_MAP);
 263   
     }
 264   
 
 265  0
     protected Object createNode(Object name, Object value) {
 266  0
         if (passThroughNodes.containsKey(name) && (value != null) && ((Class)passThroughNodes.get(name)).isAssignableFrom(value.getClass())) {
 267  0
             return value;
 268   
         }
 269   
         else {
 270  0
             Object widget = createNode(name);
 271  0
             if (widget != null && value instanceof String) {
 272  0
                 InvokerHelper.invokeMethod(widget, "setText", value);
 273   
             }
 274  0
             return widget;
 275   
         }
 276   
     }
 277   
 
 278  0
     protected Object createNode(Object name, Map attributes, Object value) {
 279  0
         if (passThroughNodes.containsKey(name) && (value != null) && ((Class)passThroughNodes.get(name)).isAssignableFrom(value.getClass())) {
 280  0
             handleWidgetAttributes(value, attributes);
 281  0
             return value;
 282   
         }
 283   
         else { 
 284  0
             Object widget = createNode(name, attributes);
 285  0
             if (widget != null) {
 286  0
                 InvokerHelper.invokeMethod(widget, "setText", value.toString());
 287   
             }
 288  0
             return widget;
 289   
         }
 290   
     }
 291   
     
 292  0
     protected Object createNode(Object name, Map attributes) {
 293  0
         constraints = attributes.remove("constraints");
 294  0
         Object widget = null;
 295  0
         if (passThroughNodes.containsKey(name)) {
 296  0
             widget = attributes.get(name);
 297  0
             if ((widget != null) && ((Class)passThroughNodes.get(name)).isAssignableFrom(widget.getClass())) {
 298  0
                 attributes.remove(name);
 299   
             }
 300   
             else {
 301  0
                 widget = null;
 302   
             }
 303   
         }
 304  0
         if (widget == null) {
 305  0
             Factory factory = (Factory) factories.get(name);
 306  0
             if (factory != null) {
 307  0
                 try {
 308  0
                     widget = factory.newInstance(attributes);
 309  0
                     if (widget == null) {
 310  0
                         log.log(Level.WARNING, "Factory for name: " + name + " returned null");
 311   
                     }
 312   
                     else {
 313  0
                         if (log.isLoggable(Level.FINE)) {
 314  0
                             log.fine("For name: " + name + " created widget: " + widget);
 315   
                         }
 316   
                     }
 317   
                 }
 318   
                 catch (Exception e) {
 319  0
                     throw new RuntimeException("Failed to create component for" + name + " reason: " + e, e);
 320   
                 }
 321   
             }
 322   
             else {
 323  0
                 log.log(Level.WARNING, "Could not find match for name: " + name);
 324   
             }
 325   
         }
 326  0
         handleWidgetAttributes(widget, attributes);
 327  0
         return widget;
 328   
     }
 329   
 
 330  0
     protected void handleWidgetAttributes(Object widget, Map attributes) {
 331  0
         if (widget != null) {
 332  0
             if (widget instanceof Action) {
 333   
                 /** @todo we could move this custom logic into the MetaClass for Action */
 334  0
                 Action action = (Action) widget;
 335   
 
 336  0
                 Closure closure = (Closure) attributes.remove("closure");
 337  0
                 if (closure != null && action instanceof DefaultAction) {
 338  0
                     DefaultAction defaultAction = (DefaultAction) action;
 339  0
                     defaultAction.setClosure(closure);
 340   
                 }
 341   
 
 342  0
                 Object accel = attributes.remove("accelerator");
 343  0
                 KeyStroke stroke = null;
 344  0
                 if (accel instanceof KeyStroke) {
 345  0
                     stroke = (KeyStroke) accel;
 346  0
                 } else if (accel != null) {
 347  0
                     stroke = KeyStroke.getKeyStroke(accel.toString());
 348   
                 }
 349  0
                 action.putValue(Action.ACCELERATOR_KEY, stroke);
 350   
 
 351  0
                 Object mnemonic = attributes.remove("mnemonic");
 352  0
                 if ((mnemonic != null) && !(mnemonic instanceof Number)) {
 353  0
                     mnemonic = new Integer(mnemonic.toString().charAt(0));
 354   
                 }
 355  0
                 action.putValue(Action.MNEMONIC_KEY, mnemonic);
 356   
 
 357  0
                 for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
 358  0
                     Map.Entry entry = (Map.Entry) iter.next();
 359  0
                     String actionName = (String) entry.getKey();
 360   
 
 361   
                     // typically standard Action names start with upper case, so lets upper case it            
 362  0
                     actionName = capitalize(actionName);
 363  0
                     Object value = entry.getValue();
 364   
 
 365  0
                     action.putValue(actionName, value);
 366   
                 }
 367   
 
 368   
             }
 369   
             else {
 370   
                 // some special cases...
 371  0
                 if (attributes.containsKey("buttonGroup")) {
 372  0
                     Object o = attributes.get("buttonGroup");
 373  0
                     if ((o instanceof ButtonGroup) && (widget instanceof AbstractButton)) {
 374  0
                         ((AbstractButton)widget).getModel().setGroup((ButtonGroup)o);
 375  0
                         attributes.remove("buttonGroup");
 376   
                     }
 377   
                 }
 378   
 
 379   
                 // this next statement nd if/else is a workaround until GROOVY-305 is fixed
 380  0
                 Object mnemonic = attributes.remove("mnemonic");
 381  0
                 if ((mnemonic != null) && (mnemonic instanceof Number)) {
 382  0
                     InvokerHelper.setProperty(widget, "mnemonic", new Character((char)((Number)mnemonic).intValue()));
 383   
                 } 
 384  0
                 else if (mnemonic != null) {
 385  0
                     InvokerHelper.setProperty(widget, "mnemonic", new Character(mnemonic.toString().charAt(0)));
 386   
                 } 
 387   
 
 388   
                 // set the properties
 389  0
                 for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
 390  0
                     Map.Entry entry = (Map.Entry) iter.next();
 391  0
                     String property = entry.getKey().toString();
 392  0
                     Object value = entry.getValue();
 393  0
                     InvokerHelper.setProperty(widget, property, value);
 394   
                 }
 395   
             }
 396   
         }
 397   
     }
 398   
 
 399  0
     protected String capitalize(String text) {
 400  0
         char ch = text.charAt(0);
 401  0
         if (Character.isUpperCase(ch)) {
 402  0
             return text;
 403   
         }
 404  0
         StringBuffer buffer = new StringBuffer(text.length());
 405  0
         buffer.append(Character.toUpperCase(ch));
 406  0
         buffer.append(text.substring(1));
 407  0
         return buffer.toString();
 408   
     }
 409   
 
 410  0
     protected void registerWidgets() {
 411  0
         registerBeanFactory("action", DefaultAction.class);
 412  0
         passThroughNodes.put("action", javax.swing.Action.class);
 413  0
         registerFactory("boxLayout", new Factory() {
 414  0
             public Object newInstance(Map properties)
 415   
                 throws InstantiationException, InstantiationException, IllegalAccessException {
 416  0
                 return createBoxLayout(properties);
 417   
             }
 418   
         });
 419  0
         registerBeanFactory("button", JButton.class);
 420  0
         registerBeanFactory("buttonGroup", ButtonGroup.class);
 421  0
         registerBeanFactory("checkBox", JCheckBox.class);
 422  0
         registerBeanFactory("checkBoxMenuItem", JCheckBoxMenuItem.class);
 423   
 
 424  0
         registerFactory("comboBox", new Factory() {
 425  0
             public Object newInstance(Map properties)
 426   
                 throws InstantiationException, InstantiationException, IllegalAccessException {
 427  0
                 return createComboBox(properties);
 428   
             }
 429   
         });
 430   
 
 431  0
         registerBeanFactory("desktopPane", JDesktopPane.class);
 432   
 
 433  0
         registerFactory("dialog", new Factory() {
 434  0
             public Object newInstance(Map properties)
 435   
                 throws InstantiationException, InstantiationException, IllegalAccessException {
 436  0
                 return createDialog(properties);
 437   
             }
 438   
         });
 439   
 
 440  0
         registerBeanFactory("editorPane", JEditorPane.class);
 441  0
         registerBeanFactory("fileChooser", JFileChooser.class);
 442  0
         registerBeanFactory("frame", JFrame.class);
 443  0
         registerBeanFactory("internalFrame", JInternalFrame.class);
 444  0
         registerBeanFactory("label", JLabel.class);
 445  0
         registerBeanFactory("list", JList.class);
 446   
 
 447  0
         registerFactory("map", new Factory() {
 448  0
             public Object newInstance(Map properties)
 449   
                 throws InstantiationException, InstantiationException, IllegalAccessException {
 450  0
                 return properties;
 451   
             }
 452   
         });
 453   
 
 454  0
         registerBeanFactory("menu", JMenu.class);
 455  0
         registerBeanFactory("menuBar", JMenuBar.class);
 456  0
         registerBeanFactory("menuItem", JMenuItem.class);
 457  0
         registerBeanFactory("panel", JPanel.class);
 458  0
         registerBeanFactory("passwordField", JPasswordField.class);
 459  0
         registerBeanFactory("popupMenu", JPopupMenu.class);
 460  0
         registerBeanFactory("progressBar", JProgressBar.class);
 461  0
         registerBeanFactory("radioButton", JRadioButton.class);
 462  0
         registerBeanFactory("radioButtonMenuItem", JRadioButtonMenuItem.class);
 463  0
         registerBeanFactory("optionPane", JOptionPane.class);
 464  0
         registerBeanFactory("scrollPane", JScrollPane.class);
 465  0
         registerBeanFactory("separator", JSeparator.class);
 466  0
         registerBeanFactory("tabbedPane", JTabbedPane.class);
 467   
 
 468  0
         registerFactory("splitPane", new Factory() {
 469  0
             public Object newInstance(Map properties) {
 470  0
                 JSplitPane answer = new JSplitPane();
 471  0
                 answer.setLeftComponent(null);
 472  0
                 answer.setRightComponent(null);
 473  0
                 answer.setTopComponent(null);
 474  0
                 answer.setBottomComponent(null);
 475  0
                 return answer;
 476   
             }
 477   
         });
 478   
 
 479   
         // Box related layout components
 480  0
         registerFactory("hbox", new Factory() {
 481  0
             public Object newInstance(Map properties) {
 482  0
                 return Box.createHorizontalBox();
 483   
             }
 484   
         });
 485  0
         registerFactory("hglue", new Factory() {
 486  0
             public Object newInstance(Map properties) {
 487  0
                 return Box.createHorizontalGlue();
 488   
             }
 489   
         });
 490  0
         registerFactory("hstrut", new Factory() {
 491  0
             public Object newInstance(Map properties) {
 492  0
                 try {
 493  0
                 Object num = properties.remove("width");
 494  0
                 if (num instanceof Number) {
 495  0
                     return Box.createHorizontalStrut(((Number)num).intValue());
 496   
                 } else {
 497  0
                     return Box.createHorizontalStrut(6);
 498   
                 }
 499   
                 } catch (RuntimeException re) {
 500  0
                     re.printStackTrace(System.out);
 501  0
                     throw re;
 502   
                 }
 503   
             }
 504   
         });
 505  0
         registerFactory("vbox", new Factory() {
 506  0
             public Object newInstance(Map properties) {
 507  0
                 return Box.createVerticalBox();
 508   
             }
 509   
         });
 510  0
         registerFactory("vglue", new Factory() {
 511  0
             public Object newInstance(Map properties) {
 512  0
                 return Box.createVerticalGlue();
 513   
             }
 514   
         });
 515  0
         registerFactory("vstrut", new Factory() {
 516  0
             public Object newInstance(Map properties) {
 517  0
                 Object num = properties.remove("height");
 518  0
                 if (num instanceof Number) {
 519  0
                     return Box.createVerticalStrut(((Number)num).intValue());
 520   
                 } else {
 521  0
                     return Box.createVerticalStrut(6);
 522   
                 }
 523   
             }
 524   
         });
 525  0
         registerFactory("glue", new Factory() {
 526  0
             public Object newInstance(Map properties) {
 527  0
                 return Box.createGlue();
 528   
             }
 529   
         });
 530  0
         registerFactory("rigidArea", new Factory() {
 531  0
             public Object newInstance(Map properties) {
 532  0
                 Dimension dim;
 533  0
                 Object o = properties.remove("size");
 534  0
                 if (o instanceof Dimension) {
 535  0
                     dim = (Dimension) o;
 536   
                 } else {
 537  0
                     int w, h;
 538  0
                     o = properties.remove("width");
 539  0
                     w = ((o instanceof Number)) ? ((Number)o).intValue() : 6;
 540  0
                     o = properties.remove("height");
 541  0
                     h = ((o instanceof Number)) ? ((Number)o).intValue() : 6;
 542  0
                     dim = new Dimension(w, h);
 543   
                 }
 544  0
                 return Box.createRigidArea(dim);
 545   
             }
 546   
         });
 547   
 
 548  0
         registerBeanFactory("tabbedPane", JTabbedPane.class);
 549  0
         registerBeanFactory("table", JTable.class);
 550   
 
 551  0
         registerBeanFactory("textArea", JTextArea.class);
 552  0
         registerBeanFactory("textPane", JTextPane.class);
 553  0
         registerBeanFactory("textField", JTextField.class);
 554  0
         registerBeanFactory("toggleButton", JToggleButton.class);
 555  0
         registerBeanFactory("tree", JTree.class);
 556  0
         registerBeanFactory("toolBar", JToolBar.class);
 557   
 
 558   
         // MVC models        
 559  0
         registerFactory("tableModel", new Factory() {
 560  0
             public Object newInstance(Map properties) {
 561  0
                 ValueModel model = (ValueModel) properties.remove("model");
 562  0
                 if (model == null) {
 563  0
                     Object list = properties.remove("list");
 564  0
                     if (list == null) {
 565  0
                         list = new ArrayList();
 566   
                     }
 567  0
                     model = new ValueHolder(list);
 568   
                 }
 569  0
                 return new DefaultTableModel(model);
 570   
             }
 571   
         });
 572  0
         passThroughNodes.put("tableModel", javax.swing.table.TableModel.class);
 573   
 
 574  0
         registerFactory("propertyColumn", new Factory() {
 575  0
             public Object newInstance(Map properties) {
 576  0
                 Object current = getCurrent();
 577  0
                 if (current instanceof DefaultTableModel) {
 578  0
                     DefaultTableModel model = (DefaultTableModel) current;
 579  0
                     Object header = properties.remove("header");
 580  0
                     if (header == null) {
 581  0
                         header = "";
 582   
                     }
 583  0
                     String property = (String) properties.remove("propertyName");
 584  0
                     if (property == null) {
 585  0
                         throw new IllegalArgumentException("Must specify a property for a propertyColumn");
 586   
                     }
 587  0
                     Class type = (Class) properties.remove("type");
 588  0
                     if (type == null) {
 589  0
                         type = Object.class;
 590   
                     }
 591  0
                     return model.addPropertyColumn(header, property, type);
 592   
                 }
 593   
                 else {
 594  0
                     throw new RuntimeException("propertyColumn must be a child of a tableModel");
 595   
                 }
 596   
             }
 597   
         });
 598   
 
 599  0
         registerFactory("closureColumn", new Factory() {
 600  0
             public Object newInstance(Map properties) {
 601  0
                 Object current = getCurrent();
 602  0
                 if (current instanceof DefaultTableModel) {
 603  0
                     DefaultTableModel model = (DefaultTableModel) current;
 604  0
                     Object header = properties.remove("header");
 605  0
                     if (header == null) {
 606  0
                         header = "";
 607   
                     }
 608  0
                     Closure readClosure = (Closure) properties.remove("read");
 609  0
                     if (readClosure == null) {
 610  0
                         throw new IllegalArgumentException("Must specify 'read' Closure property for a closureColumn");
 611   
                     }
 612  0
                     Closure writeClosure = (Closure) properties.remove("write");
 613  0
                     Class type = (Class) properties.remove("type");
 614  0
                     if (type == null) {
 615  0
                         type = Object.class;
 616   
                     }
 617  0
                     return model.addClosureColumn(header, readClosure, writeClosure, type);
 618   
                 }
 619   
                 else {
 620  0
                     throw new RuntimeException("propertyColumn must be a child of a tableModel");
 621   
                 }
 622   
             }
 623   
         });
 624   
 
 625   
         // table layout
 626  0
         registerBeanFactory("tableLayout", TableLayout.class);
 627  0
         registerFactory("tr", new Factory() {
 628  0
             public Object newInstance(Map properties) {
 629  0
                 Object parent = getCurrent();
 630  0
                 if (parent instanceof TableLayout) {
 631  0
                     return new TableLayoutRow((TableLayout) parent);
 632   
                 }
 633   
                 else {
 634  0
                     throw new RuntimeException("'tr' must be within a 'tableLayout'");
 635   
                 }
 636   
             }
 637   
         });
 638  0
         registerFactory("td", new Factory() {
 639  0
             public Object newInstance(Map properties) {
 640  0
                 Object parent = getCurrent();
 641  0
                 if (parent instanceof TableLayoutRow) {
 642  0
                     return new TableLayoutCell((TableLayoutRow) parent);
 643   
                 }
 644   
                 else {
 645  0
                     throw new RuntimeException("'td' must be within a 'tr'");
 646   
                 }
 647   
             }
 648   
         });
 649   
 
 650   
         //ulimlate pass through type
 651  0
         passThroughNodes.put("widget", java.awt.Component.class);
 652   
     }
 653   
 
 654  0
     protected Object createBoxLayout(Map properties) {
 655  0
         Object parent = getCurrent();
 656  0
         if (parent instanceof Container) {
 657  0
             Object axisObject = properties.remove("axis");
 658  0
             int axis = 0;
 659  0
             if (axisObject != null) {
 660  0
                 Integer i = (Integer) axisObject;
 661  0
                 axis = i.intValue();
 662   
             }
 663  0
             BoxLayout answer = new BoxLayout((Container) parent, axis);
 664   
             
 665   
             // now lets try set the layout property
 666  0
             InvokerHelper.setProperty(parent, "layout", answer);
 667  0
             return answer;
 668   
         }
 669   
         else {
 670  0
             throw new RuntimeException("Must be nested inside a Container");
 671   
         }
 672   
     }
 673   
 
 674  0
     protected Object createDialog(Map properties) {
 675  0
         Object owner = properties.remove("owner");
 676  0
         if (owner instanceof Frame) {
 677  0
             return new JDialog((Frame) owner);
 678   
         }
 679  0
         else if (owner instanceof Dialog) {
 680  0
             return new JDialog((Dialog) owner);
 681   
         }
 682   
         else {
 683  0
             return new JDialog();
 684   
         }
 685   
     }
 686   
 
 687  0
     protected Object createComboBox(Map properties) {
 688  0
         Object items = properties.remove("items");
 689  0
         if (items instanceof Vector) {
 690  0
             return new JComboBox((Vector) items);
 691   
         }
 692  0
         else if (items instanceof List) {
 693  0
             List list = (List) items;
 694  0
             return new JComboBox(list.toArray());
 695   
         }
 696  0
         else if (items instanceof Object[]) {
 697  0
             return new JComboBox((Object[]) items);
 698   
         }
 699   
         else {
 700  0
             return new JComboBox();
 701   
         }
 702   
     }
 703   
 
 704  0
     protected void registerBeanFactory(String name, final Class beanClass) {
 705  0
         registerFactory(name, new Factory() {
 706  0
             public Object newInstance(Map properties) throws InstantiationException, IllegalAccessException {
 707  0
                 return beanClass.newInstance();
 708   
             }
 709   
         });
 710   
 
 711   
     }
 712   
 
 713  0
     protected void registerFactory(String name, Factory factory) {
 714  0
         factories.put(name, factory);
 715   
     }
 716   
 }
 717