Skip to content

Commit 7ed6e21

Browse files
committed
better J2SInterface/J2SObjectInterface
1 parent 6158707 commit 7ed6e21

File tree

4 files changed

+92
-50
lines changed

4 files changed

+92
-50
lines changed
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
package javajs.api.js;
22

33
/**
4-
* methods in j2s JavaScript accessed in Jmol
4+
* methods in j2s JavaScript accessed in Jmol -- note that there is a different interface in SwingJS
55
*/
66
public interface J2SObjectInterface {
7-
7+
88
Object doAjax(String url, String postOut, Object bytesOrStringOut, Object info);
99

1010
Object doAjax(Object url, String postOut, Object bytesOrStringOut, boolean isBinary);
1111

1212
void applyFunc(Object func, Object data);
1313

14-
Object newGrayScaleImage(Object context, Object image, int width, int height, int[] grayBuffer);
15-
16-
void showInfo(JSAppletObject html5Applet, boolean show);
17-
1814
void clearConsole(JSAppletObject html5Applet);
1915

20-
boolean isBinaryUrl(String filename);
16+
Object getHiddenCanvas(JSAppletObject html5Applet, String string, int w, int h);
2117

22-
void saveImage(JSAppletObject html5Applet, String type, String fileName);
18+
Object loadFileAsynchronously(Object fileLoadThread, JSAppletObject html5Applet, String fileName, Object appData);
19+
20+
Object newGrayScaleImage(Object context, Object image, int width, int height, int[] grayBuffer);
2321

2422
void repaint(JSAppletObject html5Applet, boolean asNewThread);
2523

24+
void resizeApplet(Object html5Applet, int[] dims);
25+
26+
void saveImage(JSAppletObject html5Applet, String type, String fileName);
27+
2628
void setCanvasImage(Object canvas, int width, int height);
2729

28-
Object getHiddenCanvas(JSAppletObject html5Applet, String string, int w, int h);
30+
void showInfo(JSAppletObject html5Applet, boolean show);
2931

30-
Object loadFileAsynchronously(Object fileLoadThread, JSAppletObject html5Applet, String fileName, Object appData);
32+
// also in swingjs.api.js:
33+
34+
boolean isBinaryUrl(String filename);
3135

32-
void resizeApplet(Object html5Applet, int[] dims);
36+
void saveFile(String fileName, Object data, String mimeType, String encoding);
3337

3438
}

sources/net.sf.j2s.java.core/src/swingjs/api/js/J2SInterface.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import swingjs.api.js.JSSwingMenu;
1010

1111

