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.javahelp;
043:
044: import java.io.IOException;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.Map;
048: import java.util.Random;
049: import java.util.Set;
050: import org.netbeans.api.project.ProjectUtils;
051: import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
052: import org.netbeans.modules.apisupport.project.CreatedModifiedFilesFactory;
053: import org.netbeans.modules.apisupport.project.CreatedModifiedFilesFactory.ModifyManifest;
054: import org.netbeans.modules.apisupport.project.EditableManifest;
055: import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
056: import org.openide.WizardDescriptor;
057: import org.openide.filesystems.FileObject;
058:
059: /**
060: * Wizard for creating JavaHelp
061: *
062: * @author Radek Matous, Jesse Glick
063: */
064: public class NewJavaHelpIterator extends BasicWizardIterator {
065:
066: private NewJavaHelpIterator.DataModel data;
067:
068: public static NewJavaHelpIterator createIterator() {
069: return new NewJavaHelpIterator();
070: }
071:
072: public Set instantiate() throws IOException {
073: CreatedModifiedFiles cmf = data.getCreatedModifiedFiles();
074: cmf.run();
075: return getCreatedFiles(cmf, data.getProject());
076: }
077:
078: protected BasicWizardIterator.Panel[] createPanels(
079: WizardDescriptor wiz) {
080: data = new NewJavaHelpIterator.DataModel(wiz);
081: return new BasicWizardIterator.Panel[] { new JavaHelpPanel(wiz,
082: data) };
083: }
084:
085: public @Override
086: void uninitialize(WizardDescriptor wiz) {
087: super .uninitialize(wiz);
088: data = null;
089: }
090:
091: static final class DataModel extends
092: BasicWizardIterator.BasicDataModel {
093:
094: private static final String TEMPLATE_SUFFIX_HS = "-hs.xml"; // NOI18N
095: private static final String[] TEMPLATE_SUFFIXES = {
096: TEMPLATE_SUFFIX_HS, "-idx.xml", // NOI18N
097: "-map.xml", // NOI18N
098: "-toc.xml", // NOI18N
099: "-about.html", // NOI18N
100: };
101: private static final String[] TEMPLATE_RESOURCES = {
102: "template_myplugin.xml", // NOI18N
103: "template_myplugin-idx.xml", // NOI18N
104: "template_myplugin-map.xml", // NOI18N
105: "template_myplugin-toc.xml", // NOI18N
106: "template_myplugin-about.html", // NOI18N
107: };
108:
109: private CreatedModifiedFiles files;
110:
111: DataModel(WizardDescriptor wiz) {
112: super (wiz);
113: }
114:
115: public CreatedModifiedFiles getCreatedModifiedFiles() {
116: if (files == null) {
117: // org.netbeans.modules.foo
118: String codeNameBase = getModuleInfo().getCodeNameBase();
119: // foo
120: String basename = codeNameBase.substring(codeNameBase
121: .lastIndexOf('.') + 1);
122: // org/netbeans/modules/foo/docs/
123: String path = codeNameBase.replace('.', '/') + "/docs/"; // NOI18N
124:
125: files = new CreatedModifiedFiles(getProject());
126: Map<String, String> tokens = new HashMap<String, String>();
127: tokens.put("CODE_NAME", basename); // NOI18N
128: tokens.put("FULL_CODE_NAME", codeNameBase); // NOI18N
129: tokens.put("DISPLAY_NAME", ProjectUtils.getInformation(
130: getProject()).getDisplayName()); // NOI18N
131: tokens.put("HELPSET_PATH", path + basename
132: + TEMPLATE_SUFFIX_HS); // NOI18N
133:
134: //layer registration
135: files
136: .add(files
137: .createLayerEntry(
138: "Services/JavaHelp/" + basename
139: + "-helpset.xml", // NOI18N
140: CreatedModifiedFiles
141: .getTemplate("template_myplugin-helpset.xml"), // NOI18N
142: tokens,
143: null,
144: // Pick an arbitrary place to put it. Can always be moved elsewhere if anyone cares:
145: Collections
146: .<String, Object> singletonMap(
147: "position",
148: Integer
149: .toString(3000 + new Random()
150: .nextInt(1000))))); // NOI18N
151:
152: //copying templates
153: for (int i = 0; i < TEMPLATE_SUFFIXES.length; i++) {
154: FileObject template = CreatedModifiedFiles
155: .getTemplate(TEMPLATE_RESOURCES[i]);
156: String filePath = "javahelp/" + path + basename
157: + TEMPLATE_SUFFIXES[i]; // NOI18N
158: files.add(files.createFileWithSubstitutions(
159: filePath, template, tokens));
160: }
161:
162: // edit some properties
163: Map<String, String> props = new HashMap<String, String>();
164: // Default for javahelp.base (org/netbeans/modules/foo/docs) is correct.
165: // For <checkhelpset> (currently nb.org modules only, but may be bundled in harness some day):
166: props.put("javahelp.hs", basename + TEMPLATE_SUFFIX_HS); // NOI18N
167: // XXX 71527: props.put("jhall.jar", "${harness.dir}/lib/jhall.jar"); // NOI18N
168: files.add(files.propertiesModification(
169: "nbproject/project.properties", props)); // NOI18N
170:
171: //put OpenIDE-Module-Requires into manifest
172: ModifyManifest attribs = new CreatedModifiedFilesFactory.ModifyManifest(
173: getProject()) {
174: protected @Override
175: void performModification(final EditableManifest em,
176: final String name, final String value,
177: final String section)
178: throws IllegalArgumentException {
179: String originalValue = em.getAttribute(name,
180: section);
181: if (originalValue != null) {
182: em.setAttribute(name, originalValue + ","
183: + value, section);
184: } else {
185: super .performModification(em, name, value,
186: section);
187: }
188: }
189:
190: };
191: attribs.setAttribute("OpenIDE-Module-Requires",
192: "org.netbeans.api.javahelp.Help", null); // NOI18N
193: files.add(attribs);
194: }
195: return files;
196: }
197:
198: }
199:
200: }
|