KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > codestructure > FormCodeSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.codestructure;
21
22 import java.beans.PropertyEditor JavaDoc;
23 import java.lang.reflect.*;
24 import org.openide.nodes.Node;
25 import org.netbeans.modules.form.*;
26
27 /**
28  * @author Tomas Pavek
29  */

30
31 public class FormCodeSupport {
32
33     public static CodeExpressionOrigin createOrigin(Node.Property property) {
34         if (property instanceof FormProperty)
35             return new FormPropertyValueOrigin((FormProperty)property);
36         else
37             return new PropertyValueOrigin(property);
38     }
39
40     public static CodeExpressionOrigin createOrigin(Class JavaDoc type,
41                                                     PropertyEditor JavaDoc prEd)
42     {
43         return new PropertyEditorOrigin(type, prEd);
44     }
45
46     public static CodeExpressionOrigin createOrigin(RADComponent component) {
47         return new RADComponentOrigin(component);
48     }
49
50     public static void readPropertyExpression(CodeExpression expression,
51                                               Node.Property property,
52                                               boolean allowChangeFiring)
53     {
54         FormProperty fProperty = property instanceof FormProperty ?
55                                  (FormProperty) property : null;
56
57         if (fProperty != null) {
58             if (!allowChangeFiring) {
59                 if (fProperty.isChangeFiring())
60                     fProperty.setChangeFiring(false);
61                 else
62                     allowChangeFiring = true; // just not to set firing back
63
}
64
65             Object JavaDoc metaOrigin = expression.getOrigin().getMetaObject();
66             if (metaOrigin instanceof PropertyEditor JavaDoc)
67                 fProperty.setCurrentEditor((PropertyEditor JavaDoc)metaOrigin);
68         }
69
70         try {
71             property.setValue(expression.getOrigin().getValue());
72         }
73         catch (Exception JavaDoc ex) { // ignore
74
org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
75         }
76         expression.setOrigin(createOrigin(property));
77
78         if (fProperty != null && !allowChangeFiring)
79             fProperty.setChangeFiring(true);
80     }
81
82     public static void readPropertyStatement(CodeStatement statement,
83                                              Node.Property property,
84                                              boolean allowChangeFiring)
85     {
86         // expecting statement with one expression parameter
87
CodeExpression[] params = statement.getStatementParameters();
88         if (params.length != 1)
89             throw new IllegalArgumentException JavaDoc();
90
91         readPropertyExpression(params[0], property, allowChangeFiring);
92     }
93
94     // --------
95

96     static final class PropertyValueOrigin implements CodeExpressionOrigin {
97         private Node.Property property;
98
99         public PropertyValueOrigin(Node.Property property) {
100             this.property = property;
101         }
102
103         public Class JavaDoc getType() {
104             return property.getValueType();
105         }
106
107         public CodeExpression getParentExpression() {
108             return null;
109         }
110
111         public Object JavaDoc getValue() {
112             try {
113                 return property.getValue();
114             }
115             catch (Exception JavaDoc ex) {} // should no happen
116

117             return null;
118         }
119
120         public Object JavaDoc getMetaObject() {
121             return property;
122         }
123
124         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
125             try {
126                 PropertyEditor JavaDoc pred = property.getPropertyEditor();
127                 pred.setValue(property.getValue());
128                 return pred.getJavaInitializationString();
129             }
130             catch (Exception JavaDoc ex) {} // should not happen
131
return null;
132         }
133
134         public CodeExpression[] getCreationParameters() {
135             return CodeStructure.EMPTY_PARAMS;
136         }
137     }
138
139     static final class FormPropertyValueOrigin implements CodeExpressionOrigin {
140         private FormProperty property;
141
142         public FormPropertyValueOrigin(FormProperty property) {
143             this.property = property;
144         }
145
146         public Class JavaDoc getType() {
147             return property.getValueType();
148         }
149
150         public CodeExpression getParentExpression() {
151             return null;
152         }
153
154         public Object JavaDoc getValue() {
155             try {
156                 return property.getRealValue();
157                 // [or getValue() ??]
158
}
159             catch (Exception JavaDoc ex) {} // should no happen
160

161             return null;
162         }
163
164         public Object JavaDoc getMetaObject() {
165             return property;
166         }
167
168         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
169             return property.getJavaInitializationString();
170         }
171
172         public CodeExpression[] getCreationParameters() {
173             return CodeStructure.EMPTY_PARAMS;
174         }
175     }
176
177     static final class PropertyEditorOrigin implements CodeExpressionOrigin {
178         private Class JavaDoc type;
179         private PropertyEditor JavaDoc propertyEditor;
180
181         public PropertyEditorOrigin(Class JavaDoc type, PropertyEditor JavaDoc prEd) {
182             this.type = type;
183             this.propertyEditor = prEd;
184         }
185
186         public Class JavaDoc getType() {
187             return type;
188         }
189
190         public CodeExpression getParentExpression() {
191             return null;
192         }
193
194         public Object JavaDoc getValue() {
195             return propertyEditor.getValue();
196         }
197
198         public Object JavaDoc getMetaObject() {
199             return propertyEditor;
200         }
201
202         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
203             return propertyEditor.getJavaInitializationString();
204         }
205
206         public CodeExpression[] getCreationParameters() {
207             return CodeStructure.EMPTY_PARAMS;
208         }
209     }
210
211     static final class RADComponentOrigin implements CodeExpressionOrigin {
212         private RADComponent component;
213
214         public RADComponentOrigin(RADComponent component) {
215             this.component = component;
216         }
217
218         public Class JavaDoc getType() {
219             return component.getBeanClass();
220         }
221
222         public CodeExpression getParentExpression() {
223             return null;
224         }
225
226         public Object JavaDoc getMetaObject() {
227             return component;
228         }
229
230         public Object JavaDoc getValue() {
231             return component.getBeanInstance();
232         }
233
234         public CodeExpression[] getCreationParameters() {
235             return CodeStructure.EMPTY_PARAMS;
236         }
237
238         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
239             if (component == component.getFormModel().getTopRADComponent())
240                 return "this"; // NOI18N
241

242             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
243
244             buf.append("new "); // NOI18N
245
buf.append(component.getBeanClass().getName().replace('&','.')); // NOI18N
246
buf.append("()"); // NOI18N
247

248             return buf.toString();
249         }
250     }
251 }
252
Popular Tags