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;
35 import java.net.MalformedURLException;
37 import java.util.Properties;
39 import javax.swing.SwingUtilities;
41 import org.json.simple.parser.JSONParser;
42 import org.json.simple.parser.ParseException;
45 * System platform information used by Applet and Application
52 private static boolean isJS = /** @j2sNative true || */
55 private static Boolean isNoJSMac = null, isNoJSWin = null, isMac = null,
58 // private static Boolean isHeadless = null;
61 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
65 public static boolean isMac()
68 ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0))
73 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
77 public static boolean isWin()
80 ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0))
86 * @return true if HTML5 JavaScript
88 public static boolean isJS()
94 * sorry folks - Macs really are different
96 * BH: disabled for SwingJS -- will need to check key-press issues
98 * @return true if we do things in a special way.
100 public static boolean isAMacAndNotJS()
102 return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
106 * Check if we are on a Microsoft plaform...
108 * @return true if we have to cope with another platform variation
110 public static boolean isWindowsAndNotJS()
112 return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
117 // * @return true if we are running in non-interactive no UI mode
119 // public static boolean isHeadless()
121 // if (isHeadless == null)
123 // isHeadless = "true".equals(System.getProperty("java.awt.headless"));
125 // return isHeadless;
130 * @return nominal maximum command line length for this platform
132 public static int getMaxCommandLineLength()
134 // TODO: determine nominal limits for most platforms.
135 return 2046; // this is the max length for a windows NT system.
139 * escape a string according to the local platform's escape character
142 * @return escaped file
144 public static String escapeString(String file)
146 StringBuffer f = new StringBuffer();
147 int p = 0, lastp = 0;
148 while ((p = file.indexOf('\\', lastp)) > -1)
150 f.append(file.subSequence(lastp, p));
154 f.append(file.substring(lastp));
159 * Answers true if the mouse event has Meta-down (Command key on Mac) or
160 * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
161 * pressed instead of the Meta/Cmd key on Mac. To test for Ctrl-pressed on
162 * Mac, you can use e.isPopupTrigger().
167 public static boolean isControlDown(MouseEvent e)
169 return isControlDown(e, isMac());
173 * Overloaded version of method (to allow unit testing)
179 protected static boolean isControlDown(MouseEvent e, boolean aMac)
183 return e.isControlDown();
185 // answer false for right mouse button
186 // shortcut key will be META for a Mac
187 return !e.isPopupTrigger()
188 && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
189 & e.getModifiers()) != 0;
190 // could we use e.isMetaDown() here?
193 // BH: I don't know about that previous method. Here is what SwingJS uses.
194 // Notice the distinction in mouse events. (BUTTON3_MASK == META)
196 // private static boolean isPopupTrigger(int id, int mods, boolean isWin) {
197 // boolean rt = ((mods & InputEvent.BUTTON3_MASK) != 0);
199 // if (id != MouseEvent.MOUSE_RELEASED)
202 //// // Oddly, Windows returns InputEvent.META_DOWN_MASK on release, though
203 //// // BUTTON3_DOWN_MASK for pressed. So here we just accept both.
205 //// actually, we can use XXX_MASK, not XXX_DOWN_MASK and avoid this issue,
207 //// J2S adds the appropriate extended (0x3FC0) and simple (0x3F) modifiers.
211 // // mac, linux, unix
212 // if (id != MouseEvent.MOUSE_PRESSED)
214 // boolean lt = ((mods & InputEvent.BUTTON1_MASK) != 0);
215 // boolean ctrl = ((mods & InputEvent.CTRL_MASK) != 0);
216 // return rt || (ctrl && lt);
222 * Windows (not Mac, Linux, or Unix) and right button to test for the
223 * right-mouse pressed event in Windows that would have opened a menu or a
229 public static boolean isWinRightButton(MouseEvent e)
231 // was !isAMac(), but that is true also for Linux and Unix and JS,
233 return isWin() && SwingUtilities.isRightMouseButton(e);
237 * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling
238 * without pressing the button.
243 public static boolean isWinMiddleButton(MouseEvent e)
245 // was !isAMac(), but that is true also for Linux and Unix and JS
246 return isWin() && SwingUtilities.isMiddleMouseButton(e);
249 public static boolean allowMnemonics()
254 public final static int TIME_RESET = 0;
256 public final static int TIME_MARK = 1;
258 public static final int TIME_SET = 2;
260 public static final int TIME_GET = 3;
262 public static long time, mark, set, duration;
264 public static void timeCheck(String msg, int mode)
266 long t = System.currentTimeMillis();
273 System.err.println("Platform: timer reset\t\t\t" + msg);
279 duration += (t - set);
289 System.err.println("Platform: timer mark\t" + ((t - time) / 1000f)
290 + "\t" + ((t - mark) / 1000f) + "\t" + msg);
301 System.err.println("Platform: timer dur\t" + ((t - time) / 1000f)
302 + "\t" + ((duration) / 1000f) + "\t" + msg);
309 public static void cacheFileData(String path, Object data)
311 if (!isJS() || data == null)
318 * swingjs.JSUtil.cacheFileData$S$O(path, data);
323 public static void cacheFileData(File file)
326 if (!isJS() || (data = Platform.getFileBytes(file)) == null)
330 cacheFileData(file.toString(), data);
333 public static byte[] getFileBytes(File f)
335 return /** @j2sNative f && f._bytes || */
339 public static byte[] getFileAsBytes(String fileStr)
342 // BH 2018 hack for no support for access-origin
344 * @j2sNative bytes = swingjs.JSUtil.getFileAsBytes$O(fileStr)
346 cacheFileData(fileStr, bytes);
350 @SuppressWarnings("unused")
351 public static String getFileAsString(String url)
357 * ret = swingjs.JSUtil.getFileAsString$S(url);
361 cacheFileData(url, ret);
365 public static boolean setFileBytes(File f, String urlstring)
371 @SuppressWarnings("unused")
372 byte[] bytes = getFileAsBytes(urlstring);
374 * @j2sNative f._bytes = bytes;
379 public static void addJ2SBinaryType(String ext)
384 * J2S._binaryTypes.push("." + ext + "?");
390 * Encode the URI using JavaScript encodeURIComponent
393 * @return encoded value
395 public static String encodeURI(String value)
398 * @j2sNative value = encodeURIComponent(value);
404 * Open the URL using a simple window call if this is JavaScript
407 * @return true if window has been opened
409 public static boolean openURL(String url) throws IOException
413 BrowserLauncher.openURL(url);
425 public static String getUniqueAppletID()
427 @SuppressWarnings("unused")
428 ThreadGroup g = Thread.currentThread().getThreadGroup();
430 * @j2sNative return g.html5Applet._uniqueId;
438 * Read the Info block for this applet.
443 * @return unique id for this applet
445 public static void readInfoProperties(String prefix, Properties p)
451 @SuppressWarnings("unused")
452 ThreadGroup g = Thread.currentThread().getThreadGroup();
453 String id = getUniqueAppletID();
454 String key = "", value = "";
456 * @j2sNative var info = g.html5Applet.__Info || {}; for (var key in info) {
457 * if (key.indexOf(prefix) == 0) { value = "" + info[key];
461 "Platform id=" + id + " reading Info." + key + " = " + value);
472 public static void setAjaxJSON(URL url)
480 public static Object parseJSON(InputStream response)
481 throws IOException, ParseException
485 return JSON.parse(response);
488 BufferedReader br = null;
491 br = new BufferedReader(new InputStreamReader(response, "UTF-8"));
492 return new JSONParser().parse(br);
500 } catch (IOException e)
508 public static Object parseJSON(String json) throws ParseException
510 return (isJS() ? JSON.parse(json)
511 : new JSONParser().parse(json));
514 public static Object parseJSON(Reader r)
515 throws IOException, ParseException
524 return new JSONParser().parse(r);
526 // Using a file reader is not currently supported in SwingJS JavaScript
528 if (r instanceof FileReader)
530 throw new IOException(
531 "StringJS does not support FileReader parsing for JSON -- but it could...");
533 return JSON.parse(r);
538 * Dump the input stream to an output file.
542 * @throws IOException
543 * if the file cannot be created or there is a problem reading the
546 public static void streamToFile(InputStream is, File outFile)
549 FileOutputStream fio = new FileOutputStream(outFile);
554 * @j2sNative outFile.setBytes$O && outFile.setBytes$O(is) &&
560 byte[] bb = new byte[32 * 1024];
562 while ((l = is.read(bb)) > 0)
573 * Add a known domain that implements access-control-allow-origin:*
575 * These should be reviewed periodically.
578 * for a service that is not allowing ajax
580 * @author hansonr@stolaf.edu
583 public static void addJ2SDirectDatabaseCall(String domain)
589 "Platform adding known access-control-allow-origin * for domain "
594 * J2S.addDirectDatabaseCall(domain);
600 public static void getURLCommandArguments()
604 * Retrieve the first query field as command arguments to Jalview. Include
605 * only if prior to "?j2s" or "&j2s" or "#". Assign the applet's __Info.args
606 * element to this value.
609 * decodeURI((document.location.href.replace("&","?").split("?j2s")[0]
610 * + "?").split("?")[1].split("#")[0]); a &&
611 * (J2S.thisApplet.__Info.args = a.split(" "));
616 public static URL getDocumentBase()
620 return (isJS() ? new URL(/**
621 * @j2sNative J2S.thisApplet._applet.appletViewer.appletDocumentBase
625 } catch (MalformedURLException e)
631 public static URL getCodeBase()
635 return (isJS() ? new URL(/**
636 * @j2sNative J2S.thisApplet._applet.appletViewer.appletCodeBase
640 } catch (MalformedURLException e)