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;
26 import java.util.Properties;
28 import javax.swing.SwingUtilities;
31 * System platform information used by Applet and Application
38 private static boolean isJS = /** @j2sNative true || */false;
40 private static Boolean isNoJSMac = null, isNoJSWin = null,
41 isMac = null, isWin = null;
43 private static Boolean isHeadless = null;
46 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
49 public static boolean isMac()
51 return (isMac == null ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0)) : isMac);
55 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
58 public static boolean isWin()
60 return (isWin == null ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0)) : isWin);
65 * @return true if HTML5 JavaScript
67 public static boolean isJS()
73 * sorry folks - Macs really are different
75 * BH: disabled for SwingJS -- will need to check key-press issues
77 * @return true if we do things in a special way.
79 public static boolean isAMacAndNotJS()
81 return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
85 * Check if we are on a Microsoft plaform...
87 * @return true if we have to cope with another platform variation
89 public static boolean isWindowsAndNotJS()
91 return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
96 * @return true if we are running in non-interactive no UI mode
98 public static boolean isHeadless()
100 if (isHeadless == null)
102 isHeadless = "true".equals(System.getProperty("java.awt.headless"));
109 * @return nominal maximum command line length for this platform
111 public static int getMaxCommandLineLength()
113 // TODO: determine nominal limits for most platforms.
114 return 2046; // this is the max length for a windows NT system.
118 * escape a string according to the local platform's escape character
121 * @return escaped file
123 public static String escapeString(String file)
125 StringBuffer f = new StringBuffer();
126 int p = 0, lastp = 0;
127 while ((p = file.indexOf('\\', lastp)) > -1)
129 f.append(file.subSequence(lastp, p));
133 f.append(file.substring(lastp));
138 * Answers true if the mouse event has Meta-down (Command key on Mac) or
139 * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
140 * pressed instead of the Meta/Cmd key on Mac. To test for Ctrl-pressed on Mac,
141 * you can use e.isPopupTrigger().
146 public static boolean isControlDown(MouseEvent e)
148 return isControlDown(e, isMac());
152 * Overloaded version of method (to allow unit testing)
158 protected static boolean isControlDown(MouseEvent e, boolean aMac)
161 return e.isControlDown();
163 // answer false for right mouse button
164 // shortcut key will be META for a Mac
165 return !e.isPopupTrigger()
166 && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() & e.getModifiers()) != 0;
167 // could we use e.isMetaDown() here?
170 // BH: I don't know about that previous method. Here is what SwingJS uses.
171 // Notice the distinction in mouse events. (BUTTON3_MASK == META)
173 // private static boolean isPopupTrigger(int id, int mods, boolean isWin) {
174 // boolean rt = ((mods & InputEvent.BUTTON3_MASK) != 0);
176 // if (id != MouseEvent.MOUSE_RELEASED)
179 //// // Oddly, Windows returns InputEvent.META_DOWN_MASK on release, though
180 //// // BUTTON3_DOWN_MASK for pressed. So here we just accept both.
182 //// actually, we can use XXX_MASK, not XXX_DOWN_MASK and avoid this issue,
184 //// J2S adds the appropriate extended (0x3FC0) and simple (0x3F) modifiers.
188 // // mac, linux, unix
189 // if (id != MouseEvent.MOUSE_PRESSED)
191 // boolean lt = ((mods & InputEvent.BUTTON1_MASK) != 0);
192 // boolean ctrl = ((mods & InputEvent.CTRL_MASK) != 0);
193 // return rt || (ctrl && lt);
199 * Windows (not Mac, Linux, or Unix) and right button
200 * to test for the right-mouse pressed event in Windows
201 * that would have opened a menu or a Mac.
206 public static boolean isWinRightButton(MouseEvent e)
208 // was !isAMac(), but that is true also for Linux and Unix and JS,
210 return isWin() && SwingUtilities.isRightMouseButton(e);
215 * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling
216 * without pressing the button.
221 public static boolean isWinMiddleButton(MouseEvent e)
223 // was !isAMac(), but that is true also for Linux and Unix and JS
224 return isWin() && SwingUtilities.isMiddleMouseButton(e);
227 public static boolean allowMnemonics()
232 public final static int TIME_RESET = 0;
233 public final static int TIME_MARK = 1;
234 public static final int TIME_SET = 2;
235 public static final int TIME_GET = 3;
237 public static long time, mark, set, duration;
239 public static void timeCheck(String msg, int mode) {
240 long t = System.currentTimeMillis();
246 System.err.println("Platform: timer reset\t\t\t" + msg);
251 duration += (t - set);
259 System.err.println("Platform: timer mark\t" + ((t - time)/1000f) + "\t" + ((t - mark)/1000f) + "\t" + msg);
270 System.err.println("Platform: timer dur\t" + ((t - time)/1000f) + "\t" + ((duration)/1000f) + "\t" + msg);
277 public static void cacheFileData(String path, byte[] data)
285 * swingjs.JSUtil.cacheFileData$S$O(path, data);
291 public static byte[] getFileBytes(File f)
293 return /** @j2sNative f && f._bytes || */null;
296 public static byte[] getFileAsBytes(String fileStr)
298 // BH 2018 hack for no support for access-origin
299 return /** @j2sNative swingjs.JSUtil.getFileAsBytes$O(fileStr) || */ null;
302 public static String getFileAsString(String data)
304 return /** @j2sNative swingjs.JSUtil.getFileAsString$S(data) || */ null;
307 public static boolean setFileBytes(File f, String urlstring)
313 @SuppressWarnings("unused")
314 byte[] bytes = getFileAsBytes(urlstring);
322 public static void addJ2SBinaryType(String ext)
327 * J2S._binaryTypes.push("." + ext + "?");
332 public static String encodeURI(String value)
336 * return encodeURIComponent(value);
341 public static boolean openURL(String url)
356 public static String getUniqueAppletID() {
357 @SuppressWarnings("unused")
358 ThreadGroup g = Thread.currentThread().getThreadGroup();
360 * @j2sNative return g.html5Applet._uniqueId;
367 * Read the Info block for this applet.
369 * @param prefix "jalview_"
371 * @return unique id for this applet
373 public static void readInfoProperties(String prefix, Properties p)
375 @SuppressWarnings("unused")
376 ThreadGroup g = Thread.currentThread().getThreadGroup();
377 String id = getUniqueAppletID();
378 String key = "", value = "";
381 var info = g.html5Applet.__Info || {};
382 for (var key in info) {
383 if (key.indexOf(prefix) == 0) {
384 value = "" + info[key];
387 p.put(id + "_" + key, value);