Coverage report

  %line %branch
org.apache.commons.jelly.tags.swing.SwingTagLibrary
100% 
100% 

 1  
 /*
 2  
  * Copyright 2002,2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.jelly.tags.swing;
 17  
 
 18  
 import java.awt.Dimension;
 19  
 import java.awt.Point;
 20  
 import java.util.HashMap;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.swing.*;
 24  
 
 25  
 import org.apache.commons.beanutils.ConvertUtils;
 26  
 
 27  
 import org.apache.commons.jelly.JellyException;
 28  
 import org.apache.commons.jelly.Tag;
 29  
 import org.apache.commons.jelly.TagLibrary;
 30  
 import org.apache.commons.jelly.impl.TagScript;
 31  
 import org.apache.commons.jelly.impl.TagFactory;
 32  
 import org.apache.commons.jelly.tags.swing.converters.DimensionConverter;
 33  
 import org.apache.commons.jelly.tags.swing.converters.PointConverter;
 34  
 import org.apache.commons.jelly.tags.swing.converters.ColorConverter;
 35  
 import org.apache.commons.jelly.tags.swing.converters.DebugGraphicsConverter;
 36  
 
 37  
 import org.apache.commons.logging.Log;
 38  
 import org.apache.commons.logging.LogFactory;
 39  
 
 40  
 import org.xml.sax.Attributes;
 41  
 
 42  
 /**
 43  
  * A Jelly custom tag library that allows Ant tasks to be called from inside Jelly.
 44  
  *
 45  
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 46  
  * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
 47  
  * @version $Revision: 1.6 $
 48  
  */
 49  78
 public class SwingTagLibrary extends TagLibrary {
 50  
 
 51  
     /** The Log to which logging calls will be made. */
 52  4
     private static final Log log = LogFactory.getLog(SwingTagLibrary.class);
 53  
 
 54  
     /** A map of element name to bean class objects */
 55  
     private Map factoryMap;
 56  
 
 57  
     static {
 58  
 
 59  
         // ### we should create Converters from Strings to various Swing types such as
 60  
         // ### Icon, KeyStroke etc.
 61  2
         ConvertUtils.register( new DimensionConverter(), Dimension.class );
 62  2
         ConvertUtils.register( new PointConverter(), Point.class );
 63  2
         ConvertUtils.register( new ColorConverter(), java.awt.Color.class );
 64  
     }
 65  
 
 66  7
     public SwingTagLibrary() {
 67  7
         registerTag( "action", ActionTag.class );
 68  7
         registerTag( "buttonGroup", ButtonGroupTag.class );
 69  7
         registerTag( "component", ComponentTag.class );
 70  7
         registerTag( "font", FontTag.class );
 71  7
         registerTag( "windowListener", WindowListenerTag.class );
 72  7
         registerTag( "focusListener", FocusListenerTag.class );
 73  7
         registerTag( "keyListener", KeyListenerTag.class );
 74  
 
 75  
         // the model tags
 76  7
         registerTag( "tableModel", TableModelTag.class );
 77  7
         registerTag( "tableModelColumn", TableModelColumnTag.class );
 78  
 
 79  
         // the border tags...
 80  7
         registerTag( "etchedBorder", EtchedBorderTag.class );
 81  7
         registerTag( "emptyBorder", EmptyBorderTag.class );
 82  7
         registerTag( "titledBorder", TitledBorderTag.class );
 83  
         // @todo the other kinds of borders, empty, bevelled, compound etc
 84  
 
 85  
         // the layout tags...
 86  
 
 87  
         // HTML style table, tr, td layouts
 88  7
         registerTag( "tableLayout", TableLayoutTag.class );
 89  7
         registerTag( "tr", TrTag.class );
 90  7
         registerTag( "td", TdTag.class );
 91  
 
 92  
         // GridBagLayout
 93  7
         registerTag( "gridBagLayout", GridBagLayoutTag.class );
 94  7
         registerTag( "gbc", GbcTag.class );
 95  
 
 96  
         // BorderLayout
 97  7
         registerTag( "borderLayout", BorderLayoutTag.class );
 98  7
         registerTag( "borderAlign", BorderAlignTag.class );
 99  
 
 100  
         // Dialog
 101  7
         registerTag( "dialog", DialogTag.class );
 102  7
     }
 103  
 
 104  
     /** Creates a new script to execute the given tag name and attributes */
 105  
     public TagScript createTagScript(String name, Attributes attributes) throws JellyException {
 106  125
         TagScript answer = super.createTagScript(name, attributes);
 107  125
         if ( answer == null ) {
 108  80
             final Factory factory = getFactory( name );
 109  80
             if ( factory != null ) {
 110  80
                 return new TagScript(
 111  
                     new TagFactory() {
 112  
                         public Tag createTag(String name, Attributes attributes) throws JellyException {
 113  
                             if ( factory instanceof TagFactory ) {
 114  
                                 return ((TagFactory) factory).createTag(name, attributes);
 115  
                             }
 116  
                             else {
 117  
                                 return new ComponentTag(factory);
 118  
                             }
 119  
                         }
 120  
                     }
 121  
                 );
 122  
             }
 123  
         }
 124  45
         return answer;
 125  
     }
 126  
 
 127  
     /**
 128  
      * @return the Factory of the Swing component for the given element name
 129  
      */
 130  
     public Factory getFactory(String elementName) {
 131  80
         return (Factory) getFactoryMap().get(elementName);
 132  
     }
 133  
 
 134  
     // Implementation methods
 135  
     //-------------------------------------------------------------------------
 136  
 
 137  
     /**
 138  
      * Strategy method allowing derived classes to change the registration behaviour
 139  
      */
 140  
     protected void registerFactories() {
 141  5
         registerBeanFactory( "button", JButton.class );
 142  5
         registerBeanFactory( "checkBox", JCheckBox.class );
 143  5
         registerBeanFactory( "checkBoxMenuItem", JCheckBoxMenuItem.class );
 144  5
         registerBeanFactory( "comboBox", JComboBox.class );
 145  
         // how to add content there ?
 146  
         // Have a ComboBoxModel (just one should have a Table or Tree Model objects) ?
 147  
         // can the element control it's children ?
 148  
         // but children should also be able to be any component (as Swing comps. are all container)
 149  5
         registerBeanFactory( "desktopPane", JDesktopPane.class );
 150  5
         registerBeanFactory( "editorPane", JEditorPane.class );
 151  5
         registerBeanFactory( "fileChooser", JFileChooser.class );
 152  5
         registerBeanFactory( "frame", JFrame.class );
 153  5
         registerBeanFactory( "internalFrame", JInternalFrame.class );
 154  5
         registerBeanFactory( "label", JLabel.class );
 155  5
         registerBeanFactory( "list", JList.class );
 156  5
         registerBeanFactory( "menu", JMenu.class );
 157  5
         registerBeanFactory( "menuBar", JMenuBar.class );
 158  5
         registerBeanFactory( "menuItem", JMenuItem.class );
 159  5
         registerBeanFactory( "panel", JPanel.class );
 160  5
         registerBeanFactory( "passwordField", JPasswordField.class );
 161  5
         registerBeanFactory( "popupMenu", JPopupMenu.class );
 162  5
         registerBeanFactory( "progressBar", JProgressBar.class );
 163  5
         registerBeanFactory( "radioButton", JRadioButton.class );
 164  5
         registerBeanFactory( "radioButtonMenuItem", JRadioButtonMenuItem.class );
 165  5
         registerBeanFactory( "optionPane", JOptionPane.class );
 166  5
         registerBeanFactory( "scrollPane", JScrollPane.class );
 167  5
         registerBeanFactory( "separator", JSeparator.class );
 168  
 
 169  5
         registerFactory(
 170  
             "splitPane",
 171  
             new Factory() {
 172  
                 public Object newInstance() {
 173  
                     JSplitPane answer = new JSplitPane();
 174  
                     answer.setLeftComponent(null);
 175  
                     answer.setRightComponent(null);
 176  
                     answer.setTopComponent(null);
 177  
                     answer.setBottomComponent(null);
 178  
                     return answer;
 179  
                 }
 180  
             }
 181  
         );
 182  
 
 183  
         // Box related layout components
 184  5
         registerFactory(
 185  
             "hbox",
 186  
             new Factory() {
 187  
                 public Object newInstance() {
 188  
                     return Box.createHorizontalBox();
 189  
                 }
 190  
             }
 191  
         );
 192  5
         registerFactory(
 193  
             "vbox",
 194  
             new Factory() {
 195  
                 public Object newInstance() {
 196  
                     return Box.createVerticalBox();
 197  
                 }
 198  
             }
 199  
         );
 200  
 
 201  5
         registerBeanFactory( "tabbedPane", JTabbedPane.class );
 202  5
         registerBeanFactory( "table", JTable.class );
 203  5
         registerBeanFactory( "textArea", JTextArea.class );
 204  5
         registerBeanFactory( "textField", JTextField.class );
 205  5
         registerBeanFactory( "toggleButton", JToggleButton.class );
 206  5
         registerBeanFactory( "tree", JTree.class );
 207  5
         registerBeanFactory( "toolBar", JToolBar.class );
 208  5
     }
 209  
 
 210  
     /**
 211  
      * Register a widget factory for the given element name
 212  
      */
 213  
     protected void registerFactory(String name, Factory factory) {
 214  165
         getFactoryMap().put(name, factory);
 215  165
     }
 216  
 
 217  
     /**
 218  
      * Register a bean factory for the given element name and class
 219  
      */
 220  
     protected void registerBeanFactory(String name, Class beanClass) {
 221  150
         registerFactory(name, new BeanFactory(beanClass));
 222  150
     }
 223  
 
 224  
     protected Map getFactoryMap() {
 225  245
         if ( factoryMap == null ) {
 226  5
             factoryMap = new HashMap();
 227  5
             registerFactories();
 228  
         }
 229  245
         return factoryMap;
 230  
     }
 231  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.