Skip to content

Commit 032e00e

Browse files
committed
Cleanups to JavaScript mode. Reference now opens Java reference, import libraries works as in Java mode
1 parent 248733c commit 032e00e

3 files changed

Lines changed: 128 additions & 67 deletions

File tree

app/src/processing/mode/javascript/JavaScriptEditor.java

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,18 @@ public JMenu buildHelpMenu ()
197197

198198
// TODO switch to "http://js.processing.org/"?
199199

200-
item = new JMenuItem("QuickStart for JS Devs");
200+
item = new JMenuItem("QuickStart for Processing Devs");
201201
item.addActionListener(new ActionListener() {
202202
public void actionPerformed(ActionEvent e) {
203-
Base.openURL("http://processingjs.org/reference/articles/jsQuickStart");
203+
Base.openURL("http://processingjs.org/reference/articles/p5QuickStart");
204204
}
205205
});
206206
menu.add(item);
207207

208-
item = new JMenuItem("QuickStart for Processing Devs");
208+
item = new JMenuItem("QuickStart for JavaScript Devs");
209209
item.addActionListener(new ActionListener() {
210210
public void actionPerformed(ActionEvent e) {
211-
Base.openURL("http://processingjs.org/reference/articles/p5QuickStart");
211+
Base.openURL("http://processingjs.org/reference/articles/jsQuickStart");
212212
}
213213
});
214214
menu.add(item);
@@ -246,7 +246,8 @@ public void actionPerformed(ActionEvent e) {
246246
item = Toolkit.newJMenuItemShift("Find in Reference", 'F');
247247
item.addActionListener(new ActionListener() {
248248
public void actionPerformed(ActionEvent e) {
249-
handleFindReferenceImpl();
249+
//handleFindReferenceImpl();
250+
handleFindReference();
250251
}
251252
});
252253
menu.add(item);
@@ -432,22 +433,26 @@ private void handleShowDirectivesEditor ()
432433
public void showReference ( String filename )
433434
{
434435
// TODO: catch handleFindReference directly
435-
handleFindReferenceImpl();
436+
//handleFindReferenceImpl();
437+
438+
File file = new File( jsMode.getDefaultMode().getReferenceFolder(), filename );
439+
// Prepend with file:// and also encode spaces & other characters
440+
Base.openURL( file.toURI().toString() );
436441
}
437442

438443
/**
439444
* Menu item callback, handles showing a reference page.
440445
*/
441-
private void handleFindReferenceImpl ()
446+
/*private void handleFindReferenceImpl ()
442447
{
443-
if (textarea.isSelectionActive()) {
448+
if ( textarea.isSelectionActive() ) {
444449
Base.openURL(
445450
"http://www.google.com/search?q=" +
446451
textarea.getSelectedText().trim() +
447452
"+site%3Ahttp%3A%2F%2Fprocessingjs.org%2Freference"
448453
);
449454
}
450-
}
455+
}*/
451456

452457
/**
453458
* Menu item callback, replacement for RUN:
@@ -604,15 +609,44 @@ public boolean handleSaveAs ()
604609
}
605610

606611
/**
607-
* Menu item callback
612+
* Menu item callback for Sketch -> Import Library -> XXX
613+
*
614+
* Copied from JavaEditor.java
608615
*/
609-
public void handleImportLibrary (String item)
610-
{
611-
Base.showWarning("Processing.js doesn't support libraries",
612-
"Libraries are not supported. Import statements are " +
613-
"ignored, and code relying on them will break.",
614-
null);
615-
}
616+
public void handleImportLibrary ( String jarPath )
617+
{
618+
// Base.showWarning("Processing.js doesn't support libraries",
619+
// "Libraries are not supported. Import statements are " +
620+
// "ignored, and code relying on them will break.",
621+
// null);
622+
623+
// make sure the user didn't hide the sketch folder
624+
sketch.ensureExistence();
625+
626+
// import statements into the main sketch file (code[0])
627+
// if the current code is a .java file, insert into current
628+
if (mode.isDefaultExtension(sketch.getCurrentCode()))
629+
{
630+
sketch.setCurrentCode(0);
631+
}
632+
633+
// could also scan the text in the file to see if each import
634+
// statement is already in there, but if the user has the import
635+
// commented out, then this will be a problem.
636+
String[] list = Base.packageListFromClassPath(jarPath);
637+
StringBuffer buffer = new StringBuffer();
638+
for ( int i = 0; i < list.length; i++ )
639+
{
640+
buffer.append("import ");
641+
buffer.append(list[i]);
642+
buffer.append(".*;\n");
643+
}
644+
buffer.append('\n');
645+
buffer.append(getText());
646+
setText(buffer.toString());
647+
setSelection(0, 0); // scroll to start
648+
sketch.setModified(true);
649+
}
616650

617651
// ----------------------------------------
618652
// implementation BasicServerListener

app/src/processing/mode/javascript/JavaScriptMode.java

Lines changed: 76 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import processing.app.Mode;
1212
import processing.app.Sketch;
1313
import processing.app.SketchException;
14+
import processing.app.Library;
1415
import processing.core.PApplet;
1516

1617
import processing.app.syntax.PdeKeywords;
@@ -19,7 +20,7 @@
1920
import 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
*/
2526
public 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
}

javascript/todo.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@ http://code.google.com/p/processing/issues/detail?id=573
33

44
----------------------------------------------------------------------------
55

6-
Library system
6+
Library system changes, as discussed at EyeO 2012
77

88
Rewrite library system to not be linked to java-libraries.
99
<sketchbook>/libraries-js/..
1010
LibraryManager, updating, ..
1111
Update reference / wiki to mirror changes.
1212
Make tutorial / template for Library authors.
1313

14-
Check "Duplicate import!" message
15-
16-
Sketch > Import Library > ...
17-
Does not work, complains about libs not supported
18-
1914
----------------------------------------------------------------------------
2015

2116
Reference

0 commit comments

Comments
 (0)