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.Color;
19  import java.awt.Font;
20  
21  import javax.swing.BorderFactory;
22  import javax.swing.border.Border;
23  import javax.swing.border.TitledBorder;
24  
25  import org.apache.commons.jelly.JellyTagException;
26  import org.apache.commons.jelly.MissingAttributeException;
27  import org.apache.commons.jelly.XMLOutput;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /***
32   * Creates a titled border.
33   * The border will either be exported as a variable defined by the 'var' attribute
34   * or will be set on the parent widget's border property
35   *
36   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
37   * @version $Revision: 155420 $
38   */
39  public class TitledBorderTag extends BorderTagSupport {
40  
41      /*** The Log to which logging calls will be made. */
42      private static final Log log = LogFactory.getLog(TitledBorderTag.class);
43  
44      private String title;
45      private String titleJustification;
46      private String titlePosition;
47      private Border border;
48      private Font font;
49      private Color color;
50  
51  
52      // Tag interface
53      //-------------------------------------------------------------------------
54      public void doTag(final XMLOutput output) throws MissingAttributeException, JellyTagException {
55          if ( title == null) {
56              throw new MissingAttributeException("title");
57          }
58          super.doTag(output);
59      }
60  
61      // Properties
62      //-------------------------------------------------------------------------
63  
64      /***
65       * Sets the color of the title for this border. Can be set via a nested <color> tag.
66       */
67      public void setColor(Color color) {
68          this.color = color;
69      }
70  
71      /***
72       * Sets the Font to be used by the title. Can be set via a nested <font> tag.
73       */
74      public void setFont(Font font) {
75          this.font = font;
76      }
77  
78      /***
79       * Sets the title text for this border.
80       */
81      public void setTitle(String title) {
82          this.title = title;
83      }
84  
85      /***
86       * Sets the justification of the title. The String is case insensitive.
87       * Possible values are {LEFT, CENTER, RIGHT, LEADING, TRAILING}
88       */
89      public void setTitleJustification(String titleJustification) {
90          this.titleJustification = titleJustification;
91      }
92  
93      /***
94       * Sets the position of the title. The String is case insensitive.
95       * Possible values are {ABOVE_TOP, TOP, BELOW_TOP, ABOVE_BOTTOM, BOTTOM, BELOW_BOTTOM}
96       */
97      public void setTitlePosition(String titlePosition) {
98          this.titlePosition = titlePosition;
99      }
100 
101 
102 
103     // Implementation methods
104     //-------------------------------------------------------------------------
105 
106     /***
107      * Factory method to create a new Border instance.
108      */
109     protected Border createBorder() {
110         if (border != null) {
111             if (titleJustification != null && titlePosition != null) {
112                 int justification = asTitleJustification(titleJustification);
113                 int position = asTitlePosition(titlePosition);
114 
115                 if (font != null) {
116                     if (color != null) {
117                         return BorderFactory.createTitledBorder(border, title, justification, position, font, color);
118                     }
119                     else {
120                         return BorderFactory.createTitledBorder(border, title, justification, position, font);
121                     }
122                 }
123                 return BorderFactory.createTitledBorder(border, title, justification, position);
124             }
125             return BorderFactory.createTitledBorder(border, title);
126         }
127         return BorderFactory.createTitledBorder(title);
128     }
129 
130     /***
131      * @return the enumeration for the title justification
132      */
133     protected int asTitleJustification(String text) {
134         if (text.equalsIgnoreCase("LEFT")) {
135             return TitledBorder.LEFT;
136         }
137         else if (text.equalsIgnoreCase("CENTER")) {
138             return TitledBorder.CENTER;
139         }
140         else if (text.equalsIgnoreCase("RIGHT")) {
141             return TitledBorder.RIGHT;
142         }
143         else if (text.equalsIgnoreCase("LEADING")) {
144             return TitledBorder.LEADING;
145         }
146         else if (text.equalsIgnoreCase("TRAILING")) {
147             return TitledBorder.TRAILING;
148         }
149         else {
150             return TitledBorder.DEFAULT_JUSTIFICATION;
151         }
152     }
153 
154     /***
155      * @return the enumeration for the title position
156      */
157     protected int asTitlePosition(String text) {
158         if (text.equalsIgnoreCase("ABOVE_TOP")) {
159             return TitledBorder.ABOVE_TOP;
160         }
161         else if (text.equalsIgnoreCase("TOP")) {
162             return TitledBorder.TOP;
163         }
164         else if (text.equalsIgnoreCase("BELOW_TOP")) {
165             return TitledBorder.BELOW_TOP;
166         }
167         else if (text.equalsIgnoreCase("ABOVE_BOTTOM")) {
168             return TitledBorder.ABOVE_BOTTOM;
169         }
170         else if (text.equalsIgnoreCase("BOTTOM")) {
171             return TitledBorder.BOTTOM;
172         }
173         else if (text.equalsIgnoreCase("BELOW_BOTTOM")) {
174             return TitledBorder.BELOW_BOTTOM;
175         }
176         else {
177             return TitledBorder.DEFAULT_POSITION;
178         }
179     }
180 }