View Javadoc

1   /*
2    * Copyright (c) 2004-2005 by Michael Connor. All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without 
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    *  o Redistributions of source code must retain the above copyright notice, 
8    *    this list of conditions and the following disclaimer. 
9    *     
10   *  o Redistributions in binary form must reproduce the above copyright notice, 
11   *    this list of conditions and the following disclaimer in the documentation 
12   *    and/or other materials provided with the distribution. 
13   *     
14   *  o Neither the name of FormLayoutBuilder or Michael Connor nor the names of 
15   *    its contributors may be used to endorse or promote products derived 
16   *    from this software without specific prior written permission. 
17   *     
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
19   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
20   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
21   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
22   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
23   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
24   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
25   * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
26   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
27   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
28   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
29   */
30  package org.mlc.swing.layout;
31  
32  import java.awt.Component;
33  import java.beans.BeanInfo;
34  import java.beans.IntrospectionException;
35  import java.beans.Introspector;
36  import java.beans.PropertyDescriptor;
37  import java.lang.reflect.InvocationTargetException;
38  import java.lang.reflect.Method;
39  import java.util.ArrayList;
40  import java.util.HashMap;
41  import java.util.Iterator;
42  import java.util.List;
43  import java.util.Map;
44  import javax.swing.JPanel;
45  
46  /***
47   * This is meant to be subclassed when you want to create a ComponentBuilder
48   * that has simplistic behavior.
49   * 
50   * @author Michael Connor mlconnor@yahoo.com
51   */
52  public class DefaultComponentBuilder implements ComponentBuilder
53  {
54    Class clazz;
55  
56    BeanInfo beanInfo;
57  
58    List<PropertyDescriptor> editableProperties = new ArrayList<PropertyDescriptor>();
59  
60    Map<String, PropertyDescriptor> nameToDescriptor = new HashMap<String, PropertyDescriptor>();
61  
62    List<BeanProperty> properties = new ArrayList<BeanProperty>();
63  
64    /*** Creates a new instance of DefaultComponentFactory */
65    public DefaultComponentBuilder(Class clazz) throws IntrospectionException
66    {
67      this(clazz, null);
68    }
69  
70    /*** Creates a new instance of DefaultComponentFactory */
71    public DefaultComponentBuilder(Class clazz, String[] properties)
72        throws IntrospectionException
73    {
74      this.clazz = clazz;
75  
76      beanInfo = Introspector.getBeanInfo(clazz);
77      PropertyDescriptor[] propertyDescriptors = beanInfo
78          .getPropertyDescriptors();
79  
80      if (properties != null)
81      {
82        for (int i = 0; i < properties.length; i++)
83        {
84          String propertyName = properties[i];
85          PropertyDescriptor propertyDescriptor = null;
86  
87          for (int index = 0; index < propertyDescriptors.length; index++)
88          {
89            PropertyDescriptor thisDescriptor = propertyDescriptors[index];
90            if (thisDescriptor.getName().equals(propertyName))
91            {
92              propertyDescriptor = thisDescriptor;
93              break;
94            }
95          }
96  
97          if (propertyDescriptor == null)
98            throw new RuntimeException("Could not find property '" + propertyName
99                + "' in class " + clazz.getName());
100         else
101         {
102           this.properties.add(new BeanProperty(propertyDescriptor.getName(),
103               propertyDescriptor.getPropertyType()));
104           nameToDescriptor
105               .put(propertyDescriptor.getName(), propertyDescriptor);
106         }
107       }
108     }
109   }
110 
111   public String getDeclaration(String name, Map<String, Object> beanProperties)
112   {
113     StringBuffer buffer = new StringBuffer();
114     buffer.append(clazz.getName() + " " + name + " = new " + clazz.getName()
115         + "(");
116 
117     if (beanProperties.containsKey("text"))
118       buffer.append("\"" + (String) beanProperties.get("text") + "\"");
119 
120     buffer.append(");\n");
121     return buffer.toString();
122   }
123 
124   public ComponentDef getComponentDef(String name, Map<String, Object> beanProperties)
125   {
126 	  // imports
127 	  // declarations
128 	  // add
129 	  
130 	  String imp  = "import " + clazz.getName() + ";";
131 // does not work with JDK 1.4	  
132 //	  String decl = clazz.getSimpleName() + " ${name}= new " + clazz.getSimpleName() + "(";
133 	  String decl = clazz.getName() + " ${name}= new " + clazz.getName() + "(";
134 		
135 	  if (beanProperties.containsKey("text"))
136 		  decl += "\"" + (String) beanProperties.get("text") + "\"";
137       decl += ");";		
138 	  
139 //	  String decl = getDeclaration(name, beanProperties);
140 	  String add  = "${container}.add(${name}, \"${name}\");";
141 	  
142 	  ComponentDef cd = new ComponentDef(name,imp,decl,add);
143 	  return cd;
144   }
145   
146   public java.awt.Component getInstance(
147       java.util.Map<String, Object> objectProperties)
148       throws InstantiationException, IllegalAccessException,
149       InvocationTargetException
150   {
151     Object instance = clazz.newInstance();
152 
153     for (Iterator i = objectProperties.keySet().iterator(); i.hasNext();)
154     {
155       String key = (String) i.next();
156       PropertyDescriptor propertyDescriptor = nameToDescriptor.get(key);
157       Object value = objectProperties.get(key);
158       Method writeMethod = propertyDescriptor.getWriteMethod();
159       writeMethod.invoke(instance, new Object[] { value });
160     }
161 
162     return (Component) instance;
163   }
164 
165   public boolean isComponentALayoutContainer()
166   {
167     return clazz.equals(JPanel.class);
168   }
169 
170   public String toString()
171   {
172     return clazz.getName();
173   }
174 
175   public List<BeanProperty> getProperties()
176   {
177     return properties;
178   }
179 
180 }