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.Component;
19  import java.awt.LayoutManager;
20  
21  import org.apache.commons.jelly.JellyTagException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * An abstract base class used for concrete layout tags which create new LayoutManager implementations
29   * and either export them as variables or set them on parent widgets.
30   *
31   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32   * @version $Revision: 155420 $
33   */
34  public abstract class LayoutTagSupport extends TagSupport {
35  
36      /*** The Log to which logging calls will be made. */
37      private static final Log log = LogFactory.getLog(LayoutTagSupport.class);
38  
39      private String var;
40  
41      public LayoutTagSupport() {
42      }
43  
44      /***
45       * Adds the given layout component to the container with the specified constraints
46       */
47      public void addLayoutComponent(Component component, Object constraints) throws JellyTagException {
48          getComponentTag().addChild(component, constraints);
49      }
50  
51  
52      // Tag interface
53      //-------------------------------------------------------------------------
54      public void doTag(final XMLOutput output) throws JellyTagException {
55  
56          LayoutManager layout = createLayoutManager();
57  
58          if (var != null) {
59              context.setVariable(var, layout);
60          }
61  
62          getComponentTag().setLayout(layout);
63  
64          // allow some nested tags to set properties
65          invokeBody(output);
66      }
67  
68      // Properties
69      //-------------------------------------------------------------------------
70  
71  
72      /***
73       * Sets the name of the variable to use to expose the new LayoutManager object.
74       * If this attribute is not set then the parent widget tag will have its
75       * layout property set.
76       */
77      public void setVar(String var) {
78          this.var = var;
79      }
80  
81      // Implementation methods
82      //-------------------------------------------------------------------------
83  
84      /***
85       * @return the parent component tag or throw an exception
86       */
87      protected ComponentTag getComponentTag() throws JellyTagException {
88          ComponentTag tag = (ComponentTag) findAncestorWithClass( ComponentTag.class );
89          if ( tag == null ) {
90              throw new JellyTagException( "This tag must be nested within a JellySwing widget tag" );
91          }
92          return tag;
93      }
94  
95      /***
96       * Factory method to create a new LayoutManager instance.
97       */
98      protected abstract LayoutManager createLayoutManager();
99  }