From: BobHanson Date: Fri, 5 Jun 2020 12:38:09 +0000 (-0500) Subject: JAL-3446 removes all JalviewJSApp dependency from jalview.bin.Jalview; X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=d193330513f3b6aa8d1aaee0eb502601b2f7fec2;p=jalview.git JAL-3446 removes all JalviewJSApp dependency from jalview.bin.Jalview; cleans up logic in Jalview.doMain --- diff --git a/src/jalview/api/JalviewJSApp.java b/src/jalview/api/JalviewJSApp.java index 693ea63..2975317 100644 --- a/src/jalview/api/JalviewJSApp.java +++ b/src/jalview/api/JalviewJSApp.java @@ -60,8 +60,6 @@ public class JalviewJSApp implements JalviewJSApi { private ArgsParser aparser; - private String j2sAppletID; - private boolean debug; private String[] ret = new String[1]; @@ -70,11 +68,10 @@ public class JalviewJSApp implements JalviewJSApi // be '|', but many sequence IDS include // pipes. + public JalviewJSApp(ArgsParser aparser) { this.aparser = aparser; - this.j2sAppletID = (String) aparser.getAppletValue("j2sAppletID", - "Jalview", true); Platform.setAppClass(this); } @@ -103,11 +100,6 @@ public class JalviewJSApp implements JalviewJSApi loadPdbFiles(af); } - public String getAppID(String frameType) - { - return j2sAppletID + (frameType == null ? "" : "-" + frameType); - } - // TODO BH 2019 // // These are methods that are in JalviewLite that various classes call @@ -2193,11 +2185,10 @@ public class JalviewJSApp implements JalviewJSApi } - Jalview jalview = Jalview.getInstance(); - jalview.doSendCallback(_listener, + doSendCallback(_listener, new Object[] - { src, setid, jalview.arrayToSeparatorList(seqs), - jalview.arrayToSeparatorList(cols) }); + { src, setid, arrayToSeparatorList(seqs), + arrayToSeparatorList(cols) }); } public boolean isFor(AlignFrame af, String listener) diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index af9d06e..ac44bfe 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -119,11 +119,6 @@ public class Jalview implements ApplicationSingletonI { } - static - { - Platform.getURLCommandArguments(); - } - private boolean headless; private Desktop desktop; @@ -132,8 +127,8 @@ public class Jalview implements ApplicationSingletonI public String appletResourcePath; - JalviewJSApp app; // JalviewJS-specific JavaScript interface - + public String j2sAppletID; + private boolean noCalculation, noMenuBar, noStatus; private boolean noAnnotation; @@ -160,12 +155,9 @@ public class Jalview implements ApplicationSingletonI static { - if (!Platform.isJS()) - /** - * Java only - * - * @j2sIgnore - */ + if (Platform.isJS()) { + Platform.getURLCommandArguments(); + } else /** @j2sIgnore */ { // grab all the rights we can for the JVM Policy.setPolicy(new Policy() @@ -308,11 +300,12 @@ public class Jalview implements ApplicationSingletonI if (isJS) { - app = new JalviewJSApp(aparser); - Preferences.setAppletDefaults(); - Cache.loadProperties(usrPropsFile); // again, because we - // might be changing defaults here? - appletResourcePath = (String) aparser.getAppletValue("resourcepath", null, true); + j2sAppletID = Platform.getAppID(null); + Preferences.setAppletDefaults(); + Cache.loadProperties(usrPropsFile); // again, because we + // might be changing defaults here? + appletResourcePath = (String) aparser.getAppletValue("resourcepath", + null, true); } else /** @@ -451,7 +444,7 @@ public class Jalview implements ApplicationSingletonI * configure 'full' SO model if preferences say to, else use the default (full SO) * - as JS currently doesn't have OBO parsing, it must use 'Lite' version */ - boolean soDefault = !Platform.isJS(); + boolean soDefault = !isJS; if (Cache.getDefault("USE_FULL_SO", soDefault)) { SequenceOntologyFactory.setSequenceOntology(new SequenceOntology()); @@ -470,7 +463,7 @@ public class Jalview implements ApplicationSingletonI } desktop.setVisible(true); - if (!Platform.isJS()) + if (!isJS) /** * Java only * @@ -529,12 +522,13 @@ public class Jalview implements ApplicationSingletonI BioJsHTMLOutput.updateBioJS(); } } - parseArguments(aparser, true); - } /** + * Parse all command-line String[] arguments as well as all JavaScript-derived parameters from Info. + * + * We allow for this method to be run from JavaScript. Basically allowing simple scripting. * * @param aparser * @param isStartup @@ -574,17 +568,22 @@ public class Jalview implements ApplicationSingletonI String file = aparser.getValue("open", true); - if (file == null && desktop == null) + if (!isJS && file == null && desktop == null) { System.out.println("No files to open!"); System.exit(1); } + // time to open a file. + long progress = -1; DataSourceType protocol = null; FileLoader fileLoader = new FileLoader(!headless); FileFormatI format = null; // Finally, deal with the remaining input data. + JalviewJSApp jsApp = null; + AlignFrame af = null; + if (file != null) { if (!headless) @@ -616,7 +615,8 @@ public class Jalview implements ApplicationSingletonI } } } - String fileFormat = (isJS ? (String) aparser.getAppletValue("format", null, true) + String fileFormat = (isJS + ? (String) aparser.getAppletValue("format", null, true) : null); protocol = AppletFormatAdapter.checkProtocol(file); try @@ -657,8 +657,8 @@ public class Jalview implements ApplicationSingletonI System.out.println("CMD [nocalculation] executed successfully!"); } - AlignFrame af = new FileLoader(!headless).LoadFileWaitTillLoaded(file, - protocol, format); + af = new FileLoader(!headless).LoadFileWaitTillLoaded(file, protocol, + format); if (af == null) { System.out.println("error"); @@ -819,10 +819,11 @@ public class Jalview implements ApplicationSingletonI // TODO - load PDB structure(s) to alignment JAL-629 // (associate with identical sequence in alignment, or a specified // sequence) - + if (isJS) { - app.load(af); + jsApp = new JalviewJSApp(aparser); + jsApp.load(af); } else /** @@ -846,67 +847,67 @@ public class Jalview implements ApplicationSingletonI } } } - - AlignFrame startUpAlframe = null; - // We'll only open the default file if the desktop is visible. - // And the user - // //////////////////// - - if (!isJS && !headless && file == null - && Cache.getDefault("SHOW_STARTUP_FILE", true)) - /** - * Java only - * - * @j2sIgnore - */ + else { - file = Cache.getDefault("STARTUP_FILE", - Cache.getDefault("www.jalview.org", "http://www.jalview.org") - + "/examples/exampleFile_2_7.jar"); - if (file.equals( - "http://www.jalview.org/examples/exampleFile_2_3.jar")) + if (!isJS && !headless && Cache.getDefault("SHOW_STARTUP_FILE", true)) + /** + * Java only + * + * @j2sIgnore + */ { - // hardwire upgrade of the startup file - file.replace("_2_3.jar", "_2_7.jar"); - // and remove the stale setting - Cache.removeProperty("STARTUP_FILE"); - } - protocol = DataSourceType.FILE; + // We'll only open the default file if the desktop is visible. + // And the user + // //////////////////// - if (file.indexOf("http:") > -1) - { - protocol = DataSourceType.URL; - } + file = Cache.getDefault("STARTUP_FILE", + Cache.getDefault("www.jalview.org", + "http://www.jalview.org") + + "/examples/exampleFile_2_7.jar"); + if (file.equals( + "http://www.jalview.org/examples/exampleFile_2_3.jar")) + { + // hardwire upgrade of the startup file + file.replace("_2_3.jar", "_2_7.jar"); + // and remove the stale setting + Cache.removeProperty("STARTUP_FILE"); + } - if (file.endsWith(".jar")) - { - format = FileFormat.Jalview; - } - else - { - try + protocol = DataSourceType.FILE; + + if (file.indexOf("http:") > -1) { - format = new IdentifyFile().identify(file, protocol); - } catch (FileFormatException e) + protocol = DataSourceType.URL; + } + + if (file.endsWith(".jar")) { - // TODO what? + format = FileFormat.Jalview; + } + else + { + try + { + format = new IdentifyFile().identify(file, protocol); + } catch (FileFormatException e) + { + // TODO what? + } } - } - startUpAlframe = fileLoader.LoadFileWaitTillLoaded(file, protocol, - format); + af = fileLoader.LoadFileWaitTillLoaded(file, protocol, format); + } } - // extract groovy arguments before anything else. // Once all other stuff is done, execute any groovy scripts (in order) - if (groovyscript != null) + if (!isJS && groovyscript != null) { if (Cache.groovyJarsPresent()) { System.out.println("Executing script " + groovyscript); - executeGroovyScript(groovyscript, startUpAlframe); + executeGroovyScript(groovyscript, af); } else { @@ -928,7 +929,7 @@ public class Jalview implements ApplicationSingletonI if (isJS && isStartup) { - app.callInitCallback(); + jsApp.callInitCallback(); } } @@ -1007,7 +1008,7 @@ public class Jalview implements ApplicationSingletonI "Creating image map: " + imageFile.getAbsolutePath()); continue; } - if (!Platform.isJS()) + if (!Platform.isJS()) /** @j2sIgnore */ { // skipping outputFormat? System.out.println("Unknown arg: " + outputFormat); @@ -1024,13 +1025,12 @@ public class Jalview implements ApplicationSingletonI + " format!!"); } } - while (aparser.getSize() > 0) - { - System.out.println("Unknown arg: " + aparser.nextValue()); - } break; } - + while (aparser.getSize() > 0) + { + System.out.println("Unknown arg: " + aparser.nextValue()); + } } private static void showUsage() @@ -1267,32 +1267,10 @@ public class Jalview implements ApplicationSingletonI } - /** - * Get the SwingJS applet ID and combine that with the frameType - * - * @param frameType - * "alignment", "desktop", etc., or null - * @return - */ - public static String getAppID(String frameType) - { - return (Platform.isJS() ? getInstance().app.getAppID(frameType) : null); - } - - public String doSendCallback(Object callback, Object[] data) { - return (Platform.isJS() ? app.doSendCallback(callback, data) : null); - } - public void notifyWorker(AlignCalcWorkerI worker, String status) { // System.out.println("Jalview worker " + worker.getClass().getSimpleName() // + " " + status); } - - public Object arrayToSeparatorList(String[] seqs) - { - return (Platform.isJS() ? app.arrayToSeparatorList(seqs) : null); - } - } diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 6eb2dfe..22d10c1 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -3375,7 +3375,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, // BH 2019.07.26 we allow for an embedded // undecorated overview with defined size - frame.setName(Jalview.getAppID("overview")); + frame.setName(Platform.getAppID("overview")); // Dimension dim = Platform.getDimIfEmbedded(frame, -1, -1); if (dim != null && dim.width == 0) diff --git a/src/jalview/jbgui/GAlignFrame.java b/src/jalview/jbgui/GAlignFrame.java index 1eb0fb9..836aa33 100755 --- a/src/jalview/jbgui/GAlignFrame.java +++ b/src/jalview/jbgui/GAlignFrame.java @@ -25,7 +25,6 @@ import jalview.analysis.GeneticCodeI; import jalview.analysis.GeneticCodes; import jalview.api.SplitContainerI; import jalview.bin.Cache; -import jalview.bin.Jalview; import jalview.gui.JvSwingUtils; import jalview.gui.Preferences; import jalview.io.FileFormats; @@ -221,7 +220,7 @@ public class GAlignFrame extends JInternalFrame { // for Web-page embedding using id=align-frame-div - setName(Jalview.getAppID("alignment")); + setName(Platform.getAppID("alignment")); jbInit(); diff --git a/src/jalview/jbgui/GDesktop.java b/src/jalview/jbgui/GDesktop.java index c029bee..37d81fe 100755 --- a/src/jalview/jbgui/GDesktop.java +++ b/src/jalview/jbgui/GDesktop.java @@ -21,7 +21,6 @@ package jalview.jbgui; import jalview.api.AlignmentViewPanel; -import jalview.bin.Jalview; import jalview.io.FileFormatException; import jalview.util.MessageManager; import jalview.util.Platform; @@ -141,7 +140,7 @@ public class GDesktop extends JFrame private void jbInit() throws Exception { - setName(Jalview.getAppID("desktop")); + setName(Platform.getAppID("desktop")); FileMenu.setText(MessageManager.getString("action.file")); HelpMenu.setText(MessageManager.getString("action.help")); inputLocalFileMenuItem diff --git a/src/jalview/jbgui/GPCAPanel.java b/src/jalview/jbgui/GPCAPanel.java index 74b5afd..aa2f549 100755 --- a/src/jalview/jbgui/GPCAPanel.java +++ b/src/jalview/jbgui/GPCAPanel.java @@ -20,9 +20,9 @@ */ package jalview.jbgui; -import jalview.bin.Jalview; import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; +import jalview.util.Platform; import java.awt.BorderLayout; import java.awt.Color; @@ -89,7 +89,7 @@ public class GPCAPanel extends JInternalFrame private void jbInit() throws Exception { - setName(Jalview.getAppID("pca")); + setName(Platform.getAppID("pca")); this.getContentPane().setLayout(new BorderLayout()); JPanel jPanel2 = new JPanel(); jPanel2.setLayout(new FlowLayout()); diff --git a/src/jalview/jbgui/GStructureViewer.java b/src/jalview/jbgui/GStructureViewer.java index e16d63a..1e7cf37 100644 --- a/src/jalview/jbgui/GStructureViewer.java +++ b/src/jalview/jbgui/GStructureViewer.java @@ -21,10 +21,10 @@ package jalview.jbgui; import jalview.api.structures.JalviewStructureDisplayI; -import jalview.bin.Jalview; import jalview.gui.ColourMenuHelper.ColourChangeListener; import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; +import jalview.util.Platform; import java.awt.BorderLayout; import java.awt.GridLayout; @@ -90,7 +90,7 @@ public abstract class GStructureViewer extends JInternalFrame private void jbInit() throws Exception { - setName(Jalview.getAppID("structureviewer")); + setName(Platform.getAppID("structureviewer")); JMenuBar menuBar = new JMenuBar(); this.setJMenuBar(menuBar); diff --git a/src/jalview/jbgui/GTreePanel.java b/src/jalview/jbgui/GTreePanel.java index 0f9c2a2..3aff0e0 100755 --- a/src/jalview/jbgui/GTreePanel.java +++ b/src/jalview/jbgui/GTreePanel.java @@ -20,9 +20,9 @@ */ package jalview.jbgui; -import jalview.bin.Jalview; import jalview.util.ImageMaker.TYPE; import jalview.util.MessageManager; +import jalview.util.Platform; import java.awt.BorderLayout; import java.awt.Color; @@ -93,7 +93,7 @@ public class GTreePanel extends JInternalFrame private void jbInit() throws Exception { - setName(Jalview.getAppID("tree")); + setName(Platform.getAppID("tree")); this.getContentPane().setLayout(borderLayout1); this.setBackground(Color.white); this.setFont(new java.awt.Font("Verdana", 0, 12)); diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index f4ac16f..a47e2f8 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -50,6 +50,7 @@ import org.json.simple.parser.ParseException; import com.stevesoft.pat.Regex; +import jalview.bin.Jalview; import jalview.javascript.json.JSON; import swingjs.api.JSUtilI; @@ -68,14 +69,17 @@ public class Platform isWin = null; private static Boolean isHeadless = null; - + private static swingjs.api.JSUtilI jsutil; - static { - if (isJS) { + static + { + if (isJS) + { try { - // this is ok - it's a highly embedded method in Java; the deprecation is + // this is ok - it's a highly embedded method in Java; the deprecation + // is // really a recommended best practice. jsutil = ((JSUtilI) Class.forName("swingjs.JSUtil").newInstance()); } catch (InstantiationException | IllegalAccessException @@ -83,7 +87,7 @@ public class Platform { e.printStackTrace(); } - } + } } // private static Boolean isHeadless = null; @@ -142,18 +146,18 @@ public class Platform return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin); } - /** - * - * @return true if we are running in non-interactive no UI mode - */ - public static boolean isHeadless() - { - if (isHeadless == null) - { - isHeadless = "true".equals(System.getProperty("java.awt.headless")); - } - return isHeadless; - } + /** + * + * @return true if we are running in non-interactive no UI mode + */ + public static boolean isHeadless() + { + if (isHeadless == null) + { + isHeadless = "true".equals(System.getProperty("java.awt.headless")); + } + return isHeadless; + } /** * @@ -176,7 +180,7 @@ public class Platform { return s == null ? null : s.replace("\\", "\\\\"); } - + /** * Answers true if the mouse event has Meta-down (Command key on Mac) or * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is @@ -366,7 +370,6 @@ public class Platform } } - public static void cacheFileData(File file) { if (isJS) @@ -396,7 +399,7 @@ public class Platform } return null; } - + public static String getFileAsString(String url) { if (isJS && url != null) @@ -420,7 +423,6 @@ public class Platform return false; } - public static void addJ2SBinaryType(String ext) { if (isJS) @@ -428,7 +430,7 @@ public class Platform jsutil.addBinaryFileType(ext); } } - + /** * Encode the URI using JavaScript encodeURIComponent * @@ -469,7 +471,6 @@ public class Platform return (isJS ? (String) jsutil.getAppletAttribute("_uniqueId") : null); } - /** * Read the Info block for this applet. * @@ -544,8 +545,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) @@ -576,8 +576,8 @@ public class Platform * @param is * @param outFile * @throws IOException - * if the file cannot be created or there is a problem - * reading the input stream. + * 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 @@ -623,15 +623,20 @@ public class Platform jsutil.addDirectDatabaseCall(domain); System.out.println( - "Platform adding known access-control-allow-origin * for domain " - + domain); + "Platform adding known access-control-allow-origin * for domain " + + domain); } } + /** + * Allow for URL-line command arguments. Untested. + * + */ public static void getURLCommandArguments() { + try { /** * Retrieve the first query field as command arguments to Jalview. Include * only if prior to "?j2s" or "&j2s" or "#". Assign the applet's __Info.args @@ -641,13 +646,16 @@ public class Platform * decodeURI((document.location.href.replace("&","?").split("?j2s")[0] * + "?").split("?")[1].split("#")[0]); a && * (J2S.thisApplet.__Info.args = a.split(" ")); + * + * System.out.println("URL arguments: " + a); */ - + } catch (Throwable t) { + } } /** - * A (case sensitive) file path comparator that ignores the difference between / - * and \ + * A (case sensitive) file path comparator that ignores the difference between + * / and \ * * @param path1 * @param path2 @@ -668,11 +676,8 @@ public class Platform return p1.equals(p2); } + ///////////// JAL-3253 Applet additions ////////////// - -///////////// JAL-3253 Applet additions ////////////// - - /** * Retrieve the object's embedded size from a div's style on a page if * embedded in SwingJS. @@ -759,7 +764,6 @@ public class Platform } } - /** * load a resource -- probably a core file -- if and only if a particular * class has not been instantialized. We use a String here because if we used @@ -842,7 +846,8 @@ public class Platform public static String getUserPath(String subpath) { char sep = File.separatorChar; - return System.getProperty("user.home") + sep + subpath.replace('/', sep); + return System.getProperty("user.home") + sep + + subpath.replace('/', sep); } /** @@ -857,8 +862,10 @@ public class Platform */ public static boolean isFileOlderThanThreshold(File file, int noOfDays) { - if (isJS()) { - // not meaningful in SwingJS -- this is a session-specific temp file. It doesn't have a timestamp. + if (isJS()) + { + // not meaningful in SwingJS -- this is a session-specific temp file. It + // doesn't have a timestamp. return false; } Path filePath = file.toPath(); @@ -893,8 +900,10 @@ public class Platform { return failValue; } - if (isJS) { - int val = /** @j2sNative 1 ? parseInt(input) : */ 0; + if (isJS) + { + int val = /** @j2sNative 1 ? parseInt(input) : */ + 0; return (val == val + 0 ? val : failValue); } // JavaScript does not support Regex ? lookahead @@ -911,4 +920,22 @@ public class Platform return (isJS ? jsutil.getAppletInfoAsMap() : null); } + /** + * Get the SwingJS applet ID and combine that with the frameType + * + * @param frameType + * "alignment", "desktop", etc., or null + * @return + */ + public static String getAppID(String frameType) + { + String id = Jalview.getInstance().j2sAppletID; + if (id == null) + { + Jalview.getInstance().j2sAppletID = id = (String) jsutil + .getAppletAttribute("_id"); + } + return id + (frameType == null ? "" : "-" + frameType); + } + }