diff --git a/build/shared/lib/languages/PDE.properties b/build/shared/lib/languages/PDE.properties index 98e43f742..c93dfa68e 100644 --- a/build/shared/lib/languages/PDE.properties +++ b/build/shared/lib/languages/PDE.properties @@ -138,6 +138,7 @@ menu.help.about = About Processing menu.help.environment = Environment menu.help.reference = Reference menu.help.find_in_reference = Find in Reference +menu.help.reference.download = Download Offline Reference menu.help.libraries_reference = Libraries Reference menu.help.tools_reference = Tools Reference menu.help.empty = (empty) diff --git a/java/src/processing/mode/java/JavaEditor.java b/java/src/processing/mode/java/JavaEditor.java index efb37b180..4cc29bdf9 100644 --- a/java/src/processing/mode/java/JavaEditor.java +++ b/java/src/processing/mode/java/JavaEditor.java @@ -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(); diff --git a/todo.txt b/todo.txt index becfe2cc1..bb9a2f13d 100755 --- a/todo.txt +++ b/todo.txt @@ -9,6 +9,10 @@ X https://github.com/processing/processing4/issues/600 _ update wiki _ https://github.com/processing/processing4/wiki/Translations _ https://www.oracle.com/java/technologies/javase/jdk17-suported-locales.html +X implement offline reference download +X https://github.com/processing/processing-website/releases/download/2022-10-05-1459/reference.zip +_ add a note about the file size to the progress window +_ document and grab other notes from elsewhere here _ "Show Sketch Folder" for libraries needs to treat the sketch as Untitled