|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.gde.templates.jaimesascent; |
| 33 | + |
| 34 | +import java.awt.Component; |
| 35 | +import java.io.BufferedInputStream; |
| 36 | +import java.io.File; |
| 37 | +import java.io.FileOutputStream; |
| 38 | +import java.io.IOException; |
| 39 | +import java.net.HttpURLConnection; |
| 40 | +import java.net.URL; |
| 41 | +import java.util.HashSet; |
| 42 | +import java.util.Set; |
| 43 | +import javax.swing.event.ChangeEvent; |
| 44 | +import javax.swing.event.ChangeListener; |
| 45 | +import org.openide.WizardDescriptor; |
| 46 | +import org.openide.WizardValidationException; |
| 47 | +import org.openide.util.HelpCtx; |
| 48 | +import org.openide.util.NbBundle; |
| 49 | + |
| 50 | +/** |
| 51 | + * Panel just asking for basic info. |
| 52 | + */ |
| 53 | +@SuppressWarnings({"unchecked", "rawtypes"}) |
| 54 | +public class JaimesAscentDownloadPanel implements WizardDescriptor.Panel, |
| 55 | + WizardDescriptor.ValidatingPanel, WizardDescriptor.FinishablePanel { |
| 56 | + |
| 57 | + private WizardDescriptor wizardDescriptor; |
| 58 | + private JaimesAscentDownloadPanelVisual component; |
| 59 | + |
| 60 | + static String ZIP_NAME = "JaimesAscent.zip"; |
| 61 | + static String DOWNLOAD_FOLDER = System.getProperty("java.io.tmpdir"); |
| 62 | + |
| 63 | + public JaimesAscentDownloadPanel() { |
| 64 | + } |
| 65 | + |
| 66 | + public int doDownloadZip() { |
| 67 | + return downloadFile("https://github.com/neph1/JaimesAscent/archive/refs/tags/v1.1.1.zip", DOWNLOAD_FOLDER, ZIP_NAME); |
| 68 | + } |
| 69 | + |
| 70 | + private int downloadFile(String fileURL, String saveDir, String fileName) { |
| 71 | + HttpURLConnection httpConn = null; |
| 72 | + BufferedInputStream inputStream = null; |
| 73 | + FileOutputStream fileOutputStream = null; |
| 74 | + |
| 75 | + final File outputFile = new File(saveDir, fileName); |
| 76 | + |
| 77 | + if (outputFile.exists()) { |
| 78 | + return 1; |
| 79 | + } |
| 80 | + |
| 81 | + try { |
| 82 | + // Create URL object |
| 83 | + URL url = new URL(fileURL); |
| 84 | + httpConn = (HttpURLConnection) url.openConnection(); |
| 85 | + |
| 86 | + // Check HTTP response code |
| 87 | + int responseCode = httpConn.getResponseCode(); |
| 88 | + if (responseCode == HttpURLConnection.HTTP_OK) { |
| 89 | + // Open input stream from the HTTP connection |
| 90 | + inputStream = new BufferedInputStream(httpConn.getInputStream()); |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + // Create output stream to save the file |
| 95 | + fileOutputStream = new FileOutputStream(outputFile); |
| 96 | + |
| 97 | + byte[] buffer = new byte[4096]; |
| 98 | + int bytesRead; |
| 99 | + while ((bytesRead = inputStream.read(buffer)) != -1) { |
| 100 | + fileOutputStream.write(buffer, 0, bytesRead); |
| 101 | + } |
| 102 | + |
| 103 | + return 1; |
| 104 | + } |
| 105 | + } catch (IOException e) { |
| 106 | + return 0; |
| 107 | + } finally { |
| 108 | + // Close resources |
| 109 | + try { |
| 110 | + if (inputStream != null) inputStream.close(); |
| 111 | + if (fileOutputStream != null) fileOutputStream.close(); |
| 112 | + if (httpConn != null) httpConn.disconnect(); |
| 113 | + } catch (IOException ex) { |
| 114 | + return 0; |
| 115 | + } |
| 116 | + } |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Component getComponent() { |
| 122 | + if (component == null) { |
| 123 | + component = new JaimesAscentDownloadPanelVisual(this); |
| 124 | + component.setName(NbBundle.getMessage(JaimesAscentDownloadPanel.class, "LBL_DownloadProjectStep")); |
| 125 | + } |
| 126 | + return component; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public HelpCtx getHelp() { |
| 131 | + return new HelpCtx("sdk.download_project"); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean isValid() { |
| 136 | + getComponent(); |
| 137 | + return component.valid(wizardDescriptor); |
| 138 | + } |
| 139 | + |
| 140 | + private final Set<ChangeListener> listeners = new HashSet<>(1); // or can use ChangeSupport in NB 6.0 |
| 141 | + |
| 142 | + @Override |
| 143 | + public final void addChangeListener(ChangeListener l) { |
| 144 | + synchronized (listeners) { |
| 145 | + listeners.add(l); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public final void removeChangeListener(ChangeListener l) { |
| 151 | + synchronized (listeners) { |
| 152 | + listeners.remove(l); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + protected final void fireChangeEvent() { |
| 157 | + Set<ChangeListener> ls; |
| 158 | + synchronized (listeners) { |
| 159 | + ls = new HashSet<>(listeners); |
| 160 | + } |
| 161 | + ChangeEvent ev = new ChangeEvent(this); |
| 162 | + for (ChangeListener l : ls) { |
| 163 | + l.stateChanged(ev); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void readSettings(Object settings) { |
| 169 | + wizardDescriptor = (WizardDescriptor) settings; |
| 170 | + component.read(wizardDescriptor); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void storeSettings(Object settings) { |
| 175 | + WizardDescriptor d = (WizardDescriptor) settings; |
| 176 | + component.store(d); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public boolean isFinishPanel() { |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public void validate() throws WizardValidationException { |
| 186 | + getComponent(); |
| 187 | + component.validate(wizardDescriptor); |
| 188 | + } |
| 189 | +} |
0 commit comments