KickJava   Java API By Example, From Geeks To Geeks.

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


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.wizard;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
28 import org.netbeans.modules.apisupport.project.Util;
29 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
30 import org.openide.WizardDescriptor;
31
32 /**
33  * Data model used across the <em>New Wizard Wizard</em>.
34  */

35 final class DataModel extends BasicWizardIterator.BasicDataModel {
36     
37     private CreatedModifiedFiles cmf;
38     
39     // first panel data (Wizard Type)
40
private boolean branching;
41     private boolean fileTemplateType;
42     private int nOfSteps;
43     
44     // second panel data (Name, Icon and Location)
45
private String JavaDoc prefix;
46     private String JavaDoc displayName;
47     private String JavaDoc category;
48     private String JavaDoc origIconPath;
49     
50     DataModel(final WizardDescriptor wiz) {
51         super(wiz);
52     }
53     
54     CreatedModifiedFiles getCreatedModifiedFiles() {
55         if (cmf == null) {
56             regenerate();
57         }
58         return cmf;
59     }
60     
61     private void regenerate() {
62         cmf = new CreatedModifiedFiles(getProject());
63         
64         Map JavaDoc basicTokens = new HashMap JavaDoc();
65         basicTokens.put("@@PACKAGE_NAME@@", getPackageName()); // NOI18N
66
basicTokens.put("@@WIZARD_PREFIX@@", prefix); // NOI18N
67

68         StringBuffer JavaDoc panelsDefinitionBlock = new StringBuffer JavaDoc();
69         String JavaDoc newLine = System.getProperty("line.separator") + " "; // NOI18N
70

71         // Create wizard and visual panels
72
for (int stepNumber = 1; stepNumber < (nOfSteps + 1); stepNumber++) {
73             String JavaDoc visualPanelClass = prefix + "VisualPanel" + stepNumber; // NOI18N
74
String JavaDoc wizardPanelClass = prefix + "WizardPanel" + stepNumber; // NOI18N
75

76             Map JavaDoc replaceTokens = new HashMap JavaDoc(basicTokens);
77             replaceTokens.put("@@VISUAL_PANEL_CLASS@@", visualPanelClass); // NOI18N
78
replaceTokens.put("@@WIZARD_PANEL_CLASS@@", wizardPanelClass); // NOI18N
79
replaceTokens.put("@@STEP_NAME@@", "Step #" + stepNumber); // NOI18N
80

81             // generate .java file for visual panel
82
String JavaDoc path = getDefaultPackagePath(visualPanelClass + ".java", false); // NOI18N
83
// XXX use nbresloc URL protocol rather than
84
// DataModel.class.getResource(...) and all such a cases below
85
URL JavaDoc template = DataModel.class.getResource("visualPanel.javx"); // NOI18N
86
cmf.add(cmf.createFileWithSubstitutions(path, template, replaceTokens));
87             
88             // generate .form file for visual panel
89
path = getDefaultPackagePath(visualPanelClass + ".form", false); // NOI18N
90
template = DataModel.class.getResource("visualPanel.forx"); // NOI18N
91
cmf.add(cmf.createFile(path, template));
92             
93             // generate .java file for wizard panel
94
path = getDefaultPackagePath(wizardPanelClass + ".java", false); // NOI18N
95
template = DataModel.class.getResource("wizardPanel.javx"); // NOI18N
96
cmf.add(cmf.createFileWithSubstitutions(path, template, replaceTokens));
97             
98             panelsDefinitionBlock.append("new " + wizardPanelClass + "()"); // NOI18N
99
if (stepNumber != nOfSteps) {
100                 panelsDefinitionBlock.append(',').append(newLine);
101             }
102         }
103         
104         cmf.add(cmf.addModuleDependency("org.openide.util")); // NOI18N
105
cmf.add(cmf.addModuleDependency("org.openide.dialogs")); // NOI18N
106
cmf.add(cmf.addModuleDependency("org.openide.awt")); // NOI18N
107

108         // generate .java for wizard iterator
109
if (fileTemplateType || branching) {
110             String JavaDoc iteratorClass = prefix + "WizardIterator"; // NOI18N
111
Map JavaDoc replaceTokens = new HashMap JavaDoc(basicTokens);
112             replaceTokens.put("@@PANELS_DEFINITION_BLOCK@@", panelsDefinitionBlock.toString()); // NOI18N
113
replaceTokens.put("@@ITERATOR_CLASS@@", iteratorClass); // NOI18N
114
String JavaDoc path = getDefaultPackagePath(iteratorClass + ".java", false); // NOI18N
115
URL JavaDoc template = DataModel.class.getResource(fileTemplateType
116                     ? "instantiatingIterator.javx" : "wizardIterator.javx"); // NOI18N
117
cmf.add(cmf.createFileWithSubstitutions(path, template, replaceTokens));
118             
119             if (fileTemplateType) {
120                 // generate .html description for the template
121
String JavaDoc lowerCasedPrefix = prefix.substring(0, 1).toLowerCase(Locale.ENGLISH) + prefix.substring(1);
122                 template = DataModel.class.getResource("wizardDescription.html"); // NOI18N
123
cmf.add(cmf.createFile(getDefaultPackagePath(lowerCasedPrefix, true) + ".html", template)); // NOI18N
124

125                 // add layer entry about a new file wizard
126
String JavaDoc instanceFullPath = category + '/' + lowerCasedPrefix;
127                 cmf.add(cmf.createLayerEntry(instanceFullPath, null, null, displayName, null));
128                 cmf.add(cmf.createLayerAttribute(instanceFullPath, "template", Boolean.TRUE)); // NOI18N
129
String JavaDoc fqIteratorClass = getPackageName() + '.' + iteratorClass;
130                 cmf.add(cmf.createLayerAttribute(instanceFullPath, "instantiatingIterator", // NOI18N
131
"newvalue:" + fqIteratorClass)); // NOI18N
132
try {
133                     URL JavaDoc url = new URL JavaDoc("nbresloc:/" + getPackageName().replace('.','/') + '/' // NOI18N
134
+ lowerCasedPrefix + ".html"); // NOI18N
135
cmf.add(cmf.createLayerAttribute(instanceFullPath, "templateWizardURL", url)); // NOI18N
136
} catch (MalformedURLException JavaDoc ex) {
137                     Util.err.notify(ex);
138                 }
139                 
140                 // Copy wizard icon
141
if (origIconPath != null && origIconPath.length() > 0) {
142                     String JavaDoc relToSrcDir = addCreateIconOperation(cmf, origIconPath);
143                     try {
144                         URL JavaDoc url = new URL JavaDoc("nbresloc:/" + relToSrcDir); // NOI18N
145
cmf.add(cmf.createLayerAttribute(instanceFullPath, "SystemFileSystem.icon", url)); // NOI18N
146
} catch (MalformedURLException JavaDoc ex) {
147                         Util.err.notify(ex);
148                     }
149                 }
150             }
151         } else {
152             Map JavaDoc replaceTokens = new HashMap JavaDoc(basicTokens);
153             replaceTokens.put("@@PANELS_DEFINITION_BLOCK@@", panelsDefinitionBlock.toString()); // NOI18N
154
String JavaDoc path = getDefaultPackagePath(prefix + "WizardAction.java", false); // NOI18N
155
URL JavaDoc template = DataModel.class.getResource("sampleAction.javx"); // NOI18N
156
cmf.add(cmf.createFileWithSubstitutions(path, template, replaceTokens));
157         }
158     }
159     
160     private void reset() {
161         cmf = null;
162     }
163     
164     void setBranching(boolean branching) {
165         this.branching = branching;
166     }
167     
168     boolean isBranching() {
169         return branching;
170     }
171     
172     void setFileTemplateType(boolean fileTemplateType) {
173         this.fileTemplateType = fileTemplateType;
174     }
175     
176     boolean isFileTemplateType() {
177         return fileTemplateType;
178     }
179     
180     void setNumberOfSteps(int nOfSteps) {
181         this.nOfSteps = nOfSteps;
182     }
183     
184     void setClassNamePrefix(String JavaDoc prefix) {
185         reset();
186         this.prefix = prefix;
187     }
188     
189     void setDisplayName(String JavaDoc displayName) {
190         this.displayName = displayName;
191     }
192     
193     void setCategory(String JavaDoc category) {
194         this.category = category;
195     }
196     
197     void setIcon(String JavaDoc origIconPath) {
198         reset();
199         this.origIconPath = origIconPath;
200     }
201     
202     public void setPackageName(String JavaDoc packageName) {
203         super.setPackageName(packageName);
204         reset();
205     }
206     
207 }
208
Popular Tags