*/
public class Platform
{
+ private static Boolean isAMac = null;
+
+ private static Boolean isHeadless = null;
+
/**
* sorry folks - Macs really are different
*
*/
public static boolean isAMac()
{
- return java.lang.System.getProperty("os.name").indexOf("Mac") > -1;
+ if (isAMac == null)
+ {
+ isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
+ }
+ return isAMac.booleanValue();
}
public static boolean isHeadless()
{
- String hdls = java.lang.System.getProperty("java.awt.headless");
-
- return hdls != null && hdls.equals("true");
+ if (isHeadless == null)
+ {
+ isHeadless = "true".equals(System.getProperty("java.awt.headless"));
+ }
+ return isHeadless;
}
/**
*/
public static boolean isControlDown(MouseEvent e)
{
- if (isAMac())
+ boolean aMac = isAMac();
+ return isControlDown(e, aMac);
+ }
+
+ /**
+ * Overloaded version of method (to allow unit testing)
+ *
+ * @param e
+ * @param aMac
+ * @return
+ */
+ protected static boolean isControlDown(MouseEvent e, boolean aMac)
+ {
+ if (aMac)
{
/*
* answer false for right mouse button
--- /dev/null
+package jalview.util;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.awt.Button;
+import java.awt.Event;
+import java.awt.event.MouseEvent;
+
+import org.testng.annotations.Test;
+
+public class PlatformTest
+{
+ Button b = new Button();
+
+ /**
+ * isControlDown for Mac should answer true for Meta-down, but not for right
+ * mouse (popup trigger)
+ */
+ @Test(groups = "Functional")
+ public void testIsControlDown_mac()
+ {
+ int clickCount = 1;
+ boolean isPopupTrigger = false;
+ int buttonNo = MouseEvent.BUTTON1;
+ boolean mac = true;
+
+ int mods = 0;
+ // not concerned with MouseEvent id, when, x, y, xAbs, yAbs values
+ assertFalse(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0,
+ 0, 0, clickCount, isPopupTrigger, buttonNo), mac));
+
+ mods = Event.CTRL_MASK;
+ assertFalse(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0,
+ 0, 0, clickCount, isPopupTrigger, buttonNo), mac));
+
+ mods = Event.META_MASK;
+ assertTrue(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0,
+ 0, 0, clickCount, isPopupTrigger, buttonNo), mac));
+
+ isPopupTrigger = true;
+ assertFalse(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0,
+ 0, 0, clickCount, isPopupTrigger, buttonNo), mac));
+ }
+
+ /**
+ * If not a Mac, we only care whether CTRL_MASK modifier is set on the mouse
+ * event
+ */
+ @Test(groups = "Functional")
+ public void testIsControlDown_notMac()
+ {
+ int clickCount = 1;
+ boolean isPopupTrigger = false;
+ int buttonNo = MouseEvent.BUTTON1;
+ boolean mac = false;
+
+ int mods = 0;
+ // not concerned with MouseEvent id, when, x, y, xAbs, yAbs values
+ assertFalse(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0,
+ 0, 0, clickCount, isPopupTrigger, buttonNo), mac));
+
+ mods = Event.CTRL_MASK;
+ assertTrue(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0,
+ 0, 0, clickCount, isPopupTrigger, buttonNo), mac));
+
+ mods = Event.CTRL_MASK | Event.SHIFT_MASK | Event.ALT_MASK;
+ clickCount = 2;
+ buttonNo = 2;
+ isPopupTrigger = true;
+ assertTrue(Platform.isControlDown(new MouseEvent(b, 0, 0L, mods, 0, 0,
+ 0, 0, clickCount, isPopupTrigger, buttonNo), mac));
+ }
+}