|
26 | 26 | import java.awt.*; |
27 | 27 | import java.awt.event.*; |
28 | 28 | import java.io.*; |
| 29 | +import java.net.HttpURLConnection; |
| 30 | +import java.net.URL; |
29 | 31 | import java.util.ArrayList; |
30 | 32 | import java.util.List; |
31 | 33 | import java.util.Map; |
@@ -90,6 +92,9 @@ public class JavaEditor extends Editor { |
90 | 92 |
|
91 | 93 | /** P5 in decimal; if there are complaints, move to preferences.txt */ |
92 | 94 | static final int REFERENCE_PORT = 8053; |
| 95 | + // weird to link to a specific location like this, but it's versioned, so: |
| 96 | + static final String REFERENCE_URL = |
| 97 | + "https://github.com/processing/processing-website/releases/download/2022-10-05-1459/reference.zip"; |
93 | 98 | Boolean useReferenceServer; |
94 | 99 | WebServer referenceServer; |
95 | 100 |
|
@@ -284,7 +289,6 @@ public JMenu buildHelpMenu() { |
284 | 289 | item.addActionListener(e -> { |
285 | 290 | try { |
286 | 291 | new Welcome(base); |
287 | | - //new Welcome(base, Preferences.getSketchbookPath().equals(Preferences.getOldSketchbookPath())); |
288 | 292 | } catch (IOException ioe) { |
289 | 293 | Messages.showWarning("Unwelcome Error", |
290 | 294 | "Please report this error to\n" + |
@@ -294,13 +298,11 @@ public JMenu buildHelpMenu() { |
294 | 298 | menu.add(item); |
295 | 299 |
|
296 | 300 | item = new JMenuItem(Language.text("menu.help.environment")); |
297 | | - //item.addActionListener(e -> showReference("environment/index.html")); |
298 | | - item.addActionListener(e -> Platform.openURL("https://processing.org/environment/")); |
| 301 | + item.addActionListener(e -> showReference("../environment/index.html")); |
299 | 302 | menu.add(item); |
300 | 303 |
|
301 | 304 | item = new JMenuItem(Language.text("menu.help.reference")); |
302 | | - //item.addActionListener(e -> showReference("index.html")); |
303 | | - item.addActionListener(e -> Platform.openURL("https://processing.org/reference/")); |
| 305 | + item.addActionListener(e -> showReference("index.html")); |
304 | 306 | menu.add(item); |
305 | 307 |
|
306 | 308 | item = Toolkit.newJMenuItemShift(Language.text("menu.help.find_in_reference"), 'F'); |
@@ -313,6 +315,15 @@ public JMenu buildHelpMenu() { |
313 | 315 | }); |
314 | 316 | menu.add(item); |
315 | 317 |
|
| 318 | + // Not gonna use "update" since it's more about re-downloading: |
| 319 | + // it doesn't make sense to "update" the reference because it's |
| 320 | + // specific to a version of the software anyway. [fry 221125] |
| 321 | +// item = new JMenuItem(isReferenceDownloaded() ? |
| 322 | +// "menu.help.reference.update" : "menu.help.reference.download"); |
| 323 | + item = new JMenuItem("menu.help.reference.download"); |
| 324 | + item.addActionListener(e -> new Thread(this::downloadReference).start()); |
| 325 | + menu.add(item); |
| 326 | + |
316 | 327 | menu.addSeparator(); |
317 | 328 |
|
318 | 329 | final JMenu libRefSubmenu = new JMenu(Language.text("menu.help.libraries_reference")); |
@@ -783,7 +794,15 @@ public void changedUpdate(DocumentEvent e) { |
783 | 794 |
|
784 | 795 | public void showReference(String name) { |
785 | 796 | if (useReferenceServer == null) { |
| 797 | + // Because of this, it should be possible to create your own dist |
| 798 | + // that includes the reference by simply adding it to modes/java. |
786 | 799 | File referenceZip = new File(mode.getFolder(), "reference.zip"); |
| 800 | + if (!referenceZip.exists()) { |
| 801 | + // For Java Mode (the default), check for a reference.zip in the root |
| 802 | + // of the sketchbook folder. If other Modes subclass JavaEditor and |
| 803 | + // don't override this function, it may cause a little trouble. |
| 804 | + referenceZip = getOfflineReferenceFile(); |
| 805 | + } |
787 | 806 | if (referenceZip.exists()) { |
788 | 807 | try { |
789 | 808 | referenceServer = new WebServer(referenceZip, REFERENCE_PORT); |
@@ -815,6 +834,45 @@ public void showReference(String name) { |
815 | 834 | } |
816 | 835 |
|
817 | 836 |
|
| 837 | + private File getOfflineReferenceFile() { |
| 838 | + return new File(Base.getSketchbookFolder(), "reference.zip"); |
| 839 | + } |
| 840 | + |
| 841 | + |
| 842 | + /* |
| 843 | + private boolean isReferenceDownloaded() { |
| 844 | + return getOfflineReferenceFile().exists(); |
| 845 | + } |
| 846 | + */ |
| 847 | + |
| 848 | + |
| 849 | + private void downloadReference() { |
| 850 | + try { |
| 851 | + URL source = new URL(REFERENCE_URL); |
| 852 | + HttpURLConnection conn = (HttpURLConnection) source.openConnection(); |
| 853 | + HttpURLConnection.setFollowRedirects(true); |
| 854 | + conn.setConnectTimeout(15 * 1000); |
| 855 | + conn.setReadTimeout(60 * 1000); |
| 856 | + conn.setRequestMethod("GET"); |
| 857 | + conn.connect(); |
| 858 | + |
| 859 | + ProgressMonitorInputStream input = |
| 860 | + new ProgressMonitorInputStream(this, |
| 861 | + "Downloading reference…", conn.getInputStream()); |
| 862 | + input.getProgressMonitor().setMaximum(conn.getContentLength()); |
| 863 | + PApplet.saveStream(getOfflineReferenceFile(), input); |
| 864 | + useReferenceServer = null; |
| 865 | + |
| 866 | + } catch (InterruptedIOException iioe) { |
| 867 | + // download canceled |
| 868 | + |
| 869 | + } catch (IOException e) { |
| 870 | + Messages.showWarning("Error downloading reference", |
| 871 | + "Could not download the reference. Try again later.", e); |
| 872 | + } |
| 873 | + } |
| 874 | + |
| 875 | + |
818 | 876 | public void statusError(String what) { |
819 | 877 | super.statusError(what); |
820 | 878 | // new Exception("deactivating RUN").printStackTrace(); |
|