From d093b286b4fdf0efcbfc110ea74bfd9d6b35bd7b Mon Sep 17 00:00:00 2001 From: benfry Date: Tue, 20 Jan 2004 15:25:14 +0000 Subject: [PATCH] more work on saving sketches, fixing up PdeEditorListener for prefs --- app/PdeCode.java | 65 ++++++++++++---- app/PdeEditor.java | 163 ++++++--------------------------------- app/PdeEditorFind.java | 5 +- app/PdeEditorHeader.java | 2 + app/PdeSketch.java | 34 ++++++++ 5 files changed, 110 insertions(+), 159 deletions(-) diff --git a/app/PdeCode.java b/app/PdeCode.java index 2fa884cea..f41b57892 100644 --- a/app/PdeCode.java +++ b/app/PdeCode.java @@ -43,34 +43,67 @@ public class PdeCode { } - public void load() { + public void load() throws IOException { program = null; - try { - if (files[i].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 = ""; + 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 = ""; + } + + /* } catch (IOException e) { PdeBase.showWarning("Error loading file", "Error while opening the file\n" + file.getPath(), e); program = null; // just in case - } + */ //if (program != null) { //history = new History(file); //} } + + + public void save() throws IOException { + // 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 + "."); + */ + } } diff --git a/app/PdeEditor.java b/app/PdeEditor.java index 04c804d32..e482d3323 100644 --- a/app/PdeEditor.java +++ b/app/PdeEditor.java @@ -144,9 +144,7 @@ public class PdeEditor extends JFrame #endif // set the window icon - try { - //icon = Toolkit.getDefaultToolkit().getImage("lib/icon.gif"); icon = PdeBase.getImage("icon.gif", this); setIconImage(icon); } catch (Exception e) { } // fail silently, no big whup @@ -175,6 +173,9 @@ public class PdeEditor extends JFrame setJMenuBar(menubar); + // doesn't matter when this is created, just make it happen at some point + find = new PdeEditorFind(PdeEditor.this); + Container pain = getContentPane(); pain.setLayout(new BorderLayout()); @@ -430,6 +431,10 @@ public class PdeEditor extends JFrame } + // in case tab expansion stuff has changed + listener.applyPreferences(); + + // in case library option has been enabled or disabled //buildExportMenu(); @@ -528,8 +533,6 @@ public class PdeEditor extends JFrame buttons.clear(); } }); - //exportMenu = buildExportMenu(); - //menu.add(exportMenu); menu.addSeparator(); @@ -567,37 +570,6 @@ public class PdeEditor extends JFrame } - /* - protected JMenu buildExportMenu() { - if (exportMenu == null) { - exportMenu = new JMenu("Export"); - } else { - exportMenu.removeAll(); - } - JMenuItem item; - - item = newJMenuItem("Applet", 'E'); - item.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - skExport(); - } - }); - exportMenu.add(item); - - item = newJMenuItem("Application", 'E', true); - item.setEnabled(false); - exportMenu.add(item); - - if (PdePreferences.getBoolean("export.library")) { - item = new JMenuItem("Library"); - item.setEnabled(false); - exportMenu.add(item); - } - return exportMenu; - } - */ - - protected JMenu buildSketchMenu() { JMenuItem item; JMenu menu = new JMenu("Sketch"); @@ -788,11 +760,7 @@ public class PdeEditor extends JFrame item = newJMenuItem("Find...", 'F'); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - if (find == null) { - find = new PdeEditorFind(PdeEditor.this); - } else { - find.show(); - } + find.show(); } }); menu.add(item); @@ -800,7 +768,9 @@ public class PdeEditor extends JFrame item = newJMenuItem("Find Next", 'G'); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - if (find != null) find.find(); + // TODO find next should only be enabled after a + // search has actually taken place + find.find(); } }); menu.add(item); @@ -840,12 +810,10 @@ public class PdeEditor extends JFrame // This one listens for edits that can be undone. protected class PdeUndoableEditListener implements UndoableEditListener { public void undoableEditHappened(UndoableEditEvent e) { - //Remember the edit and update the menus. + // Remember the edit and update the menus. undo.addEdit(e.getEdit()); undoAction.updateUndoState(); redoAction.updateRedoState(); - //System.out.println("setting sketch to modified"); - //if (!editor.sketchModified) editor.setSketchModified(true); } } @@ -1094,10 +1062,11 @@ public class PdeEditor extends JFrame } - public void setSketchModified(boolean what) { - header.sketchModified = what; + public void setModified(boolean what) { + //header.sketchModified = what; + sketch.setModified(what); header.repaint(); - sketchModified = what; + //sketchModified = what; } @@ -1113,9 +1082,11 @@ public class PdeEditor extends JFrame openingPath = path; openingName = name; - if (sketchModified) { + if (sketch.isModified()) { String prompt = "Save changes to " + sketch.name + "? "; + // if the user selected quit, then this has to be done with + // a JOptionPane instead of internally in the editor if (checking == DO_QUIT) { int result = 0; @@ -1153,7 +1124,8 @@ public class PdeEditor extends JFrame if (result == JOptionPane.YES_OPTION) { //System.out.println("yes"); //System.out.println("saving"); - doSave(); + //doSave(); + sketch.save(); //System.out.println("done saving"); checkModified2(); @@ -1419,7 +1391,7 @@ public class PdeEditor extends JFrame sketchFile = file; setSketchModified(false); message("Done saving " + filename + "."); - + } catch (IOException e) { e.printStackTrace(); //message("Did not write file."); @@ -1510,82 +1482,6 @@ public class PdeEditor extends JFrame } - /* - public void skExport() { - doStop(); - message("Exporting for the web..."); - File appletDir = new File(sketchDir, "applet"); - handleExport(appletDir, sketchName, new File(sketchDir, "data")); - } - */ - - - /* - public void doExport() { - message("Exporting for the web..."); - String s = textarea.getText(); - FileDialog fd = new FileDialog(new Frame(), - "Create applet project named...", - FileDialog.SAVE); - - String directory = sketchFile.getPath(); //lastDirectory; - String project = sketchFile.getName(); //lastFile; - - fd.setDirectory(directory); - fd.setFile(project); - fd.show(); - - directory = fd.getDirectory(); - project = fd.getFile(); - if (project == null) { // user cancelled - message(EMPTY); - buttons.clear(); - return; - - } else if (project.indexOf(' ') != -1) { // space in filename - message("Project name cannot have spaces."); - buttons.clear(); - return; - } - handleExport(new File(directory), project, null); - } - */ - - - public void doPrint() { - /* - Frame frame = new Frame(); // bullocks - int screenWidth = getToolkit().getScreenSize().width; - frame.reshape(screenWidth + 20, 100, screenWidth + 100, 200); - frame.show(); - - Properties props = new Properties(); - PrintJob pj = getToolkit().getPrintJob(frame, "PDE", props); - if (pj != null) { - Graphics g = pj.getGraphics(); - // awful way to do printing, but sometimes brute force is - // just the way. java printing across multiple platforms is - // outrageously inconsistent. - int offsetX = 100; - int offsetY = 100; - int index = 0; - for (int y = 0; y < graphics.height; y++) { - for (int x = 0; x < graphics.width; x++) { - g.setColor(new Color(graphics.pixels[index++])); - g.drawLine(offsetX + x, offsetY + y, - offsetX + x, offsetY + y); - } - } - g.dispose(); - g = null; - pj.end(); - } - frame.dispose(); - buttons.clear(); - */ - } - - public void doQuit() { // stop isn't sufficient with external vm & quit // instead use doClose() which will kill the external vm @@ -1609,21 +1505,6 @@ public class PdeEditor extends JFrame } - /* - public void find() { - if (find == null) { - find = new PdeEditorFind(this); - } else { - find.show(); - } - } - - public void findNext() { - if (find != null) find.find(); - } - */ - - public void doBeautify() { String prog = textarea.getText(); diff --git a/app/PdeEditorFind.java b/app/PdeEditorFind.java index 426377358..22122b3a2 100644 --- a/app/PdeEditorFind.java +++ b/app/PdeEditorFind.java @@ -121,7 +121,7 @@ public class PdeEditorFind extends JFrame implements ActionListener { setBounds((screen.width - wide) / 2, (screen.height - high) / 2, wide, high); - show(); + //show(); // done byte PdeEditor instead } @@ -151,7 +151,8 @@ public class PdeEditorFind extends JFrame implements ActionListener { found = false; String search = findField.getText(); - if (search.length() == 0) return; + // this will catch "find next" being called when no search yet + if (search.length() == 0) return; String text = editor.textarea.getText(); diff --git a/app/PdeEditorHeader.java b/app/PdeEditorHeader.java index 68c4a7276..cfb8c5fa3 100644 --- a/app/PdeEditorHeader.java +++ b/app/PdeEditorHeader.java @@ -148,10 +148,12 @@ public class PdeEditorHeader extends JComponent /*implements MouseListener*/ { } + /* public void reset() { sketchLeft = 0; repaint(); } + */ public void paintComponent(Graphics screen) { diff --git a/app/PdeSketch.java b/app/PdeSketch.java index 6ee15fa81..cb34fce00 100644 --- a/app/PdeSketch.java +++ b/app/PdeSketch.java @@ -203,6 +203,21 @@ public class PdeSketch { } + /** + * Save all code in the current sketch. + */ + public void save() { + for (int i = 0; i < codeCount; i++) { + code[i].save(); + } + } + + + public void saveCurrent() { + current.save(); + } + + /** * Prompt the user for a new file to the sketch. * This could be .class or .jar files for the code folder, @@ -250,6 +265,25 @@ public class PdeSketch { } + /** + * Sets the modified value for the code in the frontmost tab. + */ + public void setCurrentModified(boolean what) { + current.modified = what; + } + + + /** + * Return true if any of the items are modified. + */ + public void isModified() { + for (int i = 0; i < codeCount; i++) { + if (code[i].modified) return true; + } + return false; + } + + /** * Change what file is currently being edited. * 1. store the String for the text of the current file.