001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.util;
020
021import icy.system.SystemUtil;
022
023import java.awt.event.InputEvent;
024import java.awt.event.MouseEvent;
025
026/**
027 * Event related utilities
028 * 
029 * @author Stephane
030 */
031public class EventUtil
032{
033    /**
034     * Returns true if Shift key is pressed for the specified event.
035     */
036    public static boolean isShiftDown(InputEvent e)
037    {
038        return isShiftDown(e, false);
039    }
040
041    /**
042     * Returns true if Shift key is pressed for the specified event.
043     */
044    public static boolean isShiftDown(InputEvent e, boolean exclusive)
045    {
046        if (exclusive)
047            return (e.getModifiers() == InputEvent.SHIFT_MASK);
048
049        return e.isShiftDown();
050    }
051
052    /**
053     * Returns true if Alt key is pressed for the specified event
054     */
055    public static boolean isAltDown(InputEvent e)
056    {
057        return isAltDown(e, false);
058    }
059
060    /**
061     * Returns true if Alt key is pressed for the specified event.
062     */
063    public static boolean isAltDown(InputEvent e, boolean exclusive)
064    {
065        if (exclusive)
066            return (e.getModifiers() == InputEvent.ALT_MASK);
067
068        return e.isAltDown();
069    }
070
071    /**
072     * Returns true if Ctrl key is pressed for the specified event
073     */
074    public static boolean isControlDown(InputEvent e)
075    {
076        return isControlDown(e, false);
077    }
078
079    /**
080     * Returns true if Ctrl key is pressed for the specified event
081     */
082    public static boolean isControlDown(InputEvent e, boolean exclusive)
083    {
084        if (exclusive)
085            return (e.getModifiers() == InputEvent.CTRL_MASK);
086
087        return e.isControlDown();
088    }
089
090    /**
091     * Returns true if Ctrl/Cmd menu key is pressed for the specified event.
092     */
093    public static boolean isMenuControlDown(InputEvent e)
094    {
095        return isMenuControlDown(e, false);
096    }
097
098    /**
099     * Returns true if Ctrl/Cmd menu key is pressed for the specified event.
100     */
101    public static boolean isMenuControlDown(InputEvent e, boolean exclusive)
102    {
103        if (exclusive)
104            return (e.getModifiers() == SystemUtil.getMenuCtrlMask());
105
106        // take care of OSX CMD key here
107        return (e.getModifiers() & SystemUtil.getMenuCtrlMask()) != 0;
108    }
109
110    /**
111     * Returns true if there is no any modifiers in the specified input event.
112     */
113    public static boolean isNoModifier(InputEvent e)
114    {
115        return e.getModifiers() == 0;
116    }
117
118    /**
119     * Returns true if the mouse event specifies the left mouse button.
120     * 
121     * @param e
122     *        a MouseEvent object
123     * @return true if the left mouse button was active
124     */
125    public static boolean isLeftMouseButton(MouseEvent e)
126    {
127        return ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK);
128    }
129
130    /**
131     * Returns true if the mouse event specifies the middle mouse button.
132     * 
133     * @param e
134     *        a MouseEvent object
135     * @return true if the middle mouse button was active
136     */
137    public static boolean isMiddleMouseButton(MouseEvent e)
138    {
139        return ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK);
140    }
141
142    /**
143     * Returns true if the mouse event specifies the right mouse button.
144     * 
145     * @param e
146     *        a MouseEvent object
147     * @return true if the right mouse button was active
148     */
149    public static boolean isRightMouseButton(MouseEvent e)
150    {
151        return ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
152    }
153
154}