Cleanups to JavaScript mode. Reference now opens Java reference, import libraries works as in Java mode

This commit is contained in:
fjenett
2012-12-02 17:02:09 +00:00
parent 37b58e8ec5
commit 09f6b1e339
3 changed files with 132 additions and 71 deletions
@@ -197,14 +197,6 @@ public class JavaScriptEditor extends ServingEditor
// TODO switch to "http://js.processing.org/"?
item = new JMenuItem("QuickStart for JS Devs");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.openURL("http://processingjs.org/reference/articles/jsQuickStart");
}
});
menu.add(item);
item = new JMenuItem("QuickStart for Processing Devs");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
@@ -213,6 +205,14 @@ public class JavaScriptEditor extends ServingEditor
});
menu.add(item);
item = new JMenuItem("QuickStart for JavaScript Devs");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.openURL("http://processingjs.org/reference/articles/jsQuickStart");
}
});
menu.add(item);
/* TODO Implement an environment page
item = new JMenuItem("Environment");
item.addActionListener(new ActionListener() {
@@ -246,7 +246,8 @@ public class JavaScriptEditor extends ServingEditor
item = Toolkit.newJMenuItemShift("Find in Reference", 'F');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleFindReferenceImpl();
//handleFindReferenceImpl();
handleFindReference();
}
});
menu.add(item);
@@ -432,22 +433,26 @@ public class JavaScriptEditor extends ServingEditor
public void showReference ( String filename )
{
// TODO: catch handleFindReference directly
handleFindReferenceImpl();
//handleFindReferenceImpl();
File file = new File( jsMode.getDefaultMode().getReferenceFolder(), filename );
// Prepend with file:// and also encode spaces & other characters
Base.openURL( file.toURI().toString() );
}
/**
* Menu item callback, handles showing a reference page.
*/
private void handleFindReferenceImpl ()
/*private void handleFindReferenceImpl ()
{
if (textarea.isSelectionActive()) {
if ( textarea.isSelectionActive() ) {
Base.openURL(
"http://www.google.com/search?q=" +
textarea.getSelectedText().trim() +
"+site%3Ahttp%3A%2F%2Fprocessingjs.org%2Freference"
);
}
}
}*/
/**
* Menu item callback, replacement for RUN:
@@ -604,15 +609,44 @@ public class JavaScriptEditor extends ServingEditor
}
/**
* Menu item callback
* Menu item callback for Sketch -> Import Library -> XXX
*
* Copied from JavaEditor.java
*/
public void handleImportLibrary (String item)
{
Base.showWarning("Processing.js doesn't support libraries",
"Libraries are not supported. Import statements are " +
"ignored, and code relying on them will break.",
null);
}
public void handleImportLibrary ( String jarPath )
{
// Base.showWarning("Processing.js doesn't support libraries",
// "Libraries are not supported. Import statements are " +
// "ignored, and code relying on them will break.",
// null);
// make sure the user didn't hide the sketch folder
sketch.ensureExistence();
// import statements into the main sketch file (code[0])
// if the current code is a .java file, insert into current
if (mode.isDefaultExtension(sketch.getCurrentCode()))
{
sketch.setCurrentCode(0);
}
// could also scan the text in the file to see if each import
// statement is already in there, but if the user has the import
// commented out, then this will be a problem.
String[] list = Base.packageListFromClassPath(jarPath);
StringBuffer buffer = new StringBuffer();
for ( int i = 0; i < list.length; i++ )
{
buffer.append("import ");
buffer.append(list[i]);
buffer.append(".*;\n");
}
buffer.append('\n');
buffer.append(getText());
setText(buffer.toString());
setSelection(0, 0); // scroll to start
sketch.setModified(true);
}
// ----------------------------------------
// implementation BasicServerListener
@@ -11,6 +11,7 @@ import processing.app.EditorState;
import processing.app.Mode;
import processing.app.Sketch;
import processing.app.SketchException;
import processing.app.Library;
import processing.core.PApplet;
import processing.app.syntax.PdeKeywords;
@@ -19,7 +20,7 @@ import processing.app.syntax.TokenMarker;
import processing.mode.java.JavaMode;
/**
* JS Mode for Processing based on Processing.js. Comes with a server as
* JS Mode for Processing based on Processing.js. Comes with a server as
* replacement for the normal runner.
*/
public class JavaScriptMode extends Mode
@@ -29,6 +30,7 @@ public class JavaScriptMode extends Mode
public boolean showSizeWarning = true;
private JavaScriptEditor jsEditor;
private JavaMode defaultJavaMode;
/**
* Constructor
@@ -72,6 +74,21 @@ public class JavaScriptMode extends Mode
return jsEditor;
}
public JavaMode getDefaultMode ()
{
if ( defaultJavaMode == null ) {
for ( Mode m : base.getModeList() )
{
if ( m.getClass() == JavaMode.class )
{
defaultJavaMode = (JavaMode)m;
break;
}
}
}
return defaultJavaMode;
}
/**
* Loads default Java keywords, JS keywords
* were already loaded in constructor.
@@ -104,7 +121,7 @@ public class JavaScriptMode extends Mode
}
/**
* Copied from JavaMode
* load the keywords from file, copied from JavaMode.java
*/
protected void loadKeywords() throws IOException
{
@@ -144,11 +161,13 @@ public class JavaScriptMode extends Mode
return tokenMarker;
}
// pretty printable name of the mode
public String getTitle()
{
return "JavaScript";
}
/**
* Return pretty title of this mode for menu listing and such
*/
public String getTitle()
{
return "JavaScript";
}
// public EditorToolbar createToolbar(Editor editor) { }
@@ -159,7 +178,7 @@ public class JavaScriptMode extends Mode
// ------------------------------------------------
/**
* For now just add JavaMode examples.
* Fetch and return examples from JS and Java mode
*/
public File[] getExampleCategoryFolders()
{
@@ -173,15 +192,7 @@ public class JavaScriptMode extends Mode
java.util.Arrays.sort(inclExamples);
// add JavaMode examples as these are supposed to run in JSMode
JavaMode jMode = null;
for ( Mode m : base.getModeList() )
{
if ( m.getClass() == JavaMode.class )
{
jMode = (JavaMode)m;
break;
}
}
JavaMode jMode = getDefaultMode();
if ( jMode == null )
return inclExamples; // js examples only
@@ -203,37 +214,58 @@ public class JavaScriptMode extends Mode
return finalExamples;
}
public String getDefaultExtension()
{
return "pde";
}
/**
* Return the default extension for this mode, same as Java
*/
public String getDefaultExtension()
{
return "pde";
}
// all file extensions it supports
public String[] getExtensions ()
{
return new String[] {"pde", "js"};
}
/**
* Return allowed extensions
*/
public String[] getExtensions ()
{
return new String[] { "pde", "js" };
}
public String[] getIgnorable ()
{
return new String[] {
"applet",
"applet_js",
JavaScriptBuild.EXPORTED_FOLDER_NAME
};
}
/**
* Return list of file- / folder-names that should be ignored when
* sketch is being copied or saved as
*/
public String[] getIgnorable ()
{
return new String[] {
"applet",
"applet_js",
JavaScriptBuild.EXPORTED_FOLDER_NAME
};
}
/**
* Override Mode.getLibrary to add our own discovery of JS-only libraries.
*
* fjenett 20121202
*/
public Library getLibrary ( String pkgName ) throws SketchException
{
return super.getLibrary( pkgName );
}
// ------------------------------------------------
public boolean handleExport(Sketch sketch) throws IOException, SketchException
{
JavaScriptBuild build = new JavaScriptBuild(sketch);
return build.export();
}
//public boolean handleExportApplet(Sketch sketch) throws SketchException, IOException { }
//public boolean handleExportApplication(Sketch sketch) throws SketchException, IOException { }
/**
* Build and export a sketch
*/
public boolean handleExport(Sketch sketch) throws IOException, SketchException
{
JavaScriptBuild build = new JavaScriptBuild(sketch);
return build.export();
}
//public boolean handleExportApplet(Sketch sketch) throws SketchException, IOException { }
//public boolean handleExportApplication(Sketch sketch) throws SketchException, IOException { }
}
+1 -6
View File
@@ -3,7 +3,7 @@ http://code.google.com/p/processing/issues/detail?id=573
----------------------------------------------------------------------------
Library system
Library system changes, as discussed at EyeO 2012
Rewrite library system to not be linked to java-libraries.
<sketchbook>/libraries-js/..
@@ -11,11 +11,6 @@ LibraryManager, updating, ..
Update reference / wiki to mirror changes.
Make tutorial / template for Library authors.
Check "Duplicate import!" message
Sketch > Import Library > ...
Does not work, complains about libs not supported
----------------------------------------------------------------------------
Reference