X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FPlatform.java;h=d9241b85fd3a286b7be2d00afd2465173f8db824;hb=d891897b082f5483159db17e46d2a5910a593512;hp=fe620ed5d99e93b95b8d3983b0364b79359286aa;hpb=60ade4437409299de040abb15588b052a932bf5c;p=jalview.git diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index fe620ed..d9241b8 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -22,7 +22,7 @@ package jalview.util; import jalview.javascript.json.JSON; -import java.awt.Color; +import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.io.BufferedReader; @@ -36,7 +36,11 @@ 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; @@ -264,6 +268,20 @@ public class Platform public static long time, mark, set, duration; + /** + * typical usage: + * + * Platform.timeCheck(null, Platform.TIME_MARK); + * + * ... + * + * Platform.timeCheck("some message", Platform.TIME_MARK); + * + * reset...[set/mark]n...get + * + * @param msg + * @param mode + */ public static void timeCheck(String msg, int mode) { long t = System.currentTimeMillis(); @@ -271,6 +289,7 @@ public class Platform { case TIME_RESET: time = mark = t; + duration = 0; if (msg != null) { System.err.println("Platform: timer reset\t\t\t" + msg); @@ -279,6 +298,7 @@ public class Platform case TIME_MARK: if (set > 0) { + // total time between set/mark points duration += (t - set); } else @@ -301,7 +321,7 @@ public class Platform case TIME_GET: if (msg != null) { - System.err.println("Platform: timer dur\t" + ((t - time) / 1000f) + System.err.println("Platform: timer get\t" + ((t - time) / 1000f) + "\t" + ((duration) / 1000f) + "\t" + msg); } set = 0; @@ -553,17 +573,18 @@ public class Platform public static void streamToFile(InputStream is, File outFile) throws IOException { + if (isJS() && /** + * JSTempFile direct transfer + * + * @j2sNative outFile.setBytes$O && outFile.setBytes$O(is) && + */ + true) + { + return; + } FileOutputStream fio = new FileOutputStream(outFile); try { - if (isJS() - && /** - * @j2sNative outFile.setBytes$O && outFile.setBytes$O(is) && - */ - true) - { - return; - } byte[] bb = new byte[32 * 1024]; int l; while ((l = is.read(bb)) > 0) @@ -679,6 +700,11 @@ public class Platform */ } + public static void ensureJmol() + { + loadStaticResource("core/core_jvjmol.z.js", "org.jmol.viewer.Viewer"); + } + public static void ensureRegex() { loadStaticResource("core/core_stevesoft.z.js", @@ -699,54 +725,124 @@ public class Platform } /** - * @param c + * Initialize Java debug logging. A representative sample -- adapt as desired. */ - public static Color getColorFromName(String name) + public static void startJavaLogging() { - if (name == null) + /** + * @j2sIgnore + */ { - return null; + 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) + { /** - * @j2sNative + * @j2sIgnore + * * - * return swingjs.JSUtil.getColorFromName$S(name); */ { - // or make a static map; or use reflection on the field name - switch (name.toLowerCase()) + Logger rootLogger = Logger.getLogger(""); + rootLogger.setLevel(Level.ALL); + ConsoleHandler consoleHandler = new ConsoleHandler(); + consoleHandler.setLevel(Level.ALL); + for (int i = classNames.length; --i >= 0;) { - case "black": - return Color.black; - case "blue": - return Color.blue; - case "cyan": - return Color.cyan; - case "darkgray": - return Color.darkGray; - case "gray": - return Color.gray; - case "green": - return Color.green; - case "lightgray": - return Color.lightGray; - case "magenta": - return Color.magenta; - case "orange": - return Color.orange; - case "pink": - return Color.pink; - case "red": - return Color.red; - case "white": - return Color.white; - case "yellow": - return Color.yellow; - default: - return null; + Logger logger = Logger.getLogger(classNames[i]); + logger.setLevel(Level.ALL); + logger.addHandler(consoleHandler); } + } + } + /** + * Set the "app" property of the HTML5 applet object, for example, + * "testApplet.app", to point to the Jalview instance. This will be the object + * that page developers use that is similar to the original Java applet object + * that was accessed via LiveConnect. + * + * @param j + */ + public static void setAppClass(Object j) + { + if (!isJS()) + { + return; } + @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. + * + * @param frame + * a JFrame or JInternalFrame + * @param type + * @return null if frame is not embedded. + */ + public static Object getEmbeddedAttribute(Object frame, String type) + { + if (!isJS()) + { + return null; + } + return (/** @j2sNative frame.ui.getEmbedded$S(type) || */ + null); + } + + public static void stackTrace() + { + try + { + throw new NullPointerException(); + } catch (Exception e) + { + e.printStackTrace(); + } + + } + + }