Skip to content

Commit 4880a16

Browse files
committed
working awt/swing applets
1 parent 64dc46e commit 4880a16

File tree

2 files changed

+417
-31
lines changed

2 files changed

+417
-31
lines changed
Lines changed: 378 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
1+
// SJSApplet.js
2+
3+
// generic SwingJS Applet
4+
5+
// BH 12/18/2016 8:09:56 AM added SwingJS.Loaded and SwingJS.isLoaded
6+
// BH 7/24/2015 9:09:39 AM allows setting Info.resourcePath
7+
// BH 4/28/2015 10:15:32 PM adds getAppletHtml
8+
// BH 4/2/2015 5:17:44 PM adds SwingJS.getJavaResource(path)
9+
10+
// BH 3/27/2015 6:34:49 AM just a shell
11+
12+
if(typeof(jQuery)=="undefined") alert ("Note -- jQuery is required for SwingJS, but it's not defined.")
13+
14+
if (typeof(SwingJS) == "undefined") {
15+
16+
SwingJS = {};
17+
18+
(function (SwingJS, $, J2S) {
19+
20+
SwingJS.getApplet = function(id, Info, checkOnly) {
21+
return SwingJS._Applet._get(id, Info, checkOnly);
22+
}
23+
24+
// optional Info here
25+
SwingJS.getAppletHtml = function(applet, Info) {
26+
if (Info) {
27+
var d = SwingJS._document;
28+
SwingJS._document = null;
29+
applet = SwingJS.getApplet(applet, Info);
30+
SwingJS._document = d;
31+
}
32+
return applet._code;
33+
}
34+
35+
SwingJS._Applet = function(id, Info, checkOnly){
36+
window[id] = this;
37+
this._appletType = "SwingJS._Applet" + (Info.isSigned ? " (signed)" : "");
38+
this._isJava = true;
39+
this._availableParams = null; // all allowed
40+
if (checkOnly)
41+
return this;
42+
this._isSigned = Info.isSigned;
43+
this._readyFunction = Info.readyFunction;
44+
this._ready = false;
45+
this._isJava = true;
46+
this._isInfoVisible = false;
47+
this._applet = null;
48+
this._memoryLimit = Info.memoryLimit || 512;
49+
this._canScript = function(script) {return true;};
50+
this._savedOrientations = [];
51+
this._initialize = function(jarPath, jarFile) {
52+
var doReport = false;
53+
SwingJS._jarFile && (jarFile = SwingJS._jarFile);
54+
if(this._jarFile) {
55+
var f = this._jarFile;
56+
if(f.indexOf("/") >= 0) {
57+
alert ("This web page URL is requesting that the applet used be " + f + ". This is a possible security risk, particularly if the applet is signed, because signed applets can read and write files on your local machine or network.");
58+
var ok = prompt("Do you want to use applet " + f + "? ", "yes or no")
59+
if(ok == "yes") {
60+
jarPath = f.substring(0, f.lastIndexOf("/"));
61+
jarFile = f.substring(f.lastIndexOf("/") + 1);
62+
} else {
63+
doReport = true;
64+
}
65+
} else {
66+
jarFile = f;
67+
}
68+
this_isSigned = Info.isSigned = (jarFile.indexOf("Signed") >= 0);
69+
}
70+
this._jarPath = Info.jarPath = jarPath || ".";
71+
this._jarFile = Info.jarFile = jarFile;
72+
if (doReport)
73+
alert ("The web page URL was ignored. Continuing using " + this._jarFile + ' in directory "' + this._jarPath + '"');
74+
// could do something like this: J2S.controls == undefined || J2S.controls._onloadResetForms();
75+
}
76+
this._create(id, Info);
77+
return this;
78+
}
79+
80+
;(function(Applet, proto) {
81+
82+
Applet._get = function(id, Info, checkOnly) {
83+
84+
checkOnly || (checkOnly = false);
85+
Info || (Info = {});
86+
var DefaultInfo = {
87+
code: null,//"swingjs.test.TanSugd3S",
88+
uncompressed: true,
89+
color: "#FFFFFF", // applet object background color
90+
width: 300,
91+
height: 300,
92+
serverURL: "http://your.server.here/jsmol.php",
93+
console: null, // div for where the JavaScript console will be.
94+
readyFunction: null,
95+
use: "HTML5",//other options include JAVA
96+
jarPath: "java",
97+
jarFile: "[code].jar",
98+
j2sPath: "j2s",
99+
disableJ2SLoadMonitor: false,
100+
disableInitialConsole: false,
101+
debug: false
102+
};
103+
104+
J2S._addDefaultInfo(Info, DefaultInfo);
105+
Info.jarFile && Info.code && Info.jarFile.replace(/\[code\]/,Info.code);
106+
J2S._debugAlert = Info.debug;
107+
Info.serverURL && (J2S._serverUrl = Info.serverURL);
108+
109+
var javaAllowed = false;
110+
var applet = null;
111+
var List = Info.use.toUpperCase().split("#")[0].split(" ");
112+
for (var i = 0; i < List.length; i++) {
113+
switch (List[i]) {
114+
case "JAVA":
115+
javaAllowed = true;
116+
if (J2S.featureDetection.supportsJava())
117+
applet = new Applet(id, Info, checkOnly);
118+
break;
119+
case "HTML5":
120+
if (J2S.featureDetection.allowHTML5){
121+
applet = Applet._getCanvas(id, Info, checkOnly);
122+
} else {
123+
List.push("JAVA");
124+
}
125+
break;
126+
}
127+
if (applet != null)
128+
break;
129+
}
130+
if (applet == null) {
131+
if (checkOnly || !javaAllowed)
132+
applet = {_appletType : "none" };
133+
else if (javaAllowed)
134+
applet = new Applet(id, Info);
135+
}
136+
137+
// keyed to both its string id and itself
138+
return (checkOnly ? applet : J2S._registerApplet(id, applet));
139+
}
140+
141+
Applet._getCanvas = function(id, Info, checkOnly) {
142+
Info._isLayered = true;
143+
Info._isSwing = true;
144+
Info._platform = "";
145+
J2S._Canvas2D.prototype = J2S._jsSetPrototype(new Applet(id, Info, true));
146+
return new J2S._Canvas2D(id, Info, Info.code, checkOnly);
147+
};
148+
149+
/* AngelH, mar2007:
150+
By (re)setting these variables in the webpage before calling J2S.getApplet(),
151+
a custom message can be provided (e.g. localized for user's language) when no Java is installed.
152+
*/
153+
Applet._noJavaMsg =
154+
"Either you do not have Java applets enabled in your web<br />browser or your browser is blocking this applet.<br />\
155+
Check the warning message from your browser and/or enable Java applets in<br />\
156+
your web browser preferences, or install the Java Runtime Environment from <a href='http://www.java.com'>www.java.com</a>";
157+
158+
Applet._setCommonMethods = function(p) {
159+
p._showInfo = proto._showInfo;
160+
/// p._search = proto._search;
161+
p._getName = proto._getName;
162+
p._readyCallback = proto._readyCallback;
163+
}
164+
165+
Applet._createApplet = function(applet, Info, params) {
166+
applet._initialize(Info.jarPath, Info.jarFile);
167+
var jarFile = applet._jarFile;
168+
var jnlp = ""
169+
if (J2S._isFile) {
170+
jarFile = jarFile.replace(/0\.jar/,".jar");
171+
}
172+
// size is set to 100% of containers' size, but only if resizable.
173+
// Note that resizability in MSIE requires:
174+
// <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
175+
var w = (applet._containerWidth.indexOf("px") >= 0 ? applet._containerWidth : "100%");
176+
var h = (applet._containerHeight.indexOf("px") >= 0 ? applet._containerHeight : "100%");
177+
var widthAndHeight = " style=\"width:" + w + ";height:" + h + "\" ";
178+
var attributes = "name='" + applet._id + "_object' id='" + applet._id + "_object' " + "\n"
179+
+ widthAndHeight + jnlp + "\n"
180+
params.codebase = applet._jarPath;
181+
params.codePath = params.codebase + "/";
182+
if (params.codePath.indexOf("://") < 0) {
183+
var base = document.location.href.split("#")[0].split("?")[0].split("/");
184+
base[base.length - 1] = params.codePath;
185+
params.codePath = base.join("/");
186+
}
187+
params.archive = jarFile;
188+
params.mayscript = 'true';
189+
params.java_arguments = "-Xmx" + Math.round(Info.memoryLimit || applet._memoryLimit) + "m";
190+
params.permissions = (applet._isSigned ? "all-permissions" : "sandbox");
191+
params.documentLocation = document.location.href;
192+
params.documentBase = document.location.href.split("#")[0].split("?")[0];
193+
194+
params.jarPath = Info.jarPath;
195+
J2S._syncedApplets.length && (params.synccallback = "J2S._mySyncCallback");
196+
applet._startupScript && (params.script = applet._startupScript);
197+
var t = "\n";
198+
for (var i in params)
199+
if(params[i])
200+
t += " <param name='"+i+"' value='"+params[i]+"' />\n";
201+
if (J2S.featureDetection.useIEObject || J2S.featureDetection.useHtml4Object) {
202+
t = "<object " + attributes
203+
+ (J2S.featureDetection.useIEObject ?
204+
" classid='clsid:8AD9C840-044E-11D1-B3E9-00805F499D93' codebase='http://java.sun.com/update/1.6.0/jinstall-6u22-windows-i586.cab'>"
205+
: " type='application/x-java-applet'>")
206+
+ t + "<p style='background-color:yellow;" + widthAndHeight.split('"')[1]
207+
+ ";text-align:center;vertical-align:middle;'>\n" + Applet._noJavaMsg + "</p></object>\n";
208+
} else { // use applet tag
209+
t = "<applet " + attributes
210+
+ " code='" + params.code + "' codebase='" + applet._jarPath + "' archive='" + jarFile + "' mayscript='true'>\n"
211+
+ t + "<table bgcolor='yellow'><tr><td align='center' valign='middle' " + widthAndHeight + ">\n"
212+
+ Applet._noJavaMsg + "</td></tr></table></applet>\n";
213+
}
214+
if (applet._deferApplet)
215+
applet._javaCode = t, t="";
216+
t = J2S._getWrapper(applet, true) + t + J2S._getWrapper(applet, false)
217+
+ (Info.addSelectionOptions ? J2S._getGrabberOptions(applet) : "");
218+
if (J2S._debugAlert)
219+
alert (t);
220+
applet._code = J2S._documentWrite(t);
221+
}
222+
223+
proto._newApplet = function(viewerOptions) {
224+
this._viewerOptions = viewerOptions;
225+
// for now assigning this._applet here instead of in readyCallback
226+
Clazz.loadClass("swingjs.JSAppletViewer");
227+
this._appletPanel = Clazz.$new(swingjs.JSAppletViewer.construct, [viewerOptions]);
228+
this._appletPanel.start();
229+
}
230+
231+
proto._addCoreFiles = function() {
232+
J2S._addCoreFile("swingjs", this._j2sPath, this.__Info.preloadCore);
233+
if (J2S._debugCode) {
234+
// no min package for that
235+
J2S._addExec([this, null, "swingjs.JSAppletViewer", "load " + this.__Info.code]);
236+
237+
}
238+
}
239+
240+
proto._create = function(id, Info){
241+
J2S._setObject(this, id, Info);
242+
var params = {
243+
syncId: J2S._syncId,
244+
progressbar: "true",
245+
progresscolor: "blue",
246+
boxbgcolor: this._color || "black",
247+
boxfgcolor: "white",
248+
boxmessage: "Downloading Applet ...",
249+
//script: (this._color ? "background \"" + this._color +"\"": ""),
250+
code: Info.appletClass + ".class"
251+
};
252+
253+
J2S._setAppletParams(this._availableParams, params, Info);
254+
function sterilizeInline(model) {
255+
model = model.replace(/\r|\n|\r\n/g, (model.indexOf("|") >= 0 ? "\\/n" : "|")).replace(/'/g, "&#39;");
256+
if(J2S._debugAlert)
257+
alert ("inline model:\n" + model);
258+
return model;
259+
}
260+
261+
params.loadInline = (Info.inlineModel ? sterilizeInline(Info.inlineModel) : "");
262+
params.appletReadyCallback = "J2S._readyCallback";
263+
if (J2S._syncedApplets.length)
264+
params.synccallback = "J2S._mySyncCallback";
265+
params.java_arguments = "-Xmx" + Math.round(Info.memoryLimit || this._memoryLimit) + "m";
266+
267+
this._initialize(Info.jarPath, Info.jarFile);
268+
Applet._createApplet(this, Info, params);
269+
}
270+
271+
272+
proto._restoreState = function(clazzName, state) {
273+
// applet-dependent
274+
}
275+
276+
proto._readyCallback = function(id, fullid, isReady) {
277+
if (!isReady)
278+
return; // ignore -- page is closing
279+
J2S._setDestroy(this);
280+
this._ready = true;
281+
var script = this._readyScript;
282+
if (this._defaultModel)
283+
J2S._search(this, this._defaultModel, (script ? ";" + script : ""));
284+
else if (script)
285+
this._script(script);
286+
else if (this._src)
287+
this._script('load "' + this._src + '"');
288+
this._showInfo(true);
289+
this._showInfo(false);
290+
J2S.Cache.setDragDrop(this);
291+
this._readyFunction && this._readyFunction(this);
292+
J2S._setReady(this);
293+
var app = this._2dapplet;
294+
if (app && app._isEmbedded && app._ready && app.__Info.visible)
295+
this._show2d(true);
296+
}
297+
298+
proto._showInfo = function(tf) {
299+
if(this._isJNLP)return;
300+
if(tf && this._2dapplet)
301+
this._2dapplet._show(false);
302+
J2S.$html(J2S.$(this, "infoheaderspan"), this._infoHeader);
303+
if (this._info)
304+
J2S.$html(J2S.$(this, "infodiv"), this._info);
305+
if ((!this._isInfoVisible) == (!tf))
306+
return;
307+
this._isInfoVisible = tf;
308+
// 1px does not work for MSIE
309+
if (this._isJava) {
310+
var x = (tf ? 2 : "100%");
311+
J2S.$setSize(J2S.$(this, "appletdiv"), x, x);
312+
}
313+
J2S.$setVisible(J2S.$(this, "infotablediv"), tf);
314+
J2S.$setVisible(J2S.$(this, "infoheaderdiv"), tf);
315+
this._show(!tf);
316+
}
317+
318+
proto._show = function(tf) {
319+
var x = (!tf ? 2 : "100%");
320+
J2S.$setSize(J2S.$(this, "object"), x, x);
321+
if (!this._isJava)
322+
J2S.$setVisible(J2S.$(this, "appletdiv"), tf);
323+
}
324+
325+
proto._clearConsole = function () {
326+
if (this._console == this._id + "_infodiv")
327+
this.info = "";
328+
if (!self.Clazz)return;
329+
J2S._setConsoleDiv(this._console);
330+
Clazz.Console.clear();
331+
}
332+
333+
proto._resizeApplet = function(size) {
334+
// See _jmolGetAppletSize() for the formats accepted as size [same used by jmolApplet()]
335+
// Special case: an empty value for width or height is accepted, meaning no change in that dimension.
336+
337+
/*
338+
* private functions
339+
*/
340+
function _getAppletSize(size, units) {
341+
/* Accepts single number, 2-value array, or object with width and height as mroperties, each one can be one of:
342+
percent (text string ending %), decimal 0 to 1 (percent/100), number, or text string (interpreted as nr.)
343+
[width, height] array of strings is returned, with units added if specified.
344+
Percent is relative to container div or element (which should have explicitly set size).
345+
*/
346+
var width, height;
347+
if(( typeof size) == "object" && size != null) {
348+
width = size[0]||size.width;
349+
height = size[1]||size.height;
350+
} else {
351+
width = height = size;
352+
}
353+
return [J2S.fixDim(width, units), J2S.fixDim(height, units)];
354+
}
355+
var sz = _getAppletSize(size, "px");
356+
var d = J2S._getElement(this, "appletinfotablediv");
357+
d.style.width = sz[0];
358+
d.style.height = sz[1];
359+
this._containerWidth = sz[0];
360+
this._containerHeight = sz[1];
361+
if (this._is2D)
362+
J2S._repaint(this, true);
363+
}
364+
365+
proto._cover = function (doCover) {
366+
// from using getAppletHtml()
367+
this._newCanvas(false);
368+
this._showInfo(false);
369+
this._init();
370+
};
371+
372+
373+
374+
})(SwingJS._Applet, SwingJS._Applet.prototype);
375+
376+
})(SwingJS, jQuery, J2S);
377+
378+
} // SwingJS undefined

0 commit comments

Comments
 (0)