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