From 91f3561130f9396d9440228fc522f9d6e7781e0b Mon Sep 17 00:00:00 2001 From: Stef Tervelde Date: Fri, 17 Jan 2025 19:53:19 +0100 Subject: [PATCH] Export to PDEZ button --- build/shared/lib/languages/PDE.properties | 1 + java/src/processing/mode/java/JavaEditor.java | 62 ++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/build/shared/lib/languages/PDE.properties b/build/shared/lib/languages/PDE.properties index fb2042f225..66fcf17298 100644 --- a/build/shared/lib/languages/PDE.properties +++ b/build/shared/lib/languages/PDE.properties @@ -25,6 +25,7 @@ menu.file.close = Close menu.file.save = Save menu.file.save_as = Save As... menu.file.export_application = Export Application... +menu.file.export_pdez = Export as PDEZ... menu.file.page_setup = Page Setup menu.file.print = Print... menu.file.preferences = Preferences... diff --git a/java/src/processing/mode/java/JavaEditor.java b/java/src/processing/mode/java/JavaEditor.java index 338a6bd298..4162192831 100644 --- a/java/src/processing/mode/java/JavaEditor.java +++ b/java/src/processing/mode/java/JavaEditor.java @@ -28,9 +28,13 @@ import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import javax.swing.*; import javax.swing.event.*; @@ -228,7 +232,17 @@ public JMenu buildFileMenu() { } }); - return buildFileMenu(new JMenuItem[] { exportApplication }); + var exportPDEZ = new JMenuItem(Language.text("menu.file.export_pdez")); + exportPDEZ.addActionListener(e -> { + if (sketch.isUntitled() || sketch.isReadOnly()) { + Messages.showMessage("Save First", "Please first save the sketch."); + } else { + handleExportPDEZ(); + } + }); + + + return buildFileMenu(new JMenuItem[] { exportApplication, exportPDEZ }); } @@ -489,6 +503,52 @@ public void handleExportApplication() { } } + /** + * Handler for File → Export PDEZ + */ + public void handleExportPDEZ() { + if (handleExportCheckModified()) { + var sketch = getSketch(); + var folder = sketch.getFolder().toPath(); + var target = new File(folder + ".pdez").toPath(); + if (Files.exists(target)) { + try { + Platform.deleteFile(target.toFile()); + } catch (IOException e) { + Messages.showError("Export Error", "Could not delete existing file: " + target, e); + } + } + + try (var zs = new ZipOutputStream(Files.newOutputStream(target))) { + Files.walk(folder) + .filter(path -> !Files.isDirectory(path)) + .forEach(path -> { + var zipEntry = new ZipEntry(folder.getParent().relativize(path).toString()); + try { + zs.putNextEntry(zipEntry); + Files.copy(path, zs); + zs.closeEntry(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } catch (IOException e) { + throw new RuntimeException(e); + } + if (Desktop.isDesktopSupported()) { + var desktop = Desktop.getDesktop(); + if (desktop.isSupported(Desktop.Action.BROWSE_FILE_DIR)) { + desktop.browseFileDirectory(target.toFile()); + } else { + try { + desktop.open(target.getParent().toFile()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + } + } /** * Checks to see if the sketch has been modified, and if so,