Platform comment about isPopupTrigger
[jalview.git] / src / jalview / util / Platform.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.util;
22
23 import java.awt.Toolkit;
24 import java.awt.event.MouseEvent;
25 import java.io.File;
26 import java.util.Properties;
27
28 import javax.swing.SwingUtilities;
29
30 /**
31  * System platform information used by Applet and Application
32  * 
33  * @author Jim Procter
34  */
35 public class Platform
36 {
37
38   private static boolean isJS = /** @j2sNative true || */false;
39
40   private static Boolean isNoJSMac = null, isNoJSWin = null, 
41                   isMac = null, isWin = null;
42   
43   private static Boolean isHeadless = null;
44
45   /**
46    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
47    * @return
48    */
49   public static boolean isMac()
50   {
51           return (isMac == null ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0)) : isMac);
52   }
53
54   /**
55    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
56    * @return
57    */
58   public static boolean isWin() 
59   {
60           return (isWin == null ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0)) : isWin);
61   }
62
63   /**
64    * 
65    * @return true if HTML5 JavaScript
66    */
67   public static boolean isJS()
68   {
69         return isJS;
70   }
71
72   /**
73    * sorry folks - Macs really are different
74    * 
75    * BH: disabled for SwingJS -- will need to check key-press issues
76    * 
77    * @return true if we do things in a special way.
78    */
79   public static boolean isAMacAndNotJS()
80   {
81         return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
82   }
83
84 /**
85    * Check if we are on a Microsoft plaform...
86    * 
87    * @return true if we have to cope with another platform variation
88    */
89   public static boolean isWindowsAndNotJS()
90   {
91         return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
92    }
93
94   /**
95    * 
96    * @return true if we are running in non-interactive no UI mode
97    */
98   public static boolean isHeadless()
99   {
100     if (isHeadless == null)
101     {
102       isHeadless = "true".equals(System.getProperty("java.awt.headless"));
103     }
104     return isHeadless;
105   }
106
107   /**
108    * 
109    * @return nominal maximum command line length for this platform
110    */
111   public static int getMaxCommandLineLength()
112   {
113     // TODO: determine nominal limits for most platforms.
114     return 2046; // this is the max length for a windows NT system.
115   }
116
117   /**
118    * escape a string according to the local platform's escape character
119    * 
120    * @param file
121    * @return escaped file
122    */
123   public static String escapeString(String file)
124   {
125     StringBuffer f = new StringBuffer();
126     int p = 0, lastp = 0;
127     while ((p = file.indexOf('\\', lastp)) > -1)
128     {
129       f.append(file.subSequence(lastp, p));
130       f.append("\\\\");
131       lastp = p + 1;
132     }
133     f.append(file.substring(lastp));
134     return f.toString();
135   }
136
137   /**
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().
142    * 
143    * @param e
144    * @return
145    */
146   public static boolean isControlDown(MouseEvent e)
147   {
148     return isControlDown(e, isMac());
149   }
150
151   /**
152    * Overloaded version of method (to allow unit testing)
153    * 
154    * @param e
155    * @param aMac
156    * @return
157    */
158   protected static boolean isControlDown(MouseEvent e, boolean aMac)
159   {
160     if (!aMac) {
161         return e.isControlDown();       
162     }
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?
168   }
169
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)
172   //
173   // private static boolean isPopupTrigger(int id, int mods, boolean isWin) {
174   // boolean rt = ((mods & InputEvent.BUTTON3_MASK) != 0);
175   // if (isWin) {
176   // if (id != MouseEvent.MOUSE_RELEASED)
177   // return false;
178   ////
179   //// // Oddly, Windows returns InputEvent.META_DOWN_MASK on release, though
180   //// // BUTTON3_DOWN_MASK for pressed. So here we just accept both.
181   ////
182   //// actually, we can use XXX_MASK, not XXX_DOWN_MASK and avoid this issue,
183   // because
184   //// J2S adds the appropriate extended (0x3FC0) and simple (0x3F) modifiers.
185   ////
186   // return rt;
187   // } else {
188   // // mac, linux, unix
189   // if (id != MouseEvent.MOUSE_PRESSED)
190   // return false;
191   // boolean lt = ((mods & InputEvent.BUTTON1_MASK) != 0);
192   // boolean ctrl = ((mods & InputEvent.CTRL_MASK) != 0);
193   // return rt || (ctrl && lt);
194   // }
195   // }
196   //
197
198   /**
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. 
202    * 
203    * @param e
204    * @return
205    */
206   public static boolean isWinRightButton(MouseEvent e) 
207   {
208           // was !isAMac(), but that is true also for Linux and Unix and JS, 
209
210           return isWin() && SwingUtilities.isRightMouseButton(e);
211   }
212   
213   
214   /**
215    * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling 
216    * without pressing the button.
217    * 
218    * @param e
219    * @return
220    */
221   public static boolean isWinMiddleButton(MouseEvent e) 
222   {
223         // was !isAMac(), but that is true also for Linux and Unix and JS
224           return isWin() && SwingUtilities.isMiddleMouseButton(e);
225   }
226
227   public static boolean allowMnemonics() 
228   {
229         return !isMac();
230   }
231   
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;
236   
237   public static long time, mark, set, duration;
238   
239   public static void timeCheck(String msg, int mode) {
240           long t = System.currentTimeMillis();
241           switch (mode) {
242           case TIME_RESET:
243                   time = mark = t;
244                   if (msg != null)
245       {
246         System.err.println("Platform: timer reset\t\t\t" + msg);
247       }
248                   break;
249           case TIME_MARK:
250                   if (set > 0) {
251                           duration += (t - set);
252                   } else {
253                   if (time == 0)
254       {
255         time = mark = t;
256       }
257                           if (msg != null)
258         {
259           System.err.println("Platform: timer mark\t" + ((t - time)/1000f) + "\t" + ((t - mark)/1000f) + "\t" + msg);
260         }
261                   mark = t;
262                   }
263                   break;
264           case TIME_SET:
265                   set = t;
266                   break;
267           case TIME_GET:
268                   if (msg != null)
269       {
270         System.err.println("Platform: timer dur\t" + ((t - time)/1000f) + "\t" + ((duration)/1000f) + "\t" + msg);
271       }
272                   set = 0;
273                   break;
274           }
275   }
276
277   public static void cacheFileData(String path, byte[] data)  
278   {
279         if (!isJS())
280   {
281     return;
282           /**
283            * @j2sNative 
284            *   
285            *   swingjs.JSUtil.cacheFileData$S$O(path, data);
286            * 
287            */
288   }
289   }
290
291   public static byte[] getFileBytes(File f) 
292   {
293         return /** @j2sNative   f && f._bytes || */null;
294   }
295
296   public static byte[] getFileAsBytes(String fileStr) 
297   {
298     // BH 2018 hack for no support for access-origin
299         return /** @j2sNative swingjs.JSUtil.getFileAsBytes$O(fileStr) || */ null;
300   }
301
302   public static String getFileAsString(String data) 
303   {
304         return /** @j2sNative swingjs.JSUtil.getFileAsString$S(data) || */ null;
305   }
306
307   public static boolean setFileBytes(File f, String urlstring) 
308   {
309         if (!isJS())
310   {
311     return false;
312   }
313         @SuppressWarnings("unused")
314         byte[] bytes = getFileAsBytes(urlstring);
315                     /** @j2sNative 
316                      * f._bytes = bytes; 
317                      */
318         return true;
319   }
320
321    
322   public static void addJ2SBinaryType(String ext)
323   {
324   /**
325    * @j2sNative
326    * 
327    *            J2S._binaryTypes.push("." + ext + "?");
328    * 
329    */
330   }
331
332   public static String encodeURI(String value) 
333   {
334     /**
335      * @j2sNative
336      * return encodeURIComponent(value);
337      */
338         return value;
339   }
340
341   public static boolean openURL(String url) 
342   {
343         if (!isJS())
344   {
345     return false;
346   }
347                 /**
348                  * @j2sNative
349                  * 
350                  * 
351                  *                      window.open(url);
352                  */
353         return true;
354   }
355
356         public static String getUniqueAppletID() {
357                 @SuppressWarnings("unused")
358                 ThreadGroup g = Thread.currentThread().getThreadGroup();
359                 /**
360                  * @j2sNative return g.html5Applet._uniqueId;
361                  *
362                  */
363                 return null;
364
365         }
366   /**
367    * Read the Info block for this applet. 
368    * 
369    * @param prefix "jalview_"
370    * @param p
371    * @return   unique id for this applet
372    */
373   public static void readInfoProperties(String prefix, Properties p) 
374   {
375           @SuppressWarnings("unused")
376         ThreadGroup g = Thread.currentThread().getThreadGroup(); 
377           String id = getUniqueAppletID();
378           String key = "", value = "";
379           /**
380            * @j2sNative
381                var info = g.html5Applet.__Info || {};
382                for (var key in info) {
383                   if (key.indexOf(prefix) == 0) {
384                      value = "" + info[key];
385         */
386          
387           p.put(id + "_" + key, value);
388           
389           /**
390            * @j2sNative
391
392                
393                   }
394                }
395            */
396   }
397
398
399 }