de.javagl.viewer.InputEventPredicates Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of viewer-core Show documentation
Show all versions of viewer-core Show documentation
A Java Swing Panel that allows rotation, translation and zooming
The newest version!
/*
* www.javagl.de - Viewer
*
* Copyright (c) 2013-2015 Marco Hutter - http://www.javagl.de
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package de.javagl.viewer;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.util.function.Predicate;
/**
* Predicates for input events.
*
* This class is not part of the public API, and may be omitted in the future.
*/
public class InputEventPredicates
{
/**
* Returns a predicate that is always false
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate alwaysFalse()
{
return Predicates.create(t -> false, "false");
}
/**
* Returns a predicate that is always true
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate alwaysTrue()
{
return Predicates.create(t -> true, "true");
}
/**
* Returns a predicate that checks whether the given input event
* is a MOUSE_PRESSED event
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate mousePressed()
{
return Predicates.create(
t -> t.getID() == MouseEvent.MOUSE_PRESSED, "mousePressed");
}
/**
* Returns a predicate that checks whether the given input event
* is a MOUSE_RELEASED event
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate mouseReleased()
{
return Predicates.create(
t -> t.getID() == MouseEvent.MOUSE_RELEASED, "mouseReleased");
}
/**
* Returns a predicate that checks whether the given input event
* is a MOUSE_CLICKED event
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate mouseClicked()
{
return Predicates.create(
t -> t.getID() == MouseEvent.MOUSE_CLICKED, "mouseClicked");
}
/**
* Returns a predicate that checks whether the given input event
* is a MOUSE_MOVED event
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate mouseMoved()
{
return Predicates.create(
t -> t.getID() == MouseEvent.MOUSE_MOVED, "mouseMoved");
}
/**
* Returns a predicate that checks whether the given input event
* is a MOUSE_DRAGGED event
*
* @param The argument type
*
* @return The predicate
*/
static Predicate mouseDragged()
{
return Predicates.create(
t -> t.getID() == MouseEvent.MOUSE_DRAGGED, "mouseDragged");
}
/**
* Returns a predicate that checks whether the given input event
* is a MOUSE_WHEEL event
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate mouseWheel()
{
return Predicates.create(
t -> t.getID() == MouseEvent.MOUSE_WHEEL, "mouseWheel");
}
/**
* Returns a predicate that checks whether the given input event
* was created while the SHIFT button was pressed
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate shiftDown()
{
return Predicates.create(
t -> t.isShiftDown(), "shiftDown");
}
/**
* Returns a predicate that checks whether the given input event
* was created while the ALT button was pressed
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate altDown()
{
return Predicates.create(
t -> t.isAltDown(), "altDown");
}
/**
* Returns a predicate that checks whether the given input event
* was created while the ALT_GRAPH button was pressed
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate altGraphDown()
{
return Predicates.create(
t -> t.isAltGraphDown(), "altGraphDown");
}
/**
* Returns a predicate that checks whether the given input event
* was created while the CONTROL button was pressed
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate controlDown()
{
return Predicates.create(
t -> t.isControlDown(), "controlDown");
}
/**
* Returns a predicate that checks whether the given input event
* was created while the META button was pressed
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate metaDown()
{
return Predicates.create(
t -> t.isMetaDown(), "metaDown");
}
/**
* Returns a predicate that checks whether the given mouse event
* is a popup trigger
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate popupTrigger()
{
return Predicates.create(
t -> t.isPopupTrigger(), "popupTrigger");
}
/**
* Returns a predicate that checks whether the given mouse event was
* causes by pressing the specified button
*
* @param The argument type
*
* @return The predicate
* @param button The button
*/
public static Predicate button(int button)
{
return Predicates.create(
t -> t.getButton() == button, "button("+button+")");
}
/**
* Returns a predicate that checks whether the given input event
* was created while the respective mouse button was pressed
*
* @param The argument type
*
* @param button The button
* @return The predicate
* @throws IllegalArgumentException If the given button is not 1, 2 or 3
*/
public static Predicate buttonDown(int button)
{
return Predicates.create(
t -> is(t, maskForButton(button)), "buttonDown("+button+")");
}
/**
* Returns the input event mask for the specified mouse button. The
* button must be 1, 2 or 3
*
* @param button The button
* @return The input event mask
* @throws IllegalArgumentException If the given button is not 1, 2 or 3
*/
private static int maskForButton(int button)
{
switch (button)
{
case 1: return InputEvent.BUTTON1_DOWN_MASK;
case 2: return InputEvent.BUTTON2_DOWN_MASK;
case 3: return InputEvent.BUTTON3_DOWN_MASK;
default:
}
throw new IllegalArgumentException(
"Button must be 1, 2 or 3");
}
/**
* Returns a predicate that checks whether the given input event
* was created without any modifier key pressed. This means that
* the event was created without SHIFT, ALT, ALT-GR, CTRL key
* being pressed.
*
* @param The argument type
*
* @return The predicate
*/
public static Predicate noModifiers()
{
return Predicates.create(t ->
!t.isShiftDown() &&
!t.isAltDown() &&
!t.isAltGraphDown() &&
!t.isControlDown(),
"noModifiers");
}
/**
* Returns whether the specified flags are set in the extended modifiers
* of the given event
*
* @param e The event
* @param flags The flags
* @return Whether the flags are set
*/
private static boolean is(InputEvent e, int flags)
{
return (e.getModifiersEx() & flags) == flags;
}
/**
* Private constructor to prevent instantiation
*/
private InputEventPredicates()
{
// Private constructor to prevent instantiation
}
}