Skip to content

Commit d6af498

Browse files
committed
adds JSUtilI.java interface
1 parent 0403ea7 commit d6af498

File tree

5 files changed

+381
-19
lines changed

5 files changed

+381
-19
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8

sources/net.sf.j2s.java.core/src/swingjs/JSUtil.java

Lines changed: 189 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package swingjs;
22

33
import java.awt.Color;
4+
import java.awt.Component;
5+
import java.awt.Dimension;
46
import java.awt.JSComponent;
57
import java.awt.Toolkit;
68
import java.io.BufferedInputStream;
@@ -9,25 +11,37 @@
911
import java.io.File;
1012
import java.io.IOException;
1113
import java.io.InputStream;
14+
import java.net.MalformedURLException;
1215
import java.net.URL;
16+
import java.util.HashMap;
1317
import java.util.Hashtable;
1418
import java.util.Locale;
1519
import java.util.Map;
20+
import java.util.Properties;
21+
22+
import javax.swing.JComponent;
23+
import javax.swing.plaf.ComponentUI;
1624

1725
import javajs.util.AU;
1826
import javajs.util.AjaxURLConnection;
1927
import javajs.util.PT;
2028
import javajs.util.Rdr;
2129
import javajs.util.SB;
2230
import javajs.util.ZipTools;
31+
import sun.awt.AppContext;
2332
import swingjs.api.Interface;
33+
import swingjs.api.JSUtilI;
34+
import swingjs.api.js.DOMNode;
2435
import swingjs.api.js.HTML5Applet;
2536
import swingjs.api.js.J2SInterface;
2637
import swingjs.api.js.JQuery;
2738
import swingjs.json.JSON;
39+
import swingjs.plaf.JSFrameUI;
2840

