1111import processing .app .Mode ;
1212import processing .app .Sketch ;
1313import processing .app .SketchException ;
14+ import processing .app .Library ;
1415import processing .core .PApplet ;
1516
1617import processing .app .syntax .PdeKeywords ;
1920import processing .mode .java .JavaMode ;
2021
2122/**
22- * JS Mode for Processing based on Processing.js. Comes with a server as
23+ * JS Mode for Processing based on Processing.js. Comes with a server as
2324 * replacement for the normal runner.
2425 */
2526public class JavaScriptMode extends Mode
@@ -29,6 +30,7 @@ public class JavaScriptMode extends Mode
2930 public boolean showSizeWarning = true ;
3031
3132 private JavaScriptEditor jsEditor ;
33+ private JavaMode defaultJavaMode ;
3234
3335 /**
3436 * Constructor
@@ -72,6 +74,21 @@ public Editor getEditor ()
7274 return jsEditor ;
7375 }
7476
77+ public JavaMode getDefaultMode ()
78+ {
79+ if ( defaultJavaMode == null ) {
80+ for ( Mode m : base .getModeList () )
81+ {
82+ if ( m .getClass () == JavaMode .class )
83+ {
84+ defaultJavaMode = (JavaMode )m ;
85+ break ;
86+ }
87+ }
88+ }
89+ return defaultJavaMode ;
90+ }
91+
7592 /**
7693 * Loads default Java keywords, JS keywords
7794 * were already loaded in constructor.
@@ -104,7 +121,7 @@ protected void loadAdditionalKeywords ( File keywords, PdeKeywords tokenMarker )
104121 }
105122
106123 /**
107- * Copied from JavaMode
124+ * load the keywords from file, copied from JavaMode.java
108125 */
109126 protected void loadKeywords () throws IOException
110127 {
@@ -144,11 +161,13 @@ public TokenMarker getTokenMarker ()
144161 return tokenMarker ;
145162 }
146163
147- // pretty printable name of the mode
148- public String getTitle ()
149- {
150- return "JavaScript" ;
151- }
164+ /**
165+ * Return pretty title of this mode for menu listing and such
166+ */
167+ public String getTitle ()
168+ {
169+ return "JavaScript" ;
170+ }
152171
153172 // public EditorToolbar createToolbar(Editor editor) { }
154173
@@ -159,7 +178,7 @@ public String getTitle()
159178 // ------------------------------------------------
160179
161180 /**
162- * For now just add JavaMode examples.
181+ * Fetch and return examples from JS and Java mode
163182 */
164183 public File [] getExampleCategoryFolders ()
165184 {
@@ -173,15 +192,7 @@ public boolean accept (File f) {
173192 java .util .Arrays .sort (inclExamples );
174193
175194 // add JavaMode examples as these are supposed to run in JSMode
176- JavaMode jMode = null ;
177- for ( Mode m : base .getModeList () )
178- {
179- if ( m .getClass () == JavaMode .class )
180- {
181- jMode = (JavaMode )m ;
182- break ;
183- }
184- }
195+ JavaMode jMode = getDefaultMode ();
185196 if ( jMode == null )
186197 return inclExamples ; // js examples only
187198
@@ -203,37 +214,58 @@ public boolean accept (File f) {
203214 return finalExamples ;
204215 }
205216
206-
207- public String getDefaultExtension ()
208- {
209- return "pde" ;
210- }
217+ /**
218+ * Return the default extension for this mode, same as Java
219+ */
220+ public String getDefaultExtension ()
221+ {
222+ return "pde" ;
223+ }
211224
212- // all file extensions it supports
213- public String [] getExtensions ()
214- {
215- return new String [] {"pde" , "js" };
216- }
225+ /**
226+ * Return allowed extensions
227+ */
228+ public String [] getExtensions ()
229+ {
230+ return new String [] { "pde" , "js" };
231+ }
217232
218- public String [] getIgnorable ()
219- {
220- return new String [] {
221- "applet" ,
222- "applet_js" ,
223- JavaScriptBuild .EXPORTED_FOLDER_NAME
224- };
225- }
233+ /**
234+ * Return list of file- / folder-names that should be ignored when
235+ * sketch is being copied or saved as
236+ */
237+ public String [] getIgnorable ()
238+ {
239+ return new String [] {
240+ "applet" ,
241+ "applet_js" ,
242+ JavaScriptBuild .EXPORTED_FOLDER_NAME
243+ };
244+ }
245+
246+ /**
247+ * Override Mode.getLibrary to add our own discovery of JS-only libraries.
248+ *
249+ * fjenett 20121202
250+ */
251+ public Library getLibrary ( String pkgName ) throws SketchException
252+ {
253+ return super .getLibrary ( pkgName );
254+ }
226255
227256
228257 // ------------------------------------------------
229258
230- public boolean handleExport (Sketch sketch ) throws IOException , SketchException
231- {
232- JavaScriptBuild build = new JavaScriptBuild (sketch );
233- return build .export ();
234- }
235-
236- //public boolean handleExportApplet(Sketch sketch) throws SketchException, IOException { }
237-
238- //public boolean handleExportApplication(Sketch sketch) throws SketchException, IOException { }
259+ /**
260+ * Build and export a sketch
261+ */
262+ public boolean handleExport (Sketch sketch ) throws IOException , SketchException
263+ {
264+ JavaScriptBuild build = new JavaScriptBuild (sketch );
265+ return build .export ();
266+ }
267+
268+ //public boolean handleExportApplet(Sketch sketch) throws SketchException, IOException { }
269+
270+ //public boolean handleExportApplication(Sketch sketch) throws SketchException, IOException { }
239271}
0 commit comments