KickJava   Java API By Example, From Geeks To Geeks.

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


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.winsys;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
30 import org.netbeans.modules.apisupport.project.Util;
31 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
32 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
33 import org.openide.ErrorManager;
34 import org.openide.WizardDescriptor;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileStateInvalidException;
37 import org.openide.filesystems.FileSystem;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.util.NbBundle;
40
41 /**
42  * Wizard for creating new TopComponent.
43  *
44  * @author Milos Kleint
45  */

46 final class NewTCIterator extends BasicWizardIterator {
47
48     private NewTCIterator.DataModel data;
49     
50     private NewTCIterator() { /* Use factory method. */ };
51     
52     public static NewTCIterator createIterator() {
53         return new NewTCIterator();
54     }
55     
56     public Set JavaDoc instantiate() throws IOException JavaDoc {
57         CreatedModifiedFiles cmf = data.getCreatedModifiedFiles();
58         cmf.run();
59         return getCreatedFiles(cmf, data.getProject());
60     }
61     
62     protected BasicWizardIterator.Panel[] createPanels(WizardDescriptor wiz) {
63         data = new NewTCIterator.DataModel(wiz);
64         return new BasicWizardIterator.Panel[] {
65             new BasicSettingsPanel(wiz, data),
66             new NameAndLocationPanel(wiz, data)
67         };
68     }
69     
70     public void uninitialize(WizardDescriptor wiz) {
71         super.uninitialize(wiz);
72         data = null;
73     }
74     
75     static final class DataModel extends BasicWizardIterator.BasicDataModel {
76         
77         private String JavaDoc name;
78         private String JavaDoc icon;
79         private String JavaDoc mode;
80         private boolean opened = false;
81         
82         private CreatedModifiedFiles files;
83         
84         DataModel(WizardDescriptor wiz) {
85             super(wiz);
86         }
87         
88         public CreatedModifiedFiles getCreatedModifiedFiles() {
89             return getFiles();
90         }
91         
92         public void setCreatedModifiedFiles(CreatedModifiedFiles files) {
93             this.setFiles(files);
94         }
95         
96         public String JavaDoc getName() {
97             return name;
98         }
99         
100         public void setName(String JavaDoc name) {
101             this.name = name;
102         }
103         
104         public CreatedModifiedFiles getFiles() {
105             return files;
106         }
107         
108         public void setFiles(CreatedModifiedFiles files) {
109             this.files = files;
110         }
111
112         public String JavaDoc getIcon() {
113             return icon;
114         }
115
116         public void setIcon(String JavaDoc icon) {
117             this.icon = icon;
118         }
119
120         public String JavaDoc getMode() {
121             return mode;
122         }
123
124         public void setMode(String JavaDoc mode) {
125             this.mode = mode;
126         }
127
128         public boolean isOpened() {
129             return opened;
130         }
131
132         public void setOpened(boolean opened) {
133             this.opened = opened;
134         }
135         
136     }
137     
138     public static void generateFileChanges(DataModel model) {
139         CreatedModifiedFiles fileChanges = new CreatedModifiedFiles(model.getProject());
140         Project project = model.getProject();
141         NbModuleProvider moduleInfo = model.getModuleInfo();
142         final String JavaDoc name = model.getName();
143         final String JavaDoc packageName = model.getPackageName();
144         final String JavaDoc mode = model.getMode();
145         
146         HashMap JavaDoc replaceTokens = new HashMap JavaDoc();
147         replaceTokens.put("@@TEMPLATENAME@@", name);//NOI18N
148
replaceTokens.put("@@PACKAGENAME@@", packageName);//NOI18N
149
replaceTokens.put("@@MODE@@", mode); //NOI18N
150
replaceTokens.put("@@OPENED@@", model.isOpened() ? "true" : "false"); //NOI18N
151

152         // 0. move icon file if necessary
153
String JavaDoc icon = model.getIcon();
154         File JavaDoc fil = null;
155         if (icon != null) {
156             fil = new File JavaDoc(icon);
157             if (!fil.exists()) {
158                 fil = null;
159             }
160         }
161         if (fil != null) {
162             FileObject fo = FileUtil.toFileObject(fil);
163             String JavaDoc relativeIconPath = null;
164             if (!FileUtil.isParentOf(Util.getResourceDirectory(project), fo)) {
165                 String JavaDoc iconPath = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName,
166                                                 "", fo.getNameExt()); //NOI18N
167
try {
168                     fileChanges.add(fileChanges.createFile(iconPath, fo.getURL()));
169                     relativeIconPath = packageName.replace('.', '/') + "/" + fo.getNameExt(); // NOI18N
170
} catch (FileStateInvalidException exc) {
171                     ErrorManager.getDefault().notify(exc);
172                 }
173             } else {
174                 relativeIconPath = FileUtil.getRelativePath(Util.getResourceDirectory(project), fo);
175             }
176             replaceTokens.put("@@ICONPATH@@", relativeIconPath);//NOI18N
177
replaceTokens.put("@@COMMENTICON@@", "");//NOI18N
178

179         } else {
180             replaceTokens.put("@@ICONPATH@@", "SET/PATH/TO/ICON/HERE"); //NOI18N
181
replaceTokens.put("@@COMMENTICON@@", "//");//NOI18N
182
}
183         
184         
185         // 2. update project dependencies
186
replaceTokens.put("@@MODULENAME@@", moduleInfo.getCodeNameBase()); // NOI18N
187
//TODO how to figure the currect specification version for module?
188
replaceTokens.put("@@SPECVERSION@@", moduleInfo.getSpecVersion()); // NOI18N
189
fileChanges.add(fileChanges.addModuleDependency("org.openide.windows")); //NOI18N
190
fileChanges.add(fileChanges.addModuleDependency("org.openide.util")); //NOI18N
191
fileChanges.add(fileChanges.addModuleDependency("org.openide.awt")); //NOI18N
192

193         // x. generate java classes
194
final String JavaDoc tcName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
195                 name, "TopComponent.java"); //NOI18N
196
// TODO use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
197
URL JavaDoc template = NewTCIterator.class.getResource("templateTopComponent.javx");//NOI18N
198
fileChanges.add(fileChanges.createFileWithSubstitutions(tcName, template, replaceTokens));
199         // x. generate java classes
200
final String JavaDoc tcFormName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
201                 name, "TopComponent.form"); //NOI18N
202
// TODO use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
203
template = NewTCIterator.class.getResource("templateTopComponent.frmx");//NOI18N
204
fileChanges.add(fileChanges.createFileWithSubstitutions(tcFormName, template, replaceTokens));
205         
206         final String JavaDoc actionName = getRelativePath(moduleInfo.getSourceDirectoryPath(), packageName,
207                 name, "Action.java"); //NOI18N
208
// TODO use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
209
template = NewTCIterator.class.getResource("templateAction.javx");//NOI18N
210
fileChanges.add(fileChanges.createFileWithSubstitutions(actionName, template, replaceTokens));
211         
212         final String JavaDoc settingsName = name + "TopComponent.settings"; //NOI18N
213
// TODO use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
214
template = NewTCIterator.class.getResource("templateSettings.xml");//NOI18N
215
fileChanges.add(fileChanges.createLayerEntry("Windows2/Components/" + settingsName, template, replaceTokens, null, null)); // NOI18N
216

