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.util.Map;
20  
21  import javax.swing.AbstractButton;
22  import javax.swing.ButtonGroup;
23  
24  import org.apache.commons.jelly.JellyTagException;
25  import org.apache.commons.jelly.XMLOutput;
26  
27  /*** Implements a ButtonGroup. This tag acts like a Swing component
28   * except that adding a component other than an AbstractButton, will be passed
29   * through to the parent tag. This is meant to make the
30   * buttonGroup easier to use like this:
31   * <pre>
32   * &lt;panel&gt;
33   *  &lt;buttonGroup&gt;
34   *      &lt;panel&gt;
35   *          &lt;radioButton/&gt;
36   *      &lt;/panel&gt;
37   *      &lt;panel&gt;
38   *          &lt;radioButton/&gt;
39   *      &lt;/panel&gt;
40   *  &lt;/buttonGroup&gt;
41   * &lt;/panel&gt;
42   * </pre>
43   *
44   * <p> Note that the following construct will silently fail, and shame on s/he who even tried it:
45   * <pre>
46    * &lt;panel&gt;
47   *  &lt;buttonGroup&gt;
48   *      &lt;font .../&gt;
49   *      &lt;panel&gt;
50   *          &lt;radioButton/&gt;
51   *      &lt;/panel&gt;
52   *      &lt;panel&gt;
53   *          &lt;radioButton/&gt;
54   *      &lt;/panel&gt;
55   *  &lt;/buttonGroup&gt;
56   * &lt;/panel&gt;
57   * </pre>
58   * </p>
59   *
60   * @author Hans Gilde
61   *
62   */
63  public class ButtonGroupTag extends ComponentTag {
64  
65      /***If the child is an AbstractButton, add it to the button group. Otherwise,
66       * pass through to the parent component tag.
67       * @throws JellyTagException
68       * @see org.apache.commons.jelly.tags.swing.ContainerTag#addChild(java.awt.Component, java.lang.Object)
69       */
70      public void addChild(Component component, Object constraints) throws JellyTagException {
71          if (component instanceof AbstractButton) {
72              getButtonGroup().add((AbstractButton) component);
73          } else {
74              if ( component != null ) {
75                  ContainerTag parentTag = (ContainerTag) findAncestorWithClass( ContainerTag.class );
76                  if ( parentTag != null ) {
77                      parentTag.addChild(component, getConstraint());
78                  }
79                  else {
80                      throw new JellyTagException( "This buttonGroup tag must be nested within a Swing component tag." );
81                  }
82              }
83          }
84      }
85  
86      /***Creates a new buttonGroup.
87       * @see org.apache.commons.jelly.tags.core.UseBeanTag#newInstance(java.lang.Class, java.util.Map, org.apache.commons.jelly.XMLOutput)
88       */
89      protected Object newInstance(Class theClass, Map attributes,
90              XMLOutput output) throws JellyTagException {
91          return new ButtonGroup();
92      }
93  
94      protected ButtonGroup getButtonGroup() {
95          return (ButtonGroup) getBean();
96      }
97  }