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