217         final String JavaDoc wstcrefName = name + "TopComponent.wstcref"; //NOI18N
218
// TODO use nbresloc URL protocol rather than NewLoaderIterator.class.getResource(...):
219
template = NewTCIterator.class.getResource("templateWstcref.xml");//NOI18N
220
fileChanges.add(fileChanges.createLayerEntry("Windows2/Modes/" + mode + "/" + wstcrefName, // NOI18N
221
template, replaceTokens, null, null));
222         
223         fileChanges.add(fileChanges.layerModifications(new CreateActionEntryOperation(name + "Action", packageName), // NOI18N
224
Collections.EMPTY_SET));
225         String JavaDoc bundlePath = getRelativePath(moduleInfo.getResourceDirectoryPath(false), packageName, "", "Bundle.properties"); //NOI18N
226
fileChanges.add(fileChanges.bundleKey(bundlePath, "CTL_" + name + "Action", // NOI18N
227
NbBundle.getMessage(NewTCIterator.class, "LBL_TemplateActionName", name))); //NOI18N
228

229         fileChanges.add(fileChanges.bundleKey(bundlePath, "CTL_" + name + "TopComponent", // NOI18N
230
NbBundle.getMessage(NewTCIterator.class, "LBL_TemplateTCName", name))); //NOI18N
231
fileChanges.add(fileChanges.bundleKey(bundlePath, "HINT_" + name + "TopComponent", // NOI18N
232
NbBundle.getMessage(NewTCIterator.class, "HINT_TemplateTCName", name))); //NOI18N
233

234         model.setCreatedModifiedFiles(fileChanges);
235     }
236     
237     private static String JavaDoc getRelativePath(String JavaDoc rootpath, String JavaDoc fullyQualifiedPackageName,
238             String JavaDoc prefix, String JavaDoc postfix) {
239         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
240         
241         sb.append(rootpath).append('/').append(fullyQualifiedPackageName.replace('.','/'))
242                         .append('/').append(prefix).append(postfix);
243         
244         return sb.toString();
245     }
246     
247     static class CreateActionEntryOperation implements CreatedModifiedFiles.LayerOperation {
248         private String JavaDoc name;
249         private String JavaDoc packageName;
250         
251         public CreateActionEntryOperation(String JavaDoc actionname, String JavaDoc packageName) {
252             this.packageName = packageName;
253             this.name = actionname;
254         }
255         
256         public void run(FileSystem layer) throws IOException JavaDoc {
257             FileObject folder = layer.getRoot().getFileObject("Actions/Window");// NOI18N
258
if (folder == null) {
259                 folder = FileUtil.createFolder(layer.getRoot(), "Actions/Window"); // NOI18N
260
}
261             String JavaDoc instance = packageName.replace('.','-') + "-" + name; // NOI18N
262
FileObject file = folder.createData(instance, "instance"); // NOI18N
263
folder = layer.getRoot().getFileObject("Menu/Window");// NOI18N
264
if (folder == null) {
265                 folder = FileUtil.createFolder(layer.getRoot(), "Menu/Window"); // NOI18N
266
}
267             file = folder.createData(name, "shadow"); // NOI18N
268
file.setAttribute("originalFile", "Actions/Window/" + instance + ".instance"); // NOI18N
269
}
270     }
271     
272 }
273
Popular Tags