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.awt.LayoutManager;
20
21 import org.apache.commons.jelly.JellyTagException;
22 import org.apache.commons.jelly.TagSupport;
23 import org.apache.commons.jelly.XMLOutput;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /***
28 * An abstract base class used for concrete layout tags which create new LayoutManager implementations
29 * and either export them as variables or set them on parent widgets.
30 *
31 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32 * @version $Revision: 155420 $
33 */
34 public abstract class LayoutTagSupport extends TagSupport {
35
36 /*** The Log to which logging calls will be made. */
37 private static final Log log = LogFactory.getLog(LayoutTagSupport.class);
38
39 private String var;
40
41 public LayoutTagSupport() {
42 }
43
44 /***
45 * Adds the given layout component to the container with the specified constraints
46 */
47 public void addLayoutComponent(Component component, Object constraints) throws JellyTagException {
48 getComponentTag().addChild(component, constraints);
49 }
50
51
52
53
54 public void doTag(final XMLOutput output) throws JellyTagException {
55
56 LayoutManager layout = createLayoutManager();
57
58 if (var != null) {
59 context.setVariable(var, layout);
60 }
61
62 getComponentTag().setLayout(layout);
63
64
65 invokeBody(output);
66 }
67
68
69
70
71
72 /***
73 * Sets the name of the variable to use to expose the new LayoutManager object.
74 * If this attribute is not set then the parent widget tag will have its
75 * layout property set.
76 */
77 public void setVar(String var) {
78 this.var = var;
79 }
80
81
82
83
84 /***
85 * @return the parent component tag or throw an exception
86 */
87 protected ComponentTag getComponentTag() throws JellyTagException {
88 ComponentTag tag = (ComponentTag) findAncestorWithClass( ComponentTag.class );
89 if ( tag == null ) {
90 throw new JellyTagException( "This tag must be nested within a JellySwing widget tag" );
91 }
92 return tag;
93 }
94
95 /***
96 * Factory method to create a new LayoutManager instance.
97 */
98 protected abstract LayoutManager createLayoutManager();
99 }