diff --git a/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java b/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java index ad1d850cb..0a57bc141 100644 --- a/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java +++ b/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java @@ -268,7 +268,8 @@ public class SketchBuilder extends IncrementalProjectBuilder{ sketchProject.sketch_width = -1; sketchProject.sketch_height = -1; - + sketchProject.renderer = ""; + String scrubbed = Utilities.scrubComments(stream.toString()); String[] matches = Utilities.match(scrubbed, Utilities.SIZE_REGEX); if(matches != null){ @@ -287,7 +288,7 @@ public class SketchBuilder extends IncrementalProjectBuilder{ ProcessingLog.logInfo("Height cannot be negative. Using default height instead."); if(matches.length==4) sketchProject.renderer = matches[3].trim(); - // "Actually matches.length should always be 4..." - ProcSketch.java + // "Actually matches.length should always be 4..." - Processing Sketch.java } catch (NumberFormatException e) { ProcessingLog.logInfo( diff --git a/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchProject.java b/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchProject.java index b643b532b..9927c8457 100644 --- a/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchProject.java +++ b/editor/processing.plugin.core/src/processing/plugin/core/builder/SketchProject.java @@ -70,7 +70,6 @@ public class SketchProject implements IProjectNature { protected int sketch_height = -1; protected String renderer = ""; - protected boolean wasLastBuildSuccessful = false; @@ -482,6 +481,10 @@ public class SketchProject implements IProjectNature { return wasLastBuildSuccessful; } + public String getRenderer(){ + return renderer; + } + /** Return the sketch's height, or the default height if size() has not been specified */ public int getHeight() { return (sketch_height == -1) ? DEFAULT_HEIGHT : sketch_height; diff --git a/editor/processing.plugin.core/src/processing/plugin/core/builder/Utilities.java b/editor/processing.plugin.core/src/processing/plugin/core/builder/Utilities.java index 3703462b2..1c215914b 100644 --- a/editor/processing.plugin.core/src/processing/plugin/core/builder/Utilities.java +++ b/editor/processing.plugin.core/src/processing/plugin/core/builder/Utilities.java @@ -10,11 +10,16 @@ */ package processing.plugin.core.builder; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.Reader; import java.util.ArrayList; import java.util.Enumeration; @@ -580,5 +585,21 @@ public class Utilities { return new String(p); } + static public void copyFile(File sourceFile, File targetFile) throws IOException { + InputStream from = new BufferedInputStream(new FileInputStream(sourceFile)); + OutputStream to = new BufferedOutputStream(new FileOutputStream(targetFile)); + byte[] buffer = new byte[16 * 1024]; + int bytesRead; + while ((bytesRead = from.read(buffer)) != -1) { + to.write(buffer, 0, bytesRead); + } + to.flush(); + from.close(); // ?? + from = null; + to.close(); // ?? + to = null; + + targetFile.setLastModified(sourceFile.lastModified()); + } } diff --git a/editor/processing.plugin.ui/src/processing/plugin/ui/wizards/ExportAsAppletWizard.java b/editor/processing.plugin.ui/src/processing/plugin/ui/wizards/ExportAsAppletWizard.java index 43f0b7d40..c2a9adf61 100644 --- a/editor/processing.plugin.ui/src/processing/plugin/ui/wizards/ExportAsAppletWizard.java +++ b/editor/processing.plugin.ui/src/processing/plugin/ui/wizards/ExportAsAppletWizard.java @@ -14,6 +14,7 @@ package processing.plugin.ui.wizards; //import org.eclipse.core.resources.IProject; //import org.eclipse.core.resources.IResource; import java.io.File; +import java.net.URL; import java.util.ArrayList; import org.eclipse.core.resources.IContainer; @@ -22,6 +23,7 @@ import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.jdt.ui.jarpackager.JarPackageData; @@ -32,6 +34,7 @@ import org.eclipse.ui.IExportWizard; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.PlatformUI; +import processing.plugin.core.ProcessingCore; import processing.plugin.core.ProcessingLog; import processing.plugin.core.builder.SketchProject; import processing.plugin.core.builder.Utilities; @@ -99,18 +102,81 @@ public class ExportAsAppletWizard extends Wizard implements IExportWizard { } /** - * Tries to export the sketch as an applet - * returns whether or not it was successful + * Tries to export the sketch as an applet, returns whether it was successful. + *

+ * This method relies on non-workspace resources, and so invoking it knocks + * things out of sync with the file system. It triggers a workspace refresh + * of the project after it is finished to realign things. */ public boolean exportAsApplet(SketchProject sp) { + if (sp == null) return false; if (!sp.wasLastBuildSuccessful()) return false; if (!sp.getProject().isAccessible()) return false; IFile code = sp.getMainFile(); - if (code == null) return false; + if (code == null) return false; + String codeContents = Utilities.readFile(code); IFolder exportFolder = sp.getAppletFolder(true); // true to nuke the folder contents, if they exist + // Get size and renderer info from the project + int wide = sp.getWidth(); + int high = sp.getHeight(); + String renderer = sp.getRenderer(); + + // Grab the Javadoc-style description from the main code + String description =""; + String[] javadoc = Utilities.match(codeContents, "/\\*{2,}(.*)\\*+/"); + if (javadoc != null){ + StringBuffer dbuffer = new StringBuffer(); + String[] pieces = Utilities.split(javadoc[1], '\n'); + for (String line : pieces){ + // if this line starts with * characters, remove em + String[] m = Utilities.match(line, "^\\s*\\*+(.*)"); + dbuffer.append(m != null ? m[1] : line); + dbuffer.append('\n'); + } + description = dbuffer.toString(); + System.out.println(description); + } + + //Copy the source files to the target, since we like to encourage people to share their code + try{ + for(IResource r : sp.getProject().members()){ + if(!(r instanceof IFile)) continue; + if(r.getName().startsWith(".")) continue; + if("pde".equalsIgnoreCase(r.getFileExtension())){ + r.copy(exportFolder.getFullPath().append(r.getName()), true, null); + System.out.println("Copied the source file " + r.getName() + + " to " + exportFolder.getFullPath().toString()); + } + } + } catch (CoreException e){ + ProcessingLog.logError("Sketch source files could not be included in export of " + + sp.getProject().getName() +". Trying to continue export anyway. ", e); + } + + // Copy the loading gif to the applet + String LOADING_IMAGE = "loading.gif"; + IFile loadingImage = sp.getProject().getFile(LOADING_IMAGE); // user can specify their own loader + try { + loadingImage.copy(exportFolder.getFullPath().append(LOADING_IMAGE), true, null); + } catch (CoreException e) { + // This will happen when the copy fails, which we expect if there is no + // image file. It isn't worth reporting. + File resourceFolder = ProcessingCore.getProcessingCore().getPluginResourceFolder(); + try { + File exportResourcesFolder = new File(resourceFolder, "export"); + File loadingImageCoreResource = new File(exportResourcesFolder, LOADING_IMAGE); + Utilities.copyFile(loadingImageCoreResource, new File(exportFolder.getFullPath().toString(), LOADING_IMAGE)); + } catch (Exception ex) { + // This is not expected, and should be reported, because we are about to bail + ProcessingLog.logError("Could not access the Processing Plug-in Core resources. " + + "Export aborted.", ex); + return false; + } + } + // add the contents of the code folder to the jar IFolder codeFolder = sp.getCodeFolder(); if (codeFolder != null){ @@ -130,7 +196,6 @@ public class ExportAsAppletWizard extends Wizard implements IExportWizard { } } - // Get the compiled source and package it as a jar // Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); // JarPackageData codePackageData = new JarPackageData(); @@ -147,26 +212,13 @@ public class ExportAsAppletWizard extends Wizard implements IExportWizard { // // operation has been canceled. // } - -// int wide = sp.getWidth(); -// int high = sp.getHeight(); -// -// String codeContents = Utilities.readFile(code); -// -// String description =""; -// String[] javadoc = Utilities.match(codeContents, "/\\*{2,}(.*)\\*+/"); -// if (javadoc != null){ -// StringBuffer dbuffer = new StringBuffer(); -// String[] pieces = Utilities.split(javadoc[1], '\n'); -// for (String line : pieces){ -// // if this line starts with * characters, remove em -// String[] m = Utilities.match(line, "^\\s*\\*+(.*)"); -// dbuffer.append(m != null ? m[1] : line); -// dbuffer.append('\n'); -// } -// description = dbuffer.toString(); -// ProcessingLog.logInfo(description); -// } + // java.io has changed things, so force the workspace to refresh or everything will disappear + try { + sp.getProject().refreshLocal(IResource.DEPTH_INFINITE, null); + } catch (CoreException e) { + ProcessingLog.logError("The workspace could not refresh after the export wizard ran. " + + "You may need to manually refresh the workspace to continue.", e); + } //DEBUG ProcessingLog.logError("Could not export " + sp.getProject().getName() + " because the exporter is not finished.", null);