Skip to content

Commit 42a49b4

Browse files
committed
JFileChooser
1 parent 3a1aa3a commit 42a49b4

8 files changed

Lines changed: 705 additions & 358 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[1222/145803.531:ERROR:process_info.cc(632)] range at 0xb7a339b000000000, size 0x1dd fully unreadable
2+
[1222/145803.531:ERROR:process_info.cc(632)] range at 0xb7a33a1000000000, size 0x1dd fully unreadable
3+
[1222/145803.532:ERROR:process_info.cc(632)] range at 0x0, size 0x1dd fully unreadable

sources/net.sf.j2s.core/test/dev/js/j2sApplet.js

Lines changed: 93 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// j2sApplet.js (based on JmolCore.js)
22

3+
// BH 12/22/2017 1:18:42 PM adds j2sargs for setting arguments
34
// BH 11/19/2017 3:55:04 AM adding support for swingjs2.js; adds static j2sHeadless=true;
45
// BH 10/4/2017 2:25:03 PM adds Clazz.loadClass("javajs.util.Base64")
56
// BH 7/18/2017 10:46:44 AM adds J2S._canClickFileReader, fixing J2S.getFileFromDialog for Chrome and Safari
@@ -24,7 +25,19 @@
2425

2526
if(typeof(jQuery)=="undefined") alert ("Note -- jQuery is required, but it's not defined.")
2627

27-
self.J2S || (J2S = {});
28+
self.J2S || (J2S = {
29+
getURIField: function(name, def) {
30+
try {
31+
var ref = document.location.href.toLowerCase();
32+
var i = ref.indexOf(name + "=");
33+
if (i >= 0)
34+
def = (document.location.href+"&").substring(i + name.length + 1).split("&")[0];
35+
} finally {
36+
return def;
37+
}
38+
}
39+
40+
});
2841

