diff --git a/jme3-templates/src/com/jme3/gde/templates/FileUtils.java b/jme3-templates/src/com/jme3/gde/templates/FileUtils.java new file mode 100644 index 000000000..ea57d7f72 --- /dev/null +++ b/jme3-templates/src/com/jme3/gde/templates/FileUtils.java @@ -0,0 +1,84 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.jme3.gde.templates; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.Exceptions; +import org.openide.xml.XMLUtil; +import org.w3c.dom.DOMException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * + * @author rickard + */ +public class FileUtils { + + public static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { + try (source) { + ZipInputStream str = new ZipInputStream(source); + ZipEntry entry; + while ((entry = str.getNextEntry()) != null) { + if (entry.isDirectory()) { + FileUtil.createFolder(projectRoot, entry.getName()); + } else { + FileObject fo = FileUtil.createData(projectRoot, entry.getName()); + if ("nbproject/project.xml".equals(entry.getName())) { + // Special handling for setting name of Ant-based projects; customize as needed: + filterProjectXML(fo, str, projectRoot.getName()); + } else { + writeFile(str, fo); + } + } + } + } + } + + private static void writeFile(ZipInputStream str, FileObject fo) throws IOException { + try (OutputStream out = fo.getOutputStream()) { + FileUtil.copy(str, out); + } + } + + private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException { + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + FileUtil.copy(str, baos); + Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null); + NodeList nl = doc.getDocumentElement().getElementsByTagName("name"); + if (nl != null) { + for (int i = 0; i < nl.getLength(); i++) { + Element el = (Element) nl.item(i); + if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) { + NodeList nl2 = el.getChildNodes(); + if (nl2.getLength() > 0) { + nl2.item(0).setNodeValue(name); + } + break; + } + } + } + try (OutputStream out = fo.getOutputStream()) { + XMLUtil.write(doc, out, "UTF-8"); + } + } catch (IOException | DOMException | SAXException ex) { + Exceptions.printStackTrace(ex); + writeFile(str, fo); + } + + } +} diff --git a/jme3-templates/src/com/jme3/gde/templates/basic/BasicGameWizardIterator.java b/jme3-templates/src/com/jme3/gde/templates/basic/BasicGameWizardIterator.java index 50e932995..da19b96c8 100644 --- a/jme3-templates/src/com/jme3/gde/templates/basic/BasicGameWizardIterator.java +++ b/jme3-templates/src/com/jme3/gde/templates/basic/BasicGameWizardIterator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2010 jMonkeyEngine + * Copyright (c) 2009-2025 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,20 +31,15 @@ */ package com.jme3.gde.templates.basic; +import com.jme3.gde.templates.FileUtils; import java.awt.Component; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.text.MessageFormat; import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.NoSuchElementException; import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; import javax.swing.JComponent; import javax.swing.event.ChangeListener; import org.netbeans.api.project.ProjectManager; @@ -53,13 +48,7 @@ import org.openide.WizardDescriptor; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; -import org.openide.util.Exceptions; import org.openide.util.NbBundle; -import org.openide.xml.XMLUtil; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; @SuppressWarnings({"unchecked", "rawtypes"}) public class BasicGameWizardIterator implements WizardDescriptor./*Progress*/InstantiatingIterator { @@ -94,7 +83,7 @@ private String[] createSteps() { FileObject template = Templates.getTemplate(wiz); FileObject dir = FileUtil.toFileObject(dirF); - unZipFile(template.getInputStream(), dir); + FileUtils.unZipFile(template.getInputStream(), dir); // Always open top dir as a project: resultSet.add(dir); @@ -130,9 +119,7 @@ public void initialize(WizardDescriptor wiz) { // chooser to appear in the list of steps. steps[i] = c.getName(); } - if (c instanceof JComponent) { // assume Swing components - JComponent jc = (JComponent) c; - // Step #. + if (c instanceof JComponent jc) { jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, i); // Step name (actually the whole list for reference). jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps); @@ -194,57 +181,4 @@ public final void addChangeListener(ChangeListener l) { public final void removeChangeListener(ChangeListener l) { } - private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { - try (source) { - ZipInputStream str = new ZipInputStream(source); - ZipEntry entry; - while ((entry = str.getNextEntry()) != null) { - if (entry.isDirectory()) { - FileUtil.createFolder(projectRoot, entry.getName()); - } else { - FileObject fo = FileUtil.createData(projectRoot, entry.getName()); - if ("nbproject/project.xml".equals(entry.getName())) { - // Special handling for setting name of Ant-based projects; customize as needed: - filterProjectXML(fo, str, projectRoot.getName()); - } else { - writeFile(str, fo); - } - } - } - } - } - - private static void writeFile(ZipInputStream str, FileObject fo) throws IOException { - try (OutputStream out = fo.getOutputStream()) { - FileUtil.copy(str, out); - } - } - - private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException { - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - FileUtil.copy(str, baos); - Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null); - NodeList nl = doc.getDocumentElement().getElementsByTagName("name"); - if (nl != null) { - for (int i = 0; i < nl.getLength(); i++) { - Element el = (Element) nl.item(i); - if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) { - NodeList nl2 = el.getChildNodes(); - if (nl2.getLength() > 0) { - nl2.item(0).setNodeValue(name); - } - break; - } - } - } - try (OutputStream out = fo.getOutputStream()) { - XMLUtil.write(doc, out, "UTF-8"); - } - } catch (Exception ex) { - Exceptions.printStackTrace(ex); - writeFile(str, fo); - } - - } } diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/Bundle.properties b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/Bundle.properties similarity index 100% rename from jme3-templates/src/com/jme3/gde/templates/jaimesascent/Bundle.properties rename to jme3-templates/src/com/jme3/gde/templates/downloadedproject/Bundle.properties diff --git a/jme3-templates/src/com/jme3/gde/templates/downloadedproject/Config.java b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/Config.java new file mode 100644 index 000000000..7cfd2becf --- /dev/null +++ b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/Config.java @@ -0,0 +1,32 @@ +package com.jme3.gde.templates.downloadedproject; + + +/** + * + * @author rickard + */ +public class Config { + + private static final String PROJECT_NAME = "JaimesAscent"; + private static final String GITHUB_REF = "JaimesAscent-1.1.1/"; + private static final String ZIP_NAME = "JaimesAscent.zip"; + private static final String GITHUB_PATH = "https://github.com/neph1/JaimesAscent/archive/refs/tags/v1.1.1.zip"; + + + + public String getProjectName() { + return PROJECT_NAME; + } + + public String getGithubRef() { + return GITHUB_REF; + } + + public String getZipName() { + return ZIP_NAME; + } + + public String getGithubPath() { + return GITHUB_PATH; + } +} diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentPanelVisual.form b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectPanelVisual.form similarity index 89% rename from jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentPanelVisual.form rename to jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectPanelVisual.form index 6451a2c59..79df761ea 100644 --- a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentPanelVisual.form +++ b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectPanelVisual.form @@ -66,7 +66,7 @@ - + @@ -81,7 +81,7 @@ - + @@ -93,10 +93,10 @@ - + - + @@ -109,7 +109,7 @@ - + diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentPanelVisual.java b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectPanelVisual.java similarity index 92% rename from jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentPanelVisual.java rename to jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectPanelVisual.java index 79eb619c3..d13e4f6e1 100644 --- a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentPanelVisual.java +++ b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectPanelVisual.java @@ -29,7 +29,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package com.jme3.gde.templates.jaimesascent; +package com.jme3.gde.templates.downloadedproject; import java.io.File; import javax.swing.JFileChooser; @@ -42,14 +42,15 @@ import org.openide.WizardValidationException; import org.openide.filesystems.FileUtil; -public class JaimesAscentPanelVisual extends JPanel implements DocumentListener { +public class ExampleProjectPanelVisual extends JPanel implements DocumentListener { public static final String PROP_PROJECT_NAME = "projectName"; - static final String PROJECT_NAME = "JaimesAscent"; - private final JaimesAscentWizardPanel panel; + private final ExampleProjectWizardPanel panel; + private final Config config; - public JaimesAscentPanelVisual(JaimesAscentWizardPanel panel) { + public ExampleProjectPanelVisual(ExampleProjectWizardPanel panel, Config config) { initComponents(); + this.config = config; this.panel = panel; // Register listener on the textFields to make the automatic updates projectNameTextField.getDocument().addDocumentListener(this); @@ -77,13 +78,13 @@ private void initComponents() { createdFolderTextField = new javax.swing.JTextField(); projectNameLabel.setLabelFor(projectNameTextField); - org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(JaimesAscentPanelVisual.class, "JaimesAscentPanelVisual.projectNameLabel.text")); // NOI18N + org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(ExampleProjectPanelVisual.class, "ExampleProjectPanelVisual.projectNameLabel.text")); // NOI18N projectLocationLabel.setLabelFor(projectLocationTextField); - org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(JaimesAscentPanelVisual.class, "JaimesAscentPanelVisual.projectLocationLabel.text")); // NOI18N + org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(ExampleProjectPanelVisual.class, "ExampleProjectPanelVisual.projectLocationLabel.text")); // NOI18N - org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(JaimesAscentPanelVisual.class, "JaimesAscentPanelVisual.browseButton.text")); // NOI18N - browseButton.setActionCommand(org.openide.util.NbBundle.getMessage(JaimesAscentPanelVisual.class, "JaimesAscentPanelVisual.browseButton.actionCommand")); // NOI18N + org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(ExampleProjectPanelVisual.class, "ExampleProjectPanelVisual.browseButton.text")); // NOI18N + browseButton.setActionCommand(org.openide.util.NbBundle.getMessage(ExampleProjectPanelVisual.class, "ExampleProjectPanelVisual.browseButton.actionCommand")); // NOI18N browseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseButtonActionPerformed(evt); @@ -91,7 +92,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { }); createdFolderLabel.setLabelFor(createdFolderTextField); - org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(JaimesAscentPanelVisual.class, "JaimesAscentPanelVisual.createdFolderLabel.text")); // NOI18N + org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(ExampleProjectPanelVisual.class, "ExampleProjectPanelVisual.createdFolderLabel.text")); // NOI18N createdFolderTextField.setEditable(false); @@ -235,7 +236,7 @@ void read(WizardDescriptor settings) { String projectName = (String) settings.getProperty("name"); if (projectName == null) { - projectName = PROJECT_NAME; + projectName = config.getProjectName(); } this.projectNameTextField.setText(projectName); this.projectNameTextField.selectAll(); diff --git a/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectWizardIterator.java b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectWizardIterator.java new file mode 100644 index 000000000..d2c5a70c4 --- /dev/null +++ b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectWizardIterator.java @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2024 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.gde.templates.downloadedproject; + +import com.jme3.gde.templates.gradledesktop.options.CachedOptionsContainer; +import java.awt.Component; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.text.MessageFormat; +import java.util.Enumeration; +import java.util.LinkedHashSet; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import javax.swing.JComponent; +import javax.swing.event.ChangeListener; +import org.netbeans.api.project.ProjectManager; +import org.netbeans.spi.project.ui.support.ProjectChooser; +import org.openide.WizardDescriptor; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.NbBundle; + +@SuppressWarnings({"unchecked", "rawtypes"}) +public class ExampleProjectWizardIterator implements WizardDescriptor.InstantiatingIterator { + + private int index; + private WizardDescriptor.Panel[] panels; + private WizardDescriptor wiz; + + private Config config = new Config(); + + public ExampleProjectWizardIterator() { + + // Initiate the options getting... + CachedOptionsContainer.getInstance(); + } + + public static ExampleProjectWizardIterator createIterator() { + return new ExampleProjectWizardIterator(); + } + + private WizardDescriptor.Panel[] createPanels() { + return new WizardDescriptor.Panel[]{ + new ProjectDownloadPanel(config), + new ExampleProjectWizardPanel(config) + }; + } + + private String[] createSteps() { + return new String[]{ + NbBundle.getMessage(ExampleProjectWizardIterator.class, "LBL_DownloadProjectStep"), + NbBundle.getMessage(ExampleProjectWizardIterator.class, "LBL_CreateProjectStep"), + }; + } + + @Override + public Set/**/ instantiate(/*ProgressHandle handle*/) throws IOException { + Set resultSet = new LinkedHashSet<>(); + File dirF = FileUtil.normalizeFile((File) wiz.getProperty("projdir")); + dirF.mkdirs(); + + FileObject template = FileUtil.toFileObject(new File( + ProjectDownloadPanel.DOWNLOAD_FOLDER, + config.getZipName())); + + FileObject dir = FileUtil.toFileObject(dirF); + unZipFile(template.getInputStream(), dir); + + // Always open top dir as a project: + resultSet.add(dir); + // Look for nested projects to open as well: + Enumeration e = dir.getFolders(true); + while (e.hasMoreElements()) { + FileObject subfolder = e.nextElement(); + if (ProjectManager.getDefault().isProject(subfolder)) { + resultSet.add(subfolder); + } + } + + File parent = dirF.getParentFile(); + if (parent != null && parent.exists()) { + ProjectChooser.setProjectsFolder(parent); + } + + return resultSet; + } + + @Override + public void initialize(WizardDescriptor wiz) { + this.wiz = wiz; + index = 0; + panels = createPanels(); + // Make sure list of steps is accurate. + String[] steps = createSteps(); + for (int i = 0; i < panels.length; i++) { + Component c = panels[i].getComponent(); + if (steps[i] == null) { + // Default step name to component name of panel. + // Mainly useful for getting the name of the target + // chooser to appear in the list of steps. + steps[i] = c.getName(); + } + if (c instanceof JComponent jc) { // Step #. + // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*: + jc.putClientProperty("WizardPanel_contentSelectedIndex", i); + // Step name (actually the whole list for reference). + jc.putClientProperty("WizardPanel_contentData", steps); + } + } + } + + @Override + public void uninitialize(WizardDescriptor wiz) { + this.wiz.putProperty("projdir", null); + this.wiz.putProperty("name", null); + this.wiz = null; + panels = null; + } + + @Override + public String name() { + return MessageFormat.format("{0} of {1}", + new Object[]{index + 1, panels.length}); + } + + @Override + public boolean hasNext() { + return index < panels.length - 1; + } + + @Override + public boolean hasPrevious() { + return index > 0; + } + + @Override + public void nextPanel() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + index++; + } + + @Override + public void previousPanel() { + if (!hasPrevious()) { + throw new NoSuchElementException(); + } + index--; + } + + @Override + public WizardDescriptor.Panel current() { + return panels[index]; + } + + // If nothing unusual changes in the middle of the wizard, simply: + @Override + public final void addChangeListener(ChangeListener l) { + } + + @Override + public final void removeChangeListener(ChangeListener l) { + } + + private void unZipFile(InputStream source, FileObject projectRoot) throws IOException { + final String master = config.getGithubRef(); + try (source) { + ZipInputStream str = new ZipInputStream(source); + ZipEntry entry; + + while ((entry = str.getNextEntry()) != null) { + if (entry.getName().endsWith(master)) { + continue; + } + final String entryName = entry.getName().replace(master, ""); + if (entry.isDirectory()) { + FileUtil.createFolder(projectRoot, entryName); + } else { + FileObject fo = FileUtil.createData(projectRoot, entryName); + try (OutputStream out = fo.getOutputStream()) { + FileUtil.copy(str, out); + } + } + } + } + } + +} diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentWizardPanel.java b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectWizardPanel.java similarity index 88% rename from jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentWizardPanel.java rename to jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectWizardPanel.java index 25db6a3f1..d03c01b61 100644 --- a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentWizardPanel.java +++ b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ExampleProjectWizardPanel.java @@ -29,7 +29,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package com.jme3.gde.templates.jaimesascent; +package com.jme3.gde.templates.downloadedproject; import java.awt.Component; import java.util.HashSet; @@ -45,20 +45,23 @@ * Panel just asking for basic info. */ @SuppressWarnings({"unchecked", "rawtypes"}) -public class JaimesAscentWizardPanel implements WizardDescriptor.Panel, +public class ExampleProjectWizardPanel implements WizardDescriptor.Panel, WizardDescriptor.ValidatingPanel, WizardDescriptor.FinishablePanel { private WizardDescriptor wizardDescriptor; - private JaimesAscentPanelVisual component; + private ExampleProjectPanelVisual component; + + private final Config config; - public JaimesAscentWizardPanel() { + public ExampleProjectWizardPanel(Config config) { + this.config = config; } @Override public Component getComponent() { if (component == null) { - component = new JaimesAscentPanelVisual(this); - component.setName(NbBundle.getMessage(JaimesAscentWizardPanel.class, "LBL_CreateProjectStep")); + component = new ExampleProjectPanelVisual(this, config); + component.setName(NbBundle.getMessage(ExampleProjectWizardPanel.class, "LBL_CreateProjectStep")); } return component; } diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanel.java b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanel.java similarity index 90% rename from jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanel.java rename to jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanel.java index b2d9c6270..c6618d4d6 100644 --- a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanel.java +++ b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanel.java @@ -29,7 +29,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package com.jme3.gde.templates.jaimesascent; +package com.jme3.gde.templates.downloadedproject; import java.awt.Component; import java.io.BufferedInputStream; @@ -51,20 +51,22 @@ * Panel just asking for basic info. */ @SuppressWarnings({"unchecked", "rawtypes"}) -public class JaimesAscentDownloadPanel implements WizardDescriptor.Panel, +public class ProjectDownloadPanel implements WizardDescriptor.Panel, WizardDescriptor.ValidatingPanel, WizardDescriptor.FinishablePanel { private WizardDescriptor wizardDescriptor; - private JaimesAscentDownloadPanelVisual component; + private ProjectDownloadPanelVisual component; - static String ZIP_NAME = "JaimesAscent.zip"; static String DOWNLOAD_FOLDER = System.getProperty("java.io.tmpdir"); + + private final Config config; - public JaimesAscentDownloadPanel() { + public ProjectDownloadPanel(Config config) { + this.config = config; } public int doDownloadZip() { - return downloadFile("https://github.com/neph1/JaimesAscent/archive/refs/tags/v1.1.1.zip", DOWNLOAD_FOLDER, ZIP_NAME); + return downloadFile(config.getGithubPath(), DOWNLOAD_FOLDER, config.getZipName()); } private int downloadFile(String fileURL, String saveDir, String fileName) { @@ -120,8 +122,8 @@ private int downloadFile(String fileURL, String saveDir, String fileName) { @Override public Component getComponent() { if (component == null) { - component = new JaimesAscentDownloadPanelVisual(this); - component.setName(NbBundle.getMessage(JaimesAscentDownloadPanel.class, "LBL_DownloadProjectStep")); + component = new ProjectDownloadPanelVisual(this); + component.setName(NbBundle.getMessage(ProjectDownloadPanel.class, "LBL_DownloadProjectStep")); } return component; } diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanelVisual.form b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanelVisual.form similarity index 86% rename from jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanelVisual.form rename to jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanelVisual.form index 9caac7e1a..ef007b75b 100644 --- a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanelVisual.form +++ b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanelVisual.form @@ -62,10 +62,10 @@ - + - + @@ -86,7 +86,7 @@ - + @@ -98,7 +98,7 @@ - + @@ -109,10 +109,10 @@ - + - + diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanelVisual.java b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanelVisual.java similarity index 86% rename from jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanelVisual.java rename to jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanelVisual.java index 04f83bef8..d93895f3a 100644 --- a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentDownloadPanelVisual.java +++ b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/ProjectDownloadPanelVisual.java @@ -29,7 +29,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package com.jme3.gde.templates.jaimesascent; +package com.jme3.gde.templates.downloadedproject; import javax.swing.JPanel; import javax.swing.event.DocumentEvent; @@ -37,12 +37,12 @@ import org.openide.WizardDescriptor; import org.openide.WizardValidationException; -public class JaimesAscentDownloadPanelVisual extends JPanel implements DocumentListener { +public class ProjectDownloadPanelVisual extends JPanel implements DocumentListener { public static final String PROP_PROJECT_NAME = "projectName"; - private final JaimesAscentDownloadPanel panel; + private final ProjectDownloadPanel panel; - public JaimesAscentDownloadPanelVisual(JaimesAscentDownloadPanel panel) { + public ProjectDownloadPanelVisual(ProjectDownloadPanel panel) { initComponents(); this.panel = panel; } @@ -61,8 +61,8 @@ private void initComponents() { statusField = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); - org.openide.awt.Mnemonics.setLocalizedText(downloadButton, org.openide.util.NbBundle.getMessage(JaimesAscentDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.downloadButton.text")); // NOI18N - downloadButton.setActionCommand(org.openide.util.NbBundle.getMessage(JaimesAscentDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.downloadButton.actionCommand")); // NOI18N + org.openide.awt.Mnemonics.setLocalizedText(downloadButton, org.openide.util.NbBundle.getMessage(ProjectDownloadPanelVisual.class, "ProjectDownloadPanelVisual.downloadButton.text")); // NOI18N + downloadButton.setActionCommand(org.openide.util.NbBundle.getMessage(ProjectDownloadPanelVisual.class, "ProjectDownloadPanelVisual.downloadButton.actionCommand")); // NOI18N downloadButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { downloadButtonActionPerformed(evt); @@ -73,13 +73,13 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { jTextArea1.setColumns(20); jTextArea1.setLineWrap(true); jTextArea1.setRows(5); - jTextArea1.setText(org.openide.util.NbBundle.getMessage(JaimesAscentDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.jTextArea1.text")); // NOI18N + jTextArea1.setText(org.openide.util.NbBundle.getMessage(ProjectDownloadPanelVisual.class, "ProjectDownloadPanelVisual.jTextArea1.text")); // NOI18N jTextArea1.setWrapStyleWord(true); jTextArea1.setEnabled(false); jScrollPane1.setViewportView(jTextArea1); statusField.setEditable(false); - statusField.setText(org.openide.util.NbBundle.getMessage(JaimesAscentDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.statusField.text")); // NOI18N + statusField.setText(org.openide.util.NbBundle.getMessage(ProjectDownloadPanelVisual.class, "ProjectDownloadPanelVisual.statusField.text")); // NOI18N statusField.setEnabled(false); statusField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { @@ -87,8 +87,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { } }); - jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jme3/gde/templates/jaimesascent/jaimesascent.png"))); // NOI18N - org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(JaimesAscentDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.jLabel1.text")); // NOI18N + jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/jme3/gde/templates/downloadedproject/jaimesascent.png"))); // NOI18N + org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ProjectDownloadPanelVisual.class, "ProjectDownloadPanelVisual.jLabel1.text")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); @@ -127,12 +127,12 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { }// //GEN-END:initComponents private void downloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadButtonActionPerformed - statusField.setText(org.openide.util.NbBundle.getMessage(JaimesAscentDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.downloading")); + statusField.setText(org.openide.util.NbBundle.getMessage(ProjectDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.downloading")); final int result = this.panel.doDownloadZip(); if (result == 1) { - statusField.setText(org.openide.util.NbBundle.getMessage(JaimesAscentDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.downloadSuccess")); + statusField.setText(org.openide.util.NbBundle.getMessage(ProjectDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.downloadSuccess")); } else { - statusField.setText(org.openide.util.NbBundle.getMessage(JaimesAscentDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.downloadFailed")); + statusField.setText(org.openide.util.NbBundle.getMessage(ProjectDownloadPanelVisual.class, "JaimesAscentDownloadPanelVisual.downloadFailed")); } }//GEN-LAST:event_downloadButtonActionPerformed diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/build.gradle.ftl b/jme3-templates/src/com/jme3/gde/templates/downloadedproject/build.gradle.ftl similarity index 100% rename from jme3-templates/src/com/jme3/gde/templates/jaimesascent/build.gradle.ftl rename to jme3-templates/src/com/jme3/gde/templates/downloadedproject/build.gradle.ftl diff --git a/jme3-templates/src/com/jme3/gde/templates/gradledesktop/GradleDesktopGameWizardIterator.java b/jme3-templates/src/com/jme3/gde/templates/gradledesktop/GradleDesktopGameWizardIterator.java index e0f204379..340c2d106 100644 --- a/jme3-templates/src/com/jme3/gde/templates/gradledesktop/GradleDesktopGameWizardIterator.java +++ b/jme3-templates/src/com/jme3/gde/templates/gradledesktop/GradleDesktopGameWizardIterator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2010 jMonkeyEngine + * Copyright (c) 2009-2025 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,17 +31,14 @@ */ package com.jme3.gde.templates.gradledesktop; +import com.jme3.gde.templates.FileUtils; import com.jme3.gde.templates.gradledesktop.options.CachedOptionsContainer; import java.awt.Component; import java.io.BufferedReader; import java.io.BufferedWriter; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; @@ -54,8 +51,6 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; @@ -68,13 +63,7 @@ import org.openide.WizardDescriptor; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; -import org.openide.util.Exceptions; import org.openide.util.NbBundle; -import org.openide.xml.XMLUtil; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; @SuppressWarnings({"unchecked", "rawtypes"}) public class GradleDesktopGameWizardIterator implements WizardDescriptor./*Progress*/InstantiatingIterator { @@ -122,7 +111,7 @@ private String[] createSteps() { FileObject template = Templates.getTemplate(wiz); FileObject dir = FileUtil.toFileObject(dirF); - unZipFile(template.getInputStream(), dir); + FileUtils.unZipFile(template.getInputStream(), dir); // Create settings.gradle from template File gradleSettingsFile = new File(dirF, "settings.gradle"); @@ -176,9 +165,7 @@ public void initialize(WizardDescriptor wiz) { // chooser to appear in the list of steps. steps[i] = c.getName(); } - if (c instanceof JComponent) { // assume Swing components - JComponent jc = (JComponent) c; - // Step #. + if (c instanceof JComponent jc) { // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*: jc.putClientProperty("WizardPanel_contentSelectedIndex", i); // Step name (actually the whole list for reference). @@ -261,57 +248,4 @@ private void createFileFromTemplate(File target, String templateResourcePath, Ma } } - private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { - try (source) { - ZipInputStream str = new ZipInputStream(source); - ZipEntry entry; - while ((entry = str.getNextEntry()) != null) { - if (entry.isDirectory()) { - FileUtil.createFolder(projectRoot, entry.getName()); - } else { - FileObject fo = FileUtil.createData(projectRoot, entry.getName()); - if ("nbproject/project.xml".equals(entry.getName())) { - // Special handling for setting name of Ant-based projects; customize as needed: - filterProjectXML(fo, str, projectRoot.getName()); - } else { - writeFile(str, fo); - } - } - } - } - } - - private static void writeFile(ZipInputStream str, FileObject fo) throws IOException { - try (OutputStream out = fo.getOutputStream()) { - FileUtil.copy(str, out); - } - } - - private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException { - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - FileUtil.copy(str, baos); - Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null); - NodeList nl = doc.getDocumentElement().getElementsByTagName("name"); - if (nl != null) { - for (int i = 0; i < nl.getLength(); i++) { - Element el = (Element) nl.item(i); - if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) { - NodeList nl2 = el.getChildNodes(); - if (nl2.getLength() > 0) { - nl2.item(0).setNodeValue(name); - } - break; - } - } - } - try (OutputStream out = fo.getOutputStream()) { - XMLUtil.write(doc, out, "UTF-8"); - } - } catch (Exception ex) { - Exceptions.printStackTrace(ex); - writeFile(str, fo); - } - - } } diff --git a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentWizardIterator.java b/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentWizardIterator.java index 9dce2a723..def3477b2 100644 --- a/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentWizardIterator.java +++ b/jme3-templates/src/com/jme3/gde/templates/jaimesascent/JaimesAscentWizardIterator.java @@ -1,221 +1,15 @@ /* - * Copyright (c) 2024 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package com.jme3.gde.templates.jaimesascent; -import com.jme3.gde.templates.gradledesktop.options.CachedOptionsContainer; -import java.awt.Component; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.text.MessageFormat; -import java.util.Enumeration; -import java.util.LinkedHashSet; -import java.util.NoSuchElementException; -import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import javax.swing.JComponent; -import javax.swing.event.ChangeListener; -import org.netbeans.api.project.ProjectManager; -import org.netbeans.spi.project.ui.support.ProjectChooser; -import org.openide.WizardDescriptor; -import org.openide.filesystems.FileObject; -import org.openide.filesystems.FileUtil; -import org.openide.util.NbBundle; - -@SuppressWarnings({"unchecked", "rawtypes"}) -public class JaimesAscentWizardIterator implements WizardDescriptor.InstantiatingIterator { +import com.jme3.gde.templates.downloadedproject.ExampleProjectWizardIterator; - private int index; - private WizardDescriptor.Panel[] panels; - private WizardDescriptor wiz; - - final static String master = "JaimesAscent-1.1.1/"; +/** + * + * @author rickard + */ +public class JaimesAscentWizardIterator extends ExampleProjectWizardIterator{ - public JaimesAscentWizardIterator() { - - // Initiate the options getting... - CachedOptionsContainer.getInstance(); - } - - public static JaimesAscentWizardIterator createIterator() { - return new JaimesAscentWizardIterator(); - } - - private WizardDescriptor.Panel[] createPanels() { - return new WizardDescriptor.Panel[]{ - new JaimesAscentDownloadPanel(), - new JaimesAscentWizardPanel() - }; - } - - private String[] createSteps() { - return new String[]{ - NbBundle.getMessage(JaimesAscentWizardIterator.class, "LBL_DownloadProjectStep"), - NbBundle.getMessage(JaimesAscentWizardIterator.class, "LBL_CreateProjectStep"), - }; - } - - @Override - public Set/**/ instantiate(/*ProgressHandle handle*/) throws IOException { - Set resultSet = new LinkedHashSet<>(); - File dirF = FileUtil.normalizeFile((File) wiz.getProperty("projdir")); - dirF.mkdirs(); - - FileObject template = FileUtil.toFileObject(new File( - JaimesAscentDownloadPanel.DOWNLOAD_FOLDER, - JaimesAscentDownloadPanel.ZIP_NAME)); - - FileObject dir = FileUtil.toFileObject(dirF); - unZipFile(template.getInputStream(), dir); - - // Always open top dir as a project: - resultSet.add(dir); - // Look for nested projects to open as well: - Enumeration e = dir.getFolders(true); - while (e.hasMoreElements()) { - FileObject subfolder = e.nextElement(); - if (ProjectManager.getDefault().isProject(subfolder)) { - resultSet.add(subfolder); - } - } - - File parent = dirF.getParentFile(); - if (parent != null && parent.exists()) { - ProjectChooser.setProjectsFolder(parent); - } - - return resultSet; - } - - @Override - public void initialize(WizardDescriptor wiz) { - this.wiz = wiz; - index = 0; - panels = createPanels(); - // Make sure list of steps is accurate. - String[] steps = createSteps(); - for (int i = 0; i < panels.length; i++) { - Component c = panels[i].getComponent(); - if (steps[i] == null) { - // Default step name to component name of panel. - // Mainly useful for getting the name of the target - // chooser to appear in the list of steps. - steps[i] = c.getName(); - } - if (c instanceof JComponent jc) { // Step #. - // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*: - jc.putClientProperty("WizardPanel_contentSelectedIndex", i); - // Step name (actually the whole list for reference). - jc.putClientProperty("WizardPanel_contentData", steps); - } - } - } - - @Override - public void uninitialize(WizardDescriptor wiz) { - this.wiz.putProperty("projdir", null); - this.wiz.putProperty("name", null); - this.wiz = null; - panels = null; - } - - @Override - public String name() { - return MessageFormat.format("{0} of {1}", - new Object[]{index + 1, panels.length}); - } - - @Override - public boolean hasNext() { - return index < panels.length - 1; - } - - @Override - public boolean hasPrevious() { - return index > 0; - } - - @Override - public void nextPanel() { - if (!hasNext()) { - throw new NoSuchElementException(); - } - index++; - } - - @Override - public void previousPanel() { - if (!hasPrevious()) { - throw new NoSuchElementException(); - } - index--; - } - - @Override - public WizardDescriptor.Panel current() { - return panels[index]; - } - - // If nothing unusual changes in the middle of the wizard, simply: - @Override - public final void addChangeListener(ChangeListener l) { - } - - @Override - public final void removeChangeListener(ChangeListener l) { - } - - private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { - try (source) { - ZipInputStream str = new ZipInputStream(source); - ZipEntry entry; - - while ((entry = str.getNextEntry()) != null) { - if (entry.getName().endsWith(master)) { - continue; - } - final String entryName = entry.getName().replace(master, ""); - if (entry.isDirectory()) { - FileUtil.createFolder(projectRoot, entryName); - } else { - FileObject fo = FileUtil.createData(projectRoot, entryName); - try (OutputStream out = fo.getOutputStream()) { - FileUtil.copy(str, out); - } - } - } - } - } - }