Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export to PDEZ button #927

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/shared/lib/languages/PDE.properties
Original file line number Diff line number Diff line change
@@ -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...
62 changes: 61 additions & 1 deletion java/src/processing/mode/java/JavaEditor.java
Original file line number Diff line number Diff line change
@@ -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,