1 19 20 package org.netbeans.modules.form.codestructure; 21 22 import java.beans.PropertyEditor ; 23 import java.lang.reflect.*; 24 import org.openide.nodes.Node; 25 import org.netbeans.modules.form.*; 26 27 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 type, 41 PropertyEditor 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; } 64 65 Object metaOrigin = expression.getOrigin().getMetaObject(); 66 if (metaOrigin instanceof PropertyEditor ) 67 fProperty.setCurrentEditor((PropertyEditor )metaOrigin); 68 } 69 70 try { 71 property.setValue(expression.getOrigin().getValue()); 72 } 73 catch (Exception ex) { 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 CodeExpression[] params = statement.getStatementParameters(); 88 if (params.length != 1) 89 throw new IllegalArgumentException (); 90 91 readPropertyExpression(params[0], property, allowChangeFiring); 92 } 93 94 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 getType() { 104 return property.getValueType(); 105 } 106 107 public CodeExpression getParentExpression() { 108 return null; 109 } 110 111 public Object getValue() { 112 try { 113 return property.getValue(); 114 } 115 catch (Exception ex) {} 117 return null; 118 } 119 120 public Object getMetaObject() { 121 return property; 122 } 123 124 public String getJavaCodeString(String parentStr, String [] paramsStr) { 125 try { 126 PropertyEditor pred = property.getPropertyEditor(); 127 pred.setValue(property.getValue()); 128 return pred.getJavaInitializationString(); 129 } 130 catch (Exception ex) {} 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 getType() { 147 return property.getValueType(); 148 } 149 150 public CodeExpression getParentExpression() { 151 return null; 152 } 153 154 public Object getValue() { 155 try { 156 return property.getRealValue(); 157 } 159 catch (Exception ex) {} 161 return null; 162 } 163 164 public Object getMetaObject() { 165 return property; 166 } 167 168 public String getJavaCodeString(String parentStr, String [] 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 type; 179 private PropertyEditor propertyEditor; 180 181 public PropertyEditorOrigin(Class type, PropertyEditor prEd) { 182 this.type = type; 183 this.propertyEditor = prEd; 184 } 185 186 public Class getType() { 187 return type; 188 } 189 190 public CodeExpression getParentExpression() { 191 return null; 192 } 193 194 public Object getValue() { 195 return propertyEditor.getValue(); 196 } 197 198 public Object getMetaObject() { 199 return propertyEditor; 200 } 201 202 public String getJavaCodeString(String parentStr, String [] 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 getType() { 219 return component.getBeanClass(); 220 } 221 222 public CodeExpression getParentExpression() { 223 return null; 224 } 225 226 public Object getMetaObject() { 227 return component; 228 } 229 230 public Object getValue() { 231 return component.getBeanInstance(); 232 } 233 234 public CodeExpression[] getCreationParameters() { 235 return CodeStructure.EMPTY_PARAMS; 236 } 237 238 public String getJavaCodeString(String parentStr, String [] paramsStr) { 239 if (component == component.getFormModel().getTopRADComponent()) 240 return "this"; 242 StringBuffer buf = new StringBuffer (); 243 244 buf.append("new "); buf.append(component.getBeanClass().getName().replace('&','.')); buf.append("()"); 248 return buf.toString(); 249 } 250 } 251 } 252 | Popular Tags |