From 475b2b880ee68623a638ea8035f780772cdc5267 Mon Sep 17 00:00:00 2001 From: benfry Date: Wed, 3 Jul 2002 09:30:12 +0000 Subject: [PATCH] working on sketchbook in the evening at the ivrea apartment --- app/PdeBase.java | 214 ++++++++++++++++++++++---------------- app/PdeEditor.java | 201 +++++++++++++++++++++++------------ app/PdeEditorButtons.java | 13 +-- app/PdeEditorHeader.java | 8 +- todo.txt | 26 ++++- 5 files changed, 289 insertions(+), 173 deletions(-) diff --git a/app/PdeBase.java b/app/PdeBase.java index 1245d524c..e3667fe87 100644 --- a/app/PdeBase.java +++ b/app/PdeBase.java @@ -8,12 +8,12 @@ import java.awt.event.*; public class PdeBase implements ActionListener { static Properties properties; - boolean errorState; + static Frame frame; + static String encoding; - String encoding; + boolean errorState; PdeEditor editor; - static Frame frame; WindowAdapter windowListener; File sketchbookFolder; @@ -27,7 +27,7 @@ public class PdeBase implements ActionListener { } public PdeBase() { - frame = new Frame(WINDOW_TITLE); + frame = new Frame(WINDOW_TITLE); windowListener = new WindowAdapter() { public void windowClosing(WindowEvent e) { @@ -55,7 +55,7 @@ public class PdeBase implements ActionListener { boolean convertSemicolons = false; String program = get("program"); if (program != null) { - program = readFile(program); + program = getFile(program); } else { program = get("inline_program"); convertSemicolons = true; @@ -78,7 +78,6 @@ public class PdeBase implements ActionListener { frame.setLayout(new BorderLayout()); frame.add("Center", editor); - MenuBar menubar = new MenuBar(); Menu menu; MenuItem item; @@ -96,6 +95,7 @@ public class PdeBase implements ActionListener { menu.add(new MenuItem("Reference")); menu.addSeparator(); menu.add(new MenuItem("Quit")); + menu.addActionListener(this); menubar.add(menu); // completely un-functional edit menu @@ -170,8 +170,11 @@ public class PdeBase implements ActionListener { //editor.handleNew(); //} else { - editor.sketchbookOpen(path + File.separator + e.getActionCommand()); + //editor.sketchbookOpen(path + File.separator + e.getActionCommand()); //} + String name = e.getActionCommand(); + editor.skOpen(path, name); // + File.separator + name + + //File.separator + name + ".pde"); } } @@ -258,12 +261,46 @@ public class PdeBase implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); + //System.out.println(command); - if (command.equals("Quit")) { + if (command.equals("New")) { + editor.skNew(); + + } else if (command.equals("Save")) { + editor.doSave(); + + } else if (command.equals("Duplicate")) { + editor.skDuplicate(); + + } else if (command.equals("Export")) { + editor.skExport(); + + } else if (command.equals("Proce55ing.net")) { + // same thing here with runtime + + } else if (command.equals("Reference")) { + /* + try { + // need to get the stream from here + Runtime.getRuntime().exec("cmd /c d:\\fry\\processing\\app\\application\\reference\\Transform.html"); + } catch (IOException e) { + e.printStackTrace(); + } + */ + + } else if (command.equals("Quit")) { editor.doQuit(); - } else if (command.equals("")) { - + + } else if (command.equals("Play")) { + editor.doPlay(); + + } else if (command.equals("Present")) { + //editor.doPresent(); + + } else if (command.equals("Stop")) { + editor.doStop(); + } //if (command.equals("Save QuickTime movie...")) { // ((PdeEditor)environment).doRecord(); @@ -290,84 +327,6 @@ public class PdeBase implements ActionListener { } */ - // this could be pruned further - public String readFile(String filename) { - if (filename.length() == 0) { - return null; - } - URL url; - InputStream stream = null; - String openMe; - byte temp[] = new byte[65536]; // 64k, 16k was too small - - try { - // if running as an application, get file from disk - stream = new FileInputStream(filename); - - } catch (Exception e1) { try { - url = getClass().getResource(filename); - stream = url.openStream(); - - } catch (Exception e2) { try { - // Try to open the param string as a URL - url = new URL(filename); - stream = url.openStream(); - - } catch (Exception e3) { - return null; - } } } - - try { - int offset = 0; - while (true) { - int byteCount = stream.read(temp, offset, 1024); - if (byteCount <= 0) break; - offset += byteCount; - } - byte program[] = new byte[offset]; - System.arraycopy(temp, 0, program, 0, offset); - - //return languageEncode(program); - // convert the bytes based on the current encoding - try { - if (encoding == null) - return new String(program); - return new String(program, encoding); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - encoding = null; - return new String(program); - } - - } catch (Exception e) { - System.err.println("problem during download"); - e.printStackTrace(); - return null; - } - } - - - // used by PdeEditorButtons - static public Image readImage(String name) { - Image image = null; - //if (isApplet()) { - //image = applet.getImage(applet.getCodeBase(), name); - //} else { - Toolkit tk = Toolkit.getDefaultToolkit(); - image = tk.getImage("lib/" + name); - //URL url = PdeApplet.class.getResource(name); - //image = tk.getImage(url); - //} - //MediaTracker tracker = new MediaTracker(applet); - MediaTracker tracker = new MediaTracker(frame); - tracker.addImage(image, 0); - try { - tracker.waitForAll(); - } catch (InterruptedException e) { } - return image; - } - - // all the information from pde.properties static public String get(String attribute) { @@ -423,6 +382,85 @@ public class PdeBase implements ActionListener { return System.getProperty("os.name").toLowerCase().indexOf("mac") != -1; } + + // used by PdeEditorButtons, but probably more later + static public Image getImage(String name, Component who) { + Image image = null; + //if (isApplet()) { + //image = applet.getImage(applet.getCodeBase(), name); + //} else { + Toolkit tk = Toolkit.getDefaultToolkit(); + image = tk.getImage(who.getClass().getResource(name)); + //image = tk.getImage("lib/" + name); + //URL url = PdeApplet.class.getResource(name); + //image = tk.getImage(url); + //} + //MediaTracker tracker = new MediaTracker(applet); + MediaTracker tracker = new MediaTracker(who); //frame); + tracker.addImage(image, 0); + try { + tracker.waitForAll(); + } catch (InterruptedException e) { } + return image; + } + + + // this could be pruned further + static public String getFile(String filename) { + if (filename.length() == 0) { + return null; + } + URL url; + InputStream stream = null; + String openMe; + byte temp[] = new byte[65536]; // 64k, 16k was too small + + try { + // if running as an application, get file from disk + stream = new FileInputStream(filename); + + } catch (Exception e1) { try { + url = frame.getClass().getResource(filename); + stream = url.openStream(); + + } catch (Exception e2) { try { + // Try to open the param string as a URL + url = new URL(filename); + stream = url.openStream(); + + } catch (Exception e3) { + return null; + } } } + + try { + int offset = 0; + while (true) { + int byteCount = stream.read(temp, offset, 1024); + if (byteCount <= 0) break; + offset += byteCount; + } + byte program[] = new byte[offset]; + System.arraycopy(temp, 0, program, 0, offset); + + //return languageEncode(program); + // convert the bytes based on the current encoding + try { + if (encoding == null) + return new String(program); + return new String(program, encoding); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + encoding = null; + return new String(program); + } + + } catch (Exception e) { + System.err.println("problem during download"); + e.printStackTrace(); + return null; + } + } + /* static public boolean hasFullPrivileges() { //if (applet == null) return true; // application diff --git a/app/PdeEditor.java b/app/PdeEditor.java index fb32972b8..1fc38a813 100644 --- a/app/PdeEditor.java +++ b/app/PdeEditor.java @@ -210,33 +210,39 @@ public class PdeEditor extends Panel { } */ - /* - public void handleNew() { + + public void skNew() { try { // does all the plumbing to create a new project // then calls handleOpen to load it up - File sketchDir = new File("sketchbook", header.user); - int index = 0; - File newguy = null; - while (new File(sketchDir, SKETCH_PREFIX_NAME + pad4(index)).exists()) { - index++; - } - String path = new File(sketchDir, - SKETCH_PREFIX_NAME + - pad4(index)).getCanonicalPath(); + File sketchbookDir = new File("sketchbook", header.user); + File sketchDir = null; + String sketchName = null; +System.out.println("1"); + do { + int index = (int) (Math.random() * 1000); + sketchName = "sketch-" + pad3(index); + sketchDir = new File(sketchbookDir, sketchName); + } while (sketchDir.exists()); +System.out.println("2"); + // mkdir for new project name - File newDir = new File(path); - newDir.mkdirs(); + sketchDir.mkdirs(); + new File(sketchDir, "data").mkdirs(); + new File(sketchDir, "build").mkdirs(); +System.out.println("3"); + + // make empty pde file + File sketchFile = new File(sketchDir, sketchName + ".pde"); + new FileOutputStream(sketchFile); // make 'data' 'applet' dirs inside that // actually, don't, that way can avoid too much extra mess - // make empty pde file - new FileOutputStream(new File(newDir, CODE_FILENAME)); - // now open it up - handleOpen(path); + //skOpen(sketchFile, sketchDir); + handleOpen(sketchFile, sketchDir); } catch (IOException e) { // NEED TO DO SOME ERROR REPORTING HERE *** @@ -244,22 +250,26 @@ public class PdeEditor extends Panel { } } + static String pad3(int what) { + if (what < 10) return "000" + what; + else if (what < 100) return "00" + what; + else return String.valueOf(what); + } + + /* static String pad4(int what) { if (what < 10) return "000" + what; else if (what < 100) return "00" + what; else if (what < 1000) return "0" + what; else return String.valueOf(what); } - - - public void handleOpen(String path) { - System.out.println("gonna open " + path); - } */ - - public void sketchbookOpen(String path) { - System.out.println("PdeEditor.sketchBookOpen: " + path); + public void skOpen(String path, String name) { + //header.isProject = true; + //header.project = name; + handleOpen(new File(path + File.separator + name, name + ".pde"), + new File(path)); } @@ -270,14 +280,22 @@ public class PdeEditor extends Panel { fd.setDirectory(lastDirectory); //fd.setFile(lastFile); fd.show(); - + String directory = fd.getDirectory(); String filename = fd.getFile(); if (filename == null) { buttons.clear(); return; // user cancelled } - File file = new File(directory, filename); + + //header.isProject = false; + //header.project = filename; + handleOpen(new File(directory, filename), null); + } + + + protected void handleOpen(File file, File projectDir) { + //File file = new File(directory, filename); try { FileInputStream input = new FileInputStream(file); @@ -290,8 +308,8 @@ public class PdeEditor extends Panel { } // set the last dir and file, so that they're // the defaults when you try to save again - lastDirectory = directory; - lastFile = filename; + lastDirectory = file.getCanonicalPath(); //directory; + lastFile = file.getName(); //filename; // once read all the bytes, convert it to the proper // local encoding for this system. @@ -302,6 +320,8 @@ public class PdeEditor extends Panel { //else //textarea.setText(new String(data, app.encoding)); + header.setProject(file.getName(), projectDir); + } catch (FileNotFoundException e1) { e1.printStackTrace(); @@ -321,13 +341,14 @@ public class PdeEditor extends Panel { handleSave(true); } - public void handleSave(boolean promptUser) { + protected void handleSave(boolean promptUser) { message("Saving file..."); String s = textarea.getText(); String directory = lastDirectory; String filename = lastFile; +System.out.println("1"); if (promptUser) { FileDialog fd = new FileDialog(new Frame(), "Save PDE program as...", @@ -345,13 +366,14 @@ public class PdeEditor extends Panel { } } 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; message("Done saving " + filename + "."); @@ -365,6 +387,17 @@ public class PdeEditor extends Panel { } + public void skDuplicate() { + System.err.println("sketch duplicate not yet implemented"); + } + + + public void skExport() { + File appletDir = new File(header.projectDir, "applet"); + handleExport(appletDir, header.project, + new File(header.projectDir, "data")); + } + public void doExport() { message("Exporting to applet..."); String s = textarea.getText(); @@ -387,27 +420,39 @@ public class PdeEditor extends Panel { return; } + handleExport(new File(directory), projectName, null); + } + + protected void handleExport(File appletDir, String projectName, + File dataDir) { try { String program = textarea.getText(); // create the project directory KjcEngine engine = new KjcEngine(program, this); - File projectDir = new File(directory, projectName); - projectDir.mkdirs(); + //File projectDir = new File(appletDir, projectName); + //projectDir.mkdirs(); + appletDir.mkdirs(); - engine.writeJava(projectName, false); - if (!engine.compileJava()) return; - // message() should already hava a message in this case + // projectName will be updated with actual class name + projectName = engine.writeJava(projectName, false); + if (!engine.compileJava()) { + //throw new Exception("error while compiling, couldn't export"); + // message() will already have error message in this case + return; + } // copy .java to project dir String javaName = projectName + ".java"; - copyFile(new File(javaName), new File(projectDir, javaName)); + //copyFile(new File(javaName), new File(projectDir, javaName)); + copyFile(new File(javaName), new File(appletDir, javaName)); // remove temporary .java and .class files //engine.cleanup(); - int wide = 320; - int high = 240; + int wide = BApplet.DEFAULT_WIDTH; + int high = BApplet.DEFAULT_HEIGHT; + int index = program.indexOf("size("); if (index != -1) { try { @@ -421,7 +466,8 @@ public class PdeEditor extends Panel { } } - File htmlOutputFile = new File(projectDir, "index.html"); + //File htmlOutputFile = new File(projectDir, "index.html"); + File htmlOutputFile = new File(appletDir, "index.html"); FileOutputStream fos = new FileOutputStream(htmlOutputFile); PrintStream ps = new PrintStream(fos); ps.println(" "); @@ -450,7 +496,8 @@ public class PdeEditor extends Panel { // create new .jar file FileOutputStream zipOutputFile = - new FileOutputStream(new File(projectDir, projectName + ".jar")); + new FileOutputStream(new File(appletDir, projectName + ".jar")); + //new FileOutputStream(new File(projectDir, projectName + ".jar")); ZipOutputStream zos = new ZipOutputStream(zipOutputFile); ZipEntry entry; @@ -463,6 +510,20 @@ public class PdeEditor extends Panel { zos.closeEntry(); } + // files to include + if (dataDir != null) { + String datafiles[] = dataDir.list(); + for (int i = 0; i < datafiles.length; i++) { + if (datafiles[i].equals(".") || datafiles[i].equals("..")) { + continue; + } + entry = new ZipEntry(datafiles[i]); + zos.putNextEntry(entry); + zos.write(grabFile(new File(dataDir, datafiles[i]))); + zos.closeEntry(); + } + } + // add the project's .class to the jar entry = new ZipEntry(projectName + ".class"); zos.putNextEntry(entry); @@ -485,33 +546,6 @@ public class PdeEditor extends Panel { buttons.clear(); } - static protected byte[] grabFile(File file) throws IOException { - int size = (int) file.length(); - FileInputStream input = new FileInputStream(file); - byte buffer[] = new byte[size]; - int offset = 0; - int bytesRead; - while ((bytesRead = input.read(buffer, offset, size-offset)) != -1) { - offset += bytesRead; - if (bytesRead == 0) break; - } - return buffer; - } - - static protected void copyFile(File afile, File bfile) { - try { - FileInputStream from = new FileInputStream(afile); - FileOutputStream to = new FileOutputStream(bfile); - byte[] buffer = new byte[4096]; - int bytesRead; - while ((bytesRead = from.read(buffer)) != -1) { - to.write(buffer, 0, bytesRead); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - public void doPrint() { /* @@ -791,6 +825,37 @@ public class PdeEditor extends Panel { //System.out.println("PdeEditor.messageClear " + msg); status.unnotice(msg); } + + + // utility functions + + + static protected byte[] grabFile(File file) throws IOException { + int size = (int) file.length(); + FileInputStream input = new FileInputStream(file); + byte buffer[] = new byte[size]; + int offset = 0; + int bytesRead; + while ((bytesRead = input.read(buffer, offset, size-offset)) != -1) { + offset += bytesRead; + if (bytesRead == 0) break; + } + return buffer; + } + + static protected void copyFile(File afile, File bfile) { + try { + FileInputStream from = new FileInputStream(afile); + FileOutputStream to = new FileOutputStream(bfile); + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = from.read(buffer)) != -1) { + to.write(buffer, 0, bytesRead); + } + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/app/PdeEditorButtons.java b/app/PdeEditorButtons.java index beecde803..bf9cec5a2 100644 --- a/app/PdeEditorButtons.java +++ b/app/PdeEditorButtons.java @@ -62,12 +62,9 @@ public class PdeEditorButtons extends Panel { int y1[], y2[]; - public PdeEditorButtons(PdeEditor editor /*, boolean useOpenSave, - boolean useCourseware, boolean usePrint, - boolean useBeautify*/) { + public PdeEditorButtons(PdeEditor editor) { this.editor = editor; - // this could be causing trouble - buttons = PdeBase.readImage("buttons.gif"); + buttons = PdeBase.getImage("buttons.gif", this); buttonCount = 0; which = new int[BUTTON_COUNT]; @@ -96,8 +93,6 @@ public class PdeEditorButtons extends Panel { public void update() { - //System.out.println(currentRollover); - //System.out.println("PdeEditorButtons.update()"); paint(this.getGraphics()); } @@ -106,8 +101,6 @@ public class PdeEditorButtons extends Panel { } public void paint(Graphics screen) { - //System.out.println("PdeEditorButtons.paint() " + screen); - if (inactive == null) { inactive = new Image[BUTTON_COUNT]; rollover = new Image[BUTTON_COUNT]; @@ -402,7 +395,7 @@ public class PdeEditorButtons extends Panel { } - public Dimension preferredSize() { + public Dimension getPreferredSize() { return new Dimension(BUTTON_WIDTH, (BUTTON_COUNT + 1)*BUTTON_HEIGHT); } } diff --git a/app/PdeEditorHeader.java b/app/PdeEditorHeader.java index 628f212c8..50d94a3e6 100644 --- a/app/PdeEditorHeader.java +++ b/app/PdeEditorHeader.java @@ -1,6 +1,6 @@ import java.awt.*; import java.awt.event.*; - +import java.io.*; /* PdeEditorLabel sketchLabel = new PdeEditorLabel(1); @@ -30,6 +30,8 @@ public class PdeEditorHeader extends Panel /* implements ActionListener*/ { int projectLeft; int projectRight; int projectTitleLeft; + //boolean isProject; + File projectDir; String user; int userLeft; @@ -61,9 +63,11 @@ public class PdeEditorHeader extends Panel /* implements ActionListener*/ { } - public void setProject(String project) { + public void setProject(String project, File projectDir) { this.project = project; + this.projectDir = projectDir; projectLeft = 0; + update(); } public void setUser(String user) { diff --git a/todo.txt b/todo.txt index 7eb1d657b..e8cf1f39b 100644 --- a/todo.txt +++ b/todo.txt @@ -1,18 +1,34 @@ 0031 +in the morning +_ take state information out of Header +_ cleanup lastFile/lastDirectory +_ renaming projects +_ sketch: sketch-000 is dumb +_ make sure compiling into build directory +_ implement popup menu +_ save (just) last program run in sketch.properties + _ background(), fill(), stroke() using color datatype _ check 0xff high bits to see if it's an int gray value or a color - _ fix curve()/bezier() _ also make curve() work as 6 point with doubled endpoints -_ make all windows 120x120? -_ default program should be large, 300x200 or so -_ what if it's smaller? boundary of color 51, 51, 51 around it +pdebase simple +_ remove 'encoding' crap from PdeBase +_ only existed b/c of reading as bytes and not using jdk11 methods +_ cleanup program/inline_program crap.. take out scheme stuff _ better readFile/loadImage inside PdeBase _ temp array should be resizable inside readFile _ use getresource for loading image -_ beautify is broken + +_ noClear -- implement with noBackground +_ need to set bg color even if updated +_ update/noUpdate() could also be done to force explicit updates only +_ make all windows 120x120? +_ default program should be large, 300x200 or so +_ what if it's smaller? boundary of color 51, 51, 51 around it +_ beautify is slightly broken _ need to update PdeKeyListener for new ui.. _ remove open, add d for duplicate, r for rename, others ?