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.lang.reflect.InvocationTargetException;
19  
20  import org.apache.commons.beanutils.BeanUtils;
21  import org.apache.commons.jelly.DynaBeanTagSupport;
22  import org.apache.commons.jelly.JellyException;
23  import org.apache.commons.jelly.JellyTagException;
24  import org.apache.commons.jelly.Tag;
25  import org.apache.commons.jelly.XMLOutput;
26  import org.apache.commons.jelly.impl.TagFactory;
27  import org.xml.sax.Attributes;
28  
29  /*** This class represents a layout-manager constraints as passed in
30      * the second argument of {@link Container#add(Component,Object)}.
31      *    <p>
32      *    In essence, it looks really like nothing else than a bean-class...
33      *    with {@link #getConstraintObject}.
34      *    Probably a shorter java-source is do-able.
35      *    <p>
36      *    TODO: this class should probably be extended with special treatment for dimensios
37      *    using the converter package.
38      */
39  public class ConstraintTag extends DynaBeanTagSupport {
40  
41  /*    TODO: make a gridbagconstraintTag class which supports an attribute "parent"
42                          (or... startWith) which is another gridbagconstraintTag whose gridbagconstraint
43                          is cloned then attributes are set
44                          This tag should also support the attributes such as fill=BOTH
45                          and anchor=NORTHEAST...
46                          Wooops... need to define setters ?? let's see if BeanUtils does it on public vars
47                          And... have an insets?? A child ?
48      */
49  
50      protected Factory factory;
51      protected String var = null;
52      protected Object bean = null;
53  
54          public static class HereFactory extends BeanFactory implements TagFactory {
55              public HereFactory(Class c) { super(c); }
56              public Tag createTag(String name, Attributes attributes) {
57                  return new ConstraintTag ( this );
58                  // still scratching my head about "this" usage...
59              }
60          } // class HereFactory
61          public static class ConstantFactory implements TagFactory, Factory {
62              public ConstantFactory(Object c) { this.constant = c;}
63              private Object constant;
64              public Object newInstance() { return constant; }
65              public Tag createTag(String name, Attributes attributes) throws JellyException {
66                  return new ConstraintTag ( this );
67              }
68          } // class ConstatnStringFactory
69  
70          // we could be able to make factories that create their tags in parametrized
71          // subclasses of the tag depending on the name and attributes
72          // it would useful, for example, to make a cardLayout's <card name="">
73  
74  
75      public ConstraintTag (Factory factory) {
76          this.factory = factory;
77      }
78  
79      protected void createBean ( Factory factory ) throws InstantiationException {
80          bean = factory.newInstance();
81      }
82  
83      // --------------------------------------------- ATTRIBUTES
84  
85      public void beforeSetAttributes (  ) throws JellyTagException {
86          try {
87              createBean(factory);
88          } catch (InstantiationException e) {
89              throw new JellyTagException(e.toString());
90          }
91      }
92  
93  
94      public void setAttribute ( String name, Object value ) throws JellyTagException {
95          // no real need for DynaBeans or ?
96          if ( "var".equals(name) ) {
97              var = value.toString();
98          } else {
99  
100             try {
101               BeanUtils.setProperty( bean, name, value );
102             } catch (IllegalAccessException e) {
103                 throw new JellyTagException(e.toString());
104             } catch (InvocationTargetException e) {
105                 throw new JellyTagException(e.toString());
106             }
107 
108         }
109     }
110 // --------------------------------------------------
111     /*** Children invocation... just nothing...
112         */
113     public void doTag ( XMLOutput output ) throws JellyTagException {
114         if ( var != null ) context.setVariable ( var, getBean() );
115         invokeBody ( output );
116         // nothing else to do... the getConstraintObject method should have been called.
117     }
118 
119     // ----------------------------------------------
120     public Object getBean() {
121         return bean;
122     }
123 
124     /*** Returns the attached constraint object.
125         */
126     public Object getConstraintObject() {
127         return getBean();
128     }
129 } // class ConstraintTag