2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.javascript.json.JSON;
25 import java.awt.Toolkit;
26 import java.awt.event.MouseEvent;
27 import java.io.BufferedReader;
29 import java.io.FileOutputStream;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.io.Reader;
36 import java.util.Properties;
38 import javax.swing.SwingUtilities;
40 import org.json.simple.parser.JSONParser;
41 import org.json.simple.parser.ParseException;
44 * System platform information used by Applet and Application
51 private static boolean isJS = /** @j2sNative true || */
54 private static Boolean isNoJSMac = null, isNoJSWin = null, isMac = null,
55 isWin = null, isLinux = null;
57 private static Boolean isHeadless = null;
60 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
64 public static boolean isMac()
67 ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0))
72 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
76 public static boolean isWin()
79 ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0))
84 * added to check LaF for Linux
88 public static boolean isLinux()
90 return (isLinux == null
91 ? (isLinux = (System.getProperty("os.name").indexOf("Linux") >= 0))
97 * @return true if HTML5 JavaScript
99 public static boolean isJS()
105 * sorry folks - Macs really are different
107 * BH: disabled for SwingJS -- will need to check key-press issues
109 * @return true if we do things in a special way.
111 public static boolean isAMacAndNotJS()
113 return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
117 * Check if we are on a Microsoft plaform...
119 * @return true if we have to cope with another platform variation
121 public static boolean isWindowsAndNotJS()
123 return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
128 * @return true if we are running in non-interactive no UI mode
130 public static boolean isHeadless()
132 if (isHeadless == null)
134 isHeadless = "true".equals(System.getProperty("java.awt.headless"));
141 * @return nominal maximum command line length for this platform
143 public static int getMaxCommandLineLength()
145 // TODO: determine nominal limits for most platforms.
146 return 2046; // this is the max length for a windows NT system.
150 * Answers the input with every backslash replaced with a double backslash (an
151 * 'escaped' single backslash)
156 public static String escapeBackslashes(String s)
158 return s == null ? null : s.replace("\\", "\\\\");
162 * Answers true if the mouse event has Meta-down (Command key on Mac) or
163 * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
164 * pressed instead of the Meta/Cmd key on Mac. To test for Ctrl-pressed on
165 * Mac, you can use e.isPopupTrigger().
170 public static boolean isControlDown(MouseEvent e)
172 return isControlDown(e, isMac());
176 * Overloaded version of method (to allow unit testing)
182 protected static boolean isControlDown(MouseEvent e, boolean aMac)
186 return e.isControlDown();
188 // Jalview 2.11 code below: above is as amended for JalviewJS
190 // * answer false for right mouse button
192 // if (e.isPopupTrigger())
197 // (jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx() //
198 // .getMenuShortcutKeyMaskEx()
199 // & jalview.util.ShortcutKeyMaskExWrapper
200 // .getModifiersEx(e)) != 0; // getModifiers()) != 0;
202 // answer false for right mouse button
203 // shortcut key will be META for a Mac
204 return !e.isPopupTrigger()
205 && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
206 & e.getModifiers()) != 0;
207 // could we use e.isMetaDown() here?
210 // BH: I don't know about that previous method. Here is what SwingJS uses.
211 // Notice the distinction in mouse events. (BUTTON3_MASK == META)
213 // private static boolean isPopupTrigger(int id, int mods, boolean isWin) {
214 // boolean rt = ((mods & InputEvent.BUTTON3_MASK) != 0);
216 // if (id != MouseEvent.MOUSE_RELEASED)
219 //// // Oddly, Windows returns InputEvent.META_DOWN_MASK on release, though
220 //// // BUTTON3_DOWN_MASK for pressed. So here we just accept both.
222 //// actually, we can use XXX_MASK, not XXX_DOWN_MASK and avoid this issue,
224 //// J2S adds the appropriate extended (0x3FC0) and simple (0x3F) modifiers.
228 // // mac, linux, unix
229 // if (id != MouseEvent.MOUSE_PRESSED)
231 // boolean lt = ((mods & InputEvent.BUTTON1_MASK) != 0);
232 // boolean ctrl = ((mods & InputEvent.CTRL_MASK) != 0);
233 // return rt || (ctrl && lt);
239 * Windows (not Mac, Linux, or Unix) and right button to test for the
240 * right-mouse pressed event in Windows that would have opened a menu or a
246 public static boolean isWinRightButton(MouseEvent e)
248 // was !isAMac(), but that is true also for Linux and Unix and JS,
250 return isWin() && SwingUtilities.isRightMouseButton(e);
254 * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling
255 * without pressing the button.
260 public static boolean isWinMiddleButton(MouseEvent e)
262 // was !isAMac(), but that is true also for Linux and Unix and JS
263 return isWin() && SwingUtilities.isMiddleMouseButton(e);
266 public static boolean allowMnemonics()
271 public final static int TIME_RESET = 0;
273 public final static int TIME_MARK = 1;
275 public static final int TIME_SET = 2;
277 public static final int TIME_GET = 3;
279 public static long time, mark, set, duration;
281 public static void timeCheck(String msg, int mode)
283 long t = System.currentTimeMillis();
290 System.err.println("Platform: timer reset\t\t\t" + msg);
296 duration += (t - set);
306 System.err.println("Platform: timer mark\t" + ((t - time) / 1000f)
307 + "\t" + ((t - mark) / 1000f) + "\t" + msg);
318 System.err.println("Platform: timer dur\t" + ((t - time) / 1000f)
319 + "\t" + ((duration) / 1000f) + "\t" + msg);
326 public static void cacheFileData(String path, Object data)
328 if (!isJS() || data == null)
335 * swingjs.JSUtil.cacheFileData$S$O(path, data);
340 public static void cacheFileData(File file)
343 if (!isJS() || (data = Platform.getFileBytes(file)) == null)
347 cacheFileData(file.toString(), data);
350 public static byte[] getFileBytes(File f)
352 return /** @j2sNative f && swingjs.JSUtil.getFileBytes$java_io_File(f) || */
356 public static byte[] getFileAsBytes(String fileStr)
359 // BH 2018 hack for no support for access-origin
361 * @j2sNative bytes = swingjs.JSUtil.getFileAsBytes$O(fileStr)
363 cacheFileData(fileStr, bytes);
367 @SuppressWarnings("unused")
368 public static String getFileAsString(String url)
374 * ret = swingjs.JSUtil.getFileAsString$S(url);
378 cacheFileData(url, ret);
382 public static boolean setFileBytes(File f, String urlstring)
388 @SuppressWarnings("unused")
389 byte[] bytes = getFileAsBytes(urlstring);
390 // TODO temporary doubling of 秘bytes and _bytes;
391 // just remove _bytes when new transpiler has been installed
393 * @j2sNative f.\u79d8bytes = f._bytes = bytes;
398 public static void addJ2SBinaryType(String ext)
403 * J2S._binaryTypes.push("." + ext + "?");
409 * Encode the URI using JavaScript encodeURIComponent
412 * @return encoded value
414 public static String encodeURI(String value)
417 * @j2sNative value = encodeURIComponent(value);
423 * Open the URL using a simple window call if this is JavaScript
426 * @return true if window has been opened
428 public static boolean openURL(String url)
443 public static String getUniqueAppletID()
446 * @j2sNative return swingjs.JSUtil.getApplet$()._uniqueId;
454 * Read the Info block for this applet.
459 * @return unique id for this applet
461 public static void readInfoProperties(String prefix, Properties p)
467 String id = getUniqueAppletID();
468 String key = "", value = "";
470 * @j2sNative var info = swingjs.JSUtil.getApplet$().__Info || {}; for (var
471 * key in info) { if (key.indexOf(prefix) == 0) { value = "" +
476 "Platform id=" + id + " reading Info." + key + " = " + value);
477 p.put(id + "_" + key, value);
487 public static void setAjaxJSON(URL url)
495 public static Object parseJSON(InputStream response)
496 throws IOException, ParseException
500 return JSON.parse(response);
503 BufferedReader br = null;
506 br = new BufferedReader(new InputStreamReader(response, "UTF-8"));
507 return new JSONParser().parse(br);
515 } catch (IOException e)
523 public static Object parseJSON(String json) throws ParseException
525 return (isJS() ? JSON.parse(json)
526 : new JSONParser().parse(json));
529 public static Object parseJSON(Reader r)
530 throws IOException, ParseException
539 return new JSONParser().parse(r);
541 // Using a file reader is not currently supported in SwingJS JavaScript
543 if (r instanceof FileReader)
545 throw new IOException(
546 "StringJS does not support FileReader parsing for JSON -- but it could...");
548 return JSON.parse(r);
553 * Dump the input stream to an output file.
557 * @throws IOException
558 * if the file cannot be created or there is a problem
559 * reading the input stream.
561 public static void streamToFile(InputStream is, File outFile)
565 * @j2sNative outFile.setBytes$O && outFile.setBytes$O(is) &&
571 FileOutputStream fio = new FileOutputStream(outFile);
574 byte[] bb = new byte[32 * 1024];
576 while ((l = is.read(bb)) > 0)
587 * Add a known domain that implements access-control-allow-origin:*
589 * These should be reviewed periodically.
592 * for a service that is not allowing ajax
594 * @author hansonr@stolaf.edu
597 public static void addJ2SDirectDatabaseCall(String domain)
603 "Platform adding known access-control-allow-origin * for domain "
608 * J2S.addDirectDatabaseCall(domain);
614 public static void getURLCommandArguments()
618 * Retrieve the first query field as command arguments to Jalview. Include
619 * only if prior to "?j2s" or "&j2s" or "#". Assign the applet's __Info.args
620 * element to this value.
623 * decodeURI((document.location.href.replace("&","?").split("?j2s")[0]
624 * + "?").split("?")[1].split("#")[0]); a &&
625 * (J2S.thisApplet.__Info.args = a.split(" "));
631 * A (case sensitive) file path comparator that ignores the difference between /
638 public static boolean pathEquals(String path1, String path2)
642 return path2 == null;
648 String p1 = path1.replace('\\', '/');
649 String p2 = path2.replace('\\', '/');
650 return p1.equals(p2);