1 19 20 package org.netbeans.modules.form; 21 22 import java.lang.reflect.Modifier ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import org.openide.DialogDescriptor; 28 import org.openide.DialogDisplayer; 29 import org.openide.ErrorManager; 30 import org.openide.NotifyDescriptor; 31 import org.openide.util.Exceptions; 32 import org.openide.util.HelpCtx; 33 import org.openide.util.NbBundle; 34 35 import static org.netbeans.modules.form.CustomCodeData.*; 36 37 43 44 public class CodeCustomizer implements CustomCodeView.Listener { 45 46 private FormModel formModel; 47 48 private Map <RADComponent, CustomCodeData> changedDataMap = new HashMap (); 50 51 private CustomCodeView codeView; 52 53 private RADComponent customizedComponent; 55 private CodeCustomizer(FormModel formModel) { 56 this.formModel = formModel; 57 codeView = new CustomCodeView(this, FormEditor.getFormDataObject(formModel)); 58 setupComponentNames(); 59 } 60 61 public static void show(RADComponent metacomp) { 62 CodeCustomizer customizer = new CodeCustomizer(metacomp.getFormModel()); 63 customizer.selectComponent(metacomp); 64 customizer.show(); 65 } 66 67 private void show() { 68 JavaCodeGenerator codeGen = (JavaCodeGenerator) FormEditor.getCodeGenerator(formModel); 69 codeGen.regenerateCode(); 71 DialogDescriptor dd = new DialogDescriptor(codeView, 72 NbBundle.getMessage(CodeCustomizer.class, "TITLE_CodeCustomizer"), true, DialogDescriptor.OK_CANCEL_OPTION, null, null); 74 dd.setHelpCtx(new HelpCtx("gui.codecustomizer")); Object res = DialogDisplayer.getDefault().notify(dd); 76 if (DialogDescriptor.OK_OPTION.equals(res)) { 77 retreiveCurrentData(); 78 storeChanges(); 79 } 80 } 81 82 private void setupComponentNames() { 83 Collection <RADComponent> allComps = formModel.getAllComponents(); 84 String [] compNames = new String [allComps.size()]; 85 int i = 0; 86 for (RADComponent metacomp : allComps) { 87 compNames[i++] = metacomp.getName(); 88 } 89 Arrays.sort(compNames, 0, compNames.length-1); 90 codeView.setComponentNames(compNames); 91 } 92 93 private void selectComponent(RADComponent metacomp) { 94 customizedComponent = metacomp; 95 CustomCodeData codeData = changedDataMap.get(metacomp); 96 if (codeData == null) { 97 codeData = JavaCodeGenerator.getCodeData(metacomp); 98 codeData.check(); 99 } 100 codeView.setCodeData(customizedComponent.getName(), codeData); 101 } 102 103 private void retreiveCurrentData() { 104 if (codeView.isChanged()) { 105 changedDataMap.put(customizedComponent, codeView.retreiveCodeData()); 106 } 107 } 108 109 private void storeChanges() { 110 for (Map.Entry <RADComponent, CustomCodeData> e : changedDataMap.entrySet()) { 111 storeComponent(e.getKey(), e.getValue(), true); 112 } 113 changedDataMap.clear(); 114 } 115 116 private void storeComponent(RADComponent metacomp, 117 CustomCodeData codeData, 118 boolean definite) 119 { 120 storeCodeCategory(metacomp, codeData, CodeCategory.CREATE_AND_INIT, definite); 121 storeCodeCategory(metacomp, codeData, CodeCategory.DECLARATION, definite); 122 storeDeclaration(metacomp, codeData.getDeclarationData(), definite); 123 } 124 125 private static void storeCodeCategory(RADComponent metacomp, 126 CustomCodeData codeData, 127 CodeCategory category, 128 boolean definite) 129 { 130 int eCount = codeData.getEditableBlockCount(category); 131 for (int i=0; i < eCount; i++) { 132 EditableBlock eBlock = codeData.getEditableBlock(category, i); 133 for (CodeEntry e : eBlock.getEntries()) { 134 storeCodeEntry(metacomp, e, definite); 135 } 136 } 137 int gCount = codeData.getGuardedBlockCount(category); 138 for (int i=0; i < gCount; i++) { 139 GuardedBlock gBlock = codeData.getGuardedBlock(category, i); 140 if (gBlock.isCustomizable()) { 141 storeCodeEntry(metacomp, gBlock.getCustomEntry(), definite); 142 } 143 } 144 } 145 146 private static void storeCodeEntry(RADComponent metacomp, CodeEntry entry, boolean definite) { 147 FormProperty prop = entry.getTargetProperty(); 148 String code = entry.getCode(); 149 150 boolean firing; 151 if (!definite) { 152 firing = prop.isChangeFiring(); 153 prop.setChangeFiring(false); 154 } 155 else firing = true; 156 157 try { 158 if (entry.isPropertyPreInit()) { 159 prop.setPreCode(code); 160 } 161 else if (entry.isPropertyPostInit()) { 162 prop.setPostCode(code); 163 } 164 else if (prop instanceof RADProperty) { if (code != null) { Object codeValue = new FormProperty.ValueWithEditor( 167 new RADConnectionPropertyEditor.RADConnectionDesignValue(code), 168 new RADConnectionPropertyEditor(prop.getValueType())); 169 prop.setValue(codeValue); 170 } 171 else if (JavaCodeGenerator.isPropertyWithCustomCode(prop)) { prop.restoreDefaultValue(); if (!definite && prop.getPreCode() == null && prop.getPostCode() == null) 174 prop.setPreCode("\n"); } 176 } 178 else { prop.setValue(code != null ? code : ""); } 181 } 182 catch (Exception ex) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 184 } 185 186 if (!definite) 187 prop.setChangeFiring(firing); 188 } 189 190 private static void storeDeclaration(RADComponent metacomp, VariableDeclaration decl, boolean definite) { 191 FormProperty varProp = (FormProperty) metacomp.getSyntheticProperty( 192 JavaCodeGenerator.PROP_VARIABLE_LOCAL); 193 FormProperty modifProp = (FormProperty) metacomp.getSyntheticProperty( 194 JavaCodeGenerator.PROP_VARIABLE_MODIFIER); 195 196 boolean firing; 197 if (!definite) { 198 firing = varProp.isChangeFiring(); 199 varProp.setChangeFiring(false); 200 modifProp.setChangeFiring(false); 201 } 202 else firing = true; 203 204 try { 205 varProp.setValue(decl.local); 206 int modif = decl.modifiers; 207 if (modif < 0) 208 modif = (((Integer )modifProp.getValue()).intValue() & ~Modifier.FINAL) 209 | (modif & Modifier.FINAL); 210 modifProp.setValue(modif); 211 } 212 catch (Exception ex) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 214 } 215 216 if (!definite) { 217 varProp.setChangeFiring(firing); 218 modifProp.setChangeFiring(firing); 219 } 220 } 221 222 225 public void componentExchanged(String compName) { 226 retreiveCurrentData(); 227 selectComponent(formModel.findRADComponent(compName)); 228 } 229 230 public void renameInvoked() { 231 NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine( 232 NbBundle.getMessage(CodeCustomizer.class, "CTL_RenameLabel"), NbBundle.getMessage(CodeCustomizer.class, "CTL_RenameTitle")); input.setInputText(customizedComponent.getName()); 235 236 if (NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(input))) { 237 retreiveCurrentData(); 239 CustomCodeData codeData = changedDataMap.get(customizedComponent); 240 if (codeData != null) { 241 NotifyDescriptor.Confirmation confirm = new NotifyDescriptor.Confirmation( 242 NbBundle.getMessage(CodeCustomizer.class, "CTL_ApplyChangesLabel"), NbBundle.getMessage(CodeCustomizer.class, "CTL_ApplyChangesTitle"), NotifyDescriptor.OK_CANCEL_OPTION); 245 if (!NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(confirm))) 246 return; 247 248 storeComponent(customizedComponent, codeData, true); 249 changedDataMap.remove(customizedComponent); 250 } 251 252 try { 253 String newName = input.getInputText(); 254 if (!newName.equals("")) customizedComponent.setName(newName); 256 } 257 catch (IllegalArgumentException e) { 258 Exceptions.printStackTrace(e); 259 return; 260 } 261 262 setupComponentNames(); 263 codeData = JavaCodeGenerator.getCodeData(customizedComponent); 264 codeData.check(); 265 codeView.setCodeData(customizedComponent.getName(), codeData); 266 } 267 } 268 269 public void declarationChanged() { 270 CustomCodeData original = JavaCodeGenerator.getCodeData(customizedComponent); 272 retreiveCurrentData(); 274 CustomCodeData actual = changedDataMap.get(customizedComponent); 275 storeComponent(customizedComponent, actual, false); 276 CustomCodeData renewed = JavaCodeGenerator.getCodeData(customizedComponent); 278 renewed.check(); 279 storeComponent(customizedComponent, original, false); 281 codeView.setCodeData(customizedComponent.getName(), renewed); 283 } 284 } 285 | Popular Tags |