diff --git a/processing/app/PdeBase.java b/processing/app/PdeBase.java index 9ef7cea87..6520e305a 100644 --- a/processing/app/PdeBase.java +++ b/processing/app/PdeBase.java @@ -101,6 +101,8 @@ public class PdeBase implements ActionListener { menu.addActionListener(this); menubar.add(menu); + // beautify, open, print, play save were key commands + // completely un-functional edit menu menu = new Menu("Edit"); menu.add(new MenuItem("Undo")); @@ -117,6 +119,8 @@ public class PdeBase implements ActionListener { menu.add(new MenuItem("Play")); menu.add(new MenuItem("Present")); menu.add(new MenuItem("Stop")); + menu.addSeparator(); + menu.add(new MenuItem("Beautify")); menu.addActionListener(this); menubar.add(menu); @@ -227,7 +231,6 @@ public class PdeBase implements ActionListener { } menu.addSeparator(); - // other available subdirectories String toplevel[] = sketchbookFolder.list(); diff --git a/processing/app/PdeEditor.java b/processing/app/PdeEditor.java index 8c88b0644..a25a268d7 100644 --- a/processing/app/PdeEditor.java +++ b/processing/app/PdeEditor.java @@ -12,9 +12,9 @@ public class PdeEditor extends Panel { static final String DEFAULT_PROGRAM = "// type program here\n"; - static final String NEW_SKETCH_ITEM = "( new sketch )"; - static final String SKETCH_PREFIX_NAME = "sketch-"; - static final String CODE_FILENAME = "sketch.pde"; + //static final String NEW_SKETCH_ITEM = "( new sketch )"; + //static final String SKETCH_PREFIX_NAME = "sketch-"; + //static final String CODE_FILENAME = "sketch.pde"; // otherwise, if the window is resized with the message label // set to blank, it's preferredSize() will be fukered @@ -101,20 +101,37 @@ public class PdeEditor extends Panel { PdeEditorListener listener = new PdeEditorListener(); textarea.addKeyListener(listener); textarea.addFocusListener(listener); - textarea.addKeyListener(new PdeKeyListener(this)); + //textarea.addKeyListener(new PdeKeyListener(this)); } runner = new PdeRunner(this); - // load the last program that was in use Properties skprops = new Properties(); try { skprops.load(getClass().getResource("sketch.properties").openStream()); - String sketch = (String) skprops.get("sketch.name"); - String path = (String) skprops.get("sketch.directory"); - String user = (String) skprops.get("user.name"); + + int windowX = Integer.parseInt(skprops.getProperty("window.x"), -1); + int windowY = Integer.parseInt(skprops.getProperty("window.y"), -1); + int windowW = Integer.parseInt(skprops.getProperty("window.w"), -1); + int windowH = Integer.parseInt(skprops.getProperty("window.h"), -1); + + // if screen size has changed, the window coordinates no longer + // make sense, so don't use them unless they're identical + int screenW = Integer.parseInt(skprops.getProperty("screen.w"), -1); + int screenH = Integer.parseInt(skprops.getProperty("screen.h"), -1); + Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); + + if ((windowX != -1) && + ((screen.width == screenW) || (screen.height == screenH))) { + PdeBase.frame.setBounds(windowX, windowY, windowW, windowH); + } + + String name = skprops.getProperty("sketch.name"); + String path = skprops.getProperty("sketch.directory"); + String user = skprops.getProperty("user.name"); + if (new File(path + File.separator + name + File.separator + name + ".pde").exists()) { userName = user; @@ -125,6 +142,10 @@ public class PdeEditor extends Panel { } } catch (Exception e) { + // even if folder for 'default' user doesn't exist, or + // sketchbook itself is missing, mkdirs() will make it happy + userName = "default"; + // doesn't exist, not available, make my own skNew(); } @@ -243,6 +264,7 @@ public class PdeEditor extends Panel { */ + // local vars prevent sketchName from being set public void skNew() { try { // does all the plumbing to create a new project @@ -272,7 +294,7 @@ public class PdeEditor extends Panel { // now open it up //skOpen(sketchFile, sketchDir); - handleOpen(sketchFile, sketchDir); + handleOpen(sketchName, sketchFile, sketchDir); } catch (IOException e) { // NEED TO DO SOME ERROR REPORTING HERE *** @@ -328,7 +350,7 @@ public class PdeEditor extends Panel { File isketchFile, File isketchDir) { try { FileInputStream input = new FileInputStream(isketchFile); - int length = (int) file.length(); + int length = (int) isketchFile.length(); byte data[] = new byte[length]; int count = 0; @@ -349,6 +371,10 @@ public class PdeEditor extends Panel { //else //textarea.setText(new String(data, app.encoding)); + sketchName = isketchName; + sketchFile = isketchFile; + sketchDir = isketchDir; + //header.setProject(file.getName(), projectDir); header.reset(); @@ -364,7 +390,8 @@ public class PdeEditor extends Panel { public void doSave() { // true if lastfile not set, otherwise false, meaning no prompt - handleSave(lastFile == null); + //handleSave(lastFile == null); + handleSave(sketchName == null); } public void doSaveAs() { @@ -375,16 +402,15 @@ public class PdeEditor extends Panel { message("Saving file..."); String s = textarea.getText(); - String directory = lastDirectory; - String filename = lastFile; + String directory = sketchFile.getPath(); //lastDirectory; + String filename = sketchFile.getName(); //lastFile; -System.out.println("1"); if (promptUser) { FileDialog fd = new FileDialog(new Frame(), "Save PDE program as...", FileDialog.SAVE); - fd.setDirectory(lastDirectory); - fd.setFile(lastFile); + fd.setDirectory(directory); + fd.setFile(filename); fd.show(); directory = fd.getDirectory(); @@ -396,16 +422,16 @@ System.out.println("1"); } } File file = new File(directory, filename); -System.out.println("2"); try { FileWriter writer = new FileWriter(file); writer.write(s); writer.flush(); writer.close(); -System.out.println("3"); - lastDirectory = directory; - lastFile = filename; + + //lastDirectory = directory; + //lastFile = filename; + sketchFile = file; message("Done saving " + filename + "."); } catch (IOException e) { @@ -423,9 +449,8 @@ System.out.println("3"); public void skExport() { - File appletDir = new File(header.projectDir, "applet"); - handleExport(appletDir, header.project, - new File(header.projectDir, "data")); + File appletDir = new File(sketchDir, "applet"); + handleExport(appletDir, sketchName, new File(sketchDir, "data")); } public void doExport() { @@ -434,26 +459,30 @@ System.out.println("3"); FileDialog fd = new FileDialog(new Frame(), "Create applet project named...", FileDialog.SAVE); - fd.setDirectory(lastDirectory); - fd.setFile(lastFile); + + String directory = sketchFile.getPath(); //lastDirectory; + String project = sketchFile.getName(); //lastFile; + + fd.setDirectory(directory); + fd.setFile(project); fd.show(); - String directory = fd.getDirectory(); - String projectName = fd.getFile(); - if (projectName == null) { // user cancelled + directory = fd.getDirectory(); + project = fd.getFile(); + if (project == null) { // user cancelled message(EMPTY); buttons.clear(); return; - } else if (projectName.indexOf(' ') != -1) { // space in filename + + } else if (project.indexOf(' ') != -1) { // space in filename message("Project name cannot have spaces."); buttons.clear(); return; } - - handleExport(new File(directory), projectName, null); + handleExport(new File(directory), project, null); } - protected void handleExport(File appletDir, String projectName, + protected void handleExport(File appletDir, String exportSketchName, File dataDir) { try { String program = textarea.getText(); @@ -465,7 +494,7 @@ System.out.println("3"); appletDir.mkdirs(); // projectName will be updated with actual class name - projectName = engine.writeJava(projectName, false); + exportSketchName = engine.writeJava(exportSketchName, false); if (!engine.compileJava()) { //throw new Exception("error while compiling, couldn't export"); // message() will already have error message in this case @@ -473,7 +502,7 @@ System.out.println("3"); } // copy .java to project dir - String javaName = projectName + ".java"; + String javaName = exportSketchName + ".java"; //copyFile(new File(javaName), new File(projectDir, javaName)); copyFile(new File(javaName), new File(appletDir, javaName)); @@ -505,13 +534,13 @@ System.out.println("3"); ps.println("


"); ps.println(); - ps.print(""); ps.println(""); ps.println(); - ps.println("source code"); + ps.println("source code"); ps.println(); ps.println("
"); @@ -526,7 +555,7 @@ System.out.println("3"); // create new .jar file FileOutputStream zipOutputFile = - new FileOutputStream(new File(appletDir, projectName + ".jar")); + new FileOutputStream(new File(appletDir, exportSketchName + ".jar")); //new FileOutputStream(new File(projectDir, projectName + ".jar")); ZipOutputStream zos = new ZipOutputStream(zipOutputFile); ZipEntry entry; @@ -555,9 +584,11 @@ System.out.println("3"); } // add the project's .class to the jar - entry = new ZipEntry(projectName + ".class"); + // actually, these should grab everything from the build directory + // since there may be some inner classes + entry = new ZipEntry(exportSketchName + ".class"); zos.putNextEntry(entry); - zos.write(grabFile(new File("lib", projectName + ".class"))); + zos.write(grabFile(new File("lib", exportSketchName + ".class"))); zos.closeEntry(); // close up the jar file @@ -613,8 +644,59 @@ System.out.println("3"); public void doQuit() { // write sketch.properties - URL url = getClass().getResource("sketch.properties"); - System.out.println(url); + try { + URL url = getClass().getResource("buttons.gif"); + String urlstr = url.toString(); + urlstr = urlstr.substring(6, urlstr.lastIndexOf("/") + 1) + + "sketch.properties"; + //System.out.println(urlstr); + //System.exit(0); + FileOutputStream output = new FileOutputStream(urlstr); + + //url = new URL(urlstr + "sketch.properties"); + + /* + URL url = getClass().getResource("sketch.properties"); + if (url == null) { + //url = getClass().getResource(getClass().getName() + ".class"); + url = getClass().getResource("buttons.gif"); + String urlstr = url.toString(); + //int lastSlash = urlstr.lastIndexOf("/"); + urlstr = urlstr.substring(0, urlstr.lastIndexOf("/") + 1); + //System.out.println(urlstr); + url = new URL(urlstr + "sketch.properties"); + } + //System.out.println(url); + //System.exit(0); + + URLConnection conn = url.openConnection(); + conn.setDoOutput(true); + OutputStream pstream = conn.getOutputStream(); + */ + + Properties skprops = new Properties(); + + Rectangle window = PdeBase.frame.getBounds(); + Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); + + skprops.put("window.x", String.valueOf(window.x)); + skprops.put("window.y", String.valueOf(window.y)); + skprops.put("window.w", String.valueOf(window.width)); + skprops.put("window.h", String.valueOf(window.height)); + + skprops.put("screen.w", String.valueOf(screen.width)); + skprops.put("screen.h", String.valueOf(screen.height)); + + skprops.put("sketch.name", sketchName); + skprops.put("sketch.directory", sketchDir.getCanonicalPath()); + skprops.put("user.name", userName); + + skprops.save(output, "auto-generated by pde, please don't touch"); + + } catch (IOException e) { + System.err.println("doQuit: error saving properties"); + e.printStackTrace(); + } System.exit(0); } diff --git a/processing/app/PdeEditorHeader.java b/processing/app/PdeEditorHeader.java index b93572c40..232bc1a18 100644 --- a/processing/app/PdeEditorHeader.java +++ b/processing/app/PdeEditorHeader.java @@ -152,7 +152,7 @@ public class PdeEditorHeader extends Panel /* implements ActionListener*/ { g.setColor(backgroundColor); g.fillRect(0, 0, imageW, imageH); - boolean boringUser = user.equals("default"); + boolean boringUser = editor.userName.equals("default"); g.setColor(secondaryColor); g.drawString(SKETCH_TITLER, sketchTitleLeft, baseline); diff --git a/processing/app/PdeKeyListener.java b/processing/app/PdeKeyListener.java deleted file mode 100644 index c6954b401..000000000 --- a/processing/app/PdeKeyListener.java +++ /dev/null @@ -1,31 +0,0 @@ -#ifdef EDITOR - - -import java.awt.*; -import java.awt.event.*; - - -public class PdeKeyListener extends KeyAdapter { - PdeEditor editor; - - public PdeKeyListener(PdeEditor editor) { - this.editor = editor; - } - - public void keyPressed(KeyEvent event) { - switch ((int) event.getKeyChar()) { - case 2: editor.doBeautify(); break; // control b for beautify - case 15: editor.doOpen(); break; // control o for open - case 16: editor.doPrint(); break; // control p for print - case 18: editor.doPlay(); break; // control r for run - case 19: editor.doSave(); break; // control s for save - //case 20: editor.doSnapshot(); break; // control t for snapshot - // escape only works from the runpanel, because that's - // who's getting all the key events while running - //case 27: editor.terminate(); break; // escape to stop - } - } -} - - -#endif