Skip to content

Commit e318223

Browse files
authored
Merge pull request #927 from Stefterv/export-to-pdez
Export to PDEZ button
2 parents 628b69d + 91f3561 commit e318223

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

Diff for: build/shared/lib/languages/PDE.properties

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ menu.file.close = Close
2525
menu.file.save = Save
2626
menu.file.save_as = Save As...
2727
menu.file.export_application = Export Application...
28+
menu.file.export_pdez = Export as PDEZ...
2829
menu.file.page_setup = Page Setup
2930
menu.file.print = Print...
3031
menu.file.preferences = Preferences...

Diff for: java/src/processing/mode/java/JavaEditor.java

+61-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828
import java.io.*;
2929
import java.net.HttpURLConnection;
3030
import java.net.URL;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
3133
import java.util.ArrayList;
3234
import java.util.List;
3335
import java.util.Map;
36+
import java.util.zip.ZipEntry;
37+
import java.util.zip.ZipOutputStream;
3438

3539
import javax.swing.*;
3640
import javax.swing.event.*;
@@ -228,7 +232,17 @@ public JMenu buildFileMenu() {
228232
}
229233
});
230234

231-
return buildFileMenu(new JMenuItem[] { exportApplication });
235+
var exportPDEZ = new JMenuItem(Language.text("menu.file.export_pdez"));
236+
exportPDEZ.addActionListener(e -> {
237+
if (sketch.isUntitled() || sketch.isReadOnly()) {
238+
Messages.showMessage("Save First", "Please first save the sketch.");
239+
} else {
240+
handleExportPDEZ();
241+
}
242+
});
243+
244+
245+
return buildFileMenu(new JMenuItem[] { exportApplication, exportPDEZ });
232246
}
233247

234248

@@ -489,6 +503,52 @@ public void handleExportApplication() {
489503
}
490504
}
491505

506+
/**
507+
* Handler for File → Export PDEZ
508+
*/
509+
public void handleExportPDEZ() {
510+
if (handleExportCheckModified()) {
511+
var sketch = getSketch();
512+
var folder = sketch.getFolder().toPath();
513+
var target = new File(folder + ".pdez").toPath();
514+
if (Files.exists(target)) {
515+
try {
516+
Platform.deleteFile(target.toFile());
517+
} catch (IOException e) {
518+
Messages.showError("Export Error", "Could not delete existing file: " + target, e);
519+
}
520+
}
521+
522+
try (var zs = new ZipOutputStream(Files.newOutputStream(target))) {
523+
Files.walk(folder)
524+
.filter(path -> !Files.isDirectory(path))
525+
.forEach(path -> {
526+
var zipEntry = new ZipEntry(folder.getParent().relativize(path).toString());
527+
try {
528+
zs.putNextEntry(zipEntry);
529+
Files.copy(path, zs);
530+
zs.closeEntry();
531+
} catch (IOException e) {
532+
throw new RuntimeException(e);
533+
}
534+
});
535+
} catch (IOException e) {
536+
throw new RuntimeException(e);
537+
}
538+
if (Desktop.isDesktopSupported()) {
539+
var desktop = Desktop.getDesktop();
540+
if (desktop.isSupported(Desktop.Action.BROWSE_FILE_DIR)) {
541+
desktop.browseFileDirectory(target.toFile());
542+
} else {
543+
try {
544+
desktop.open(target.getParent().toFile());
545+
} catch (IOException e) {
546+
throw new RuntimeException(e);
547+
}
548+
}
549+
}
550+
}
551+
}
492552

493553
/**
494554
* Checks to see if the sketch has been modified, and if so,

0 commit comments

Comments
 (0)