2942
if (!J2S._version)
3043
J2S = (function(document) {
@@ -45,6 +58,7 @@ J2S = (function(document) {
4558
monitorZIndex:z+99999 // way way front
4659
}
4760
};
61+
4862

4963
var j = {
5064

@@ -89,10 +103,8 @@ J2S = (function(document) {
89103
var ref = document.location.href.toLowerCase();
90104
j._debugCode = (ref.indexOf("j2sdebugcode") >= 0);
91105
j._debugCore = (ref.indexOf("j2sdebugcore") >= 0);
92-
var i = ref.indexOf("j2sdebugname=");
93-
j._debugName = (i >= 0 ? (document.location.href+"&").substring(i + 13).split("&")[0] : null);
94-
var i = ref.indexOf("j2slang=");
95-
j._lang = (i >= 0 ? (document.location.href+"&").substring(i + 8).split("&")[0] : null);
106+
j._debugName = J2S.getURIField("j2sdebugname", null);
107+
j._lang = J2S.getURIField("j2slang", null);
96108
j._httpProto = (ref.indexOf("https") == 0 ? "https://" : "http://");
97109
j._isFile = (ref.indexOf("file:") == 0);
98110
if (j._isFile) // ensure no attempt to read XML in local request:
@@ -307,22 +319,29 @@ J2S = (function(document) {
307319

308320

309321
J2S._clearVars = function() {
322+
310323
// only on page closing -- appears to improve garbage collection
311324

312325
delete jQuery;
313326
delete $;
314327
delete J2S;
328+
delete Clazz;
329+
330+
delete java;
331+
delete javajs;
332+
delete org;
333+
delete com;
334+
delete edu;
335+
336+
// these are for Jmol:
337+
315338
delete SwingController;
316339
delete J;
317340
delete JM;
318341
delete JS;
319342
delete JSV;
320343
delete JU;
321344
delete JV;
322-
delete java;
323-
delete javajs;
324-
delete Clazz;
325-
delete c$; // used in p0p; could be gotten rid of
326345
}
327346

328347
////////////// feature detection ///////////////
@@ -786,8 +805,19 @@ J2S._getDefaultLanguage = function(isAll) { return (isAll ? J2S.featureDetection
786805
b[i] = data[i];
787806
return b;
788807
}
789-
790-
J2S._getFileFromDialog = function(fDone, format) {
808+
809+
/**
810+
* fDone: callback function, in the form of fDone(data, fileName).
811+
* Note that this can be a Java Runnable.run(), as a j2sNative call can
812+
* still read the arguments.
813+
*
814+
* format: "ArrayBuffer" for the raw array, "string" for a string,
815+
* "java.util.Map" meaning something with a get$TK(key) method that is looking
816+
* for fileName:string and bytes:byte[], or anything else for byte[] directly.
817+
*
818+
* parentDiv: div id in which to insert this div, or null to use body
819+
*/
820+
J2S._getFileFromDialog = function(fDone, format, parentDiv) {
791821
// streamlined file dialog using <input type="file">.click()
792822
format || (format = "string");
793823
var id = "filereader" + ("" + Math.random()).split(".")[1]
@@ -796,18 +826,30 @@ J2S._getDefaultLanguage = function(isAll) { return (isAll ? J2S.featureDetection
796826
J2S.$remove(id);
797827
var reader = new FileReader();
798828
reader.onloadend = function(evt) {
829+
var data = null;
799830
if (evt.target.readyState == FileReader.DONE) {
800831
var data = evt.target.result;
801-
if (format != "ArrayBuffer") {
832+
switch (format) {
833+
case "java.util.Map":
834+
var map = Clazz.new_(Clazz.load("java.util.Hashtable"));
835+
map.put$TK$TV("fileName", file.name);
836+
map.put$TK$TV("bytes", J2S._toBytes(data));
837+
fDone(map);
838+
return;
839+
case "ArrayBuffer":
840+
break;
841+
case "string":
842+
data = String.instantialize(data);
843+
break;
844+
default:
802845
data = J2S._toBytes(data);
803-
if (format == "string")
804-
data = String.instantialize(data);
846+
break;
805847
}
806848
}
807849
fDone(data, file.name);
808850
};
809851
reader.readAsArrayBuffer(file);
810-
}
852+
};
811853

812854
// x.click() in any manifestation will not work from Chrome or Safari.
813855
// These browers require that the user see and click the link.
@@ -817,8 +859,22 @@ J2S._getDefaultLanguage = function(isAll) { return (isAll ? J2S.featureDetection
817859
x.onchange = function(ev){ readFile(this.files[0]) };
818860
x.click();
819861
} else {
820-
var div = '<div id="ID" style="z-index:1000000;position:absolute;background:#E0E0E0;left:10px;top:10px"><div style="margin:5px 5px 5px 5px;"><input type="file" id="ID_files" /><button id="ID_loadfile">load</button><button id="ID_cancel">cancel</button></div><div>'
821-
J2S.$after("body", div.replace(/ID/g, id));
862+
var div = ('<div id="ID" style="z-index:1000000;position:absolute;background:#E0E0E0;left:400px;top:400px">'
863+
+'<div id="ID_modalscreen" style="z-index:999999;background:rgba(100,100,100,0.4);position:absolute;left:0px;top:0px;width:'+screen.width+';height:'+screen.height+'"></div>'
864+
+'<div style="margin:5px 5px 5px 5px;">'
865+
+'<input type="file" id="ID_files" />'
866+
+'<button id="ID_loadfile">load</button>'
867+
+'<button id="ID_cancel">cancel</button>'
868+
+'</div>'
869+
+'<div>').replace(/ID/g, id);
870+
var parent = (!parentDiv || parentDiv == "body" ? parentDiv
871+
: typeof parentDiv == "string" ? "#" + parentDiv
872+
: parentDiv);
873+
if (parent == "body") {
874+
J2S.$after(body, div);
875+
} else {
876+
J2S.$append(parent, div);
877+
}
822878
J2S.$appEvent("#" + id + "_loadfile", null, "click");
823879
J2S.$appEvent("#" + id + "_loadfile", null, "click", function(evt) { readFile(J2S.$("#" + id + "_files")[0].files[0]); });
824880
J2S.$appEvent("#" + id + "_cancel", null, "click");
@@ -1886,19 +1942,23 @@ J2S.Cache.put = function(filename, data) {
18861942

18871943
proto.__startAppletJS = function(applet) {
18881944
if (J2S._version.indexOf("$Date: ") == 0)
1889-
J2S._version = (J2S._version.substring(7) + " -").split(" -")[0] + " (J2S)"
1945+
J2S._version = (J2S._version.substring(7) + " -").split(" -")[0] + " (J2S)";
18901946
Clazz.load("java.lang.Class");
18911947
J2S._registerApplet(applet._id, applet);
1892-
try {
1893-
if (applet.__Info.main) {
1894-
try{
1895-
var cl = Clazz.load(applet.__Info.main);
1896-
if (cl.j2sHeadless)
1897-
applet.__Info.headless = true;
1898-
}catch(e) {
1899-
alert ("Java class " + applet.__Info.main + " was not found.");
1900-
return;
1901-
}
1948+
if (!applet.__Info.args || applet.__Info.args == "?") {
1949+
var s = J2S.getURIField("j2sargs", null);
1950+
if (s !== null)
1951+
applet.__Info.args = decodeURIComponent(s);
1952+
}
1953+
try {
1954+
var clazz = (applet.__Info.main || applet.__Info.code);
1955+
try {
1956+
var cl = Clazz.load(clazz);
1957+
if (applet.__Info.main && cl.j2sHeadless)
1958+
applet.__Info.headless = true;
1959+
}catch(e) {
1960+
alert ("Java class " + clazz + " was not found.");
1961+
return;
19021962
}
19031963
if (applet.__Info.main && applet.__Info.headless) {
19041964
cl.main(applet.__Info.args || []);
@@ -2149,10 +2209,10 @@ J2S._setDraggable = function(tag, targetOrArray) {
21492209
tag._isDragger = false;
21502210
if (isBind) {
21512211
$tag.bind('mousemoveoutjsmol touchmoveoutjsmol', function(ev) {
2152-
drag(ev);
2212+
drag && drag(ev);
21532213
});
21542214
$tag.bind('mouseupoutjsmol touchendoutjsmol', function(ev) {
2155-
up(ev);
2215+
up && up(ev);
21562216
});
21572217
}
21582218
};
@@ -2230,15 +2290,15 @@ J2S._setDraggable = function(tag, targetOrArray) {
22302290
};
22312291

22322292
$tag.bind('mousedown touchstart', function(ev) {
2233-
return down(ev);
2293+
return down && down(ev);
22342294
});
22352295

22362296
$tag.bind('mousemove touchmove', function(ev) {
2237-
return drag(ev);
2297+
return drag && drag(ev);
22382298
});
22392299

22402300
$tag.bind('mouseup touchend', function(ev) {
2241-
return up(ev);
2301+
return up && up(ev);
22422302
});
22432303

22442304
dragBind(true);

0 commit comments

Comments
 (0)