001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.ui.wizard.winsys;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.Map;
049: import java.util.Set;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
052: import org.netbeans.modules.apisupport.project.Util;
053: import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
054: import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
055: import org.openide.WizardDescriptor;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileSystem;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.util.NbBundle;
060:
061: /**
062: * Wizard for creating new TopComponent.
063: *
064: * @author Milos Kleint
065: */
066: final class NewTCIterator extends BasicWizardIterator {
067:
068: private NewTCIterator.DataModel data;
069:
070: private NewTCIterator() { /* Use factory method. */
071: };
072:
073: public static NewTCIterator createIterator() {
074: return new NewTCIterator();
075: }
076:
077: public Set instantiate() throws IOException {
078: CreatedModifiedFiles cmf = data.getCreatedModifiedFiles();
079: cmf.run();
080: return getCreatedFiles(cmf, data.getProject());
081: }
082:
083: protected BasicWizardIterator.Panel[] createPanels(
084: WizardDescriptor wiz) {
085: data = new NewTCIterator.DataModel(wiz);
086: return new BasicWizardIterator.Panel[] {
087: new BasicSettingsPanel(wiz, data),
088: new NameAndLocationPanel(wiz, data) };
089: }
090:
091: public @Override
092: void uninitialize(WizardDescriptor wiz) {
093: super .uninitialize(wiz);
094: data = null;
095: }
096:
097: static final class DataModel extends
098: BasicWizardIterator.BasicDataModel {
099:
100: private String name;
101: private String icon;
102: private String mode;
103: private boolean opened = false;
104:
105: private CreatedModifiedFiles files;
106:
107: DataModel(WizardDescriptor wiz) {
108: super (wiz);
109: }
110:
111: public CreatedModifiedFiles getCreatedModifiedFiles() {
112: return getFiles();
113: }
114:
115: public void setCreatedModifiedFiles(CreatedModifiedFiles files) {
116: this .setFiles(files);
117: }
118:
119: public String getName() {
120: return name;
121: }
122:
123: public void setName(String name) {
124: this .name = name;
125: }
126:
127: public CreatedModifiedFiles getFiles() {
128: return files;
129: }
130:
131: public void setFiles(CreatedModifiedFiles files) {
132: this .files = files;
133: }
134:
135: public String getIcon() {
136: return icon;
137: }
138:
139: public void setIcon(String icon) {
140: this .icon = icon;
141: }
142:
143: public String getMode() {
144: return mode;
145: }
146:
147: public void setMode(String mode) {
148: this .mode = mode;
149: }
150:
151: public boolean isOpened() {
152: return opened;
153: }
154:
155: public void setOpened(boolean opened) {
156: this .opened = opened;
157: }
158:
159: }
160:
161: public static void generateFileChanges(DataModel model) {
162: CreatedModifiedFiles fileChanges = new CreatedModifiedFiles(
163: model.getProject());
164: Project project = model.getProject();
165: NbModuleProvider moduleInfo = model.getModuleInfo();
166: final String name = model.getName();
167: final String packageName = model.getPackageName();
168: final String mode = model.getMode();
169:
170: Map<String, String> replaceTokens = new HashMap<String, String>();
171: replaceTokens.put("TEMPLATENAME", name);//NOI18N
172: replaceTokens.put("PACKAGENAME", packageName);//NOI18N
173: replaceTokens.put("MODE", mode); //NOI18N
174: replaceTokens
175: .put("OPENED", model.isOpened() ? "true" : "false"); //NOI18N
176:
177: // 0. move icon file if necessary
178: String icon = model.getIcon();
179: File fil = null;
180: if (icon != null) {
181: fil = new File(icon);
182: if (!fil.exists()) {
183: fil = null;
184: }
185: }
186: if (fil != null) {
187: FileObject fo = FileUtil.toFileObject(fil);
188: String relativeIconPath = null;
189: if (!FileUtil.isParentOf(
190: Util.getResourceDirectory(project), fo)) {
191: String iconPath = getRelativePath(moduleInfo
192: .getResourceDirectoryPath(false), packageName,
193: "", fo.getNameExt()); //NOI18N
194: fileChanges.add(fileChanges.createFile(iconPath, fo));
195: relativeIconPath = packageName.replace('.', '/') + "/"
196: + fo.getNameExt(); // NOI18N
197: } else {
198: relativeIconPath = FileUtil.getRelativePath(Util
199: .getResourceDirectory(project), fo);
200: }
201: replaceTokens.put("ICONPATH", relativeIconPath);//NOI18N
202: replaceTokens.put("COMMENTICON", "");//NOI18N
203:
204: } else {
205: replaceTokens.put("ICONPATH", "SET/PATH/TO/ICON/HERE"); //NOI18N
206: replaceTokens.put("COMMENTICON", "//");//NOI18N
207: }
208:
209: // 2. update project dependencies
210: replaceTokens.put("MODULENAME", moduleInfo.getCodeNameBase()); // NOI18N
211: //TODO how to figure the currect specification version for module?
212: replaceTokens.put("SPECVERSION", moduleInfo.getSpecVersion()); // NOI18N
213: fileChanges.add(fileChanges
214: .addModuleDependency("org.openide.windows")); //NOI18N
215: fileChanges.add(fileChanges
216: .addModuleDependency("org.openide.util")); //NOI18N
217: fileChanges.add(fileChanges
218: .addModuleDependency("org.openide.awt")); //NOI18N
219: fileChanges.add(fileChanges
220: .addModuleDependency("org.jdesktop.layout")); //NOI18N
221:
222: // x. generate java classes
223: final String tcName = getRelativePath(moduleInfo
224: .getSourceDirectoryPath(), packageName, name,
225: "TopComponent.java"); //NOI18N
226: FileObject template = CreatedModifiedFiles
227: .getTemplate("templateTopComponent.java");//NOI18N
228: fileChanges.add(fileChanges.createFileWithSubstitutions(tcName,
229: template, replaceTokens));
230: // x. generate java classes
231: final String tcFormName = getRelativePath(moduleInfo
232: .getSourceDirectoryPath(), packageName, name,
233: "TopComponent.form"); //NOI18N
234: template = CreatedModifiedFiles
235: .getTemplate("templateTopComponent.form");//NOI18N
236: fileChanges.add(fileChanges.createFileWithSubstitutions(
237: tcFormName, template, replaceTokens));
238:
239: final String actionName = getRelativePath(moduleInfo
240: .getSourceDirectoryPath(), packageName, name,
241: "Action.java"); //NOI18N
242: template = CreatedModifiedFiles
243: .getTemplate("templateAction.java");//NOI18N
244: fileChanges.add(fileChanges.createFileWithSubstitutions(
245: actionName, template, replaceTokens));
246:
247: final String settingsName = name + "TopComponent.settings"; //NOI18N
248: template = CreatedModifiedFiles
249: .getTemplate("templateSettings.xml");//NOI18N
250: fileChanges.add(fileChanges.createLayerEntry(
251: "Windows2/Components/" + settingsName, template,
252: replaceTokens, null, null)); // NOI18N
253:
254: final String wstcrefName = name + "TopComponent.wstcref"; //NOI18N
255: template = CreatedModifiedFiles
256: .getTemplate("templateWstcref.xml");//NOI18N
257: fileChanges.add(fileChanges.createLayerEntry("Windows2/Modes/"
258: + mode + "/" + wstcrefName, // NOI18N
259: template, replaceTokens, null, null));
260:
261: fileChanges.add(fileChanges.layerModifications(
262: new CreateActionEntryOperation(name + "Action",
263: packageName), // NOI18N
264: Collections.<String> emptySet()));
265: String bundlePath = getRelativePath(moduleInfo
266: .getResourceDirectoryPath(false), packageName, "",
267: "Bundle.properties"); //NOI18N
268: fileChanges.add(fileChanges.bundleKey(bundlePath, "CTL_" + name
269: + "Action", // NOI18N
270: NbBundle.getMessage(NewTCIterator.class,
271: "LBL_TemplateActionName", name))); //NOI18N
272:
273: fileChanges.add(fileChanges.bundleKey(bundlePath, "CTL_" + name
274: + "TopComponent", // NOI18N
275: NbBundle.getMessage(NewTCIterator.class,
276: "LBL_TemplateTCName", name))); //NOI18N
277: fileChanges.add(fileChanges.bundleKey(bundlePath, "HINT_"
278: + name + "TopComponent", // NOI18N
279: NbBundle.getMessage(NewTCIterator.class,
280: "HINT_TemplateTCName", name))); //NOI18N
281:
282: model.setCreatedModifiedFiles(fileChanges);
283: }
284:
285: private static String getRelativePath(String rootpath,
286: String fullyQualifiedPackageName, String prefix,
287: String postfix) {
288: StringBuffer sb = new StringBuffer();
289:
290: sb.append(rootpath).append('/').append(
291: fullyQualifiedPackageName.replace('.', '/'))
292: .append('/').append(prefix).append(postfix);
293:
294: return sb.toString();
295: }
296:
297: static class CreateActionEntryOperation implements
298: CreatedModifiedFiles.LayerOperation {
299: private String name;
300: private String packageName;
301:
302: public CreateActionEntryOperation(String actionname,
303: String packageName) {
304: this .packageName = packageName;
305: this .name = actionname;
306: }
307:
308: public void run(FileSystem layer) throws IOException {
309: FileObject folder = layer.getRoot().getFileObject(
310: "Actions/Window");// NOI18N
311: if (folder == null) {
312: folder = FileUtil.createFolder(layer.getRoot(),
313: "Actions/Window"); // NOI18N
314: }
315: String instance = packageName.replace('.', '-') + "-"
316: + name; // NOI18N
317: FileObject file = folder.createData(instance, "instance"); // NOI18N
318: folder = layer.getRoot().getFileObject("Menu/Window");// NOI18N
319: if (folder == null) {
320: folder = FileUtil.createFolder(layer.getRoot(),
321: "Menu/Window"); // NOI18N
322: }
323: file = folder.createData(name, "shadow"); // NOI18N
324: file.setAttribute("originalFile", "Actions/Window/"
325: + instance + ".instance"); // NOI18N
326: }
327: }
328:
329: }
|