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 java.awt.Toolkit;
24 import java.awt.event.MouseEvent;
25 import java.io.BufferedReader;
27 import java.io.FileOutputStream;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.Reader;
33 import java.net.MalformedURLException;
35 import java.util.Properties;
37 import javax.swing.SwingUtilities;
39 import org.json.simple.parser.JSONParser;
40 import org.json.simple.parser.ParseException;
42 import jalview.bin.Console;
43 import jalview.javascript.json.JSON;
46 * System platform information used by Applet and Application
53 private static boolean isJS = /** @j2sNative true || */
56 private static Boolean isNoJSMac = null, isNoJSWin = null, isMac = null,
57 isWin = null, isLinux = null;
59 private static Boolean isHeadless = null;
61 // If launched from CLI with launcher script then -DCOLUMNWIDTH is set
62 private static final int CONSOLEWIDTH;
64 private static final String CONSOLEWIDTHPROPERTY = "CONSOLEWIDTH";
69 if (System.getProperty(CONSOLEWIDTHPROPERTY) != null
70 && System.getProperty(CONSOLEWIDTHPROPERTY).length() > 0)
74 cw = Integer.parseInt(System.getProperty(CONSOLEWIDTHPROPERTY));
75 } catch (NumberFormatException e)
83 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
87 public static boolean isMac()
90 ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0))
95 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
99 public static boolean isWin()
101 return (isWin == null
102 ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0))
107 * added to check LaF for Linux
111 public static boolean isLinux()
113 return (isLinux == null
114 ? (isLinux = (System.getProperty("os.name")
115 .indexOf("Linux") >= 0))
121 * @return true if HTML5 JavaScript
123 public static boolean isJS()
129 * sorry folks - Macs really are different
131 * BH: disabled for SwingJS -- will need to check key-press issues
133 * @return true if we do things in a special way.
135 public static boolean isAMacAndNotJS()
137 return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
141 * Check if we are on a Microsoft plaform...
143 * @return true if we have to cope with another platform variation
145 public static boolean isWindowsAndNotJS()
147 return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
152 * @return true if we are running in non-interactive no UI mode
154 public static boolean isHeadless()
156 if (isHeadless == null)
158 isHeadless = "true".equals(System.getProperty("java.awt.headless"));
165 * @return nominal maximum command line length for this platform
167 public static int getMaxCommandLineLength()
169 // TODO: determine nominal limits for most platforms.
170 return 2046; // this is the max length for a windows NT system.
174 * Answers the input with every backslash replaced with a double backslash (an
175 * 'escaped' single backslash)
180 public static String escapeBackslashes(String s)
182 return s == null ? null : s.replace("\\", "\\\\");
186 * Answers true if the mouse event has Meta-down (Command key on Mac) or
187 * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
188 * pressed instead of the Meta/Cmd key on Mac. To test for Ctrl-pressed on
189 * Mac, you can use e.isPopupTrigger().
194 public static boolean isControlDown(MouseEvent e)
196 return isControlDown(e, isMac());
200 * Overloaded version of method (to allow unit testing)
206 protected static boolean isControlDown(MouseEvent e, boolean aMac)
210 return e.isControlDown();
212 // Jalview 2.11 code below: above is as amended for JalviewJS
214 // * answer false for right mouse button
216 // if (e.isPopupTrigger())
221 // (jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx() //
222 // .getMenuShortcutKeyMaskEx()
223 // & jalview.util.ShortcutKeyMaskExWrapper
224 // .getModifiersEx(e)) != 0; // getModifiers()) != 0;
226 // answer false for right mouse button
227 // shortcut key will be META for a Mac
228 return !e.isPopupTrigger()
229 && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
230 & e.getModifiers()) != 0;
231 // could we use e.isMetaDown() here?
234 // BH: I don't know about that previous method. Here is what SwingJS uses.
235 // Notice the distinction in mouse events. (BUTTON3_MASK == META)
237 // private static boolean isPopupTrigger(int id, int mods, boolean isWin) {
238 // boolean rt = ((mods & InputEvent.BUTTON3_MASK) != 0);
240 // if (id != MouseEvent.MOUSE_RELEASED)
243 //// // Oddly, Windows returns InputEvent.META_DOWN_MASK on release, though
244 //// // BUTTON3_DOWN_MASK for pressed. So here we just accept both.
246 //// actually, we can use XXX_MASK, not XXX_DOWN_MASK and avoid this issue,
248 //// J2S adds the appropriate extended (0x3FC0) and simple (0x3F) modifiers.
252 // // mac, linux, unix
253 // if (id != MouseEvent.MOUSE_PRESSED)
255 // boolean lt = ((mods & InputEvent.BUTTON1_MASK) != 0);
256 // boolean ctrl = ((mods & InputEvent.CTRL_MASK) != 0);
257 // return rt || (ctrl && lt);
263 * Windows (not Mac, Linux, or Unix) and right button to test for the
264 * right-mouse pressed event in Windows that would have opened a menu or a
270 public static boolean isWinRightButton(MouseEvent e)
272 // was !isAMac(), but that is true also for Linux and Unix and JS,
274 return isWin() && SwingUtilities.isRightMouseButton(e);
278 * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling
279 * without pressing the button.
284 public static boolean isWinMiddleButton(MouseEvent e)
286 // was !isAMac(), but that is true also for Linux and Unix and JS
287 return isWin() && SwingUtilities.isMiddleMouseButton(e);
290 public static boolean allowMnemonics()
295 public final static int TIME_RESET = 0;
297 public final static int TIME_MARK = 1;
299 public static final int TIME_SET = 2;
301 public static final int TIME_GET = 3;
303 public static long time, mark, set, duration;
305 public static void timeCheck(String msg, int mode)
307 long t = System.currentTimeMillis();
314 Console.errPrintln("Platform: timer reset\t\t\t" + msg);
320 duration += (t - set);
330 Console.errPrintln("Platform: timer mark\t" + ((t - time) / 1000f)
331 + "\t" + ((t - mark) / 1000f) + "\t" + msg);
342 Console.errPrintln("Platform: timer dur\t" + ((t - time) / 1000f)
343 + "\t" + ((duration) / 1000f) + "\t" + msg);
350 public static void cacheFileData(String path, Object data)
352 if (!isJS() || data == null)
359 * swingjs.JSUtil.cacheFileData$S$O(path, data);
364 public static void cacheFileData(File file)
367 if (!isJS() || (data = Platform.getFileBytes(file)) == null)
371 cacheFileData(file.toString(), data);
374 public static byte[] getFileBytes(File f)
376 return /** @j2sNative f && swingjs.JSUtil.getFileAsBytes$O(f) || */
380 public static byte[] getFileAsBytes(String fileStr)
383 // BH 2018 hack for no support for access-origin
385 * @j2sNative bytes = swingjs.JSUtil.getFileAsBytes$O(fileStr)
387 cacheFileData(fileStr, bytes);
391 @SuppressWarnings("unused")
392 public static String getFileAsString(String url)
398 * ret = swingjs.JSUtil.getFileAsString$S(url);
402 cacheFileData(url, ret);
406 public static boolean setFileBytes(File f, String urlstring)
412 @SuppressWarnings("unused")
413 byte[] bytes = getFileAsBytes(urlstring);
414 // TODO temporary doubling of 秘bytes and _bytes;
415 // just remove _bytes when new transpiler has been installed
417 * @j2sNative f.\u79d8bytes = f._bytes = bytes;
422 public static void addJ2SBinaryType(String ext)
427 * J2S._binaryTypes.push("." + ext + "?");
433 * Encode the URI using JavaScript encodeURIComponent
436 * @return encoded value
438 public static String encodeURI(String value)
441 * @j2sNative value = encodeURIComponent(value);
447 * Open the URL using a simple window call if this is JavaScript
450 * @return true if window has been opened
452 public static boolean openURL(String url)
467 public static String getUniqueAppletID()
470 * @j2sNative return swingjs.JSUtil.getApplet$()._uniqueId;
478 * Read the Info block for this applet.
483 * @return unique id for this applet
485 public static void readInfoProperties(String prefix, Properties p)
491 String id = getUniqueAppletID();
492 String key = "", value = "";
494 * @j2sNative var info = swingjs.JSUtil.getApplet$().__Info || {}; for (var
495 * key in info) { if (key.indexOf(prefix) == 0) { value = "" +
500 "Platform id=" + id + " reading Info." + key + " = " + value);
501 p.put(id + "_" + key, value);
511 public static void setAjaxJSON(URL url)
519 public static Object parseJSON(InputStream response)
520 throws IOException, ParseException
524 return JSON.parse(response);
527 BufferedReader br = null;
530 br = new BufferedReader(new InputStreamReader(response, "UTF-8"));
531 return new JSONParser().parse(br);
539 } catch (IOException e)
547 public static Object parseJSON(String json) throws ParseException
549 return (isJS() ? JSON.parse(json) : new JSONParser().parse(json));
552 public static Object parseJSON(Reader r)
553 throws IOException, ParseException
562 return new JSONParser().parse(r);
564 // Using a file reader is not currently supported in SwingJS JavaScript
566 if (r instanceof FileReader)
568 throw new IOException(
569 "StringJS does not support FileReader parsing for JSON -- but it could...");
571 return JSON.parse(r);
576 * Dump the input stream to an output file.
580 * @throws IOException
581 * if the file cannot be created or there is a problem reading the
584 public static void streamToFile(InputStream is, File outFile)
588 * @j2sNative outFile.setBytes$O && outFile.setBytes$O(is) &&
594 FileOutputStream fio = new FileOutputStream(outFile);
597 byte[] bb = new byte[32 * 1024];
599 while ((l = is.read(bb)) > 0)
610 * Add a known domain that implements access-control-allow-origin:*
612 * These should be reviewed periodically.
615 * for a service that is not allowing ajax
617 * @author hansonr@stolaf.edu
620 public static void addJ2SDirectDatabaseCall(String urlString)
625 String domainString = urlString;
628 URL url = new URL(urlString);
629 URL domain = new URL(url.getProtocol(), url.getHost(),
631 domainString = domain.toString();
632 } catch (MalformedURLException e)
634 Console.warn("Could not add '" + urlString
635 + "' in J2SDirectDatabase -- not a recognised URL");
638 "Platform adding known access-control-allow-origin * for domain "
643 * J2S.addDirectDatabaseCall(domain);
650 * A (case sensitive) file path comparator that ignores the difference between
657 public static boolean pathEquals(String path1, String path2)
661 return path2 == null;
667 String p1 = path1.replace('\\', '/');
668 String p2 = path2.replace('\\', '/');
669 return p1.equals(p2);
673 * If started on command line using launch script, return the console width
675 public static int consoleWidth()