1414//import org.eclipse.core.resources.IProject; 
1515//import org.eclipse.core.resources.IResource; 
1616import  java .io .File ;
17+ import  java .net .URL ;
1718import  java .util .ArrayList ;
1819
1920import  org .eclipse .core .resources .IContainer ;
2223import  org .eclipse .core .resources .IResource ;
2324import  org .eclipse .core .resources .ResourcesPlugin ;
2425import  org .eclipse .core .runtime .CoreException ;
26+ import  org .eclipse .core .runtime .FileLocator ;
2527import  org .eclipse .core .runtime .IPath ;
2628import  org .eclipse .core .runtime .Path ;
2729import  org .eclipse .jdt .ui .jarpackager .JarPackageData ;
3234import  org .eclipse .ui .IWorkbench ;
3335import  org .eclipse .ui .PlatformUI ;
3436
37+ import  processing .plugin .core .ProcessingCore ;
3538import  processing .plugin .core .ProcessingLog ;
3639import  processing .plugin .core .builder .SketchProject ;
3740import  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 */ 
105111public  boolean  exportAsApplet (SketchProject  sp ) {
112+ if  (sp  == null ) return  false ;
106113if  (!sp .wasLastBuildSuccessful ()) return  false ;
107114if  (!sp .getProject ().isAccessible ()) return  false ;
108115
109116IFile  code  = sp .getMainFile ();
110- if  (code  == null ) return  false ;
117+ if  (code  == null ) return  false ;
118+ String  codeContents  = Utilities .readFile (code );
111119
112120IFolder  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 
115181IFolder  codeFolder  = sp .getCodeFolder ();
116182if  (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 
172224ProcessingLog .logError ("Could not export "  + sp .getProject ().getName () + " because the exporter is not finished." , null );
0 commit comments