From 4df5716384518c13fe836a689c0769c803836de9 Mon Sep 17 00:00:00 2001 From: benfry Date: Mon, 31 Oct 2005 16:16:45 +0000 Subject: [PATCH] addding folder export and changing its name --- app/Editor.java | 4 +- app/tools/ExportFolder.java | 118 ++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 2 deletions(-) create mode 100755 app/tools/ExportFolder.java diff --git a/app/Editor.java b/app/Editor.java index 9414cfd15..5f121e98a 100644 --- a/app/Editor.java +++ b/app/Editor.java @@ -702,10 +702,10 @@ public class Editor extends JFrame }); menu.add(item); - item = new JMenuItem("Export Multiple..."); + item = new JMenuItem("Export Folder..."); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - new ExportMultiple(Editor.this).show(); + new ExportFolder(Editor.this).show(); } }); menu.add(item); diff --git a/app/tools/ExportFolder.java b/app/tools/ExportFolder.java new file mode 100755 index 000000000..66ed8c06c --- /dev/null +++ b/app/tools/ExportFolder.java @@ -0,0 +1,118 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + ExportFolder - tool to export all sketches within a certain folder + Part of the Processing project - http://processing.org + + Copyright (c) 2005- Ben Fry and Casey Reas + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +package processing.app.tools; + +import processing.app.*; + +import java.io.*; +import java.text.*; +import java.util.*; +import java.util.zip.*; + +import javax.swing.*; + + +public class ExportFolder { + Editor editor; + static JFileChooser fc; + + + public ExportFolder(Editor editor) { + this.editor = editor; + + if (fc == null) { + fc = new JFileChooser(); + fc.setSelectedFile(new File(Sketchbook.getSketchbookPath())); + fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + } + } + + + public void show() { + if (fc.showOpenDialog(new JDialog()) != JFileChooser.APPROVE_OPTION) { + return; + } + + File folder = fc.getSelectedFile(); + // export everything under this folder + + Vector sketches = new Vector(); + try { + addSketches(sketches, folder); + } catch (IOException e) { + e.printStackTrace(); + } + + boolean success = true; + int counter = 0; + + try { + // iterate through the list + Enumeration en = sketches.elements(); + while (en.hasMoreElements()) { + editor.message("Exporting sketch " + (++counter) + + " of " + sketches.size()); + String path = (String) en.nextElement(); + editor.handleOpen(path); + // success may not be that useful, usually an ex is thrown + success = editor.sketch.exportApplet(); + if (!success) break; + //System.out.println("success was " + success); + } + } catch (Exception e) { + editor.error(e); + success = false; + //e.printStackTrace(); + } + + if (success) { + editor.message("Done exporting."); + } // else the error message will be visible + } + + + protected void addSketches(Vector sketches, File folder) throws IOException { + + // skip .DS_Store files, etc + if (!folder.isDirectory()) return; // false; + + String list[] = folder.list(); + // if a bad folder or something like that, this might come back null + if (list == null) return; // false; + + for (int i = 0; i < list.length; i++) { + if (list[i].charAt(0) == '.') continue; + + File subfolder = new File(folder, list[i]); + File entry = new File(subfolder, list[i] + ".pde"); + // if a .pde file of the same prefix as the folder exists.. + if (entry.exists()) { + sketches.add(entry.getAbsolutePath()); + + } else if (subfolder.isDirectory()) { // only follow if a dir + addSketches(sketches, subfolder); + } + } + } +}