1f03f88f44a3937a9ad7e1cf1f895acf5a78a9a9
[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
26 import javax.swing.SwingUtilities;
27
28 /**
29  * System platform information used by Applet and Application
30  * 
31  * @author Jim Procter
32  */
33 public class Platform
34 {
35
36   private static boolean isJS = /** @j2sNative true || */false;
37
38   private static Boolean isNoJSMac = null, isNoJSWin = null, 
39                   isMac = null, isWin = null;
40   
41   private static Boolean isHeadless = null;
42
43   /**
44    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
45    * @return
46    */
47   public static boolean isMac()
48   {
49           return (isMac == null ? (isMac = (System.getProperty("os.name").indexOf("Mac") >= 0)) : isMac);
50   }
51
52   /**
53    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
54    * @return
55    */
56   public static boolean isWin() 
57   {
58           return (isWin == null ? (isWin = (System.getProperty("os.name").indexOf("Win") >= 0)) : isWin);
59   }
60
61   /**
62    * 
63    * @return true if HTML5 JavaScript
64    */
65   public static boolean isJS()
66   {
67         return isJS;
68   }
69
70   /**
71    * sorry folks - Macs really are different
72    * 
73    * BH: disabled for SwingJS -- will need to check key-press issues
74    * 
75    * @return true if we do things in a special way.
76    */
77   public static boolean isAMacAndNotJS()
78   {
79         return (isNoJSMac == null ? (isNoJSMac = !isJS && isMac()) : isNoJSMac);
80   }
81
82 /**
83    * Check if we are on a Microsoft plaform...
84    * 
85    * @return true if we have to cope with another platform variation
86    */
87   public static boolean isWindowsAndNotJS()
88   {
89         return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
90    }
91
92   /**
93    * 
94    * @return true if we are running in non-interactive no UI mode
95    */
96   public static boolean isHeadless()
97   {
98     if (isHeadless == null)
99     {
100       isHeadless = "true".equals(System.getProperty("java.awt.headless"));
101     }
102     return isHeadless;
103   }
104
105   /**
106    * 
107    * @return nominal maximum command line length for this platform
108    */
109   public static int getMaxCommandLineLength()
110   {
111     // TODO: determine nominal limits for most platforms.
112     return 2046; // this is the max length for a windows NT system.
113   }
114
115   /**
116    * escape a string according to the local platform's escape character
117    * 
118    * @param file
119    * @return escaped file
120    */
121   public static String escapeString(String file)
122   {
123     StringBuffer f = new StringBuffer();
124     int p = 0, lastp = 0;
125     while ((p = file.indexOf('\\', lastp)) > -1)
126     {
127       f.append(file.subSequence(lastp, p));
128       f.append("\\\\");
129       lastp = p + 1;
130     }
131     f.append(file.substring(lastp));
132     return f.toString();
133   }
134
135   /**
136    * Answers true if the mouse event has Meta-down (Command key on Mac) or
137    * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
138    * pressed instead of the Meta/Cmd key on Mac. To test for Ctrl-pressed on Mac,
139    * you can use e.isPopupTrigger().
140    * 
141    * @param e
142    * @return
143    */
144   public static boolean isControlDown(MouseEvent e)
145   {
146     return isControlDown(e, isMac());
147   }
148
149   /**
150    * Overloaded version of method (to allow unit testing)
151    * 
152    * @param e
153    * @param aMac
154    * @return
155    */
156   protected static boolean isControlDown(MouseEvent e, boolean aMac)
157   {
158     if (!aMac) {
159         return e.isControlDown();       
160     }
161       // answer false for right mouse button
162       // shortcut key will be META for a Mac 
163     return !e.isPopupTrigger() 
164                   && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() & e.getModifiers()) != 0;
165       // could we use e.isMetaDown() here?
166   }
167
168   /**
169    * Windows (not Mac, Linux, or Unix) and right button
170    * to test for the right-mouse pressed event in Windows
171    * that would have opened a menu or a Mac. 
172    * 
173    * @param e
174    * @return
175    */
176   public static boolean isWinRightButton(MouseEvent e) 
177   {
178           // was !isAMac(), but that is true also for Linux and Unix and JS, 
179
180           return isWin() && SwingUtilities.isRightMouseButton(e);
181   }
182   
183   
184   /**
185    * Windows (not Mac, Linux, or Unix) and middle button -- for mouse wheeling 
186    * without pressing the button.
187    * 
188    * @param e
189    * @return
190    */
191   public static boolean isWinMiddleButton(MouseEvent e) 
192   {
193         // was !isAMac(), but that is true also for Linux and Unix and JS
194           return isWin() && SwingUtilities.isMiddleMouseButton(e);
195   }
196
197   public static boolean allowMnemonics() 
198   {
199         return !isMac();
200   }
201   
202   public final static int TIME_RESET = 0;
203   public final static int TIME_MARK  = 1;
204   
205   public static long time, mark;
206   
207   public static void timeCheck(String msg, int mode) {
208           switch (mode) {
209           case TIME_RESET:
210                   time = mark = System.currentTimeMillis();
211                   System.err.println("Platform: timer reset\t\t\t" + msg);
212                   break;
213           case TIME_MARK:
214                   long t = System.currentTimeMillis();
215                   if (time == 0)
216                           time = mark = t;
217                   System.err.println("Platform: timer mark\t" + ((t - time)/1000f) + "\t" + ((t - mark)/1000f) + "\t" + msg);
218                   mark = t;
219                   break;
220           }
221   }
222    
223 }