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?
171 * Windows (not Mac, Linux, or Unix) and right button
172 * to test for the right-mouse pressed event in Windows
173 * that would have opened a menu or a Mac.
178 public static boolean isWinRightButton(MouseEvent e)
180 // was !isAMac(), but that is true also for Linux and Unix and JS,
182 return isWin() && SwingUtilities.isRightMouseButton(e);
187 * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling
188 * without pressing the button.
193 public static boolean isWinMiddleButton(MouseEvent e)
195 // was !isAMac(), but that is true also for Linux and Unix and JS
196 return isWin() && SwingUtilities.isMiddleMouseButton(e);
199 public static boolean allowMnemonics()
204 public final static int TIME_RESET = 0;
205 public final static int TIME_MARK = 1;
206 public static final int TIME_SET = 2;
207 public static final int TIME_GET = 3;
209 public static long time, mark, set, duration;
211 public static void timeCheck(String msg, int mode) {
212 long t = System.currentTimeMillis();
217 System.err.println("Platform: timer reset\t\t\t" + msg);
221 duration += (t - set);
226 System.err.println("Platform: timer mark\t" + ((t - time)/1000f) + "\t" + ((t - mark)/1000f) + "\t" + msg);
235 System.err.println("Platform: timer dur\t" + ((t - time)/1000f) + "\t" + ((duration)/1000f) + "\t" + msg);
241 public static void cacheFileData(String path, byte[] data)
248 * swingjs.JSUtil.cacheFileData$S$O(path, data);
253 public static byte[] getFileBytes(File f)
255 return /** @j2sNative f && f._bytes || */null;
258 public static byte[] getFileAsBytes(String fileStr)
260 // BH 2018 hack for no support for access-origin
261 return /** @j2sNative swingjs.JSUtil.getFileAsBytes$O(fileStr) || */ null;
264 public static String getFileAsString(String data)
266 return /** @j2sNative swingjs.JSUtil.getFileAsString$S(data) || */ null;
269 public static boolean setFileBytes(File f, String urlstring)
273 @SuppressWarnings("unused")
274 byte[] bytes = getFileAsBytes(urlstring);
282 public static void addJ2SBinaryType(String ext)
287 * J2S._binaryTypes.push("." + ext + "?");
292 public static String encodeURI(String value)
296 * return encodeURIComponent(value);
301 public static boolean openURL(String url)
314 public static String getUniqueAppletID() {
315 @SuppressWarnings("unused")
316 ThreadGroup g = Thread.currentThread().getThreadGroup();
318 * @j2sNative return g.html5Applet._uniqueId;
325 * Read the Info block for this applet.
327 * @param prefix "jalview_"
329 * @return unique id for this applet
331 public static void readInfoProperties(String prefix, Properties p)
333 @SuppressWarnings("unused")
334 ThreadGroup g = Thread.currentThread().getThreadGroup();
335 String id = getUniqueAppletID();
336 String key = "", value = "";
339 var info = g.html5Applet.__Info || {};
340 for (var key in info) {
341 if (key.indexOf(prefix) == 0) {
342 value = "" + info[key];
345 p.put(id + "_" + key, value);