Skip to content

Commit 9a089b0

Browse files
committed
File copying and resource refreshing added to export wizard.
1 parent efddeb2 commit 9a089b0

File tree

4 files changed

+104
-27
lines changed

4 files changed

+104
-27
lines changed

editor/processing.plugin.core/src/processing/plugin/core/builder/SketchBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ protected IProject[] fullBuild( SketchProject sketchProject, IProgressMonitor mo
268268

269269
sketchProject.sketch_width = -1;
270270
sketchProject.sketch_height = -1;
271-
271+
sketchProject.renderer = "";
272+
272273
String scrubbed = Utilities.scrubComments(stream.toString());
273274
String[] matches = Utilities.match(scrubbed, Utilities.SIZE_REGEX);
274275
if(matches != null){
@@ -287,7 +288,7 @@ protected IProject[] fullBuild( SketchProject sketchProject, IProgressMonitor mo
287288
ProcessingLog.logInfo("Height cannot be negative. Using default height instead.");
288289

289290
if(matches.length==4) sketchProject.renderer = matches[3].trim();
290-
// "Actually matches.length should always be 4..." - ProcSketch.java
291+
// "Actually matches.length should always be 4..." - Processing Sketch.java
291292

292293
} catch (NumberFormatException e) {
293294
ProcessingLog.logInfo(

editor/processing.plugin.core/src/processing/plugin/core/builder/SketchProject.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public class SketchProject implements IProjectNature {
7070
protected int sketch_height = -1;
7171

7272
protected String renderer = "";
73-
7473

7574
protected boolean wasLastBuildSuccessful = false;
7675

@@ -482,6 +481,10 @@ public boolean wasLastBuildSuccessful(){
482481
return wasLastBuildSuccessful;
483482
}
484483

484+
public String getRenderer(){
485+
return renderer;
486+
}
487+
485488
/** Return the sketch's height, or the default height if size() has not been specified */
486489
public int getHeight() {
487490
return (sketch_height == -1) ? DEFAULT_HEIGHT : sketch_height;

editor/processing.plugin.core/src/processing/plugin/core/builder/Utilities.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
*/
1111
package processing.plugin.core.builder;
1212

13+
import java.io.BufferedInputStream;
14+
import java.io.BufferedOutputStream;
1315
import java.io.BufferedReader;
1416
import java.io.File;
17+
import java.io.FileInputStream;
18+
import java.io.FileOutputStream;
1519
import java.io.IOException;
1620
import java.io.InputStream;
1721
import java.io.InputStreamReader;
22+
import java.io.OutputStream;
1823
import java.io.Reader;
1924
import java.util.ArrayList;
2025
import java.util.Enumeration;
@@ -580,5 +585,21 @@ static public String scrubComments(String what) {
580585
return new String(p);
581586
}
582587

588+
static public void copyFile(File sourceFile, File targetFile) throws IOException {
589+
InputStream from = new BufferedInputStream(new FileInputStream(sourceFile));
590+
OutputStream to = new BufferedOutputStream(new FileOutputStream(targetFile));
591+
byte[] buffer = new byte[16 * 1024];
592+
int bytesRead;
593+
while ((bytesRead = from.read(buffer)) != -1) {
594+
to.write(buffer, 0, bytesRead);
595+
}
596+
to.flush();
597+
from.close(); // ??
598+
from = null;
599+
to.close(); // ??
600+
to = null;
601+
602+
targetFile.setLastModified(sourceFile.lastModified());
603+
}
583604

584605
}

editor/processing.plugin.ui/src/processing/plugin/ui/wizards/ExportAsAppletWizard.java

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//import org.eclipse.core.resources.IProject;
1515
//import org.eclipse.core.resources.IResource;
1616
import java.io.File;
17+
import java.net.URL;
1718
import java.util.ArrayList;
1819

1920
import org.eclipse.core.resources.IContainer;
@@ -22,6 +23,7 @@
2223
import org.eclipse.core.resources.IResource;
2324
import org.eclipse.core.resources.ResourcesPlugin;
2425
import org.eclipse.core.runtime.CoreException;
26+
import org.eclipse.core.runtime.FileLocator;
2527
import org.eclipse.core.runtime.IPath;
2628
import org.eclipse.core.runtime.Path;
2729
import org.eclipse.jdt.ui.jarpackager.JarPackageData;
@@ -32,6 +34,7 @@
3234
import org.eclipse.ui.IWorkbench;
3335
import org.eclipse.ui.PlatformUI;
3436

37+
import processing.plugin.core.ProcessingCore;
3538
import processing.plugin.core.ProcessingLog;
3639
import processing.plugin.core.builder.SketchProject;
3740
import processing.plugin.core.builder.Utilities;
@@ -99,18 +102,81 @@ public boolean performFinish() {
99102
}
100103

101104
/**
102-
* Tries to export the sketch as an applet
103-
* returns whether or not it was successful
105+
* Tries to export the sketch as an applet, returns whether it was successful.
106+
* <p>
107+
* This method relies on non-workspace resources, and so invoking it knocks
108+
* things out of sync with the file system. It triggers a workspace refresh
109+
* of the project after it is finished to realign things.
104110
*/
105111
public boolean exportAsApplet(SketchProject sp) {
112+
if (sp == null) return false;
106113
if (!sp.wasLastBuildSuccessful()) return false;
107114
if (!sp.getProject().isAccessible()) return false;
108115

109116
IFile code = sp.getMainFile();
110-
if (code == null) return false;
117+
if (code == null) return false;
118+
String codeContents = Utilities.readFile(code);
111119

112120
IFolder exportFolder = sp.getAppletFolder(true); // true to nuke the folder contents, if they exist
113121

122+
// Get size and renderer info from the project
123+
int wide = sp.getWidth();
124+
int high = sp.getHeight();
125+
String renderer = sp.getRenderer();
126+
127+
// Grab the Javadoc-style description from the main code
128+
String description ="";
129+
String[] javadoc = Utilities.match(codeContents, "/\\*{2,}(.*)\\*+/");
130+
if (javadoc != null){
131+
StringBuffer dbuffer = new StringBuffer();
132+
String[] pieces = Utilities.split(javadoc[1], '\n');
133+
for (String line : pieces){
134+
// if this line starts with * characters, remove em
135+
String[] m = Utilities.match(line, "^\\s*\\*+(.*)");
136+
dbuffer.append(m != null ? m[1] : line);
137+
dbuffer.append('\n');
138+
}
139+
description = dbuffer.toString();
140+
System.out.println(description);
141+
}
142+
143+
//Copy the source files to the target, since we like to encourage people to share their code
144+
try{
145+
for(IResource r : sp.getProject().members()){
146+
if(!(r instanceof IFile)) continue;
147+
if(r.getName().startsWith(".")) continue;
148+
if("pde".equalsIgnoreCase(r.getFileExtension())){
149+
r.copy(exportFolder.getFullPath().append(r.getName()), true, null);
150+
System.out.println("Copied the source file " + r.getName()
151+
+ " to " + exportFolder.getFullPath().toString());
152+
}
153+
}
154+
} catch (CoreException e){
155+
ProcessingLog.logError("Sketch source files could not be included in export of "
156+
+ sp.getProject().getName() +". Trying to continue export anyway. ", e);
157+
}
158+
159+
// Copy the loading gif to the applet
160+
String LOADING_IMAGE = "loading.gif";
161+
IFile loadingImage = sp.getProject().getFile(LOADING_IMAGE); // user can specify their own loader
162+
try {
163+
loadingImage.copy(exportFolder.getFullPath().append(LOADING_IMAGE), true, null);
164+
} catch (CoreException e) {
165+
// This will happen when the copy fails, which we expect if there is no
166+
// image file. It isn't worth reporting.
167+
File resourceFolder = ProcessingCore.getProcessingCore().getPluginResourceFolder();
168+
try {
169+
File exportResourcesFolder = new File(resourceFolder, "export");
170+
File loadingImageCoreResource = new File(exportResourcesFolder, LOADING_IMAGE);
171+
Utilities.copyFile(loadingImageCoreResource, new File(exportFolder.getFullPath().toString(), LOADING_IMAGE));
172+
} catch (Exception ex) {
173+
// This is not expected, and should be reported, because we are about to bail
174+
ProcessingLog.logError("Could not access the Processing Plug-in Core resources. " +
175+
"Export aborted.", ex);
176+
return false;
177+
}
178+
}
179+
114180
// add the contents of the code folder to the jar
115181
IFolder codeFolder = sp.getCodeFolder();
116182
if (codeFolder != null){
@@ -130,7 +196,6 @@ public boolean exportAsApplet(SketchProject sp) {
130196
}
131197
}
132198

133-
134199
// Get the compiled source and package it as a jar
135200
// Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
136201
// JarPackageData codePackageData = new JarPackageData();
@@ -147,26 +212,13 @@ public boolean exportAsApplet(SketchProject sp) {
147212
// // operation has been canceled.
148213
// }
149214

150-
151-
// int wide = sp.getWidth();
152-
// int high = sp.getHeight();
153-
//
154-
// String codeContents = Utilities.readFile(code);
155-
//
156-
// String description ="";
157-
// String[] javadoc = Utilities.match(codeContents, "/\\*{2,}(.*)\\*+/");
158-
// if (javadoc != null){
159-
// StringBuffer dbuffer = new StringBuffer();
160-
// String[] pieces = Utilities.split(javadoc[1], '\n');
161-
// for (String line : pieces){
162-
// // if this line starts with * characters, remove em
163-
// String[] m = Utilities.match(line, "^\\s*\\*+(.*)");
164-
// dbuffer.append(m != null ? m[1] : line);
165-
// dbuffer.append('\n');
166-
// }
167-
// description = dbuffer.toString();
168-
// ProcessingLog.logInfo(description);
169-
// }
215+
// java.io has changed things, so force the workspace to refresh or everything will disappear
216+
try {
217+
sp.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
218+
} catch (CoreException e) {
219+
ProcessingLog.logError("The workspace could not refresh after the export wizard ran. " +
220+
"You may need to manually refresh the workspace to continue.", e);
221+
}
170222

171223
//DEBUG
172224
ProcessingLog.logError("Could not export " + sp.getProject().getName() + " because the exporter is not finished.", null);

0 commit comments

Comments
 (0)