001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.codegen.builder;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024:
025: import javax.swing.JLabel;
026: import javax.swing.JPanel;
027:
028: import com.jeta.forms.beanmgr.BeanManager;
029: import com.jeta.forms.components.label.JETALabel;
030: import com.jeta.forms.gui.beans.DynamicBeanInfo;
031: import com.jeta.forms.gui.beans.DynamicPropertyDescriptor;
032: import com.jeta.forms.gui.beans.JETABean;
033: import com.jeta.forms.gui.beans.JETABeanFactory;
034: import com.jeta.forms.gui.beans.JETAPropertyDescriptor;
035: import com.jeta.forms.gui.form.GridView;
036: import com.jeta.forms.logger.FormsLogger;
037: import com.jeta.forms.store.memento.PropertiesMemento;
038: import com.jeta.forms.store.properties.TransformOptionsProperty;
039: import com.jeta.open.registry.JETARegistry;
040:
041: public class BeanWriter {
042: private String m_bean_variable_name;
043: private Class m_bean_type;
044:
045: private String m_result_variable_name;
046: private Class m_result_type;
047:
048: private LinkedList m_statements = new LinkedList();
049:
050: /** @directed */
051: public BeanWriter(DeclarationManager decl_mgr, PropertiesMemento pm) {
052: assert (pm != null);
053: String classname = pm.getBeanClassName();
054:
055: if (GridView.class.getName().equals(classname))
056: classname = "javax.swing.JPanel";
057: else if (JETALabel.class.getName().equals(classname))
058: classname = "javax.swing.JLabel";
059:
060: try {
061: Class beanclass = null;
062: try {
063: BeanManager bmgr = (BeanManager) JETARegistry
064: .lookup(BeanManager.COMPONENT_ID);
065: if (bmgr != null) {
066: beanclass = bmgr.getBeanClass(classname);
067: }
068: } catch (Exception e) {
069: FormsLogger.severe(e);
070: }
071:
072: if (beanclass == null) {
073: beanclass = Class.forName(classname);
074: }
075:
076: VariableDeclaration ds = null;
077: /**
078: * don't declare panels or labels as member variables if they don't
079: * have a name
080: */
081: if (JLabel.class.isAssignableFrom(beanclass)
082: || JPanel.class.isAssignableFrom(beanclass)) {
083: String compname = pm.getComponentName();
084: if (compname == null)
085: compname = "";
086: else
087: compname = compname.trim();
088:
089: if (compname.length() == 0) {
090: ds = new LocalVariableDeclaration(decl_mgr,
091: beanclass, null);
092: /** temporary hack */
093: ((MethodWriter) decl_mgr).addStatement(ds);
094: }
095: }
096:
097: if (ds == null) {
098: ds = new MemberVariableDeclaration(decl_mgr, beanclass,
099: pm.getComponentName());
100: decl_mgr.addMemberVariable(ds);
101: }
102:
103: setBeanVariable(ds.getVariable(), beanclass);
104: setResultVariable(ds.getVariable(), beanclass);
105:
106: PropertyWriterFactory fac = (PropertyWriterFactory) JETARegistry
107: .lookup(PropertyWriterFactory.COMPONENT_ID);
108: LinkedList dynamic_props = new LinkedList();
109:
110: Class lookup_class = beanclass;
111: if (GridView.class.getName().equals(pm.getBeanClassName()))
112: lookup_class = GridView.class;
113:
114: DynamicBeanInfo beaninfo = JETABeanFactory
115: .getBeanInfo(lookup_class);
116: Collection jeta_pds = beaninfo.getPropertyDescriptors();
117: Iterator iter = jeta_pds.iterator();
118: while (iter.hasNext()) {
119: try {
120: JETAPropertyDescriptor jpd = (JETAPropertyDescriptor) iter
121: .next();
122: if (pm.containsProperty(jpd.getName())) {
123:
124: if (jpd instanceof DynamicPropertyDescriptor) {
125: dynamic_props.add(jpd);
126: } else {
127: Object prop_value = pm.getPropertyValue(jpd
128: .getName());
129: if ("name".equals(jpd.getName())
130: && "".equals(prop_value)) {
131: continue;
132: }
133:
134: PropertyWriter pw = fac.createWriter(jpd
135: .getPropertyType());
136: if (pw != null) {
137: pw.writeProperty(decl_mgr, this , jpd,
138: prop_value);
139: }
140: }
141: }
142: } catch (Exception e) {
143: FormsLogger.debug(e);
144: }
145: }
146:
147: /** now do the dynamic props */
148: iter = dynamic_props.iterator();
149: while (iter.hasNext()) {
150: DynamicPropertyDescriptor dpd = (DynamicPropertyDescriptor) iter
151: .next();
152:
153: PropertyWriter pw = fac.createWriter(dpd
154: .getPropertyType());
155: if (pw != null) {
156: Object prop_value = null;
157: if (dpd.getPropertyType() == TransformOptionsProperty.class) {
158: JETABean jetabean = JETABeanFactory.createBean(
159: beanclass.getName(), null, true, true);
160: jetabean.setState(pm);
161: TransformOptionsProperty tprop = (TransformOptionsProperty) jetabean
162: .getCustomProperty(dpd.getName());
163: prop_value = tprop;
164: } else {
165: prop_value = pm.getPropertyValue(dpd.getName());
166: }
167: pw.writeProperty(decl_mgr, this , dpd, prop_value);
168: }
169: }
170: } catch (Exception e) {
171: e.printStackTrace();
172: }
173: }
174:
175: public void addStatement(Statement stmt) {
176: if (stmt != null)
177: m_statements.add(stmt);
178: }
179:
180: public Collection getStatements() {
181: return m_statements;
182: }
183:
184: public void setBeanVariable(String varName, Class beanType) {
185: m_bean_variable_name = varName;
186: m_bean_type = beanType;
187: }
188:
189: public String getBeanVariable() {
190: return m_bean_variable_name;
191: }
192:
193: public Class getBeanType() {
194: return m_bean_type;
195: }
196:
197: public String getResultVariable() {
198: return m_result_variable_name;
199: }
200:
201: public Class getResultType() {
202: return m_result_type;
203: }
204:
205: public void setResultVariable(String varName, Class resultType) {
206: m_result_variable_name = varName;
207: m_result_type = resultType;
208: }
209: }
|