View Javadoc

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  public class SwingTagLibrary extends TagLibrary {
50  
51      /*** The Log to which logging calls will be made. */
52      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          ConvertUtils.register( new DimensionConverter(), Dimension.class );
62          ConvertUtils.register( new PointConverter(), Point.class );
63          ConvertUtils.register( new ColorConverter(), java.awt.Color.class );
64      }
65  
66      public SwingTagLibrary() {
67          registerTag( "action", ActionTag.class );
68          registerTag( "buttonGroup", ButtonGroupTag.class );
69          registerTag( "component", ComponentTag.class );
70          registerTag( "font", FontTag.class );
71          registerTag( "windowListener", WindowListenerTag.class );
72          registerTag( "focusListener", FocusListenerTag.class );
73          registerTag( "keyListener", KeyListenerTag.class );
74  
75          // the model tags
76          registerTag( "tableModel", TableModelTag.class );
77          registerTag( "tableModelColumn", TableModelColumnTag.class );
78  
79          // the border tags...
80          registerTag( "etchedBorder", EtchedBorderTag.class );
81          registerTag( "emptyBorder", EmptyBorderTag.class );
82          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          registerTag( "tableLayout", TableLayoutTag.class );
89          registerTag( "tr", TrTag.class );
90          registerTag( "td", TdTag.class );
91  
92          // GridBagLayout
93          registerTag( "gridBagLayout", GridBagLayoutTag.class );
94          registerTag( "gbc", GbcTag.class );
95  
96          // BorderLayout
97          registerTag( "borderLayout", BorderLayoutTag.class );
98          registerTag( "borderAlign", BorderAlignTag.class );
99  
100         // Dialog
101         registerTag( "dialog", DialogTag.class );
102     }
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         TagScript answer = super.createTagScript(name, attributes);
107         if ( answer == null ) {
108             final Factory factory = getFactory( name );
109             if ( factory != null ) {
110                 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         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         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         registerBeanFactory( "button", JButton.class );
142         registerBeanFactory( "checkBox", JCheckBox.class );
143         registerBeanFactory( "checkBoxMenuItem", JCheckBoxMenuItem.class );
144         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         registerBeanFactory( "desktopPane", JDesktopPane.class );
150         registerBeanFactory( "editorPane", JEditorPane.class );
151         registerBeanFactory( "fileChooser", JFileChooser.class );
152         registerBeanFactory( "frame", JFrame.class );
153         registerBeanFactory( "internalFrame", JInternalFrame.class );
154         registerBeanFactory( "label", JLabel.class );
155         registerBeanFactory( "list", JList.class );
156         registerBeanFactory( "menu", JMenu.class );
157         registerBeanFactory( "menuBar", JMenuBar.class );
158         registerBeanFactory( "menuItem", JMenuItem.class );
159         registerBeanFactory( "panel", JPanel.class );
160         registerBeanFactory( "passwordField", JPasswordField.class );
161         registerBeanFactory( "popupMenu", JPopupMenu.class );
162         registerBeanFactory( "progressBar", JProgressBar.class );
163         registerBeanFactory( "radioButton", JRadioButton.class );
164         registerBeanFactory( "radioButtonMenuItem", JRadioButtonMenuItem.class );
165         registerBeanFactory( "optionPane", JOptionPane.class );
166         registerBeanFactory( "scrollPane", JScrollPane.class );
167         registerBeanFactory( "separator", JSeparator.class );
168 
169         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         registerFactory(
185             "hbox",
186             new Factory() {
187                 public Object newInstance() {
188                     return Box.createHorizontalBox();
189                 }
190             }
191         );
192         registerFactory(
193             "vbox",
194             new Factory() {
195                 public Object newInstance() {
196                     return Box.createVerticalBox();
197                 }
198             }
199         );
200 
201         registerBeanFactory( "tabbedPane", JTabbedPane.class );
202         registerBeanFactory( "table", JTable.class );
203         registerBeanFactory( "textArea", JTextArea.class );
204         registerBeanFactory( "textField", JTextField.class );
205         registerBeanFactory( "toggleButton", JToggleButton.class );
206         registerBeanFactory( "tree", JTree.class );
207         registerBeanFactory( "toolBar", JToolBar.class );
208     }
209 
210     /***
211      * Register a widget factory for the given element name
212      */
213     protected void registerFactory(String name, Factory factory) {
214         getFactoryMap().put(name, factory);
215     }
216 
217     /***
218      * Register a bean factory for the given element name and class
219      */
220     protected void registerBeanFactory(String name, Class beanClass) {
221         registerFactory(name, new BeanFactory(beanClass));
222     }
223 
224     protected Map getFactoryMap() {
225         if ( factoryMap == null ) {
226             factoryMap = new HashMap();
227             registerFactories();
228         }
229         return factoryMap;
230     }
231 }