1 /*
2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $
3 * $Revision: 1.7 $
4 * $Date: 2002/05/17 15:18:12 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * $Id: DynamicTag.java,v 1.7 2002/05/17 15:18:12 jstrachan Exp $
61 */
62 package org.apache.commons.jelly.tags.swing;
63
64
65 import java.awt.Component;
66 import java.awt.Container;
67 import java.awt.Dimension;
68 import java.awt.Point;
69 import java.awt.Window;
70 import java.awt.event.WindowListener;
71
72 import javax.swing.*;
73
74 import org.apache.commons.beanutils.BeanUtils;
75 import org.apache.commons.beanutils.ConvertingWrapDynaBean;
76 import org.apache.commons.beanutils.ConvertUtils;
77
78 import org.apache.commons.collections.BeanMap;
79
80 import org.apache.commons.jelly.DynaBeanTagSupport;
81 import org.apache.commons.jelly.XMLOutput;
82 import org.apache.commons.jelly.impl.BeanSource;
83
84 import org.apache.commons.logging.Log;
85 import org.apache.commons.logging.LogFactory;
86
87 /***
88 * This tag creates a Swing component and adds it to its parent tag, optionally declaring this
89 * component as a variable if the <i>var</i> attribute is specified.</p>
90 *
91 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
92 * @version $Revision: 1.7 $
93 */
94 public class ComponentTag extends DynaBeanTagSupport implements BeanSource {
95
96 /*** The Log to which logging calls will be made. */
97 private static final Log log = LogFactory.getLog(ComponentTag.class);
98
99 /*** the factory of widgets */
100 private Factory factory;
101
102 /*** the current bean instance */
103 private Object bean;
104
105 /*** the current variable name that the bean should be exported as */
106 private String var;
107
108 public ComponentTag(Factory factory) {
109 this.factory = factory;
110 }
111
112 /***
113 * Adds a child component to this parent
114 */
115 public void addChild(Component component) {
116 Object parent = getBean();
117 if ( parent instanceof JFrame && component instanceof JMenuBar ) {
118 JFrame frame = (JFrame) parent;
119 frame.setJMenuBar( (JMenuBar) component );
120 }
121 else if ( parent instanceof RootPaneContainer ) {
122 RootPaneContainer rpc = (RootPaneContainer) parent;
123 rpc.getContentPane().add( component );
124 }
125 else if ( parent instanceof JScrollPane ) {
126 JScrollPane scrollPane = (JScrollPane) parent;
127 scrollPane.setViewportView( component );
128 }
129 else if ( parent instanceof JSplitPane) {
130 JSplitPane splitPane = (JSplitPane) parent;
131 if ( splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT ) {
132 if ( splitPane.getTopComponent() == null ) {
133 splitPane.setTopComponent( component );
134 }
135 else {
136 splitPane.setBottomComponent( component );
137 }
138 }
139 else {
140 if ( splitPane.getLeftComponent() == null ) {
141 splitPane.setLeftComponent( component );
142 }
143 else {
144 splitPane.setRightComponent( component );
145 }
146 }
147 }
148 else if ( parent instanceof JMenuBar && component instanceof JMenu ) {
149 JMenuBar menuBar = (JMenuBar) parent;
150 menuBar.add( (JMenu) component );
151 }
152 else if ( parent instanceof Container ) {
153 Container container = (Container) parent;
154 container.add( component );
155 }
156 }
157
158
159 /***
160 * Sets the Action of this component
161 */
162 public void setAction(Action action) throws Exception {
163 Component component = getComponent();
164 if ( component != null ) {
165 // lets just try set the 'action' property
166 BeanUtils.setProperty( component, "action", action );
167 }
168 }
169
170 /***
171 * Adds a WindowListener to this component
172 */
173 public void addWindowListener(WindowListener listener) throws Exception {
174 Component component = getComponent();
175 if ( component instanceof Window ) {
176 Window window = (Window) component;
177 window.addWindowListener(listener);
178 }
179 }
180
181 // DynaTag interface
182 //-------------------------------------------------------------------------
183 public void beforeSetAttributes() throws Exception {
184 // create a new dynabean before the attributes are set
185 bean = factory.newInstance();
186 setDynaBean( new ConvertingWrapDynaBean( bean ) );
187 }
188
189 public void setAttribute(String name, Object value) throws Exception {
190 if (name.equals( "var" ) ) {
191 this.var = value.toString();
192 }
193 else {
194 // ### special hacks for properties that don't introspect properly
195 Component component = getComponent();
196 if ( component != null ) {
197 if ( name.equals( "location" ) ) {
198 Point p = null;
199 if ( value instanceof Point ) {
200 p = (Point) value;
201 }
202 else if ( value != null) {
203 p = (Point) ConvertUtils.convert( value.toString(), Point.class );
204 }
205 component.setLocation(p);
206 }
207 else if ( name.equals( "size" ) ) {
208 Dimension d = null;
209 if ( value instanceof Dimension ) {
210 d = (Dimension) value;
211 }
212 else if ( value != null) {
213 d = (Dimension) ConvertUtils.convert( value.toString(), Dimension.class );
214 }
215 component.setSize(d);
216 }
217 else {
218 super.setAttribute(name, value);
219 }
220 }
221 else {
222 super.setAttribute(name, value);
223 }
224 }
225 }
226
227 // Tag interface
228 //-------------------------------------------------------------------------
229 public void doTag(XMLOutput output) throws Exception {
230
231 // export the bean if required
232 if ( var != null ) {
233 context.setVariable(var, bean);
234 }
235
236 invokeBody(output);
237
238 // now we should add this component to its parent...
239 // This is currently quite simplistic.
240 // We could well support layout managers and such like
241
242 Component component = getComponent();
243 if ( component != null ) {
244 ComponentTag parentTag = (ComponentTag) findAncestorWithClass( ComponentTag.class );
245 if ( parentTag != null ) {
246 parentTag.addChild(component);
247 }
248 }
249 }
250
251 // Properties
252 //-------------------------------------------------------------------------
253
254 /***
255 * @return the bean that has just been created
256 */
257 public Object getBean() {
258 return bean;
259 }
260
261 /***
262 * @return the visible component, if there is one.
263 */
264 public Component getComponent() {
265 if ( bean instanceof Component ) {
266 return (Component) bean;
267 }
268 return null;
269 }
270 }
This page was automatically generated by Maven