0a788d936cd7adf0428a152841e7bd423dbf5bf6
[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
27 import javax.swing.SwingUtilities;
28
29 /**
30  * System platform information used by Applet and Application
31  * 
32  * @author Jim Procter
33  */
34 public class Platform
35 {
36
37   private static boolean isJS = /** @j2sNative true || */false;
38
39   private static Boolean isNoJSMac = null, isNoJSWin = null, 
40                   isMac = null, isWin = null;
41   
42   private static Boolean isHeadless = null;
43
44   /**
45    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
46    * @return
47    */
48   public static boolean isMac()
49   {
50           return (isMac == null ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0)) : isMac);
51   }
52
53   /**
54    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
55    * @return
56    */
57   public static boolean isWin() 
58   {
59           return (isWin == null ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0)) : isWin);
60   }
61
62   /**
63    * Answers true if Jalview is running as Javascript, else false. The value is
64    * set at compile time.
65    * 
66    * @return
67    */
68   public static boolean isJS()
69   {
70     return /** @j2sNative true || */ false;
71   }
72
73   /**
74    * sorry folks - Macs really are different
75    * 
76    * BH: disabled for SwingJS -- will need to check key-press issues
77    * 
78    * @return true if we do things in a special way.
79    */
80   public static boolean isAMacAndNotJS()
81   {
82         return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
83   }
84
85 /**
86    * Check if we are on a Microsoft plaform...
87    * 
88    * @return true if we have to cope with another platform variation
89    */
90   public static boolean isWindowsAndNotJS()
91   {
92         return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
93    }
94
95   /**
96    * 
97    * @return true if we are running in non-interactive no UI mode
98    */
99   public static boolean isHeadless()
100   {
101     if (isHeadless == null)
102     {
103       isHeadless = "true".equals(System.getProperty("java.awt.headless"));
104     }
105     return isHeadless;
106   }
107
108   /**
109    * 
110    * @return nominal maximum command line length for this platform
111    */
112   public static int getMaxCommandLineLength()
113   {
114     // TODO: determine nominal limits for most platforms.
115     return 2046; // this is the max length for a windows NT system.
116   }
117
118   /**
119    * escape a string according to the local platform's escape character
120    * 
121    * @param file
122    * @return escaped file
123    */
124   public static String escapeString(String file)
125   {
126     StringBuffer f = new StringBuffer();
127     int p = 0, lastp = 0;
128     while ((p = file.indexOf('\\', lastp)) > -1)
129     {
130       f.append(file.subSequence(lastp, p));
131       f.append("\\\\");
132       lastp = p + 1;
133     }
134     f.append(file.substring(lastp));
135     return f.toString();
136   }
137
138   /**
139    * Answers true if the mouse event has Meta-down (Command key on Mac) or
140    * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
141    * pressed instead of the Meta/Cmd key on Mac. To test for Ctrl-pressed on Mac,
142    * you can use e.isPopupTrigger().
143    * 
144    * @param e
145    * @return
146    */
147   public static boolean isControlDown(MouseEvent e)
148   {
149     return isControlDown(e, isMac());
150   }
151
152   /**
153    * Overloaded version of method (to allow unit testing)
154    * 
155    * @param e
156    * @param aMac
157    * @return
158    */
159   protected static boolean isControlDown(MouseEvent e, boolean aMac)
160   {
161     if (!aMac) {
162         return e.isControlDown();       
163     }
164       // answer false for right mouse button
165       // shortcut key will be META for a Mac 
166     return !e.isPopupTrigger() 
167                   && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() & e.getModifiers()) != 0;
168       // could we use e.isMetaDown() here?
169   }
170
171   /**
172    * Windows (not Mac, Linux, or Unix) and right button
173    * to test for the right-mouse pressed event in Windows
174    * that would have opened a menu or a Mac. 
175    * 
176    * @param e
177    * @return
178    */
179   public static boolean isWinRightButton(MouseEvent e) 
180   {
181           // was !isAMac(), but that is true also for Linux and Unix and JS, 
182
183           return isWin() && SwingUtilities.isRightMouseButton(e);
184   }
185   
186   
187   /**
188    * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling 
189    * without pressing the button.
190    * 
191    * @param e
192    * @return
193    */
194   public static boolean isWinMiddleButton(MouseEvent e) 
195   {
196         // was !isAMac(), but that is true also for Linux and Unix and JS
197           return isWin() && SwingUtilities.isMiddleMouseButton(e);
198   }
199
200   public static boolean allowMnemonics() 
201   {
202         return !isMac();
203   }
204   
205   public final static int TIME_RESET = 0;
206   public final static int TIME_MARK  = 1;
207   
208   public static long time, mark;
209   
210   public static void timeCheck(String msg, int mode) {
211           switch (mode) {
212           case TIME_RESET:
213                   time = mark = System.currentTimeMillis();
214                   System.err.println("Platform: timer reset\t\t\t" + msg);
215                   break;
216           case TIME_MARK:
217                   long t = System.currentTimeMillis();
218                   if (time == 0)
219                           time = mark = t;
220                   System.err.println("Platform: timer mark\t" + ((t - time)/1000f) + "\t" + ((t - mark)/1000f) + "\t" + msg);
221                   mark = t;
222                   break;
223           }
224   }
225
226 public static void cacheFileData(String path, byte[] data) {
227         if (!isJS())
228                 return;
229           /**
230            * @j2sNative 
231            *   
232            *   swingjs.JSUtil.cacheFileData$S$O(path, data);
233            * 
234            */
235 }
236
237 public static byte[] getFileBytes(File f) {
238         return /** @j2sNative   f && f._bytes || */null;
239 }
240
241 public static byte[] getFileAsBytes(String fileStr) {
242     // BH 2018 hack for no support for access-origin
243                 return /** @j2sNative swingjs.JSUtil.getFileAsBytes$O(fileStr) || */ null;
244 }
245
246 public static String getFileAsString(String data) {
247         return /** @j2sNative swingjs.JSUtil.getFileAsString$S(data) || */ null;
248 }
249
250 public static boolean setFileBytes(File f, String urlstring) {
251         if (!isJS()) 
252                 return false;
253         @SuppressWarnings("unused")
254         byte[] bytes = getFileAsBytes(urlstring);
255                     /** @j2sNative 
256                      * f._bytes = bytes; 
257                      */
258         return true;
259 }
260
261    
262 public static void addJ2SBinaryType(String ext)
263 {
264   ext = "." + ext + "?";
265
266   /**
267    * @j2sNative
268    * 
269    *            J2S._binaryTypes.push(ext);
270    * 
271    */
272 }
273
274 public static String encodeURI(String value) {
275     /**
276      * @j2sNative
277      * return encodeURIComponent(value);
278      */
279         return value;
280 }
281
282 public static boolean openURL(String url) {
283         if (!isJS()) 
284                 return false;
285                 /**
286                  * @j2sNative
287                  * 
288                  * 
289                  *                      window.open(url);
290                  */
291         return true;
292 }
293
294
295 }