29-
public class JSUtil {
41+
public class JSUtil implements JSUtilI {
3042

43+
public JSUtil() {}
44+
3145
static {
3246
boolean j2sdebug = false;
3347
J2SInterface j2sself = null;
@@ -91,7 +105,7 @@ public static Object removeCachedFileData(String path) {
91105
@SuppressWarnings("unused")
92106
private static Object getFileContents(Object uriOrJSFile, boolean asBytes) {
93107
if (uriOrJSFile instanceof File) {
94-
byte[] bytes = /** @j2sNative uriOrJSFile.秘bytes || */
108+
byte[] bytes = /** @j2sNative uriOrJSFile.秘bytes || */
95109
null;
96110
if (bytes != null)
97111
return bytes;
@@ -717,11 +731,21 @@ public static Color getColorFromName(String c) {
717731
}
718732

719733
public static byte[] getFileBytes(File f) {
720-
return f.秘bytes;
734+
return f.秘bytes;
721735
}
722736

737+
@Override
738+
public byte[] getBytes(File f) {
739+
return f.秘bytes;
740+
}
741+
742+
@Override
743+
public HTML5Applet getAppletForComponent(Component c) {
744+
return getHTML5Applet(c);
745+
}
746+
723747
public static HTML5Applet getApplet() {
724-
return Thread.currentThread().getThreadGroup().秘html5Applet;
748+
return getHTML5Applet(null);
725749
}
726750

727751
public static String getJSID(Object o) {
@@ -734,7 +758,168 @@ public static String getJSID(Object o) {
734758
{
735759
return null;
736760
}
761+
}
762+
763+
public static HTML5Applet getHTML5Applet(Component c) {
764+
ThreadGroup g = (c == null ? Thread.currentThread().getThreadGroup() : c.getAppContext().getThreadGroup());
765+
return ((JSThreadGroup) g).getHtmlApplet();
766+
}
767+
768+
public static boolean setFileBytesStatic(File f, Object isOrBytes) {
769+
// Used in JalviewJS
770+
if (isOrBytes instanceof InputStream) {
771+
f.秘bytes = /**
772+
* @j2sNative (isOrBytes.$in.$in || isOrBytes.$in).buf ||
773+
*/
774+
null;
775+
} else if (isOrBytes instanceof byte[]) {
776+
f.秘bytes = /**
777+
* @j2sNative isOrBytes ||
778+
*/
779+
null;
780+
} else {
781+
f.秘bytes = null;
782+
}
783+
return (f.秘bytes != null);
737784
}
738785

786+
@Override
787+
public HashMap<?,?> getJSContext(Object key) {
788+
HashMap<?,?> map = (HashMap<?, ?>) AppContext.getAppContext().get(key);
789+
if (map == null)
790+
AppContext.getAppContext().put(key, map = new HashMap<Object, Object>());
791+
return map;
792+
}
793+
794+
@Override
795+
public void addBinaryFileType(String ext) {
796+
J2S.addBinaryFileType(ext);
797+
}
798+
799+
@Override
800+
public void readInfoProperties(String prefix, Properties p) {
801+
Object info = DOMNode.getAttr(getApplet(), "__Info");
802+
if (info == null)
803+
return;
804+
String key = "";
805+
String value = "";
806+
/**
807+
* @j2sNative for (var key in info) { if (prefix == null || key.indexOf(prefix) == 0) { value = ""
808+
* + info[key];
809+
*/
810+
System.out.println("Platform reading Info." + key + " = " + value);
811+
p.put(key, value);
812+
813+
/**
814+
* @j2sNative } }
815+
*/
816+
817+
}
818+
819+
@Override
820+
public void loadResourceIfClassUnknown(String resource, String className) {
821+
if (!isClassLoaded(className))
822+
loadStaticResource(resource);
823+
}
824+
825+
@Override
826+
public void setAppletAttribute(String key, Object val) {
827+
@SuppressWarnings("unused")
828+
HTML5Applet applet = getApplet();
829+
/**
830+
*
831+
* @j2sNative
832+
* applet[key] = val;
833+
*/
834+
835+
}
836+
837+
@Override
838+
public Object getAppletAttribute(String key) {
839+
@SuppressWarnings("unused")
840+
HTML5Applet applet = getApplet();
841+
/**
842+
*
843+
* @j2sNative
844+
* return applet[key];
845+
*/
846+
{
847+
return null;
848+
}
849+
}
850+
851+
@Override
852+
public Object getEmbeddedAttribute(Component frame, String type) {
853+
ComponentUI ui = ((JComponent)frame).getUI();
854+
return (ui instanceof JSFrameUI ? (Dimension) ((JSFrameUI) ui).getEmbedded(type) : null);
855+
}
856+
857+
@Override
858+
public boolean streamToFile(InputStream is, File outFile) {
859+
return (outFile instanceof JSTempFile ? ((JSTempFile) outFile).setBytes(is) : setFileBytes(outFile, is));
860+
}
861+
862+
@Override
863+
public boolean setFileBytes(File f, Object isOrBytes) {
864+
return setFileBytesStatic(f, isOrBytes);
865+
}
866+
867+
/**
868+
* Add a known domain that implements access-control-allow-origin:*
869+
*
870+
* These should be reviewed periodically.
871+
*
872+
* @param domain for a service that is not allowing ajax
873+
*
874+
* @author hansonr@stolaf.edu
875+
*
876+
*/
877+
@Override
878+
public void addDirectDatabaseCall(String domain) {
879+
880+
System.out.println("JSUtil adding known access-control-allow-origin * for domain " + domain);
881+
J2S.addDirectDatabaseCall(domain);
882+
883+
}
884+
885+
@Override
886+
public void cachePathData(String path, Object data) {
887+
cacheFileData(path, data);
888+
}
889+
890+
@Override
891+
public Object getFile(String path, boolean asString) {
892+
return (asString ? getFileAsString(path) : getFileAsBytes(path));
893+
}
894+
895+
@Override
896+
public void setAppletInfo(String infoKey, Object val) {
897+
HTML5Applet applet = getApplet();
898+
/** @j2sNative
899+
*
900+
* applet.__Info[infoKey] = val;
901+
*/
902+
}
903+
904+
@Override
905+
public URL getDocumentBase() {
906+
JSFrameViewer ap = (JSFrameViewer) this.getAppletAttribute("_appletPanel");
907+
try {
908+
return new URL(ap.appletDocumentBase);
909+
} catch (MalformedURLException e) {
910+
return null;
911+
}
912+
}
913+
914+
@Override
915+
public URL getCodeBase() {
916+
JSFrameViewer ap = (JSFrameViewer) this.getAppletAttribute("_appletPanel");
917+
try {
918+
return new URL(ap.appletCodeBase);
919+
} catch (MalformedURLException e) {
920+
return null;
921+
}
922+
}
923+
739924
}
740925

0 commit comments

Comments
 (0)