KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > CodeCustomizer


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;
21
22 import java.lang.reflect.Modifier JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
38  * This class controls the code customization - opens the
39  * customizer dialog, reads and stores data, etc.
40  *
41  * @author Tomas Pavek
42  */

43
44 public class CodeCustomizer implements CustomCodeView.Listener {
45
46     private FormModel formModel;
47
48     // changed code data for components
49
private Map JavaDoc<RADComponent, CustomCodeData> changedDataMap = new HashMap JavaDoc();
50
51     private CustomCodeView codeView;
52
53     private RADComponent customizedComponent; // actual selected component
54

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(); // to have fresh code for code completion
70

71         DialogDescriptor dd = new DialogDescriptor(codeView,
72                 NbBundle.getMessage(CodeCustomizer.class, "TITLE_CodeCustomizer"), // NOI18N
73
true, DialogDescriptor.OK_CANCEL_OPTION, null, null);
74         dd.setHelpCtx(new HelpCtx("gui.codecustomizer")); // NOI18N
75
Object JavaDoc 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 JavaDoc<RADComponent> allComps = formModel.getAllComponents();
84         String JavaDoc[] compNames = new String JavaDoc[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 JavaDoc<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 JavaDoc 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) { // custom code for bean property
165
if (code != null) { // custom code specified
166
Object JavaDoc 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)) { // default code
172
prop.restoreDefaultValue(); // cancel custom code
173
if (!definite && prop.getPreCode() == null && prop.getPostCode() == null)
174                         prop.setPreCode("\n"); // set something to get the property generated again // NOI18N
175
}
176                 // otherwise do nothing (property does not contain custom code)
177
}
178             else { // synthetic code property
179
prop.setValue(code != null ? code : ""); // NOI18N
180
}
181         }
182         catch (Exception JavaDoc ex) { // should not happen
183
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 JavaDoc)modifProp.getValue()).intValue() & ~Modifier.FINAL)
209                         | (modif & Modifier.FINAL);
210             modifProp.setValue(modif);
211         }
212         catch (Exception JavaDoc ex) { // should not happen
213
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
214         }
215
216         if (!definite) {
217             varProp.setChangeFiring(firing);
218             modifProp.setChangeFiring(firing);
219         }
220     }
221
222     // -----
223
// CustomCodeView.Listener implementation
224

225     public void componentExchanged(String JavaDoc 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"), // NOI18N
233
NbBundle.getMessage(CodeCustomizer.class, "CTL_RenameTitle")); // NOI18N
234
input.setInputText(customizedComponent.getName());
235
236         if (NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(input))) {
237             // code data must be saved to component before renaming
238
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"), // NOI18N
243
NbBundle.getMessage(CodeCustomizer.class, "CTL_ApplyChangesTitle"), // NOI18N
244
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 JavaDoc newName = input.getInputText();
254                 if (!newName.equals("")) // NOI18N
255
customizedComponent.setName(newName);
256             }
257             catch (IllegalArgumentException JavaDoc 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         // remeber the current data - we'll return to it so there is no change in the model
271
CustomCodeData original = JavaCodeGenerator.getCodeData(customizedComponent);
272         // write the customized configuration to the component
273
retreiveCurrentData();
274         CustomCodeData actual = changedDataMap.get(customizedComponent);
275         storeComponent(customizedComponent, actual, false);
276         // get the new code data for the changed configuartion
277
CustomCodeData renewed = JavaCodeGenerator.getCodeData(customizedComponent);
278         renewed.check();
279         // restore the original data in the component
280
storeComponent(customizedComponent, original, false);
281         // set the new data to the view
282
codeView.setCodeData(customizedComponent.getName(), renewed);
283     }
284 }
285
Popular Tags