KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > wizard > options > NewOptionsIterator


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.apisupport.project.ui.wizard.options;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
32 import org.netbeans.modules.apisupport.project.ManifestManager;
33 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
34 import org.netbeans.modules.apisupport.project.ui.UIUtil;
35 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
36 import org.openide.util.Utilities;
37 import org.openide.WizardDescriptor;
38 import org.openide.util.NbBundle;
39
40 /**
41  * Wizard for generating OptionsPanel
42  *
43  * @author Radek Matous
44  */

45 final class NewOptionsIterator extends BasicWizardIterator {
46     
47     private NewOptionsIterator.DataModel data;
48     
49     private NewOptionsIterator() { /* Use factory method. */ }
50     
51     public static NewOptionsIterator createIterator() {
52         return new NewOptionsIterator();
53     }
54     
55     public Set JavaDoc instantiate() throws IOException JavaDoc {
56         CreatedModifiedFiles cmf = data.getCreatedModifiedFiles();
57         cmf.run();
58         return getCreatedFiles(cmf, data.getProject());
59     }
60     
61     protected BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz) {
62         data = new NewOptionsIterator.DataModel(wiz);
63         return new BasicWizardIterator.Panel[] {
64             new OptionsPanel0(wiz, data),
65             new OptionsPanel(wiz, data)
66         };
67     }
68     
69     public void uninitialize(WizardDescriptor wiz) {
70         super.uninitialize(wiz);
71         data = null;
72     }
73     
74     static final class DataModel extends BasicWizardIterator.BasicDataModel {
75         
76         private static final int ERR_BLANK_DISPLAYNAME = 1;
77         private static final int ERR_BLANK_TOOLTIP = 2;
78         private static final int ERR_BLANK_TITLE = 4;
79         private static final int ERR_BLANK_CATEGORY_NAME = 5;
80         private static final int ERR_BLANK_ICONPATH = 6;
81         private static final int ERR_BLANK_PACKAGE_NAME = 7;
82         private static final int ERR_BLANK_CLASSNAME_PREFIX = 8;
83         private static final int ERR_INVALID_CLASSNAME_PREFIX = 9;
84         
85         
86         private static final int WARNING_INCORRECT_ICON_SIZE = -1;
87         
88         private static final String JavaDoc[] CATEGORY_BUNDLE_KEYS = new String JavaDoc[] {
89             "@@OptionsCategory_Title@@", // NOI18N
90
"@@OptionsCategory_Name@@", // NOI18N
91
};
92         
93         private static final String JavaDoc[] ADVANCED_BUNDLE_KEYS = new String JavaDoc[] {
94             "@@AdvancedOption_DisplayName@@", // NOI18N
95
"@@AdvancedOption_Tooltip@@" // NOI18N
96
};
97         
98         private static final String JavaDoc[] TOKENS = new String JavaDoc[] {
99             "@@PACKAGE_NAME@@", // NOI18N
100
"@@AdvancedOption_CLASS_NAME@@", // NOI18N
101
"@@OptionsCategory_CLASS_NAME@@", // NOI18N
102
"@@Panel_CLASS_NAME@@", // NOI18N
103
"@@OptionsPanelController_CLASS_NAME@@", // NOI18N
104
"@@ICON_PATH@@", // NOI18N
105
ADVANCED_BUNDLE_KEYS[0],
106             ADVANCED_BUNDLE_KEYS[1],
107             CATEGORY_BUNDLE_KEYS[0],
108             CATEGORY_BUNDLE_KEYS[1]
109         };
110                 
111         private static final String JavaDoc FORM_TEMPLATE_SUFFIXES[] = new String JavaDoc[]{"Panel"}; // NOI18N
112
private static final String JavaDoc[] JAVA_TEMPLATE_SUFFIXES = new String JavaDoc[] {
113             "AdvancedOption",//NOI18N
114
"OptionsCategory",//NOI18N
115
"Panel",//NOI18N
116
"OptionsPanelController"//NOI18N
117
};
118         private static final String JavaDoc JAVA_TEMPLATE_PREFIX = "template_myplugin"; // NOI18N
119
private static final String JavaDoc FORM_TEMPLATE_PREFIX = "template_myplugin_form"; // NOI18N
120

121         private CreatedModifiedFiles files;
122         private String JavaDoc codeNameBase;
123         private boolean advanced;
124         
125         //Advanced panel
126
private String JavaDoc displayName;
127         private String JavaDoc tooltip;
128         
129         //OptionsCategory
130
private String JavaDoc title;
131         private String JavaDoc categoryName;
132         private String JavaDoc iconPath;
133         
134         private String JavaDoc classNamePrefix;
135         
136         DataModel(WizardDescriptor wiz) {
137             super(wiz);
138         }
139         
140         int setDataForAdvanced(final String JavaDoc displayName, final String JavaDoc tooltip) {
141             this.advanced = true;
142             this.displayName = displayName;
143             this.tooltip = tooltip;
144             return checkFirstPanel();
145         }
146         
147         int setDataForOptionCategory(final String JavaDoc title,
148                 final String JavaDoc categoryName, final String JavaDoc iconPath) {
149             this.advanced = false;
150             this.title = title;
151             this.categoryName = categoryName;
152             this.iconPath = iconPath;
153             return checkFirstPanel();
154         }
155         
156         public String JavaDoc getPackageName() {
157             String JavaDoc retValue;
158             retValue = super.getPackageName();
159             if (retValue == null) {
160                 retValue = getCodeNameBase();
161                 super.setPackageName(retValue);
162             }
163             return retValue;
164         }
165         
166         public int setPackageAndPrefix(String JavaDoc packageName, String JavaDoc classNamePrefix) {
167             setPackageName(packageName);
168             this.classNamePrefix = classNamePrefix;
169             int errCode = checkFinalPanel();
170             if (isSuccessCode(errCode)) {
171                 generateCreatedModifiedFiles();
172             }
173             return errCode;
174         }
175         
176         private Map JavaDoc getTokenMap() {
177             Map JavaDoc retval = new HashMap JavaDoc();
178             for (int i = 0; i < TOKENS.length; i++) {
179                 if (isAdvanced() && "@@ICON_PATH@@".equals(TOKENS[i])) { // NOI18N
180
continue;
181                 }
182                 retval.put(TOKENS[i], getReplacement(TOKENS[i]));
183             }
184             return retval;
185         }
186         
187         private String JavaDoc getReplacement(String JavaDoc key) {
188             if ("@@PACKAGE_NAME@@".equals(key)) {// NOI18N
189
return getPackageName();
190             } else if ("@@AdvancedOption_CLASS_NAME@@".equals(key)) {// NOI18N
191
return getAdvancedOptionClassName();
192             } else if ("@@OptionsCategory_CLASS_NAME@@".equals(key)) {// NOI18N
193
return getOptionsCategoryClassName();
194             } else if ("@@Panel_CLASS_NAME@@".equals(key)) {// NOI18N
195
return getPanelClassName();
196             } else if ("@@OptionsPanelController_CLASS_NAME@@".equals(key)) {// NOI18N
197
return getOptionsPanelControllerClassName();
198             } else if ("@@ICON_PATH@@".equals(key)) {// NOI18N
199
return addCreateIconOperation(new CreatedModifiedFiles(getProject()), getIconPath());
200             } else if (key.startsWith("@@") && key.endsWith("@@")) {// NOI18N
201
return key.substring(2, key.length()-2)+"_"+getClassNamePrefix();
202             }else {
203                 throw new AssertionError JavaDoc(key);
204             }
205             
206         }
207         
208         
209         private String JavaDoc getBundleValue(String JavaDoc key) {
210             if (key.startsWith("OptionsCategory_Title")) {// NOI18N
211
return getTitle();
212             } else if (key.startsWith("OptionsCategory_Name")) {// NOI18N
213
return getCategoryName();
214             } else if (key.startsWith("AdvancedOption_DisplayName")) {// NOI18N
215
return getDisplayName();
216             } else if (key.startsWith("AdvancedOption_Tooltip")) {// NOI18N
217
return getTooltip();
218             } else {
219                 throw new AssertionError JavaDoc(key);
220             }
221         }
222         
223         /**
224          * getErrorCode() and getErrorMessage are tigthly coupled. Moreover the
225          * order should depend on ordering of textfields in panels.
226          */

227         String JavaDoc getErrorMessage(int errCode) {
228             assert errCode > 0;
229             String JavaDoc field = null;
230             switch(errCode) {
231                 case ERR_BLANK_DISPLAYNAME:
232                     field = "FIELD_DisplayName";//NOI18N
233
break;
234                 case ERR_BLANK_TOOLTIP:
235                     field = "FIELD_Tooltip";//NOI18N
236
break;
237                 case ERR_BLANK_TITLE:
238                     field = "FIELD_Title";//NOI18N
239
break;
240                 case ERR_BLANK_CATEGORY_NAME:
241                     field = "FIELD_CategoryName";//NOI18N
242
break;
243                 case ERR_BLANK_ICONPATH:
244                     field = "FIELD_IconPath";//NOI18N
245
break;
246                 case ERR_BLANK_PACKAGE_NAME:
247                     field = "FIELD_PackageName";//NOI18N
248
break;
249                 case ERR_BLANK_CLASSNAME_PREFIX:
250                     field = "FIELD_ClassNamePrefix";//NOI18N
251
break;
252                 case ERR_INVALID_CLASSNAME_PREFIX:
253                     return NbBundle.getMessage(NewOptionsIterator.class, "ERR_Name_Prefix_Invalid");//NOI18N
254
default:
255                     assert false : "Unknown errCode: " + errCode;
256             }
257             field = NbBundle.getMessage(NewOptionsIterator.class, field);
258             return (errCode > 0) ?
259                 NbBundle.getMessage(NewOptionsIterator.class, "ERR_FieldInvalid",field) : "";//NOI18N
260
}
261         
262         /**
263          * getErrorCode() and getWarningMessage are tigthly coupled. Moreover the
264          * order should depend on ordering of textfields in panels.
265          */

266         String JavaDoc getWarningMessage(int warningCode) {
267             assert warningCode < 0;
268             String JavaDoc result;
269             switch(warningCode) {
270                 case WARNING_INCORRECT_ICON_SIZE:
271                     File JavaDoc icon = new File JavaDoc(getIconPath());
272                     assert icon.exists();
273                     result = UIUtil.getIconDimensionWarning(icon, 32, 32);
274                     break;
275                 default:
276                     assert false : "Unknown warningCode: " + warningCode;
277                     result = "";
278             }
279             return result;
280         }
281         
282         static boolean isSuccessCode(int code) {
283             return code == 0;
284         }
285         
286         static boolean isErrorCode(int code) {
287             return code > 0;
288         }
289         
290         static boolean isWarningCode(int code) {
291             return code < 0;
292         }
293         
294         private int checkFirstPanel() {
295             if (advanced) {
296                 if (getDisplayName().length() == 0) {
297                     return ERR_BLANK_DISPLAYNAME;
298                 } else if (getTooltip().length() == 0) {
299                     return ERR_BLANK_TOOLTIP;
300                 }
301             } else {
302                 if (getTitle().length() == 0) {
303                     return ERR_BLANK_TITLE;
304                 } else if (getCategoryName().length() == 0) {
305                     return ERR_BLANK_CATEGORY_NAME;
306                 } else if (getIconPath().length() == 0) {
307                     return ERR_BLANK_ICONPATH;
308                 } else if (getTitle().length() == 0) {
309                     return ERR_BLANK_TITLE;
310                 } else {
311                     File JavaDoc icon = new File JavaDoc(getIconPath());
312                     if (!icon.exists()) {
313                         return ERR_BLANK_ICONPATH;
314                     }
315                 }
316                 //warnings should go at latest
317
File JavaDoc icon = new File JavaDoc(getIconPath());
318                 assert icon.exists();
319                 if (!UIUtil.isValidIcon(icon, 32, 32)) {
320                     return WARNING_INCORRECT_ICON_SIZE;
321                 }
322             }
323             return 0;
324         }
325         
326         private int checkFinalPanel() {
327             if (getPackageName().length() == 0) {
328                 return ERR_BLANK_PACKAGE_NAME;
329             } else if (getClassNamePrefix().length() == 0) {
330                 return ERR_BLANK_CLASSNAME_PREFIX;
331             } else if (!Utilities.isJavaIdentifier(getClassNamePrefix())) {
332                 return ERR_INVALID_CLASSNAME_PREFIX;
333         }
334             
335             return 0;
336         }
337         
338         public CreatedModifiedFiles getCreatedModifiedFiles() {
339             if (files == null) {
340                 files = generateCreatedModifiedFiles();
341             }
342             return files;
343         }
344         
345         private CreatedModifiedFiles generateCreatedModifiedFiles() {
346             assert isSuccessCode(checkFirstPanel()) || isWarningCode(checkFirstPanel());
347             assert isSuccessCode(checkFinalPanel());
348             files = new CreatedModifiedFiles(getProject());
349             generateFiles();
350             generateBundleKeys();
351             generateDependencies();
352             generateLayerEntry();
353             if (!isAdvanced()) {
354                 addCreateIconOperation(files, getIconPath());
355             }
356             return files;
357         }
358         
359         private void generateFiles() {
360             List JavaDoc allForms = Arrays.asList(FORM_TEMPLATE_SUFFIXES);
361             for (int i = 0; i < JAVA_TEMPLATE_SUFFIXES.length; i++) {
362                 boolean ommit = (isAdvanced()) ? "OptionsCategory".equals(JAVA_TEMPLATE_SUFFIXES[i]) : // NOI18N
363
"AdvancedOption".equals(JAVA_TEMPLATE_SUFFIXES[i]);// NOI18N
364
if (ommit) {
365                     continue;
366                 }
367                 files.add(createJavaFileCopyOperation(JAVA_TEMPLATE_SUFFIXES[i]));
368                 if (allForms.contains(JAVA_TEMPLATE_SUFFIXES[i])) {
369                     files.add(createFormFileCopyOperation(JAVA_TEMPLATE_SUFFIXES[i]));
370                 }
371             }
372         }
373         
374         private void generateBundleKeys() {
375             String JavaDoc[] bundleKeys = (isAdvanced()) ? ADVANCED_BUNDLE_KEYS : CATEGORY_BUNDLE_KEYS;
376             for (int i = 0; i < bundleKeys.length; i++) {
377                 String JavaDoc key = bundleKeys[i];
378                 if (key.startsWith("@@") && key.endsWith("@@")) {//NOI18N
379
key = getReplacement(key);
380                 }
381                 String JavaDoc value = getBundleValue(key);
382                 files.add(files.bundleKey(getDefaultPackagePath("Bundle.properties", true),key,value));// NOI18N
383
}
384         }
385         
386         private void generateDependencies() {
387             files.add(files.addModuleDependency("org.openide.util")); // NOI18N
388
files.add(files.addModuleDependency("org.netbeans.modules.options.api","0-1",null,true));// NOI18N
389
files.add(files.addModuleDependency("org.openide.awt")); // NOI18N
390
}
391         
392         private void generateLayerEntry() {
393             String JavaDoc resourcePathPrefix = (isAdvanced()) ? "OptionsDialog/Advanced/" : "OptionsDialog/";// NOI18N
394
String JavaDoc instanceName = isAdvanced() ? getAdvancedOptionClassName() : getOptionsCategoryClassName();
395             String JavaDoc instanceFullPath = resourcePathPrefix + getPackageName().replace('.','-') + "-" + instanceName + ".instance";//NOI18N
396
files.add(files.createLayerEntry(instanceFullPath, null, null, null, null));
397         }
398         
399         private CreatedModifiedFiles.Operation createJavaFileCopyOperation(final String JavaDoc templateSuffix) {
400             URL JavaDoc template = NewOptionsIterator.class.getResource(JAVA_TEMPLATE_PREFIX+templateSuffix);
401             assert template != null : JAVA_TEMPLATE_PREFIX+templateSuffix;
402             return files.createFileWithSubstitutions(getFilePath(templateSuffix), template,getTokenMap());
403         }
404         
405         private String JavaDoc getFilePath(final String JavaDoc templateSuffix) {
406             String JavaDoc fileName = getClassNamePrefix()+templateSuffix+ ".java"; // NOI18N
407
return getDefaultPackagePath(fileName, false);//NOI18N
408
}
409         
410         private CreatedModifiedFiles.Operation createFormFileCopyOperation(final String JavaDoc templateSuffix) {
411             URL JavaDoc template = NewOptionsIterator.class.getResource(FORM_TEMPLATE_PREFIX+templateSuffix);
412             assert template != null : JAVA_TEMPLATE_PREFIX+templateSuffix;
413             String JavaDoc fileName = getClassNamePrefix()+templateSuffix+ ".form";// NOI18N
414
String JavaDoc filePath = getDefaultPackagePath(fileName, false);
415             return files.createFile(filePath, template);
416         }
417         
418         private String JavaDoc getCodeNameBase() {
419             if (codeNameBase == null) {
420                 NbModuleProvider mod = getProject().getLookup().lookup(NbModuleProvider.class);
421                 codeNameBase = mod.getCodeNameBase();
422             }
423             return codeNameBase;
424         }
425         
426         private String JavaDoc getDisplayName() {
427             assert !isAdvanced() || displayName != null;
428             return displayName;
429         }
430         
431         private String JavaDoc getTooltip() {
432             assert !isAdvanced() || tooltip != null;
433             return tooltip;
434         }
435         
436         private String JavaDoc getTitle() {
437             assert isAdvanced() || title != null;
438             return title;
439         }
440         
441         
442         private String JavaDoc getCategoryName() {
443             assert isAdvanced() || categoryName != null;
444             return categoryName;
445         }
446         
447         private String JavaDoc getIconPath() {
448             assert isAdvanced() || iconPath != null;
449             return iconPath;
450         }
451         
452         String JavaDoc getClassNamePrefix() {
453             if (classNamePrefix == null) {
454                 classNamePrefix = getCodeNameBase();
455                 classNamePrefix = classNamePrefix.substring(classNamePrefix.lastIndexOf(".")+1);// NOI18N
456
classNamePrefix = classNamePrefix.substring(0,1).toUpperCase(Locale.ENGLISH) + classNamePrefix.substring(1); // NOI18N
457
}
458             return classNamePrefix;
459         }
460         
461         private boolean isAdvanced() {
462             return advanced;
463         }
464         
465         private String JavaDoc getAdvancedOptionClassName() {
466             return getClassName(JAVA_TEMPLATE_SUFFIXES[0]);
467         }
468         
469         private String JavaDoc getOptionsCategoryClassName() {
470             return getClassName(JAVA_TEMPLATE_SUFFIXES[1]);
471         }
472         
473         private String JavaDoc getPanelClassName() {
474             return getClassName(JAVA_TEMPLATE_SUFFIXES[2]);
475         }
476         
477         private String JavaDoc getOptionsPanelControllerClassName() {
478             return getClassName(JAVA_TEMPLATE_SUFFIXES[3]);
479         }
480         
481         private String JavaDoc getClassName(String JavaDoc suffix) {
482             return getClassNamePrefix() + suffix;
483         }
484         
485     }
486 }
487
Popular Tags