From ea4c3eb48f99c4d90af90db121894e7ce8d6f209 Mon Sep 17 00:00:00 2001 From: benfry Date: Tue, 2 Jul 2002 17:24:14 +0000 Subject: [PATCH] sketchbook finally moving --- processing/app/PdeBase.java | 146 +++++------------- processing/app/PdeEditor.java | 226 ++++------------------------ processing/app/PdeEditorHeader.java | 8 +- processing/app/PdeException.java | 9 -- processing/app/PdeKeyListener.java | 2 +- 5 files changed, 71 insertions(+), 320 deletions(-) diff --git a/processing/app/PdeBase.java b/processing/app/PdeBase.java index 7fc9fc90b..1245d524c 100644 --- a/processing/app/PdeBase.java +++ b/processing/app/PdeBase.java @@ -6,73 +6,13 @@ import java.util.*; import java.awt.event.*; -/* - - -[ File ] -New -Open -> -Save -Duplicate -Export -- -Proce55ing.net -Reference -- -Quit - - -[ Open-> (Sketchbook) ] -sketch-001 -sketch-002 -sketch-003 --- -Course -> -Examples -> -Users -> -Proce55ing.net -> --- -Refresh List -Compile All - - -[ Edit ] -Undo -- -Cut -Copy -Paste -- -Select all - - -[ Sketch ] -Play -Present -Stop - - -new sketch just makes a new sketch, doesn't ask for name -tries to do numbered version based on sketches already present - -last section is configurable -choose a name for the entries, and a url for the base of the code - file urls will be local, don't include file:/// to user - http urls are base of directory where the code is found - -*/ - - -public class PdeBase /*extends Component*/ implements ActionListener { - //static PdeApplet applet; +public class PdeBase implements ActionListener { static Properties properties; boolean errorState; String encoding; PdeEditor editor; - // made static so that toFront() can be called by - // full screen code in editor static Frame frame; WindowAdapter windowListener; @@ -91,24 +31,16 @@ public class PdeBase /*extends Component*/ implements ActionListener { windowListener = new WindowAdapter() { public void windowClosing(WindowEvent e) { - System.exit(0); + //System.exit(0); + editor.doQuit(); } }; frame.addWindowListener(windowListener); -#ifdef RECORDER - MenuBar menubar = new MenuBar(); - Menu goodies = new Menu("Processing"); - goodies.add(new MenuItem("Save QuickTime movie...")); - goodies.add(new MenuItem("Quit")); - goodies.addActionListener(this); - menubar.add(goodies); - frame.setMenuBar(menubar); -#endif - properties = new Properties(); try { - properties.load(new FileInputStream("lib/pde.properties")); + //properties.load(new FileInputStream("lib/pde.properties")); + properties.load(getClass().getResource("pde.properties").openStream()); } catch (Exception e) { System.err.println("Error reading pde.properties"); @@ -117,23 +49,8 @@ public class PdeBase /*extends Component*/ implements ActionListener { } int width = getInteger("window.width", 600); int height = getInteger("window.height", 350); - // ms jdk requires that BorderLayout is set explicitly - //frame.add("Center", this); - - //applet = this; - //System.getProperties().list(System.out); - //System.out.println("home = " + System.getProperty("user.home")); - //System.out.println("prefix = " + System.getProperty("sys.prefix")); encoding = get("encoding"); - - //String mode = get("mode", "editor"); - //System.err.println("mode is " + mode); - //if (mode.equals("editor")) { - - //System.err.println("editor not yet complete"); - //System.err.println("editor dammit"); - //System.exit(0); boolean beautify = false; boolean convertSemicolons = false; String program = get("program"); @@ -157,40 +74,28 @@ public class PdeBase /*extends Component*/ implements ActionListener { } } } - //add(hostess = new PdeEditor(this, program)); - //PdeEditor editor = new PdeEditor(this, program); editor = new PdeEditor(/*this,*/ program); - //if (beautify) editor.doBeautify(); - frame.setLayout(new BorderLayout()); - //setLayout(new BorderLayout()); - //add("Center", editor); frame.add("Center", editor); - MenuBar menubar = new MenuBar(); + MenuBar menubar = new MenuBar(); Menu menu; MenuItem item; menu = new Menu("File"); menu.add(new MenuItem("New")); - //menu.add(new MenuItem("Open")); - Menu openMenu = new Menu("Open"); rebuildSketchbookMenu(openMenu); menu.add(openMenu); - menu.add(new MenuItem("Save")); menu.add(new MenuItem("Duplicate")); menu.add(new MenuItem("Export")); menu.addSeparator(); - menu.add(new MenuItem("Proce55ing.net")); menu.add(new MenuItem("Reference")); menu.addSeparator(); - menu.add(new MenuItem("Quit")); - menubar.add(menu); // completely un-functional edit menu @@ -205,7 +110,6 @@ public class PdeBase /*extends Component*/ implements ActionListener { menu.setEnabled(false); menubar.add(menu); - menu = new Menu("Sketch"); menu.add(new MenuItem("Play")); menu.add(new MenuItem("Present")); @@ -213,7 +117,6 @@ public class PdeBase /*extends Component*/ implements ActionListener { menu.addActionListener(this); menubar.add(menu); - frame.setMenuBar(menubar); @@ -284,12 +187,25 @@ public class PdeBase /*extends Component*/ implements ActionListener { sketchbookFolder = new File("sketchbook"); sketchbookPath = sketchbookFolder.getCanonicalPath(); + if (!sketchbookFolder.exists()) { + System.err.println("sketchbook folder doesn't exist, " + + "making a new one"); + sketchbookFolder.mkdirs(); + } // files for the current user (for now, most likely 'default') // header knows what the current user is - String userPath = sketchbookPath + File.separator + header.user; + String userPath = sketchbookPath + + File.separator + editor.header.user; + + File userFolder = new File(userPath); + if (!userFolder.exists()) { + System.err.println("sketchbook folder for '" + editor.header.user + + "' doesn't exist, creating a new one"); + userFolder.mkdirs(); + } SketchbookMenuListener userMenuListener = new SketchbookMenuListener(userPath); @@ -309,27 +225,30 @@ public class PdeBase /*extends Component*/ implements ActionListener { // other available subdirectories String toplevel[] = sketchbookFolder.list(); - for (int i = 0; i < toplevel; i++) { - if ((toplevel[i].equals(header.user)) || + for (int i = 0; i < toplevel.length; i++) { + if ((toplevel[i].equals(editor.header.user)) || (toplevel[i].equals(".")) || (toplevel[i].equals(".."))) continue; Menu subMenu = new Menu(toplevel[i]); File subFolder = new File(sketchbookFolder, toplevel[i]); - String subPath = subfolder.getCanonicalPath(); + String subPath = subFolder.getCanonicalPath(); SketchbookMenuListener subMenuListener = new SketchbookMenuListener(subPath); - String entries[] = subFolder.list(); + entries = subFolder.list(); for (int j = 0; j < entries.length; j++) { if ((entries[j].equals(".")) || (entries[j].equals(".."))) continue; - subMenu.add(entries[j]); + //subMenu.add(entries[j]); + MenuItem item = new MenuItem(entries[j]); + item.addActionListener(subMenuListener); + subMenu.add(item); } menu.add(subMenu); } - menu.addSeparator(); + //menu.addSeparator(); } catch (IOException e) { e.printStackTrace(); @@ -339,6 +258,13 @@ public class PdeBase /*extends Component*/ implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); + + if (command.equals("Quit")) { + editor.doQuit(); + + } else if (command.equals("")) { + + } //if (command.equals("Save QuickTime movie...")) { // ((PdeEditor)environment).doRecord(); //} else if (command.equals("Quit")) { diff --git a/processing/app/PdeEditor.java b/processing/app/PdeEditor.java index 6d5521684..fb32972b8 100644 --- a/processing/app/PdeEditor.java +++ b/processing/app/PdeEditor.java @@ -210,7 +210,7 @@ public class PdeEditor extends Panel { } */ - + /* public void handleNew() { try { // does all the plumbing to create a new project @@ -258,7 +258,8 @@ public class PdeEditor extends Panel { */ - public void sketchbookOpen() { + public void sketchbookOpen(String path) { + System.out.println("PdeEditor.sketchBookOpen: " + path); } @@ -512,168 +513,6 @@ public class PdeEditor extends Panel { } - public void doSnapshot() { - /* - //dbcp.msg("Sending your file to the server..."); - message("Sending your file to the server..."); - - try { - String programStr = textarea.getText(); - //byte imageData[] = dbrp.runners[dbrp.current].dbg.getPixels(); - //byte imageData[] = graphics.getPixels(); - String imageStr = new String(graphics.makeTiffData()); - - URL appletUrl = app.getDocumentBase(); - String document = appletUrl.getFile(); - document = document.substring(0, document.lastIndexOf("?")); - URL url = new URL("http", appletUrl.getHost(), document); - - URLConnection conn = url.openConnection(); - conn.setDoInput(true); - conn.setDoOutput(true); - conn.setUseCaches(false); - conn.setRequestProperty("Content-Type", - "application/x-www-form-urlencoded"); - - DataOutputStream printout = - new DataOutputStream(conn.getOutputStream()); - - String content = - "save_as=" + URLEncoder.encode(PdeBase.get("save_as")) + - "&save_image=" + URLEncoder.encode(imageStr) + - "&save_program=" + URLEncoder.encode(programStr); - - printout.writeBytes(content); - printout.flush(); - printout.close(); - - // what did they say back? - DataInputStream input = - new DataInputStream(conn.getInputStream()); - String str = null; - while ((str = input.readLine()) != null) { - //System.out.println(str); - } - input.close(); - message("Done saving file."); - - } catch (Exception e) { - e.printStackTrace(); - message("Problem: Your work could not be saved."); - } - buttons.clear(); - */ - } - - - static byte tiffHeader[] = { - 77, 77, 0, 42, 0, 0, 0, 8, 0, 9, 0, -2, 0, 4, 0, 0, 0, 1, 0, 0, - 0, 0, 1, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 3, 0, 0, 0, 1, - 0, 0, 0, 0, 1, 2, 0, 3, 0, 0, 0, 3, 0, 0, 0, 122, 1, 6, 0, 3, 0, - 0, 0, 1, 0, 2, 0, 0, 1, 17, 0, 4, 0, 0, 0, 1, 0, 0, 3, 0, 1, 21, - 0, 3, 0, 0, 0, 1, 0, 3, 0, 0, 1, 22, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, - 1, 23, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 8, 0, 8 - }; - - static byte[] makeTiffData(int pixels[], int width, int height) { - byte tiff[] = new byte[768 + width*height*3]; - System.arraycopy(tiffHeader, 0, tiff, 0, tiffHeader.length); - tiff[30] = (byte) ((width >> 8) & 0xff); - tiff[31] = (byte) ((width) & 0xff); - tiff[42] = tiff[102] = (byte) ((height >> 8) & 0xff); - tiff[43] = tiff[103] = (byte) ((height) & 0xff); - int count = width*height*3; - tiff[114] = (byte) ((count >> 24) & 0xff); - tiff[115] = (byte) ((count >> 16) & 0xff); - tiff[116] = (byte) ((count >> 8) & 0xff); - tiff[117] = (byte) ((count) & 0xff); - int index = 768; - for (int i = 0; i < pixels.length; i++) { - tiff[index++] = (byte) ((pixels[i] >> 16) & 0xff); - tiff[index++] = (byte) ((pixels[i] >> 8) & 0xff); - tiff[index++] = (byte) ((pixels[i] >> 0) & 0xff); - } - return tiff; - } - - //public byte[] makeTiffData() { - //return makeTiffData(pixels, width, height); - //} - - public void doSaveTiff() { - /* - message("Saving TIFF image..."); - String s = textarea.getText(); - FileDialog fd = new FileDialog(new Frame(), - "Save image as...", - FileDialog.SAVE); - fd.setDirectory(lastDirectory); - fd.setFile("untitled.tif"); - fd.show(); - - String directory = fd.getDirectory(); - String filename = fd.getFile(); - if (filename == null) return; - - File file = new File(directory, filename); - try { - FileOutputStream fos = new FileOutputStream(file); - byte data[] = graphics.makeTiffData(); - fos.write(data); - fos.flush(); - fos.close(); - - lastDirectory = directory; - message("Done saving image."); - - } catch (IOException e) { - e.printStackTrace(); - message("An error occurred, no image could be written."); - } - */ - } - - - /* - static void writeTiffHeader(OutputStream os, int width, int height) - throws IOException { - byte header[] = new byte[768]; - System.arraycopy(tiffHeader, 0, header, 0, tiffHeader.length); - header[30] = (byte) ((width >> 8) & 0xff); - header[31] = (byte) ((width) & 0xff); - header[42] = header[102] = (byte) ((height >> 8) & 0xff); - header[43] = header[103] = (byte) ((height) & 0xff); - int count = width*height*3; - header[114] = (byte) ((count >> 24) & 0xff); - header[115] = (byte) ((count >> 16) & 0xff); - header[116] = (byte) ((count >> 8) & 0xff); - header[117] = (byte) ((count) & 0xff); - os.write(header); - } - - static void writeTiff(OutputStream os, int width, int height, int pixels[]) { - writeTiffHeader(os, width, height); - for (int i = 0; i < pixels.length; i++) { - write((pixels[i] >> 16) & 0xff); - write((pixels[i] >> 8) & 0xff); - write(pixels[i] & 0xff); - } - os.flush(); - } - */ - - /* - static public byte[] makePgmData(byte inData[], int width, int height) { - String headerStr = "P5 " + width + " " + height + " 255\n"; - byte header[] = headerStr.getBytes(); - int count = width * height; - byte outData[] = new byte[header.length + count]; - System.arraycopy(header, 0, outData, 0, header.length); - System.arraycopy(inData, 0, outData, header.length, count); - return outData; - } - */ - public void doPrint() { /* Frame frame = new Frame(); // bullocks @@ -708,20 +547,24 @@ public class PdeEditor extends Panel { } + public void doQuit() { + System.exit(0); + } + + public void doBeautify() { - /* String prog = textarea.getText(); - if ((prog.charAt(0) == '#') || (prog.charAt(0) == ';')) { - message("Only DBN code can be made beautiful."); - buttons.clear(); - return; - } + //if ((prog.charAt(0) == '#') || (prog.charAt(0) == ';')) { + //message("Only DBN code can be made beautiful."); + //buttons.clear(); + //return; + //} char program[] = prog.toCharArray(); StringBuffer buffer = new StringBuffer(); boolean gotBlankLine = false; int index = 0; int level = 0; - + while (index != program.length) { int begin = index; while ((program[index] != '\n') && @@ -752,22 +595,33 @@ public class PdeEditor extends Panel { gotBlankLine = true; } } else { - if (line.charAt(0) == '}') { + System.out.println(level); + int idx = -1; + String myline = line.substring(0); + while (myline.lastIndexOf('}') != idx) { + idx = myline.indexOf('}'); + myline = myline.substring(idx+1); level--; } - for (int i = 0; i < level*3; i++) { + for (int i = 0; i < level*2; i++) { buffer.append(' '); } buffer.append(line); buffer.append('\n'); - if (line.charAt(0) == '{') { + //if (line.charAt(0) == '{') { + //level++; + //} + idx = -1; + myline = line.substring(0); + while (myline.lastIndexOf('{') != idx) { + idx = myline.indexOf('{'); + myline = myline.substring(idx+1); level++; } gotBlankLine = false; } } textarea.setText(buffer.toString()); - */ buttons.clear(); } @@ -940,27 +794,5 @@ public class PdeEditor extends Panel { } -/* -class OpenMenuListener implements ActionListener { - PdeEditor editor; - String path; - - public OpenMenuListener(PdeEditor editor, String path) { - this.editor = editor; - this.path = path; - } - - public void actionPerformed(ActionEvent e) { - if (e.getActionCommand().equals(NEW_SKETCH_ITEM)) { - editor.handleNew(); - - } else { - editor.handleOpen(path + File.separator + e.getActionCommand()); - } - } -} -*/ - - #endif diff --git a/processing/app/PdeEditorHeader.java b/processing/app/PdeEditorHeader.java index a39d8b278..628f212c8 100644 --- a/processing/app/PdeEditorHeader.java +++ b/processing/app/PdeEditorHeader.java @@ -56,7 +56,7 @@ public class PdeEditorHeader extends Panel /* implements ActionListener*/ { primaryColor = PdeBase.getColor("editor.header.fgcolor.primary", new Color(255, 255, 255)); secondaryColor = PdeBase.getColor("editor.header.fgcolor.secondary", - new Color(153, 153, 153)); + new Color(153, 153, 153)); } } @@ -135,13 +135,15 @@ public class PdeEditorHeader extends Panel /* implements ActionListener*/ { g.setColor(backgroundColor); g.fillRect(0, 0, imageW, imageH); + boolean boringUser = user.equals("default"); + g.setColor(secondaryColor); g.drawString(PROJECT_TITLER, projectTitleLeft, baseline); - g.drawString(USER_TITLER, userTitleLeft, baseline); + if (!boringUser) g.drawString(USER_TITLER, userTitleLeft, baseline); g.setColor(primaryColor); g.drawString(project, projectLeft, baseline); - g.drawString(user, userLeft, baseline); + if (!boringUser) g.drawString(user, userLeft, baseline); //g.setColor(fgColor[mode]); //g.drawString(message, PdeEditor.INSET_SIZE, (sizeH + fontAscent) / 2); diff --git a/processing/app/PdeException.java b/processing/app/PdeException.java index 59fc52725..8c95c2db7 100644 --- a/processing/app/PdeException.java +++ b/processing/app/PdeException.java @@ -22,13 +22,4 @@ public class PdeException extends Exception { return msg; //return (dot == -1) ? msg : msg.substring(dot+1); } - -#ifndef PLAYER -#ifdef DBN - public PdeException(String message, DbnToken token) { - super(message + ", token: " + token.toString()); - } -#endif -#endif } - diff --git a/processing/app/PdeKeyListener.java b/processing/app/PdeKeyListener.java index b9d9fd916..c6954b401 100644 --- a/processing/app/PdeKeyListener.java +++ b/processing/app/PdeKeyListener.java @@ -19,7 +19,7 @@ public class PdeKeyListener extends KeyAdapter { 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 + //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