From: Ben Soares Date: Thu, 23 Nov 2023 17:47:03 +0000 (+0000) Subject: JAL-4059 Made jalview.bin.JalviewJS to use new command-line args in querystring.... X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=487504e783d9292c8fad2813051ff7337eb47a0a;p=jalview.git JAL-4059 Made jalview.bin.JalviewJS to use new command-line args in querystring. See jalview_bin_JalviewJS-corenamespace.html for example. --- diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index d0a8be3..3a2b8c1 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -40,7 +40,6 @@ import java.security.PermissionCollection; import java.security.Permissions; import java.security.Policy; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -103,6 +102,7 @@ import jalview.util.ChannelProperties; import jalview.util.HttpUtils; import jalview.util.IdUtils; import jalview.util.IdUtils.IdType; +import jalview.util.JalviewJSUtil; import jalview.util.LaunchUtils; import jalview.util.MessageManager; import jalview.util.Platform; @@ -127,7 +127,7 @@ public class Jalview implements JalviewObjectI { static { - Platform.getURLCommandArguments(); + JalviewJSUtil.getURLCommandArguments(); Platform.addJ2SDirectDatabaseCall("https://www.jalview.org"); Platform.addJ2SDirectDatabaseCall("http://www.jalview.org"); Platform.addJ2SDirectDatabaseCall("http://www.compbio.dundee.ac.uk"); @@ -312,7 +312,7 @@ public class Jalview implements JalviewObjectI } else { - setJ2sInfo(); + JalviewJSUtil.setJ2sInfo(); } if (args == null || args.length == 0 || (args.length == 1 @@ -2098,106 +2098,4 @@ public class Jalview implements JalviewObjectI } } } - - /** - * To be populated by Info={} hash set by instantiating JalviewJS - */ - private Map j2sInfo = null; - - /** - * The non-j2s_-prefixed parameters to store - */ - private final String[] j2sSavedKeys = { "main", "core", "width", "height", - "serverURL", "j2sPath", "console" }; - - /** - * The prefix for non-named keys in the Info={} block - */ - private static final String j2sParameterPrefix = "j2s_"; - - /** - * The Info key name for namespacing (query string parameters, class and id - * values) - */ - private static final String j2sNamespaceKey = j2sParameterPrefix - + "namespace"; - - /** - * Get a value passed in from the Info={} hash. key should NOT use the - * namespace prefix. - * - * @param key - * key name without the j2s_ namespace in the Info={} hash. - * @return value value from the Info={} hash. - */ - public String getJ2sInfoValue(String key) - { - if (j2sInfo == null || key == null) - { - return null; - } - String j2sKey = Arrays.asList(j2sSavedKeys).contains(key) ? key - : j2sParameterPrefix + key; - return j2sInfo.get(j2sKey); - } - - /** - * Get and save parameters and values from the Info={} hash. - */ - public void setJ2sInfo() - { - if (!Platform.isJS()) - { - return; - } - j2sInfo = new HashMap<>(); - String key = null; - String val = null; - /** - * @j2sNative if (J2S.thisApplet.__Info !== undefined) { - * - * Object.entries(J2S.thisApplet.__Info).forEach( entry => { - * - * key = entry[0]; - * - * val = entry[1]; - */ - if (key != null && (key.startsWith(j2sParameterPrefix) - || Arrays.asList(j2sSavedKeys).contains(key))) - { - j2sInfo.put(key, val); - - if (key.equals(j2sNamespaceKey)) - { - setJ2sNamespace(val); - } - } - /** - * @j2sNative } - * - * ); - * - * } - */ - } - - /** - * Namespace (if set) by JalviewJS - */ - private String j2sNamespace = null; - - private void setJ2sNamespace(String ns) - { - if (!Platform.isJS()) - { - return; - } - Console.outPrintln("j2sNamespace set to '" + ns + "'"); - j2sNamespace = ns; - } - - public String getJ2sNamespace() - { - return j2sNamespace; - } } diff --git a/src/jalview/bin/JalviewJS.java b/src/jalview/bin/JalviewJS.java new file mode 100755 index 0000000..5b4d5e9 --- /dev/null +++ b/src/jalview/bin/JalviewJS.java @@ -0,0 +1,74 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.bin; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.Timer; + +import jalview.util.JalviewJSUtil; +import jalview.util.Platform; + +public class JalviewJS +{ + static + { + JalviewJSUtil.getURLCommandArguments(); + Platform.addJ2SDirectDatabaseCall("https://www.jalview.org"); + Platform.addJ2SDirectDatabaseCall("http://www.jalview.org"); + Platform.addJ2SDirectDatabaseCall("http://www.compbio.dundee.ac.uk"); + Platform.addJ2SDirectDatabaseCall("https://www.compbio.dundee.ac.uk"); + } + + public static void main(String[] args) throws Exception + { + Jalview.main(args); + // showFocusTimer(); + } + + protected static int focusTime = 0; + + private static void showFocusTimer() + { + if (Platform.isJS()) + { + Timer t = new Timer(100, new ActionListener() + { + + @Override + public void actionPerformed(ActionEvent e) + { + String s = /** @j2sNative document.activeElement.id || */ + null; + + s += " " + (++focusTime); + + /** @j2sNative document.title = s; */ + } + + }); + + t.setRepeats(true); + t.start(); + } + } +} diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index ad989af..4770ebd 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -144,6 +144,7 @@ import jalview.util.ChannelProperties; import jalview.util.IdUtils; import jalview.util.IdUtils.IdType; import jalview.util.ImageMaker.TYPE; +import jalview.util.JalviewJSUtil; import jalview.util.LaunchUtils; import jalview.util.MessageManager; import jalview.util.Platform; @@ -656,7 +657,7 @@ public class Desktop extends jalview.jbgui.GDesktop if (Platform.isJS()) { - final String ns = Jalview.getInstance().getJ2sNamespace(); + final String ns = JalviewJSUtil.getJ2sNamespace(); if (ns != null) { final String jalviewjsDesktopElementId = "testApplet_LayeredPaneUI_10_8div"; @@ -666,11 +667,17 @@ public class Desktop extends jalview.jbgui.GDesktop final String splashClassActive = nsu + "jalviewjsSplashActive"; final String splashClassInactive = nsu + "jalviewjsSplashInactive"; final String splashClassHidden = nsu + "jalviewjsSplashHidden"; - final String j2s_overflow = Jalview.getInstance() + final String j2s_overflow = JalviewJSUtil .getJ2sInfoValue("overflow"); /** * @j2sNative // splash element disappearance * + * function sleep(ms) { + * + * return new Promise(resolve => setTimeout(resolve, ms)); + * + * } + * * var splashElement = document.getElementById(splashId); * * if (splashElement !== undefined) { @@ -679,12 +686,6 @@ public class Desktop extends jalview.jbgui.GDesktop * * splashElement.classList.add(splashClassInactive); * - * function sleep(ms) { - * - * return new Promise(resolve => setTimeout(resolve, ms)); - * - * } - * * async function hideSplash() { * * await sleep(5000); diff --git a/src/jalview/util/JalviewJSUtil.java b/src/jalview/util/JalviewJSUtil.java new file mode 100644 index 0000000..0b95cdb --- /dev/null +++ b/src/jalview/util/JalviewJSUtil.java @@ -0,0 +1,255 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.util; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import jalview.bin.Console; +import jalview.bin.argparser.ArgParser; + +public class JalviewJSUtil +{ + /** + * To be populated by Info={} hash set by instantiating JalviewJS + */ + private static Map j2sInfo = null; + + /** + * The non-j2s_-prefixed parameters to store + */ + private static final String[] j2sSavedKeys = { "main", "core", "width", + "height", "serverURL", "j2sPath", "console" }; + + /** + * The prefix for non-named keys in the Info={} block + */ + private static final String j2sParameterPrefix = "j2s_"; + + /** + * The Info key name for namespacing (query string parameters, class and id + * values) + */ + private static final String j2sNamespaceKey = j2sParameterPrefix + + "namespace"; + + /** + * Get a value passed in from the Info={} hash. key should NOT use the + * namespace prefix. + * + * @param key + * key name without the j2s_ namespace in the Info={} hash. + * @return value value from the Info={} hash. + */ + public static String getJ2sInfoValue(String key) + { + if (j2sInfo == null || key == null) + { + return null; + } + String j2sKey = Arrays.asList(j2sSavedKeys).contains(key) ? key + : j2sParameterPrefix + key; + return j2sInfo.get(j2sKey); + } + + /** + * Get and save parameters and values from the Info={} hash. + */ + public static void setJ2sInfo() + { + if (!Platform.isJS()) + { + return; + } + j2sInfo = new HashMap<>(); + String key = null; + String val = null; + /** + * @j2sNative if (J2S.thisApplet.__Info !== undefined) { + * + * Object.entries(J2S.thisApplet.__Info).forEach( entry => { + * + * key = entry[0]; + * + * val = entry[1]; + */ + if (key != null && (key.startsWith(j2sParameterPrefix) + || Arrays.asList(j2sSavedKeys).contains(key))) + { + j2sInfo.put(key, val); + + if (key.equals(j2sNamespaceKey)) + { + setJ2sNamespace(val); + } + } + /** + * @j2sNative } + * + * ); + * + * } + */ + } + + /** + * Namespace (if set) by JalviewJS + */ + private static String j2sNamespace = null; + + private static void setJ2sNamespace(String ns) + { + if (!Platform.isJS()) + { + return; + } + Console.outPrintln("j2sNamespace set to '" + ns + "'"); + j2sNamespace = ns; + } + + public static String getJ2sNamespace() + { + return j2sNamespace; + } + + public static void getURLCommandArguments() + { + + // setting ArgParser.ignoreNonStringValues allows non-string args to be + // set with, e.g., --wrap=hello + // which might be necessary for a querystring, plus we don't have access + // to Arg.hasOption(Opt.STRING) + if (Platform.isJS()) + { + ArgParser.setIgnoreNonStringValues(true); + try + { + + String ns = null; + + // extra spaces between lines of javascript to avoid eclipse comment + // munging into one line + + /** + * 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 element to this value. + * + * if a namespace has been given in Info={...}. Use this namespace to + * find arguments and values in the querystring parameters. Arguments + * that do not take a value do not need to have a value in the + * querystring. If they do they will be ignored. Note, this means you + * cannot do 'debug=false' instead of 'nodebug'. If querystringnamepsace + * is an empty string ("") then no colon (":") will be expected. + * + * if namespace is not defined then use the old style single first + * parameter for arguments + * + * @j2sNative var namespace = J2S.thisApplet.__Info.j2s_namespace; + * + * if (namespace === undefined) + * + * { + * + * System.out.println("No namespace given"); + * + * var a = + * decodeURI((document.location.href.replace("&","?").split("?j2s")[0] + * + "?").split("?")[1].split("#")[0]); + * + * a && (System.out.println("URL arguments detected were + * "+a)) && (J2S.thisApplet.__Info.urlargs = a.split(" ")); + * + * (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args + * == "" || J2S.thisApplet.__Info.args == "??") && + * (J2S.thisApplet.__Info.args = a) && + * (System.out.println("URL arguments were passed to J2S + * main.")); + * + * } + * + * else // namespace is defined + * + * { + * + * ns = ""; + * + * var nsc = ""; + * + * if (namespace) { + * + * ns = namespace; + * + * nsc = ns + ":"; + * + * } + * + * System.out.println("Querystring namespace is '" + nsc + + * "'"); + * + * var qsParams = new + * URLSearchParams(window.location.search); + * + * var qsargs = []; + * + * for (var param of qsParams) { + * + * var key = param[0]; + * + * var val = param[1]; + * + * if (key.startsWith(nsc)) { + * + * var arg = key.substring(nsc.length); + * + * qsargs.push("--" + arg + "=" + val); + * + * System.out.println("Setting arg '"+arg+"' to '"+val+"'"); + * + * } + * + * } + * + * qsargs && (System.out.println("URL parameters detected + * were "+qsargs.join(" "))) && + * (J2S.thisApplet.__Info.urlargs = qsargs); + * + * (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args + * == "" || J2S.thisApplet.__Info.args == "??") && + * (J2S.thisApplet.__Info.args = qsargs.join(" ")) && + * (System.out.println("URL parameters were passed to J2S + * main.")); + * + * } + */ + } catch (Throwable t) + { + /** + * @j2sNative System.err.println("Problem looking for arguments"); + * console.log("Problem looking for arguments"); + * console.log(t); + */ + } + } + } + +} diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 87e308e..daaed77 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -38,7 +38,6 @@ import javax.swing.SwingUtilities; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -import jalview.bin.argparser.ArgParser; import jalview.javascript.json.JSON; /** @@ -634,127 +633,6 @@ public class Platform } - public static void getURLCommandArguments() - { - - // setting ArgParser.ignoreNonStringValues allows non-string args to be - // set with, e.g., --wrap=hello - // which might be necessary for a querystring, plus we don't have access - // to Arg.hasOption(Opt.STRING) - if (Platform.isJS()) - { - ArgParser.setIgnoreNonStringValues(true); - try - { - - String ns = null; - - // extra spaces between lines of javascript to avoid eclipse comment - // munging into one line - - /** - * 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 element to this value. - * - * if a namespace has been given in Info={...}. Use this namespace to - * find arguments and values in the querystring parameters. Arguments - * that do not take a value do not need to have a value in the - * querystring. If they do they will be ignored. Note, this means you - * cannot do 'debug=false' instead of 'nodebug'. If querystringnamepsace - * is an empty string ("") then no colon (":") will be expected. - * - * if namespace is not defined then use the old style single first - * parameter for arguments - * - * @j2sNative var namespace = J2S.thisApplet.__Info.j2s_namespace; - * - * if (namespace === undefined) - * - * { - * - * System.out.println("No namespace given"); - * - * var a = - * decodeURI((document.location.href.replace("&","?").split("?j2s")[0] - * + "?").split("?")[1].split("#")[0]); - * - * a && (System.out.println("URL arguments detected were - * "+a)) && (J2S.thisApplet.__Info.urlargs = a.split(" ")); - * - * (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args - * == "" || J2S.thisApplet.__Info.args == "??") && - * (J2S.thisApplet.__Info.args = a) && - * (System.out.println("URL arguments were passed to J2S - * main.")); - * - * } - * - * else // namespace is defined - * - * { - * - * ns = ""; - * - * var nsc = ""; - * - * if (namespace) { - * - * ns = namespace; - * - * nsc = ns + ":"; - * - * } - * - * System.out.println("Querystring namespace is '" + nsc + - * "'"); - * - * var qsParams = new - * URLSearchParams(window.location.search); - * - * var qsargs = []; - * - * for (var param of qsParams) { - * - * var key = param[0]; - * - * var val = param[1]; - * - * if (key.startsWith(nsc)) { - * - * var arg = key.substring(nsc.length); - * - * qsargs.push("--" + arg + "=" + val); - * - * System.out.println("Setting arg '"+arg+"' to '"+val+"'"); - * - * } - * - * } - * - * qsargs && (System.out.println("URL parameters detected - * were "+qsargs.join(" "))) && - * (J2S.thisApplet.__Info.urlargs = qsargs); - * - * (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args - * == "" || J2S.thisApplet.__Info.args == "??") && - * (J2S.thisApplet.__Info.args = qsargs.join(" ")) && - * (System.out.println("URL parameters were passed to J2S - * main.")); - * - * } - */ - } catch (Throwable t) - { - /** - * @j2sNative System.err.println("Problem looking for arguments"); - * console.log("Problem looking for arguments"); - * console.log(t); - */ - } - } - } - /** * A (case sensitive) file path comparator that ignores the difference between * / and \ diff --git a/utils/jalviewjs/site-resources/jalview_bin_Jalview-corenamespace.html b/utils/jalviewjs/site-resources/jalview_bin_JalviewJS-corenamespace.html similarity index 98% rename from utils/jalviewjs/site-resources/jalview_bin_Jalview-corenamespace.html rename to utils/jalviewjs/site-resources/jalview_bin_JalviewJS-corenamespace.html index fbe7ad2..cb58e91 100644 --- a/utils/jalviewjs/site-resources/jalview_bin_Jalview-corenamespace.html +++ b/utils/jalviewjs/site-resources/jalview_bin_JalviewJS-corenamespace.html @@ -8,7 +8,7 @@ if (!self.SwingJS)alert('swingjs2.js was not found. It needs to be in swingjs folder in the same directory as ' + document.location.href) Info = { code: null, - main: "jalview.bin.JalviewJS2", + main: "jalview.bin.JalviewJS", core: "_jalview", width: 850, height: 550,