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 javax.swing.BorderFactory;
20  import javax.swing.border.Border;
21  import javax.swing.border.EtchedBorder;
22  
23  import org.apache.commons.jelly.JellyTagException;
24  import org.apache.commons.jelly.MissingAttributeException;
25  import org.apache.commons.jelly.XMLOutput;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /***
30   * Creates an etched border.
31   * The border will either be exported as a variable defined by the 'var' attribute
32   * or will be set on the parent widget's border property
33   *
34   * @author <a href="mailto:robert@bull-enterprises.com">Robert McIntosh</a>
35   * @version $Revision: 155420 $
36   */
37  public class EtchedBorderTag extends BorderTagSupport {
38  
39      /*** The Log to which logging calls will be made. */
40      private static final Log log = LogFactory.getLog(EtchedBorderTag.class);
41  
42      private int     etchType  = -1;
43      private Color   highlight = null;
44      private Color   shadow    = null;
45  
46      // Tag interface
47      //-------------------------------------------------------------------------
48      public void doTag(final XMLOutput output) throws MissingAttributeException, JellyTagException {
49          if( highlight != null && shadow == null ) {
50              throw new MissingAttributeException("shadow must be supplied when highlight is supplied");
51          }
52          if( shadow != null && highlight == null) {
53              throw new MissingAttributeException("highlight must be supplied when shadow is supplied");
54          }
55          if( etchType != EtchedBorder.LOWERED || etchType != EtchedBorder.RAISED ) {
56              if( log.isDebugEnabled() ) log.debug( "etchType set to [" + etchType + "], which is invalid. Reseting to -1" );
57          }
58          super.doTag(output);
59      }
60  
61      // Properties
62      //-------------------------------------------------------------------------
63      /***
64       * Sets the etch type. Must be either EtchedBorder.LOWERED or EtchedBorder.RAISED
65       * @param type
66       */
67      public void setEtchType( int type ) {
68          etchType = type;
69      }
70  
71      /***
72       * Sets the highlight color
73       * @param highlight
74       */
75      public void setHighlight( Color highlight ) {
76          this.highlight = highlight;
77      }
78  
79      /***
80       * Sets the shadow color
81       * @param shadow
82       */
83      public void setTop( Color shadow ) {
84          this.shadow = shadow;
85      }
86  
87      /***
88       * Factory method to create a new EtchedBorder instance.
89       */
90      protected Border createBorder() {
91          if( etchType == -1 && shadow == null && highlight == null) {
92              return BorderFactory.createEtchedBorder();
93          }
94          else if ( highlight != null && shadow != null && etchType > -1 ) {
95              return BorderFactory.createEtchedBorder( etchType, highlight, shadow );
96          }
97          else {
98              return BorderFactory.createEtchedBorder( highlight, shadow );
99          }
100 
101     }
102 
103 }