11package swingjs ;
22
33import java .awt .Color ;
4+ import java .awt .Component ;
5+ import java .awt .Dimension ;
46import java .awt .JSComponent ;
57import java .awt .Toolkit ;
68import java .io .BufferedInputStream ;
911import java .io .File ;
1012import java .io .IOException ;
1113import java .io .InputStream ;
14+ import java .net .MalformedURLException ;
1215import java .net .URL ;
16+ import java .util .HashMap ;
1317import java .util .Hashtable ;
1418import java .util .Locale ;
1519import java .util .Map ;
20+ import java .util .Properties ;
21+
22+ import javax .swing .JComponent ;
23+ import javax .swing .plaf .ComponentUI ;
1624
1725import javajs .util .AU ;
1826import javajs .util .AjaxURLConnection ;
1927import javajs .util .PT ;
2028import javajs .util .Rdr ;
2129import javajs .util .SB ;
2230import javajs .util .ZipTools ;
31+ import sun .awt .AppContext ;
2332import swingjs .api .Interface ;
33+ import swingjs .api .JSUtilI ;
34+ import swingjs .api .js .DOMNode ;
2435import swingjs .api .js .HTML5Applet ;
2536import swingjs .api .js .J2SInterface ;
2637import swingjs .api .js .JQuery ;
2738import 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