implement download option for offline reference

This commit is contained in:
Ben Fry
2022-11-25 13:52:20 -05:00
parent 4f60e15b6c
commit 143fcfd4d9
3 changed files with 68 additions and 5 deletions

View File

@@ -26,6 +26,8 @@ package processing.mode.java;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -90,6 +92,9 @@ public class JavaEditor extends Editor {
/** P5 in decimal; if there are complaints, move to preferences.txt */
static final int REFERENCE_PORT = 8053;
// weird to link to a specific location like this, but it's versioned, so:
static final String REFERENCE_URL =
"https://github.com/processing/processing-website/releases/download/2022-10-05-1459/reference.zip";
Boolean useReferenceServer;
WebServer referenceServer;
@@ -284,7 +289,6 @@ public class JavaEditor extends Editor {
item.addActionListener(e -> {
try {
new Welcome(base);
//new Welcome(base, Preferences.getSketchbookPath().equals(Preferences.getOldSketchbookPath()));
} catch (IOException ioe) {
Messages.showWarning("Unwelcome Error",
"Please report this error to\n" +
@@ -294,13 +298,11 @@ public class JavaEditor extends Editor {
menu.add(item);
item = new JMenuItem(Language.text("menu.help.environment"));
//item.addActionListener(e -> showReference("environment/index.html"));
item.addActionListener(e -> Platform.openURL("https://processing.org/environment/"));
item.addActionListener(e -> showReference("../environment/index.html"));
menu.add(item);
item = new JMenuItem(Language.text("menu.help.reference"));
//item.addActionListener(e -> showReference("index.html"));
item.addActionListener(e -> Platform.openURL("https://processing.org/reference/"));
item.addActionListener(e -> showReference("index.html"));
menu.add(item);
item = Toolkit.newJMenuItemShift(Language.text("menu.help.find_in_reference"), 'F');
@@ -313,6 +315,15 @@ public class JavaEditor extends Editor {
});
menu.add(item);
// Not gonna use "update" since it's more about re-downloading:
// it doesn't make sense to "update" the reference because it's
// specific to a version of the software anyway. [fry 221125]
// item = new JMenuItem(isReferenceDownloaded() ?
// "menu.help.reference.update" : "menu.help.reference.download");
item = new JMenuItem("menu.help.reference.download");
item.addActionListener(e -> new Thread(this::downloadReference).start());
menu.add(item);
menu.addSeparator();
final JMenu libRefSubmenu = new JMenu(Language.text("menu.help.libraries_reference"));
@@ -783,7 +794,15 @@ public class JavaEditor extends Editor {
public void showReference(String name) {
if (useReferenceServer == null) {
// Because of this, it should be possible to create your own dist
// that includes the reference by simply adding it to modes/java.
File referenceZip = new File(mode.getFolder(), "reference.zip");
if (!referenceZip.exists()) {
// For Java Mode (the default), check for a reference.zip in the root
// of the sketchbook folder. If other Modes subclass JavaEditor and
// don't override this function, it may cause a little trouble.
referenceZip = getOfflineReferenceFile();
}
if (referenceZip.exists()) {
try {
referenceServer = new WebServer(referenceZip, REFERENCE_PORT);
@@ -815,6 +834,45 @@ public class JavaEditor extends Editor {
}
private File getOfflineReferenceFile() {
return new File(Base.getSketchbookFolder(), "reference.zip");
}
/*
private boolean isReferenceDownloaded() {
return getOfflineReferenceFile().exists();
}
*/
private void downloadReference() {
try {
URL source = new URL(REFERENCE_URL);
HttpURLConnection conn = (HttpURLConnection) source.openConnection();
HttpURLConnection.setFollowRedirects(true);
conn.setConnectTimeout(15 * 1000);
conn.setReadTimeout(60 * 1000);
conn.setRequestMethod("GET");
conn.connect();
ProgressMonitorInputStream input =
new ProgressMonitorInputStream(this,
"Downloading reference…", conn.getInputStream());
input.getProgressMonitor().setMaximum(conn.getContentLength());
PApplet.saveStream(getOfflineReferenceFile(), input);
useReferenceServer = null;
} catch (InterruptedIOException iioe) {
// download canceled
} catch (IOException e) {
Messages.showWarning("Error downloading reference",
"Could not download the reference. Try again later.", e);
}
}
public void statusError(String what) {
super.statusError(what);
// new Exception("deactivating RUN").printStackTrace();