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