12+
1213
public interface J2SInterface {
1314

1415
void addBinaryFileType(String ext);
@@ -71,6 +72,8 @@ void readyCallback(String appId, String fullId, boolean isReady,
7172

7273
void unsetMouse(DOMNode frameNode);
7374

75+
String fixCachePath(String uri);
76+
7477

7578
}
7679

sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ window.J2S = J2S = (function() {
150150
*/
151151
_appletCssClass : "",
152152
_appletCssText : "",
153-
_fileCache : null, // enabled by J2S.setFileCaching(applet,
154-
// true/false)
153+
_fileCache : {}, // a simple object used only in J2S._loadFileData and J2S.loadFileAsynchronously
154+
// via J2S._checkCache and only for non-js files and only if Info.cacheFiles == true (which it is not in SwingJS)
155+
_javaFileCache : null, // a Hashtable, for JSUtil and /TEMP/
155156
_jarFile : null, // can be set in URL using _JAR=
156157
_j2sPath : null, // can be set in URL using _J2S=
157158
_use : null, // can be set in URL using _USE=
@@ -300,6 +301,8 @@ window.J2S = J2S = (function() {
300301
}
301302

302303
var fixProtocol = function(url) {
304+
if (url.indexOf("file://") >= 0)
305+
url = "http" + url.substring(4);
303306
// force https if page is https
304307
if (J2S._httpProto == "https://" && url.indexOf("http://") == 0)
305308
url = "https" + url.substring(4);
@@ -710,9 +713,30 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) {
710713
}
711714
return fileName;
712715
}
716+
717+
J2S.fixCachePath = function(uri) {
718+
if (uri.startsWith("./"))
719+
uri = "/" + uri;
720+
var n = (uri.startsWith("https:/") || uri.startsWith("file://") ? 7
721+
: uri.startsWith("http:/") || uri.startsWith("file:/") ? 6
722+
: 0);
723+
if (n > 0)
724+
uri = uri.substring(n);
725+
uri = uri.replace("//", "/");
726+
var pt;
727+
while ((pt = uri.indexOf("/././")) >= 0) {
728+
// https://././xxx --> /./xxx
729+
uri = uri.substring(0, pt) + uri.substring(pt + 2);
730+
}
731+
if (uri.startsWith("/"))
732+
uri = uri.substring(1);
733+
if (uri.startsWith("./"))
734+
uri = uri.substring(2);
735+
return uri;
736+
}
713737

714738
J2S._checkCache = function(applet, fileName, fSuccess) {
715-
if (applet._cacheFiles && J2S._fileCache && !fileName.endsWith(".js")) {
739+
if (applet._cacheFiles && !fileName.endsWith(".js")) {
716740
var data = J2S._fileCache[fileName];
717741
if (data) {
718742
System.out.println("using " + (data.length)
@@ -730,8 +754,9 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) {
730754

731755
J2S.getSetJavaFileCache = function(map) {
732756
// called by swingjs.JSUtil
733-
return (map == null ? J2S._javaFileCache
734-
: (J2S._javaFileCache = map));
757+
if (map == null && !J2S._javaFileCache)
758+
J2S._javaFileCache = Clazz.new_("java.util.Hashtable");
759+
return (map == null ? J2S._javaFileCache : (J2S._javaFileCache = map));
735760
}
736761

737762
J2S.getCachedJavaFile = function(key) {
@@ -845,10 +870,15 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) {
845870
var isBinary = info.isBinary;
846871
// swingjs.api.J2SInterface
847872
// use host-server PHP relay if not from this host
848-
if (fileName.indexOf("https://./") == 0)
873+
874+
if (fileName.indexOf("/") == 0)
875+
fileName = "." + fileName;
876+
else if (fileName.indexOf("https://./") == 0)
849877
fileName = fileName.substring(10);
850878
else if (fileName.indexOf("http://./") == 0)
851879
fileName = fileName.substring(9);
880+
else if (fileName.indexOf("file:") >= 0)
881+
fileName = "./" + fileName.substring(5);
852882
isBinary = (isBinary || J2S.isBinaryUrl(fileName));
853883
var isPDB = (fileName.indexOf("pdb.gz") >= 0 && fileName
854884
.indexOf("//www.rcsb.org/pdb/files/") >= 0);
@@ -970,9 +1000,7 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) {
9701000
var fileName0 = fileName;
9711001
fileName = J2S._checkFileName(applet, fileName);
9721002
var fSuccess = function(data) {
973-
J2S
974-
._setData(fileLoadThread, fileName, fileName0, data,
975-
appData)
1003+
J2S._setData(fileLoadThread, fileName, fileName0, data, appData)
9761004
};
9771005
fSuccess = J2S._checkCache(applet, fileName, fSuccess);
9781006
if (fileName.indexOf("|") >= 0)
@@ -1180,6 +1208,10 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) {
11801208
// FileSave interface? return true if successful
11811209

11821210
J2S.saveFile = J2S._saveFile = function(filename, data, mimetype, encoding) {
1211+
var isString = (typeof data == "string");
1212+
if (filename.indexOf(J2S.getGlobal("j2s.tmpdir")) == 0) {
1213+
return J2S.getSetJavaFileCache().put$O$O(J2S.fixCachePath(filename), (isString ? data.getBytes$S("UTF-8") : data));
1214+
}
11831215
if (J2S._localFileSaveFunction
11841216
&& J2S._localFileSaveFunction(filename, data))
11851217
return "OK";
@@ -1193,7 +1225,6 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) {
11931225
| filename
11941226
.indexOf(".jpeg") >= 0 ? "image/jpg"
11951227
: ""));
1196-
var isString = (typeof data == "string");
11971228
data = Clazz.loadClass("javajs.util.Base64").getBase64$BA(
11981229
isString ? data.getBytes$S("UTF-8") : data).toString();
11991230
encoding || (encoding = "base64");
@@ -1406,7 +1437,6 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) {
14061437
J2S.View.__init(obj);
14071438
obj._currentView = null;
14081439
}
1409-
!J2S._fileCache && obj._cacheFiles && (J2S._fileCache = {});
14101440
if (!obj._console)
14111441
obj._console = obj._id + "_infodiv";
14121442
if (obj._console == "none" || obj._console == "NONE")
@@ -2628,6 +2658,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
26282658
console : this._console,
26292659
monitorZIndex : J2S.getZ(this, "monitorZIndex")
26302660
});
2661+
J2S.setGlobal("j2s.tmpdir", "/TEMP/");
26312662
var isFirst = (__execStack.length == 0);
26322663
if (isFirst)
26332664
J2S._addExec([ this, __loadClazz, null, "loadClazz" ]);
@@ -2983,7 +3014,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) {
29833014

29843015
var $tag = $(tag);
29853016
tag = $tag[0];
2986-
if (tag._isDragger)
3017+
if (!tag || tag._isDragger)
29873018
return;
29883019

29893020
var target, fDown, fDrag, fUp;

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3130,33 +3130,6 @@ var getURIField = function(name, def) {
31303130
}
31313131
}
31323132

3133-
var fixAgent = function(agent) {return "" + ((agent = agent.split(";")[0]),
3134-
(agent + (agent.indexOf("(") >= 0 && agent.indexOf(")") < 0 ? ")" : ""))) }
3135-
3136-
var agent = navigator.userA;
3137-
var sysprops = {
3138-
"file.separator" : "/",
3139-
"line.separator" : "\n",
3140-
"java.awt.printerjob" : "swingjs.JSPrinterJob",
3141-
"java.class.path" : "/",
3142-
"java.class.version" : "80",
3143-
"java.home" : "https://.",
3144-
"java.vendor" : "java2script/SwingJS/OpenJDK",
3145-
"java.vendor.url" : "https://github.com/BobHanson/java2script",
3146-
"java.version" : "1.8",
3147-
"java.vm.version" : "1.8",
3148-
"java.specification.version" : "1.8",
3149-
"os.arch" : navigator.userAgent,
3150-
"os.name" : fixAgent(navigator.userAgent).split("(")[0],
3151-
"os.version": fixAgent(navigator.appVersion).replace(fixAgent(navigator.userAgent), ""),
3152-
"path.separator" : ":",
3153-
"user.dir" : "https://.",
3154-
"user.home" : "https://.",
3155-
"user.name" : "user",
3156-
"javax.xml.datatype.DatatypeFactory" : "swingjs.xml.JSJAXBDatatypeFactory",
3157-
"javax.xml.bind.JAXBContextFactory" : "swingjs.xml.JSJAXBContextFactory"
3158-
}
3159-
31603133
Clazz._setDeclared("java.lang.System", java.lang.System = System = {});
31613134
;(function(C$){
31623135

@@ -3281,13 +3254,44 @@ C$.getenv$=function () {
32813254
return env || (env = Clazz.load("java.util.Properties"));
32823255
}
32833256

3257+
3258+
32843259
C$.exit$I=function (status) {
32853260
Clazz.loadClass("java.lang.Runtime").getRuntime$().exit$I(status | 0);
32863261
}
32873262

32883263
C$.gc$=C$.runFinalization$=C$.runFinalizersOnExit$Z=C$.load$S=C$.loadLibrary$S=C$.mapLibraryName$S=
32893264
function (libname) {return null;}
32903265

3266+
var fixAgent = function(agent) {return "" + ((agent = agent.split(";")[0]),
3267+
(agent + (agent.indexOf("(") >= 0 && agent.indexOf(")") < 0 ? ")" : ""))) }
3268+
3269+
var agent = navigator.userA;
3270+
var sysprops = {
3271+
"file.separator" : "/",
3272+
"line.separator" : "\n",
3273+
"java.awt.printerjob" : "swingjs.JSPrinterJob",
3274+
"java.class.path" : "/",
3275+
"java.class.version" : "80",
3276+
"java.home" : "https://.",
3277+
"java.vendor" : "java2script/SwingJS/OpenJDK",
3278+
"java.vendor.url" : "https://github.com/BobHanson/java2script",
3279+
"java.version" : "1.8",
3280+
"java.vm.version" : "1.8",
3281+
"java.specification.version" : "1.8",
3282+
"java.io.tmpdir" : J2S.getGlobal("j2s.tmpdir"),
3283+
"os.arch" : navigator.userAgent,
3284+
"os.name" : fixAgent(navigator.userAgent).split("(")[0],
3285+
"os.version": fixAgent(navigator.appVersion).replace(fixAgent(navigator.userAgent), ""),
3286+
"path.separator" : ":",
3287+
"user.dir" : "https://.",
3288+
"user.home" : "https://.",
3289+
"user.name" : "user",
3290+
"javax.xml.datatype.DatatypeFactory" : "swingjs.xml.JSJAXBDatatypeFactory",
3291+
"javax.xml.bind.JAXBContextFactory" : "swingjs.xml.JSJAXBContextFactory"
3292+
}
3293+
3294+
32913295
})(System);
32923296

32933297
;(function(Con, Sys) {

0 commit comments

Comments
 (0)