From 9f10f5f19964da13e62b9a3ef5a317bc6e0e2719 Mon Sep 17 00:00:00 2001 From: benfry Date: Mon, 19 Jan 2004 19:50:16 +0000 Subject: [PATCH] further separation of PdeEditor and PdeSketch --- processing/app/PdeBase.java | 15 ++- processing/app/PdeEditor.java | 226 +++------------------------------- processing/app/PdeSketch.java | 44 ++++++- 3 files changed, 66 insertions(+), 219 deletions(-) diff --git a/processing/app/PdeBase.java b/processing/app/PdeBase.java index 3da3c8168..06ffdba1f 100644 --- a/processing/app/PdeBase.java +++ b/processing/app/PdeBase.java @@ -422,17 +422,22 @@ public class PdeBase { * Remove all files in a directory and the directory itself. */ static public void removeDir(File dir) { - //System.out.println("removing " + dir); - removeDescendants(dir); - dir.delete(); + if (dir.exists()) { + removeDescendants(dir); + dir.delete(); + } } /** * Recursively remove all files within a directory, - * used with removeDir(). + * used with removeDir(), or when the contents of a dir + * should be removed, but not the directory itself. + * (i.e. when cleaning temp files from lib/build) */ - static protected void removeDescendants(File dir) { + static public void removeDescendants(File dir) { + if (!dir.exists()) return; + String files[] = dir.list(); for (int i = 0; i < files.length; i++) { if (files[i].equals(".") || files[i].equals("..")) continue; diff --git a/processing/app/PdeEditor.java b/processing/app/PdeEditor.java index 9357dab64..5d7d51c05 100644 --- a/processing/app/PdeEditor.java +++ b/processing/app/PdeEditor.java @@ -87,10 +87,10 @@ public class PdeEditor extends JFrame RunButtonWatcher watcher; - PdeRuntime runtime; - boolean externalRuntime; - String externalPaths; - File externalCode; + //PdeRuntime runtime; + //boolean externalRuntime; + //String externalPaths; + //File externalCode; JMenuItem saveMenuItem; JMenuItem saveAsMenuItem; @@ -626,7 +626,7 @@ public class PdeEditor extends JFrame item = new JMenuItem("Add file..."); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - addFile(); + sketch.addFile(); } }); menu.add(item); @@ -701,10 +701,10 @@ public class PdeEditor extends JFrame }); menu.add(item); - item = newMenuItem("Proce55ing.net", '5'); + item = newMenuItem("Visit processing.org", '5'); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - PdeBase.openURL("http://Proce55ing.net/"); + PdeBase.openURL("http://processing.org/"); } }); menu.add(item); @@ -1110,12 +1110,8 @@ public class PdeEditor extends JFrame * X. afterwards, some of these steps need a cleanup function */ public void doRun(boolean present) { - //System.out.println(System.getProperty("java.class.path")); - - //doStop(); doClose(); running = true; - //System.out.println("RUNNING"); buttons.run(); // spew some blank lines so it's clear what's new on the console @@ -1137,121 +1133,9 @@ public class PdeEditor extends JFrame } catch (Exception e) { e.printStackTrace(); } + sketch.cleanup(); } - /* - String program = textarea.getText(); - history.record(program, PdeHistory.RUN); - - // if an external editor is being used, need to grab the - // latest version of the code from the file. - if (PdePreferences.getBoolean("editor.external")) { - // history gets screwed by the open.. - String historySaved = history.lastRecorded; - //handleOpen(sketchName, sketchFile, sketchDir); - //handleOpen(sketch.name, sketch.file, sketch.directory); - handleOpen(sketch); - history.lastRecorded = historySaved; - } - - // temporary build folder is inside 'lib' - // this is added to the classpath by default - tempBuildPath = "lib" + File.separator + "build"; - File buildDir = new File(tempBuildPath); - if (!buildDir.exists()) { - buildDir.mkdirs(); - } - // copy (changed) files from data directory into build folder - sketch.updateDataDirectory(buildDir); - - // make up a temporary class name to suggest - int numero1 = (int) (Math.random() * 10000); - int numero2 = (int) (Math.random() * 10000); - //String className = TEMP_CLASS + "_" + numero1 + "_" + numero2; - String className = "Temporary_" + numero1 + "_" + numero2; - - // handle building the code - className = build(program, className, tempBuildPath, false); - - // if the compilation worked, run the applet - if (className != null) { - - if (externalPaths == null) { - externalPaths = - PdeCompiler.calcClassPath(null) + File.pathSeparator + - tempBuildPath; - } else { - externalPaths = - tempBuildPath + File.pathSeparator + - PdeCompiler.calcClassPath(null) + File.pathSeparator + - externalPaths; - } - - // get a useful folder name for the 'code' folder - // so that it can be included in the java.library.path - String codeFolderPath = ""; - if (externalCode != null) { - codeFolderPath = externalCode.getCanonicalPath(); - } - - // create a runtime object - runtime = new PdeRuntime(this, className, - externalRuntime, - codeFolderPath, externalPaths); - - // if programType is ADVANCED - // or the code/ folder is not empty -> or just exists (simpler) - // then set boolean for external to true - // include path to build in front, then path for code folder - // when passing the classpath through - // actually, build will already be in there, just prepend code - - // use the runtime object to consume the errors now - //messageStream.setMessageConsumer(runtime); - // no need to bother recycling the old guy - PdeMessageStream messageStream = new PdeMessageStream(runtime); - - // start the applet - runtime.start(presenting ? presentLocation : appletLocation, - new PrintStream(messageStream)); - //leechErr); - - // spawn a thread to update PDE GUI state - watcher = new RunButtonWatcher(); - - } else { - // [dmose] throw an exception here? - // [fry] iirc the exception will have already been thrown - cleanTempFiles(); //tempBuildPath); - } - } catch (PdeException e) { - // if it made it as far as creating a Runtime object, - // call its stop method to unwind its thread - if (runtime != null) runtime.stop(); - cleanTempFiles(); //tempBuildPath); - - // printing the stack trace may be overkill since it happens - // even on a simple parse error - //e.printStackTrace(); - - error(e); - - } catch (Exception e) { // something more general happened - e.printStackTrace(); - - // if it made it as far as creating a Runtime object, - // call its stop method to unwind its thread - if (runtime != null) runtime.stop(); - - cleanTempFiles(); //tempBuildPath); - } - */ - - //engine = null; - //System.out.println("out of doRun()"); - // required so that key events go to the panel and works - //graphics.requestFocus(); // removed for pde - public void handleStop() { // called by menu or buttons if (presenting) { @@ -1268,16 +1152,13 @@ public class PdeEditor extends JFrame public void doStop() { if (runtime != null) runtime.stop(); if (watcher != null) watcher.stop(); - //System.out.println("stop2"); message(EMPTY); - // the buttons are still null during the constructor + // the buttons are sometimes still null during the constructor + // is this still true? are people still hitting this error? /*if (buttons != null)*/ buttons.clear(); - //System.out.println("stop4"); running = false; - //System.out.println("NOT RUNNING"); - //System.out.println("stop5"); } @@ -1286,11 +1167,8 @@ public class PdeEditor extends JFrame * mode, this will always be called instead of doStop(). */ public void doClose() { - //System.out.println("doclose1"); if (presenting) { presentationWindow.hide(); - //if ((presentationWindow == null) || - //(!presentationWindow.isVisible())) { } else { try { @@ -1303,26 +1181,20 @@ public class PdeEditor extends JFrame } } catch (NullPointerException e) { } } - //System.out.println("doclose2"); if (running) { - //System.out.println("was running, will call doStop()"); doStop(); } - //System.out.println("doclose3"); try { if (runtime != null) { runtime.close(); // kills the window runtime = null; // will this help? } } catch (Exception e) { } - //System.out.println("doclose4"); //buttons.clear(); // done by doStop - //if (buildPath != null) { - cleanTempFiles(); //buildPath); - //} + sketch.cleanup(); // toxi_030903: focus the PDE again after quitting presentation mode toFront(); @@ -1947,51 +1819,6 @@ public class PdeEditor extends JFrame } - public void addFile() { - // get a dialog, select a file to add to the sketch - String prompt = "Select an image or other data file to copy to your sketch"; - FileDialog fd = new FileDialog(new Frame(), prompt, FileDialog.LOAD); - //if (sketchFile != null) { - //fd.setDirectory(sketchFile.getPath()); - //} - fd.show(); - - String directory = fd.getDirectory(); - String filename = fd.getFile(); - if (filename == null) return; - - // copy the file into the folder - // if people don't want it to copy, they can do it by hand - File sourceFile = new File(directory, filename); - - File destFile = null; - - // if the file appears to be code related, drop it - // into the code folder, instead of the data folder - if (filename.toLowerCase().endsWith(".class") || - filename.toLowerCase().endsWith(".jar") || - filename.toLowerCase().endsWith(".dll") || - filename.toLowerCase().endsWith(".jnilib") || - filename.toLowerCase().endsWith(".so")) { - File codeFolder = new File(sketchDir, "code"); - if (!codeFolder.exists()) codeFolder.mkdirs(); - destFile = new File(codeFolder, filename); - - } else if (filename.toLowerCase().endsWith(".pde") || - filename.toLowerCase().endsWith(".java")) { - destFile = new File(sketchDir, filename); - - } else { - File dataFolder = new File(sketchDir, "data"); - if (!dataFolder.exists()) dataFolder.mkdirs(); - destFile = new File(dataFolder, filename); - } - //System.out.println("copying from " + sourceFile); - //System.out.println("copying to " + destFile); - PdeBase.copyFile(sourceFile, destFile); - } - - // TODO iron out bugs with this code under // different platforms, especially macintosh public void highlightLine(int lnum) { @@ -2046,7 +1873,7 @@ public class PdeEditor extends JFrame // ................................................................... - public void error(PdeException e) { // part of PdeEnvironment + public void error(PdeException e) { if (e.line >= 0) highlightLine(e.line); status.error(e.getMessage()); @@ -2054,14 +1881,14 @@ public class PdeEditor extends JFrame } - public void finished() { // part of PdeEnvironment + public void finished() { running = false; buttons.clearRun(); message("Done."); } - public void message(String msg) { // part of PdeEnvironment + public void message(String msg) { status.notice(msg); } @@ -2074,31 +1901,6 @@ public class PdeEditor extends JFrame // ................................................................... - /** - * Cleanup temporary files - */ - protected void cleanTempFiles() { - if (tempBuildPath == null) return; - - // if the java runtime is holding onto any files in the build dir, we - // won't be able to delete them, so we need to force a gc here - // - System.gc(); - - //File dirObject = new File(buildPath); - File dirObject = new File(tempBuildPath); - - // note that we can't remove the builddir itself, otherwise - // the next time we start up, internal runs using PdeRuntime won't - // work because the build dir won't exist at startup, so the classloader - // will ignore the fact that that dir is in the CLASSPATH in run.sh - // - if (dirObject.exists()) { - PdeBase.removeDescendants(dirObject); - } - } - - /** * Returns the edit popup menu. */ diff --git a/processing/app/PdeSketch.java b/processing/app/PdeSketch.java index b3180d8b8..9b530bac3 100644 --- a/processing/app/PdeSketch.java +++ b/processing/app/PdeSketch.java @@ -23,6 +23,8 @@ */ public class PdeSketch { + static String TEMP_BUILD_PATH = "lib" + File.separator + "build"; + // name of sketch, which is the name of main file // (without .pde or .java extension) String name; @@ -251,11 +253,49 @@ public class PdeSketch { /** + * Cleanup temporary files used during a build/run. + */ + protected void cleanup() { + // if the java runtime is holding onto any files in the build dir, we + // won't be able to delete them, so we need to force a gc here + // + System.gc(); + + // note that we can't remove the builddir itself, otherwise + // the next time we start up, internal runs using PdeRuntime won't + // work because the build dir won't exist at startup, so the classloader + // will ignore the fact that that dir is in the CLASSPATH in run.sh + // + File dirObject = new File(TEMP_BUILD_PATH); + PdeBase.removeDescendants(dirObject); + } + + + /** + * Preprocess, Compile, and Run the current code. * This is not Runnable.run(), but a handler for the run() command. * - * run externally if a code folder exists, - * or if more than one file is in the project + * There are three main parts to this process: * + * (0. if not java, then use another 'engine'.. i.e. python) + * + * 1. do the p5 language preprocessing + * this creates a working .java file in a specific location + * better yet, just takes a chunk of java code and returns a + * new/better string editor can take care of saving this to a + * file location + * + * 2. compile the code from that location + * catching errors along the way + * placing it in a ready classpath, or .. ? + * + * 3. run the code + * needs to communicate location for window + * and maybe setup presentation space as well + * run externally if a code folder exists, + * or if more than one file is in the project + * + * X. afterwards, some of these steps need a cleanup function */ public void run() throws PdeException { current.program = textarea.getText();