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-2007 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.moduleinstall;
043:
044: import java.util.HashMap;
045: import java.util.Map;
046: import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
047: import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
048: import org.openide.WizardDescriptor;
049: import org.openide.filesystems.FileObject;
050:
051: /**
052: * Data model used across the <em>New Module Installer</em>.
053: */
054: final class DataModel extends BasicWizardIterator.BasicDataModel {
055:
056: static final String OPENIDE_MODULE_INSTALL = "OpenIDE-Module-Install"; // NOI18N
057: private static final String INSTALLER_CLASS_NAME = "Installer"; // NOI18N
058:
059: private CreatedModifiedFiles cmf;
060:
061: DataModel(final WizardDescriptor wiz) {
062: super (wiz);
063: }
064:
065: CreatedModifiedFiles getCreatedModifiedFiles() {
066: if (cmf == null) {
067: regenerate();
068: }
069: return cmf;
070: }
071:
072: private void regenerate() {
073: cmf = new CreatedModifiedFiles(getProject());
074:
075: // obtain unique class name
076: String className = INSTALLER_CLASS_NAME;
077: String path = getDefaultPackagePath(className + ".java", false); // NOI18N
078: int i = 0;
079: while (alreadyExist(path)) {
080: className = INSTALLER_CLASS_NAME + '_' + ++i;
081: path = getDefaultPackagePath(className + ".java", false); // NOI18N
082: }
083:
084: // generate .java file for ModuleInstall
085: Map<String, String> basicTokens = new HashMap<String, String>();
086: basicTokens.put("PACKAGE_NAME", getPackageName()); // NOI18N
087: basicTokens.put("CLASS_NAME", className); // NOI18N
088: // XXX use nbresloc URL protocol rather than
089: // DataModel.class.getResource(...) and all such a cases below
090: FileObject template = CreatedModifiedFiles
091: .getTemplate("moduleInstall.java"); // NOI18N
092: cmf.add(cmf.createFileWithSubstitutions(path, template,
093: basicTokens));
094:
095: cmf.add(cmf.addModuleDependency("org.openide.modules")); // NOI18N
096: cmf.add(cmf.addModuleDependency("org.openide.util")); // NOI18N
097:
098: // add manifest attribute
099: Map<String, String> attribs = new HashMap<String, String>();
100: attribs.put(OPENIDE_MODULE_INSTALL, getPackageName().replace(
101: '.', '/')
102: + '/' + className + ".class"); // NOI18N
103: cmf.add(cmf.manifestModification(null, attribs));
104: }
105:
106: private void reset() {
107: cmf = null;
108: }
109:
110: public @Override
111: void setPackageName(String packageName) {
112: super .setPackageName(packageName);
113: reset();
114: }
115:
116: private boolean alreadyExist(String relPath) {
117: return getProject().getProjectDirectory()
118: .getFileObject(relPath) != null;
119: }
120:
121: }
|