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.Font;
19  import java.util.Map;
20  
21  import org.apache.commons.jelly.JellyTagException;
22  import org.apache.commons.jelly.MapTagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /***
29   * Creates an Font and attaches it to the parent component or exports the font as
30   * a reusable variable that can be attached to multiple widgets.
31   *
32   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33   * @version $Revision: 155420 $
34   */
35  public class FontTag extends MapTagSupport {
36  
37      /*** The Log to which logging calls will be made. */
38      private static final Log log = LogFactory.getLog(FontTag.class);
39  
40      /*** the current font instance */
41      private Font font;
42  
43      public FontTag() {
44      }
45  
46      // Tag interface
47      //-------------------------------------------------------------------------
48  /*
49   * maybe do some type conversions or name mapping code...
50   *
51      public void setAttribute(String name, Object value) {
52          if (name.equals("size")) {
53              super.setAttribute(name, ConvertUtils.convert(Integer.class, value));
54          }
55          else {
56              super.setAttribute(name, value);
57          }
58      }
59  */
60  
61      public void doTag(final XMLOutput output) throws JellyTagException {
62          Map attributes = getAttributes();
63          String var = (String) attributes.remove("var");
64  
65          font = createFont(attributes);
66  
67          if (var != null) {
68              context.setVariable(var, font);
69          }
70          else {
71              // now lets add this font to its parent if we have one
72              ComponentTag tag = (ComponentTag) findAncestorWithClass( ComponentTag.class );
73              if ( tag != null ) {
74                  tag.setFont(font);
75              }
76              else {
77                  throw new JellyTagException( "this tag must be nested within a JellySwing widget tag or the 'var' attribute must be specified" );
78              }
79          }
80      }
81  
82      // Properties
83      //-------------------------------------------------------------------------
84  
85      /***
86       * @return the Font object for this tag
87       */
88      public Font getFont() {
89          return font;
90      }
91  
92  
93      // Implementation methods
94      //-------------------------------------------------------------------------
95  
96      /***
97       * Factory method to create a new Font based on the given properties
98       */
99      protected Font createFont(Map map) {
100         log.info( "Creating font from properties: " + map );
101         Font font = new Font(map);
102         //Font font = Font.getFont(map);
103         log.info( "Created font: " + font );
104         return font;
105     }
106 }