diff --git a/app/PdeBase.java b/app/PdeBase.java index b767c7a89..5d258ee4f 100644 --- a/app/PdeBase.java +++ b/app/PdeBase.java @@ -358,7 +358,6 @@ public class PdeBase { static public void copyFile(File afile, File bfile) throws IOException { - //try { InputStream from = new BufferedInputStream(new FileInputStream(afile)); OutputStream to = new BufferedOutputStream(new FileOutputStream(bfile)); byte[] buffer = new byte[16 * 1024]; @@ -373,7 +372,7 @@ public class PdeBase { to = null; #ifdef JDK13 - bfile.setLastModified(afile.lastModified()); // jdk13+ required + bfile.setLastModified(afile.lastModified()); // jdk13+ required #endif //} catch (IOException e) { // e.printStackTrace(); @@ -381,21 +380,46 @@ public class PdeBase { } + /** + * Grab the contents of a file as a string. + */ + static public String loadFile(File file) throws IOException { + // empty code file.. no worries, might be getting filled up later + if (file.length() == 0) return ""; + + InputStreamReader isr = new InputStreamReader(new FileInputStream(file)); + BufferedReader reader = new BufferedReader(isr); + + StringBuffer buffer = new StringBuffer(); + String line = null; + while ((line = reader.readLine()) != null) { + buffer.append(line); + buffer.append('\n'); + } + reader.close(); + return buffer.toString(); + } + + /** * Spew the contents of a String object out to a file. */ - static public void saveFile(String contents, File location) - throws IOException { + static public void saveFile(String str, + File file) throws IOException { - BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(contents.getBytes()))); - PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(location))); + ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes()); + InputStreamReader isr = new InputStreamReader(bis); + BufferedReader reader = new BufferedReader(isr); + + FileWriter fw = new FileWriter(file); + PrintWriter writer = new PrintWriter(new BufferedWriter(fw)); String line = null; while ((line = reader.readLine()) != null) { writer.println(line); } writer.flush(); - writer.close(); + writer.close(); } diff --git a/app/PdeCode.java b/app/PdeCode.java index f41b57892..16bedae58 100644 --- a/app/PdeCode.java +++ b/app/PdeCode.java @@ -27,13 +27,14 @@ import java.io.*; public class PdeCode { String name; // pretty name (no extension), not the full file name - String preprocName; // name of .java file after preproc File file; int flavor; String program; boolean modified; - //History history; // later + //PdeHistory history; // TODO add history information + + String preprocName; // name of .java file after preproc public PdeCode(String name, File file, int flavor) { @@ -44,24 +45,9 @@ public class PdeCode { public void load() throws IOException { - program = null; - - if (file.length() != 0) { - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(files[i]))); - StringBuffer buffer = new StringBuffer(); - String line = null; - while ((line = reader.readLine()) != null) { - buffer.append(line); - buffer.append('\n'); - } - reader.close(); - program = buffer.toString(); - - } else { - // empty code file.. no worries, might be getting filled up - program = ""; - } + program = PdeBase.loadFile(file); + //program = null; /* } catch (IOException e) { PdeBase.showWarning("Error loading file", @@ -80,30 +66,6 @@ public class PdeCode { // TODO re-enable history //history.record(s, PdeHistory.SAVE); - //File file = new File(directory, filename); - //try { - //System.out.println("handleSave: results of getText"); - //System.out.print(s); - - ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes()); - InputStreamReader isr = new InputStreamReader(bis); - BufferedReader reader = new BufferedReader(isr); - - FileWriter fw = new FileWriter(file); - PrintWriter writer = new PrintWriter(new BufferedWriter(fw)); - - String line = null; - while ((line = reader.readLine()) != null) { - //System.out.println("w '" + line + "'"); - writer.println(line); - } - writer.flush(); - writer.close(); - - /* - sketchFile = file; - setSketchModified(false); - message("Done saving " + filename + "."); - */ + PdeBase.saveFile(program, file); } } diff --git a/app/PdeEditor.java b/app/PdeEditor.java index f3363c293..589669d78 100644 --- a/app/PdeEditor.java +++ b/app/PdeEditor.java @@ -506,18 +506,7 @@ public class PdeEditor extends JFrame item = newJMenuItem("Export", 'E'); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - editor.message("Exporting code..."); - try { - if (sketch.export()) { - message("Done exporting."); - } else { - // error message will already be visible - } - } catch (Exception e) { - editor.message("Error during export."); - e.printStackTrace(); - } - buttons.clear(); + handleExport(); } }); @@ -920,17 +909,6 @@ public class PdeEditor extends JFrame } - /** - * Quit, but first ask user if it's ok. Also store preferences - * to disk just in case they want to quit. Final exit() happens - * in PdeEditor since it has the callback from PdeEditorStatus. - */ - public void handleQuit() { - // check to see if the person actually wants to quit - doQuit(); - } - - // ................................................................... @@ -1468,7 +1446,32 @@ public class PdeEditor extends JFrame } - public void doQuit() { + /** + * Handles calling the export() function on sketch, and + * queues all the gui status stuff that comes along with it. + */ + public void handleExport() { + editor.message("Exporting code..."); + try { + if (sketch.export()) { + message("Done exporting."); + } else { + // error message will already be visible + } + } catch (Exception e) { + editor.message("Error during export."); + e.printStackTrace(); + } + buttons.clear(); + } + + + /** + * Quit, but first ask user if it's ok. Also store preferences + * to disk just in case they want to quit. Final exit() happens + * in PdeEditor since it has the callback from PdeEditorStatus. + */ + public void handleQuit() { // stop isn't sufficient with external vm & quit // instead use doClose() which will kill the external vm //doStop();