1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 * <panel>
33 * <buttonGroup>
34 * <panel>
35 * <radioButton/>
36 * </panel>
37 * <panel>
38 * <radioButton/>
39 * </panel>
40 * </buttonGroup>
41 * </panel>
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 * <panel>
47 * <buttonGroup>
48 * <font .../>
49 * <panel>
50 * <radioButton/>
51 * </panel>
52 * <panel>
53 * <radioButton/>
54 * </panel>
55 * </buttonGroup>
56 * </panel>
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 }