From 9909b8068be62226faab9817b21ace11e811ed55 Mon Sep 17 00:00:00 2001 From: benfry Date: Fri, 13 Aug 2010 13:56:22 +0000 Subject: [PATCH] class cleanup, breaking out the preprocessor --- .../app/tools/android/AndroidMode.java | 15 +- .../processing/app/tools/android/Build.java | 156 +++--------------- .../tools/android/{Reset.java => Export.java} | 21 +-- .../app/tools/android/Preprocessor.java | 134 +++++++++++++++ 4 files changed, 165 insertions(+), 161 deletions(-) rename android/tool/src/processing/app/tools/android/{Reset.java => Export.java} (64%) create mode 100644 android/tool/src/processing/app/tools/android/Preprocessor.java diff --git a/android/tool/src/processing/app/tools/android/AndroidMode.java b/android/tool/src/processing/app/tools/android/AndroidMode.java index 780bfc636..9c34ae818 100644 --- a/android/tool/src/processing/app/tools/android/AndroidMode.java +++ b/android/tool/src/processing/app/tools/android/AndroidMode.java @@ -96,13 +96,13 @@ public class AndroidMode implements DeviceListener { menu.addSeparator(); - item = new JMenuItem("Sketch Options"); - item.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - //new Permissions(editor); - } - }); - menu.add(item); +// item = new JMenuItem("Sketch Options"); +// item.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// //new Permissions(editor); +// } +// }); +// menu.add(item); item = new JMenuItem("Sketch Permissions"); item.addActionListener(new ActionListener() { @@ -134,6 +134,7 @@ public class AndroidMode implements DeviceListener { item = new JMenuItem("Reset Connections"); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { +// editor.statusNotice("Resetting the Android Debug Bridge server."); AndroidEnvironment.killAdbServer(); } }); diff --git a/android/tool/src/processing/app/tools/android/Build.java b/android/tool/src/processing/app/tools/android/Build.java index 20becfc57..1b0788657 100644 --- a/android/tool/src/processing/app/tools/android/Build.java +++ b/android/tool/src/processing/app/tools/android/Build.java @@ -1,3 +1,24 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2009-10 Ben Fry and Casey Reas + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 + as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + package processing.app.tools.android; import java.io.*; @@ -5,14 +26,9 @@ import java.util.*; import org.apache.tools.ant.*; -import antlr.RecognitionException; -import antlr.TokenStreamException; - import processing.app.*; import processing.app.exec.*; import processing.app.debug.RunnerException; -import processing.app.preproc.PdePreprocessor; -import processing.app.preproc.PreprocessResult; import processing.core.PApplet; @@ -141,7 +157,7 @@ class Build { } className = sketch.preprocess(srcFolder.getAbsolutePath(), //buildPath, manifest.getPackageName(), - new Preproc(sketch.getName())); + new Preprocessor(sketch.getName(), this)); if (className != null) { // final File androidXML = new File(tempBuildFolder, "AndroidManifest.xml"); // writeAndroidManifest(androidXML, sketch.getName(), className); @@ -350,11 +366,6 @@ class Build { } -// protected String getPackageName() { -// return basePackage + "." + editor.getSketch().getName().toLowerCase(); -// } - - protected String getClassName() { return className; } @@ -368,130 +379,7 @@ class Build { return apkFile.getAbsolutePath(); } - - class Preproc extends PdePreprocessor { - - public Preproc(final String sketchName) throws IOException { - super(sketchName); - } - - public PreprocessResult write(Writer out, String program, String codeFolderPackages[]) - throws RunnerException, RecognitionException, TokenStreamException { - if (sizeStatement != null) { - int start = program.indexOf(sizeStatement); - program = program.substring(0, start) + - program.substring(start + sizeStatement.length()); - } -// String[] found = PApplet.match(program, "import\\s+processing.opengl.*\\s*"); -// if (found != null) { -// } - program = program.replaceAll("import\\s+processing\\.opengl\\.\\S+;", ""); -// PApplet.println(program); - return super.write(out, program, codeFolderPackages); - } - @Override - protected int writeImports(final PrintWriter out, - final List programImports, - final List codeFolderImports) { - out.println("package " + getPackageName() + ";"); - out.println(); - // add two lines for the package above - return 2 + super.writeImports(out, programImports, codeFolderImports); - } - - protected void writeFooter(PrintWriter out, String className) { - if (mode == Mode.STATIC) { - // close off draw() definition - out.println("noLoop();"); - out.println(indent + "}"); - } - - if ((mode == Mode.STATIC) || (mode == Mode.ACTIVE)) { - out.println(); - if (sketchWidth != null) { - out.println(indent + "public int sketchWidth() { return " + sketchWidth + "; }"); - } - if (sketchHeight != null) { - out.println(indent + "public int sketchHeight() { return " + sketchHeight + "; }"); - } - if (sketchRenderer != null) { - out.println(indent + "public String sketchRenderer() { return " + sketchRenderer + "; }"); - } - - // close off the class definition - out.println("}"); - } - } - - @Override - public String[] getCoreImports() { - return new String[] { - "processing.core.*", - "processing.xml.*" - }; - } - - @Override - public String[] getDefaultImports() { - final String prefsLine = Preferences.get("android.preproc.imports.list"); - if (prefsLine != null) { - return PApplet.splitTokens(prefsLine, ", "); - } - - // In the future, this may include standard classes for phone or - // accelerometer access within the Android APIs. This is currently living - // in code rather than preferences.txt because Android mode needs to - // maintain its independence from the rest of processing.app. - final String[] androidImports = new String[] { - "android.view.MotionEvent", "android.view.KeyEvent", - "android.graphics.Bitmap", //"java.awt.Image", - "java.io.*", // for BufferedReader, InputStream, etc - //"java.net.*", "java.text.*", // leaving otu for now - "java.util.*" // for ArrayList and friends - //"java.util.zip.*", "java.util.regex.*" // not necessary w/ newer i/o - }; - - Preferences.set("android.preproc.imports.list", - PApplet.join(androidImports, ",")); - - return androidImports; - } - } - - - /* - private void writeAndroidManifest(final File file, final String sketchName, - final String className) { - final PrintWriter writer = PApplet.createWriter(file); - writer.println(""); - writer.println(""); - - writer.println(" "); - - writer.println(" "); - writer.println(" "); - - writer.println(" "); - writer.println(" "); - writer.println(" "); - writer.println(" "); - writer.println(" "); - writer.println(" "); - writer.println(" "); - writer.println(" "); - writer.println(""); - writer.flush(); - writer.close(); - } - */ - - private void writeBuildProps(final File file) { final PrintWriter writer = PApplet.createWriter(file); writer.println("application-package=" + getPackageName()); diff --git a/android/tool/src/processing/app/tools/android/Reset.java b/android/tool/src/processing/app/tools/android/Export.java similarity index 64% rename from android/tool/src/processing/app/tools/android/Reset.java rename to android/tool/src/processing/app/tools/android/Export.java index 0c02ba6bd..25200932f 100644 --- a/android/tool/src/processing/app/tools/android/Reset.java +++ b/android/tool/src/processing/app/tools/android/Export.java @@ -3,7 +3,7 @@ /* Part of the Processing project - http://processing.org - Copyright (c) 2009 Ben Fry and Casey Reas + Copyright (c) 2009-10 Ben Fry and Casey Reas This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 @@ -19,22 +19,3 @@ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -package processing.app.tools.android; - -import processing.app.Editor; -import processing.app.tools.Tool; - -public class Reset implements Tool { - static final String MENU_TITLE = "Reset Android"; - - public String getMenuTitle() { - return MENU_TITLE; - } - - public void init(final Editor editor) { - } - - public void run() { - AndroidEnvironment.killAdbServer(); - } -} \ No newline at end of file diff --git a/android/tool/src/processing/app/tools/android/Preprocessor.java b/android/tool/src/processing/app/tools/android/Preprocessor.java new file mode 100644 index 000000000..1b1b12fb6 --- /dev/null +++ b/android/tool/src/processing/app/tools/android/Preprocessor.java @@ -0,0 +1,134 @@ +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Processing project - http://processing.org + + Copyright (c) 2009-10 Ben Fry and Casey Reas + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License version 2 + as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package processing.app.tools.android; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.util.List; + +import processing.app.Preferences; +import processing.app.debug.RunnerException; +import processing.app.preproc.PdePreprocessor; +import processing.app.preproc.PreprocessResult; +import processing.core.PApplet; +import antlr.RecognitionException; +import antlr.TokenStreamException; + + +public class Preprocessor extends PdePreprocessor { + Build build; + + + public Preprocessor(final String sketchName, final Build build) throws IOException { + super(sketchName); + this.build = build; + } + + + public PreprocessResult write(Writer out, String program, String codeFolderPackages[]) + throws RunnerException, RecognitionException, TokenStreamException { + if (build.sizeStatement != null) { + int start = program.indexOf(build.sizeStatement); + program = program.substring(0, start) + + program.substring(start + build.sizeStatement.length()); + } + // String[] found = PApplet.match(program, "import\\s+processing.opengl.*\\s*"); + // if (found != null) { + // } + program = program.replaceAll("import\\s+processing\\.opengl\\.\\S+;", ""); + // PApplet.println(program); + return super.write(out, program, codeFolderPackages); + } + + + @Override + protected int writeImports(final PrintWriter out, + final List programImports, + final List codeFolderImports) { + out.println("package " + build.getPackageName() + ";"); + out.println(); + // add two lines for the package above + return 2 + super.writeImports(out, programImports, codeFolderImports); + } + + + protected void writeFooter(PrintWriter out, String className) { + if (mode == Mode.STATIC) { + // close off draw() definition + out.println("noLoop();"); + out.println(indent + "}"); + } + + if ((mode == Mode.STATIC) || (mode == Mode.ACTIVE)) { + out.println(); + if (build.sketchWidth != null) { + out.println(indent + "public int sketchWidth() { return " + build.sketchWidth + "; }"); + } + if (build.sketchHeight != null) { + out.println(indent + "public int sketchHeight() { return " + build.sketchHeight + "; }"); + } + if (build.sketchRenderer != null) { + out.println(indent + "public String sketchRenderer() { return " + build.sketchRenderer + "; }"); + } + + // close off the class definition + out.println("}"); + } + } + + + @Override + public String[] getCoreImports() { + return new String[] { + "processing.core.*", + "processing.xml.*" + }; + } + + + @Override + public String[] getDefaultImports() { + final String prefsLine = Preferences.get("android.preproc.imports.list"); + if (prefsLine != null) { + return PApplet.splitTokens(prefsLine, ", "); + } + + // In the future, this may include standard classes for phone or + // accelerometer access within the Android APIs. This is currently living + // in code rather than preferences.txt because Android mode needs to + // maintain its independence from the rest of processing.app. + final String[] androidImports = new String[] { + "android.view.MotionEvent", "android.view.KeyEvent", + "android.graphics.Bitmap", //"java.awt.Image", + "java.io.*", // for BufferedReader, InputStream, etc + //"java.net.*", "java.text.*", // leaving otu for now + "java.util.*" // for ArrayList and friends + //"java.util.zip.*", "java.util.regex.*" // not necessary w/ newer i/o + }; + + Preferences.set("android.preproc.imports.list", + PApplet.join(androidImports, ",")); + + return androidImports; + } +} \ No newline at end of file