From f8bded5a809a36e3d3fa8c6a8fe8d1622b3e324f Mon Sep 17 00:00:00 2001 From: BobHanson Date: Thu, 28 May 2020 21:48:36 -0500 Subject: [PATCH 1/1] JSUtilI interface --- src/jalview/util/Platform.java | 512 ++++++++++++++++++---------------------- 1 file changed, 236 insertions(+), 276 deletions(-) diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 4d2a09e..c511ce3 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -22,6 +22,7 @@ package jalview.util; import jalview.javascript.json.JSON; +import java.awt.Component; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.MouseEvent; @@ -33,14 +34,12 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; -import java.net.MalformedURLException; import java.net.URL; import java.util.Properties; import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.Logger; -import javax.swing.JComponent; import javax.swing.SwingUtilities; import org.json.simple.parser.JSONParser; @@ -345,67 +344,38 @@ public class Platform } } - public static void cacheFileData(String path, Object data) + /** + * Dump the input stream to an output file. + * + * @param is + * @param outFile + * @throws IOException + * if the file cannot be created or there is a problem reading the + * input stream. + */ + public static void streamToFile(InputStream is, File outFile) + throws IOException { - if (!isJS() || data == null) - { - return; - } - jsutil.cachePathData(path, data); - } - public static void cacheFileData(File file) - { - byte[] data; - if (!isJS() || (data = Platform.getFileBytes(file)) == null) + if (isJS) { + jsutil.setFileBytes(outFile, is); return; } - cacheFileData(file.toString(), data); - } - - public static byte[] getFileBytes(File f) - { - // JavaScript only - return jsutil.getBytes(f); - } - public static byte[] getFileAsBytes(String fileStr) - { - byte[] bytes = (byte[]) jsutil.getFile(fileStr, false); - cacheFileData(fileStr, bytes); - return bytes; - } - - @SuppressWarnings("unused") - public static String getFileAsString(String url) - { - String ret = (String) jsutil.getFile(url, true); - cacheFileData(url, ret); - return ret; - } - - public static boolean setFileBytes(File f, String urlstring) - { - if (!isJS()) + FileOutputStream fio = new FileOutputStream(outFile); + try { - return false; + byte[] bb = new byte[32 * 1024]; + int l; + while ((l = is.read(bb)) > 0) + { + fio.write(bb, 0, l); + } + } finally + { + fio.close(); } - @SuppressWarnings("unused") - byte[] bytes = getFileAsBytes(urlstring); - jsutil.setFileBytes(f, bytes); - return true; - } - - public static void addJ2SBinaryType(String ext) - { - - /** - * @j2sNative - * - * J2S._binaryTypes.push("." + ext + "?"); - * - */ } /** @@ -430,7 +400,7 @@ public class Platform */ public static boolean openURL(String url) throws IOException { - if (!isJS()) + if (!isJS) { BrowserLauncher.openURL(url); return false; @@ -444,65 +414,10 @@ public class Platform return true; } - public static String getUniqueAppletID() - { - @SuppressWarnings("unused") - ThreadGroup g = Thread.currentThread().getThreadGroup(); - /** - * @j2sNative return g.秘html5Applet._uniqueId; - * - */ - return null; - - } - - /** - * Read the Info block for this applet. - * - * @param prefix - * "jalview_" - * @param p - * @return unique id for this applet - */ - public static void readInfoProperties(String prefix, Properties p) - { - if (!isJS()) - { - return; - } - @SuppressWarnings("unused") - ThreadGroup g = Thread.currentThread().getThreadGroup(); - String id = getUniqueAppletID(); - String key = "", value = ""; - /** - * @j2sNative var info = g.秘html5Applet.__Info || {}; for (var key in info) - * { if (key.indexOf(prefix) == 0) { value = "" + info[key]; - */ - - System.out.println( - "Platform id=" + id + " reading Info." + key + " = " + value); - p.put(key, value); - - /** - * @j2sNative - * - * - * } } - */ - } - - public static void setAjaxJSON(URL url) - { - if (isJS()) - { - JSON.setAjax(url); - } - } - public static Object parseJSON(InputStream response) throws IOException, ParseException { - if (isJS()) + if (isJS) { return JSON.parse(response); } @@ -529,8 +444,7 @@ public class Platform public static Object parseJSON(String json) throws ParseException { - return (isJS() ? JSON.parse(json) - : new JSONParser().parse(json)); + return (isJS ? JSON.parse(json) : new JSONParser().parse(json)); } public static Object parseJSON(Reader r) @@ -541,7 +455,7 @@ public class Platform return null; } - if (!isJS()) + if (!isJS) { return new JSONParser().parse(r); } @@ -557,40 +471,208 @@ public class Platform } /** - * Dump the input stream to an output file. + * Retrieve the object's embedded size from a div's style on a page if + * embedded in SwingJS. * - * @param is - * @param outFile - * @throws IOException - * if the file cannot be created or there is a problem reading the - * input stream. + * @param frame + * JFrame or JInternalFrame + * @param defaultWidth + * use -1 to return null (no default size) + * @param defaultHeight + * @return the embedded dimensions or null (no default size or not embedded) */ - public static void streamToFile(InputStream is, File outFile) - throws IOException + public static Dimension getDimIfEmbedded(Component frame, + int defaultWidth, int defaultHeight) { - FileOutputStream fio = new FileOutputStream(outFile); - try + Dimension d = null; + if (isJS) + { + d = (Dimension) getEmbeddedAttribute(frame, "dim"); + } + return (d == null && defaultWidth >= 0 + ? new Dimension(defaultWidth, defaultHeight) + : d); + + } + + public static Regex newRegex(String searchString, String replaceString) + { + ensureRegex(); + return (replaceString == null ? new Regex(searchString) + : new Regex(searchString, replaceString)); + } + + public static Regex newRegexPerl(String code) + { + ensureRegex(); + return Regex.perlCode(code); + } + + /** + * Initialize Java debug logging. A representative sample -- adapt as desired. + */ + public static void startJavaLogging() + { + /** + * @j2sIgnore + */ { - if (isJS() - && /** - * @j2sNative outFile.setBytes$O && outFile.setBytes$O(is) && - */ - true) + logClass("java.awt.EventDispatchThread", "java.awt.EventQueue", + "java.awt.Component", "java.awt.focus.Component", + "java.awt.event.Component", + "java.awt.focus.DefaultKeyboardFocusManager"); + } + } + + /** + * Initiate Java logging for a given class. Only for Java, not JavaScript; + * Allows debugging of complex event processing. + * + * @param className + */ + public static void logClass(String... classNames) + { + /** + * @j2sIgnore + * + * + */ + { + Logger rootLogger = Logger.getLogger(""); + rootLogger.setLevel(Level.ALL); + ConsoleHandler consoleHandler = new ConsoleHandler(); + consoleHandler.setLevel(Level.ALL); + for (int i = classNames.length; --i >= 0;) { - return; + Logger logger = Logger.getLogger(classNames[i]); + logger.setLevel(Level.ALL); + logger.addHandler(consoleHandler); } - byte[] bb = new byte[32 * 1024]; - int l; - while ((l = is.read(bb)) > 0) + } + } + + /// JavaScript-only methods /// + + public static void cacheFileData(String path, Object data) + { + if (isJS && data != null) + { + jsutil.cachePathData(path, data); + } + } + + public static void cacheFileData(File file) + { + if (isJS) + { + byte[] data = Platform.getFileBytes(file); { - fio.write(bb, 0, l); + if (data != null) + { + cacheFileData(file.toString(), data); + } } - } finally + } + } + + public static byte[] getFileBytes(File f) + { + return (isJS && f != null ? jsutil.getBytes(f) : null); + } + + public static byte[] getFileAsBytes(String fileStr) + { + if (isJS && fileStr != null) { - fio.close(); + byte[] bytes = (byte[]) jsutil.getFile(fileStr, false); + cacheFileData(fileStr, bytes); + return bytes; + } + return null; + } + + @SuppressWarnings("unused") + public static String getFileAsString(String url) + { + if (isJS && url != null) + { + String ret = (String) jsutil.getFile(url, true); + cacheFileData(url, ret); + return ret; + } + return null; + } + + public static boolean setFileBytes(File f, String urlstring) + { + if (isJS && f != null && urlstring != null) + { + @SuppressWarnings("unused") + byte[] bytes = getFileAsBytes(urlstring); + jsutil.setFileBytes(f, bytes); + return true; + } + return false; + } + + public static void addJ2SBinaryType(String ext) + { + if (isJS) + { + jsutil.addBinaryFileType(ext); + } + } + + public static String getUniqueAppletID() + { + return (isJS ? (String) jsutil.getAppletAttribute("_uniqueId") : null); + } + + /** + * Read the Info block for this applet. + * + * @param prefix + * "jalview_" + * @param p + * @return unique id for this applet + */ + public static void readInfoProperties(String prefix, Properties p) + { + if (isJS) + { + String id = getUniqueAppletID(); + + String key = ""; + String value = ""; + @SuppressWarnings("unused") + Object info = jsutil.getAppletAttribute("__Info"); + /** + * @j2sNative for (key in info) { value = info[key]; + */ + + if (key.indexOf(prefix) == 0) + { + System.out.println("Platform id=" + id + " reading Info." + key + + " = " + value); + p.put(key, value); + + } + + /** + * @j2sNative } + */ } } + public static void setAjaxJSON(URL url) + { + if (isJS) + { + JSON.setAjax(url); + } + } + + /** * Add a known domain that implements access-control-allow-origin:* * @@ -605,16 +687,13 @@ public class Platform public static void addJ2SDirectDatabaseCall(String domain) { - if (isJS()) + if (isJS) { + jsutil.addDirectDatabaseCall(domain); + System.out.println( "Platform adding known access-control-allow-origin * for domain " + domain); - /** - * @j2sNative - * - * J2S.addDirectDatabaseCall(domain); - */ } } @@ -637,42 +716,12 @@ public class Platform public static URL getDocumentBase() { - try - { - if (isJS()) - { - @SuppressWarnings("unused") - Object g = Thread.currentThread().getThreadGroup(); - return new URL(/** - * @j2sNative g.秘html5Applet._appletPanel.appletDocumentBase - * || - */ - ""); - } - } catch (MalformedURLException e) - { - } - return null; + return (isJS ? jsutil.getDocumentBase() : null); } public static URL getCodeBase() { - try - { - if (isJS()) - { - @SuppressWarnings("unused") - Object g = Thread.currentThread().getThreadGroup(); - return new URL(/** - * @j2sNative g.秘html5Applet._appletPanel.appletCodeBase - * || - */ - ""); - } - } catch (MalformedURLException e) - { - } - return null; + return (isJS ? jsutil.getCodeBase() : null); } /** @@ -684,75 +733,21 @@ public class Platform * @param resourcePath * @param className */ - public static void loadStaticResource(Object resourcePath, + public static void loadStaticResource(String resourcePath, String className) { - /** - * - * @j2sNative if (!swingjs.JSUtil.isClassLoaded$S(className)) - * swingjs.JSUtil.loadStaticResource$S(resourcePath); - */ - } - - public static void ensureRegex() - { - loadStaticResource("core/core_stevesoft.z.js", - "com.stevesoft.pat.Regex"); - } - - public static Regex newRegex(String searchString, String replaceString) - { - ensureRegex(); - return (replaceString == null ? new Regex(searchString) - : new Regex(searchString, replaceString)); - } - - public static Regex newRegexPerl(String code) - { - ensureRegex(); - return Regex.perlCode(code); - } - - /** - * Initialize Java debug logging. A representative sample -- adapt as desired. - */ - public static void startJavaLogging() - { - /** - * @j2sIgnore - */ + if (isJS) { - logClass("java.awt.EventDispatchThread", "java.awt.EventQueue", - "java.awt.Component", "java.awt.focus.Component", - "java.awt.event.Component", - "java.awt.focus.DefaultKeyboardFocusManager"); + jsutil.loadResourceIfClassUnknown(resourcePath, className); } } - /** - * Initiate Java logging for a given class. Only for Java, not JavaScript; - * Allows debugging of complex event processing. - * - * @param className - */ - public static void logClass(String... classNames) + public static void ensureRegex() { - /** - * @j2sIgnore - * - * - */ + if (isJS) { - Logger rootLogger = Logger.getLogger(""); - rootLogger.setLevel(Level.ALL); - ConsoleHandler consoleHandler = new ConsoleHandler(); - consoleHandler.setLevel(Level.ALL); - for (int i = classNames.length; --i >= 0;) - { - Logger logger = Logger.getLogger(classNames[i]); - logger.setLevel(Level.ALL); - logger.addHandler(consoleHandler); - } + loadStaticResource("core/core_stevesoft.z.js", + "com.stevesoft.pat.Regex"); } } @@ -766,59 +761,24 @@ public class Platform */ public static void setAppClass(Object j) { - if (!isJS()) + if (isJS) { - return; + jsutil.setAppletAttribute("app", j); } - @SuppressWarnings("unused") - Thread t = Thread.currentThread(); - /** - * Set up "testApplet.app" to be this instance - * - * @j2sNative - * - * try {self[t.name].app = j}catch(e){} - */ - } - - /** - * Retrieve the object's embedded size from a div's style on a page if - * embedded in SwingJS. - * - * @param frame - * JFrame or JInternalFrame - * @param defaultWidth - * use -1 to return null (no default size) - * @param defaultHeight - * @return the embedded dimensions or null (no default size or not embedded) - */ - public static Dimension getDimIfEmbedded(JComponent frame, - int defaultWidth, int defaultHeight) - { - Dimension d = /** @j2sNative frame.ui.getEmbedded$S("dim") || */ - null; - return (d == null && defaultWidth >= 0 - ? new Dimension(defaultWidth, defaultHeight) - : d); } /** * - * If this frame Is this frame embedded in a web page, return a known type. + * If this frame ia embedded in a web page, return a known type. * * @param frame * a JFrame or JInternalFrame * @param type * @return null if frame is not embedded. */ - public static Object getEmbeddedAttribute(Object frame, String type) + public static Object getEmbeddedAttribute(Component frame, String type) { - if (!isJS()) - { - return null; - } - return (/** @j2sNative frame.ui.getEmbedded$S(type) || */ - null); + return (isJS ? jsutil.getEmbeddedAttribute(frame, type) : null); } public static void stackTrace() -- 1.7.10.2