KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > model > mapping > TextAreaMapper


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.kilim.model.mapping;
20
21 import java.util.HashMap JavaDoc;
22 import javax.swing.JTextArea JavaDoc;
23
24 import org.objectweb.kilim.KilimException;
25
26 /**
27  * @author horn
28  */

29 public class TextAreaMapper implements Mapper {
30     private Mapper wrappedMapper;
31     private JTextArea JavaDoc textArea;
32     private String JavaDoc prefix = "\n ";
33     
34     /**
35      * Method JavaLogMapper.
36      * @param aMapper :
37      */

38     public TextAreaMapper(Mapper aMapper) {
39         wrappedMapper = aMapper;
40         textArea = new JTextArea JavaDoc("++++++++++++++START TRACE++++++++++++++++++++");
41     }
42     
43     /**
44      * @see java.lang.Object#Object()
45      */

46     public TextAreaMapper() {
47         this(null);
48     }
49     
50     /**
51      * Method getTreeModel.
52      * @return KilimTraceTreeModel
53      */

54     public JTextArea JavaDoc getTextArea() {
55         return textArea;
56     }
57     
58     /**
59      * @see org.objectweb.kilim.model.mapping.Mapper#enterContext(Object)
60      */

61     public void enterContext(MappingContext aContext) throws KilimException { }
62     
63     /**
64      * @see org.objectweb.kilim.model.mapping.Mapper#leaveContext()
65      */

66     public void leaveContext(MappingContext aContext) throws KilimException { }
67
68     /**
69      * @see org.objectweb.kilim.model.mapping.KilimMapper#getGetterValue(Object, boolean, String, MappingContext)
70      */

71     public Object JavaDoc getGetterValue (Object JavaDoc aSupport, boolean isStatic, String JavaDoc fieldName, MappingContext aContext) throws KilimException {
72         Object JavaDoc resultValue = null;
73         if (wrappedMapper != null) {
74             resultValue = wrappedMapper.getGetterValue (aSupport, isStatic, fieldName, aContext);
75         }
76         textArea.append(prefix + getResultString(resultValue) + ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + fieldName + ";");
77         //prefix = "\n";
78
return resultValue;
79     }
80     
81     /**
82      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeSetter(Object, boolean, String, Object, MappingContext)
83      */

84     public void executeSetter(Object JavaDoc aSupport, boolean isStatic, String JavaDoc fieldName, Object JavaDoc toBeSet, MappingContext aContext) throws KilimException {
85         textArea.append(prefix + ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + fieldName + " = " + toBeSet + ";");
86         
87         if (wrappedMapper != null) {
88             wrappedMapper.executeSetter(aSupport, isStatic, fieldName, toBeSet, aContext);
89         }
90         //prefix = "\n";
91
}
92
93     /**
94      * @see org.objectweb.kilim.model.mapping.KilimMapper#getMethodValue(Object, boolean, String, Object[], String[], MappingContext)
95      */

96     public Object JavaDoc getMethodValue(Object JavaDoc aSupport, boolean isStatic, String JavaDoc aMethodName, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
97         Object JavaDoc resultValue = null;
98         if (wrappedMapper != null) {
99             resultValue = wrappedMapper.getMethodValue (aSupport, isStatic, aMethodName, paramObjects, typeNames, aContext);
100         }
101         
102         StringBuffer JavaDoc bffr = new StringBuffer JavaDoc(getResultString(resultValue) + ((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + aMethodName);
103         addParameters(bffr , paramObjects , typeNames);
104         bffr.append(";");
105         textArea.append(prefix + bffr.toString());
106         //prefix = "\n";
107
return resultValue;
108     }
109     
110     /**
111      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeMethod(Object, boolean, String, Object[], String[], MappingContext)
112      */

113     public void executeMethod(Object JavaDoc aSupport, boolean isStatic, String JavaDoc aMethodName, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
114         if (wrappedMapper != null) {
115             wrappedMapper.executeMethod(aSupport, isStatic, aMethodName, paramObjects, typeNames, aContext);
116         }
117         
118         StringBuffer JavaDoc bffr = new StringBuffer JavaDoc(((isStatic) ? "[static] " : "") + normalizeToString(aSupport) + "." + aMethodName);
119         addParameters(bffr , paramObjects , typeNames);
120         bffr.append(";");
121         textArea.append(prefix + bffr.toString());
122         //prefix = "\n";
123
}
124             
125     /**
126      * @see org.objectweb.kilim.model.mapping.KilimMapper#getConstructorValue(Class, Object[], String[], MappingContext)
127      */

128     public Object JavaDoc getConstructorValue(Class JavaDoc aClass, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
129         Object JavaDoc resultValue = null;
130         if (wrappedMapper != null) {
131             resultValue = wrappedMapper.getConstructorValue(aClass, paramObjects, typeNames, aContext);
132         }
133         
134         StringBuffer JavaDoc bffr = new StringBuffer JavaDoc(getVariableName(resultValue) + " = new " + aClass.getName());
135         addParameters(bffr , paramObjects , typeNames);
136         bffr.append(";");
137         textArea.append(prefix + bffr.toString());
138         //prefix = "\n";
139
return resultValue;
140     }
141     
142     /**
143      * @see org.objectweb.kilim.model.mapping.KilimMapper#executeConstructor(Class, Object[], String[], MappingContext)
144      */

145     public void executeConstructor(Class JavaDoc aClass, Object JavaDoc[] paramObjects, String JavaDoc[] typeNames, MappingContext aContext) throws KilimException {
146         if (wrappedMapper != null) {
147             wrappedMapper.getConstructorValue(aClass, paramObjects, typeNames, aContext);
148         }
149         
150         StringBuffer JavaDoc bffr = new StringBuffer JavaDoc("new " + aClass.getName());
151         addParameters(bffr , paramObjects , typeNames);
152         bffr.append(";");
153         textArea.append(prefix + bffr.toString());
154         //prefix = "\n";
155
}
156
157     /**
158      * @see org.objectweb.kilim.model.mapping.KilimMapper#getExternalValue(Object, MappingContext)
159      */

160     public Object JavaDoc getExternalValue(Object JavaDoc aValue, MappingContext aContext) throws KilimException {
161         Object JavaDoc resultValue = null;
162         if (wrappedMapper != null) {
163             resultValue = wrappedMapper.getExternalValue(aValue, aContext);
164         }
165         return resultValue;
166     }
167     
168     /**
169      * @see org.objectweb.kilim.model.mapping.KilimMapper#getPropertyValue(Object, MappingContext)
170      */

171     public Object JavaDoc getPropertyValue(Object JavaDoc aValue, MappingContext aContext) throws KilimException {
172         Object JavaDoc resultValue = null;
173         if (wrappedMapper != null) {
174             resultValue = wrappedMapper.getPropertyValue(aValue, aContext);
175         }
176         return resultValue;
177     }
178         
179     /**
180      * @see org.objectweb.kilim.model.mapping.KilimMapper#getClassValue(String, MappingContext)
181      */

182     public Object JavaDoc getClassValue(String JavaDoc aClassName, MappingContext aContext) throws KilimException {
183         Object JavaDoc resultValue = null;
184         if (wrappedMapper != null) {
185             resultValue = wrappedMapper.getClassValue(aClassName, aContext);
186         }
187         return resultValue;
188     }
189     
190     /**
191      * @see org.objectweb.kilim.model.mapping.KilimMapper#getEventSourceValue(MappingContext)
192      */

193     public Object JavaDoc getEventSourceValue(MappingContext aContext) throws KilimException {
194         Object JavaDoc resultValue = null;
195         if (wrappedMapper != null) {
196             resultValue = wrappedMapper.getEventSourceValue(aContext);
197         }
198         return resultValue;
199     }
200     
201     /**
202      * @see org.objectweb.kilim.model.mapping.KilimMapper#getNullElementValue(MappingContext)
203      */

204     public Object JavaDoc getNullElementValue(MappingContext aContext) throws KilimException {
205         Object JavaDoc resultValue = null;
206         if (wrappedMapper != null) {
207             resultValue = wrappedMapper.getNullElementValue(aContext);
208         }
209         return resultValue;
210
211     }
212
213     /**
214      * Method executeNullElement.
215      * @param aContext :
216      */

217     public void executeNullElement(MappingContext aContext) {
218         if (wrappedMapper != null) {
219             executeNullElement(aContext);
220         }
221     }
222     
223     //Logging facilities ------------------------------------------------------------------
224

225     private static final void addParameters(StringBuffer JavaDoc bffr , Object JavaDoc[] paramObjects, String JavaDoc[] typeNames) {
226             bffr.append("(");
227             for (int i = 0 ; i < paramObjects.length ; i++) {
228                 if (i != 0) { bffr.append(" , "); }
229                 bffr.append(normalizeToString(paramObjects[i]));
230             }
231             bffr.append(")");
232     }
233     
234     private static String JavaDoc normalizeToString(Object JavaDoc obj) {
235         if (obj instanceof Number JavaDoc) { return obj.toString(); }
236         return getVariableName(obj);
237     }
238     
239     private static HashMap JavaDoc variables = new HashMap JavaDoc() , inverse_table = new HashMap JavaDoc();
240     
241     private static String JavaDoc getResultString(Object JavaDoc obj) {
242         String JavaDoc rslt = getVariableName(obj);
243         if (rslt == null) { return ""; }
244         return rslt + " = ";
245     }
246     
247     private static String JavaDoc getVariableName(Object JavaDoc obj) {
248         
249         if (obj == null) { return null; }
250         
251         Object JavaDoc vl = inverse_table.get(obj);
252         if (vl != null) { return (String JavaDoc) vl; }
253         
254         String JavaDoc clss_nm = obj.getClass().getName();
255         String JavaDoc nm = clss_nm.substring(clss_nm.lastIndexOf('.') + 1);
256         nm = nm.toLowerCase();
257         Object JavaDoc allrd_thr = variables.get(nm);
258         int nb = 0;
259         while (allrd_thr != null) {
260             allrd_thr = variables.get(nm + (++nb));
261         }
262         if (nb != 0) { nm = nm + nb; }
263         variables.put (nm , obj);
264         inverse_table.put(obj , nm);
265         return nm;
266     }
267 }
Popular Tags