View Javadoc
1 /* 2 * $Header: /cvsroot/xwing/projects/xwing/src/java/org/apache/commons/jelly/tags/swing/GbcTag.java,v 1.2 2003/10/04 02:01:45 jshowlett Exp $ 3 * $Revision: 1.2 $ 4 * $Date: 2003/10/04 02:01:45 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 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: GbcTag.java,v 1.2 2003/10/04 02:01:45 jshowlett Exp $ 61 */ 62 package org.apache.commons.jelly.tags.swing; 63 64 import java.awt.Component; 65 import java.awt.GridBagConstraints; 66 import java.awt.Insets; 67 import java.util.Map; 68 69 import org.apache.commons.jelly.JellyTagException; 70 import org.apache.commons.jelly.MissingAttributeException; 71 import org.apache.commons.jelly.Tag; 72 import org.apache.commons.jelly.XMLOutput; 73 import org.apache.commons.jelly.tags.core.UseBeanTag; 74 import org.apache.commons.jelly.tags.swing.impl.GridBagConstraintBean; 75 import org.apache.commons.lang.StringUtils; 76 77 /*** 78 * This class represents a {@link GridBagConstraints} constraints as passed in 79 * the second argument of {@link Container#add(Component,Object)}. 80 * It supports inheritence between such tags in the following fashion: 81 * <ul> 82 * <li>either using a <code>basedOn</code> attribute which is 83 * supposed to provide a reference to another {@link GbcTag}.</li> 84 * <li>either using a parent {@link GbcTag}.</li> 85 * </ul> 86 * The first version takes precedence. 87 * A Grid-bag-constraint inherits from another simply by setting other attributes 88 * as is done in {@link GridBagConstraintBean#setBasedOn}. 89 * <p> 90 * In essence, it looks really like nothing else than a bean-class... 91 * with {@link #getConstraints}. 92 * Probably a shorter java-source is do-able. 93 * <p> 94 * TODO: this class should probably be extended with special treatment for dimensions 95 * using the converter package. 96 * 97 * @author <a href="mailto:paul@activemath.org">Paul Libbrecht</a> 98 * @version $Revision: 1.2 $ 99 */ 100 public class GbcTag extends UseBeanTag implements ContainerTag { 101 102 public GridBagConstraints getConstraints() { 103 return (GridBagConstraints) getBean(); 104 } 105 106 // ContainerTag interface 107 //------------------------------------------------------------------------- 108 109 /*** 110 * Adds a child component to this parent 111 * @param component the child to add 112 * @param constraints the constraints to use 113 * @TODO constraints looks like it's ignored 114 */ 115 public void addChild(Component component, Object constraints) 116 throws JellyTagException { 117 GridBagLayoutTag tag = 118 (GridBagLayoutTag) findAncestorWithClass(GridBagLayoutTag.class); 119 if (tag == null) { 120 throw new JellyTagException("this tag must be nested within a <tr> tag"); 121 } 122 tag.addLayoutComponent(component, getConstraints()); 123 } 124 125 // Implementation methods 126 //------------------------------------------------------------------------- 127 128 /*** 129 * A class may be specified otherwise the Factory will be used. 130 * @param classObject the object to be converted 131 */ 132 protected Class convertToClass(Object classObject) 133 throws MissingAttributeException, ClassNotFoundException { 134 if (classObject == null) { 135 return null; 136 } else { 137 return super.convertToClass(classObject); 138 } 139 } 140 141 /*** 142 * A class may be specified otherwise the Factory will be used. 143 */ 144 protected Object newInstance( 145 Class theClass, 146 Map attributes, 147 XMLOutput output) 148 throws JellyTagException { 149 if (theClass != null) { 150 try { 151 return theClass.newInstance(); 152 } catch (IllegalAccessException e) { 153 throw new JellyTagException(e); 154 } catch (InstantiationException e) { 155 throw new JellyTagException(e); 156 } 157 } else { 158 return new GridBagConstraintBean(); 159 } 160 } 161 162 protected void setBeanProperties(Object bean, Map attributes) 163 throws JellyTagException { 164 165 Insets ins = null; 166 Object insetString = attributes.get("insets"); 167 if (insetString instanceof String) { 168 attributes.remove("insets"); 169 170 String[] parts = StringUtils.split((String) insetString, ","); 171 172 if (parts.length != 4) { 173 throw new JellyTagException( 174 "insets must be specified" 175 + "as four comma - separated integers."); 176 } 177 178 ins = 179 new Insets( 180 Integer.parseInt(parts[0].trim()), 181 Integer.parseInt(parts[1].trim()), 182 Integer.parseInt(parts[2].trim()), 183 Integer.parseInt(parts[3].trim())); 184 } 185 186 super.setBeanProperties(bean, attributes); 187 188 // set basedOn info of the bean if we have a parent gbc tag 189 // in the context of the closest gridbaglayout tag 190 191 if (bean instanceof GridBagConstraintBean) { 192 GridBagConstraintBean gbc = (GridBagConstraintBean) bean; 193 194 if (ins != null) { 195 gbc.setInsets(ins); 196 } 197 198 GridBagLayoutTag parentLayoutTag = 199 (GridBagLayoutTag) (findAncestorWithClass(GridBagLayoutTag 200 .class)); 201 if (parentLayoutTag != null) { 202 GbcTag parentGbcTag = 203 (GbcTag) (findAncestorWithClass(getParent(), 204 GbcTag.class, 205 parentLayoutTag)); 206 if (parentGbcTag != null) { 207 GridBagConstraints parentGbc = 208 parentGbcTag.getConstraints(); 209 210 if (parentGbc != null 211 && parentGbc instanceof GridBagConstraintBean) { 212 gbc.setBasedOn((GridBagConstraintBean) parentGbc); 213 if (insetString == null) { 214 gbc.setInsets(parentGbc.insets); 215 } 216 } 217 } 218 } 219 } 220 } 221 222 public static Tag findAncestorWithClass( 223 Tag from, 224 Class tagClass, 225 Tag parent) { 226 while (from != null && from != parent) { 227 if (tagClass.isInstance(from)) { 228 return from; 229 } 230 from = from.getParent(); 231 } 232 return null; 233 } 234 }

This page was automatically generated by Maven