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 || */
41 private static Boolean isNoJSMac = null, isNoJSWin = null, isMac = null,
44 private static Boolean isHeadless = null;
47 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
51 public static boolean isMac()
54 ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0))
59 * added to group mouse events into Windows and nonWindows (mac, unix, linux)
63 public static boolean isWin()
66 ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0))
72 * @return true if HTML5 JavaScript
74 public static boolean isJS()
80 * sorry folks - Macs really are different
82 * BH: disabled for SwingJS -- will need to check key-press issues
84 * @return true if we do things in a special way.
86 public static boolean isAMacAndNotJS()
88 return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
92 * Check if we are on a Microsoft plaform...
94 * @return true if we have to cope with another platform variation
96 public static boolean isWindowsAndNotJS()
98 return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
103 * @return true if we are running in non-interactive no UI mode
105 public static boolean isHeadless()
107 if (isHeadless == null)
109 isHeadless = "true".equals(System.getProperty("java.awt.headless"));
116 * @return nominal maximum command line length for this platform
118 public static int getMaxCommandLineLength()
120 // TODO: determine nominal limits for most platforms.
121 return 2046; // this is the max length for a windows NT system.
125 * escape a string according to the local platform's escape character
128 * @return escaped file
130 public static String escapeString(String file)
132 StringBuffer f = new StringBuffer();
133 int p = 0, lastp = 0;
134 while ((p = file.indexOf('\\', lastp)) > -1)
136 f.append(file.subSequence(lastp, p));
140 f.append(file.substring(lastp));
145 * Answers true if the mouse event has Meta-down (Command key on Mac) or
146 * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
147 * pressed instead of the Meta/Cmd key on Mac. To test for Ctrl-pressed on
148 * Mac, you can use e.isPopupTrigger().
153 public static boolean isControlDown(MouseEvent e)
155 return isControlDown(e, isMac());
159 * Overloaded version of method (to allow unit testing)
165 protected static boolean isControlDown(MouseEvent e, boolean aMac)
169 return e.isControlDown();
171 // answer false for right mouse button
172 // shortcut key will be META for a Mac
173 return !e.isPopupTrigger()
174 && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
175 & e.getModifiers()) != 0;
176 // could we use e.isMetaDown() here?
179 // BH: I don't know about that previous method. Here is what SwingJS uses.
180 // Notice the distinction in mouse events. (BUTTON3_MASK == META)
182 // private static boolean isPopupTrigger(int id, int mods, boolean isWin) {
183 // boolean rt = ((mods & InputEvent.BUTTON3_MASK) != 0);
185 // if (id != MouseEvent.MOUSE_RELEASED)
188 //// // Oddly, Windows returns InputEvent.META_DOWN_MASK on release, though
189 //// // BUTTON3_DOWN_MASK for pressed. So here we just accept both.
191 //// actually, we can use XXX_MASK, not XXX_DOWN_MASK and avoid this issue,
193 //// J2S adds the appropriate extended (0x3FC0) and simple (0x3F) modifiers.
197 // // mac, linux, unix
198 // if (id != MouseEvent.MOUSE_PRESSED)
200 // boolean lt = ((mods & InputEvent.BUTTON1_MASK) != 0);
201 // boolean ctrl = ((mods & InputEvent.CTRL_MASK) != 0);
202 // return rt || (ctrl && lt);
208 * Windows (not Mac, Linux, or Unix) and right button to test for the
209 * right-mouse pressed event in Windows that would have opened a menu or a
215 public static boolean isWinRightButton(MouseEvent e)
217 // was !isAMac(), but that is true also for Linux and Unix and JS,
219 return isWin() && SwingUtilities.isRightMouseButton(e);
223 * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling
224 * without pressing the button.
229 public static boolean isWinMiddleButton(MouseEvent e)
231 // was !isAMac(), but that is true also for Linux and Unix and JS
232 return isWin() && SwingUtilities.isMiddleMouseButton(e);
235 public static boolean allowMnemonics()
240 public final static int TIME_RESET = 0;
242 public final static int TIME_MARK = 1;
244 public static final int TIME_SET = 2;
246 public static final int TIME_GET = 3;
248 public static long time, mark, set, duration;
250 public static void timeCheck(String msg, int mode)
252 long t = System.currentTimeMillis();
259 System.err.println("Platform: timer reset\t\t\t" + msg);
265 duration += (t - set);
275 System.err.println("Platform: timer mark\t" + ((t - time) / 1000f)
276 + "\t" + ((t - mark) / 1000f) + "\t" + msg);
287 System.err.println("Platform: timer dur\t" + ((t - time) / 1000f)
288 + "\t" + ((duration) / 1000f) + "\t" + msg);
295 public static void cacheFileData(String path, byte[] data)
304 * swingjs.JSUtil.cacheFileData$S$O(path, data);
309 public static byte[] getFileBytes(File f)
311 return /** @j2sNative f && f._bytes || */
315 public static byte[] getFileAsBytes(String fileStr)
317 // BH 2018 hack for no support for access-origin
318 return /** @j2sNative swingjs.JSUtil.getFileAsBytes$O(fileStr) || */
322 public static String getFileAsString(String data)
324 return /** @j2sNative swingjs.JSUtil.getFileAsString$S(data) || */
328 public static boolean setFileBytes(File f, String urlstring)
334 @SuppressWarnings("unused")
335 byte[] bytes = getFileAsBytes(urlstring);
337 * @j2sNative f._bytes = bytes;
342 public static void addJ2SBinaryType(String ext)
347 * J2S._binaryTypes.push("." + ext + "?");
352 public static String encodeURI(String value)
355 * @j2sNative return encodeURIComponent(value);
360 public static boolean openURL(String url)
375 public static String getUniqueAppletID()
377 @SuppressWarnings("unused")
378 ThreadGroup g = Thread.currentThread().getThreadGroup();
380 * @j2sNative return g.html5Applet._uniqueId;
388 * Read the Info block for this applet.
393 * @return unique id for this applet
395 public static void readInfoProperties(String prefix, Properties p)
397 @SuppressWarnings("unused")
398 ThreadGroup g = Thread.currentThread().getThreadGroup();
399 String id = getUniqueAppletID();
400 String key = "", value = "";
402 * @j2sNative var info = g.html5Applet.__Info || {}; for (var key in info) {
403 * if (key.indexOf(prefix) == 0) { value = "" + info[key];
406 p.put(id + "_" + key, value);