Using Alternative Modifiers for ViewMode Mouse Gestures
Tips & TricksSummary
Description
View modes that handle special mouse gestures, like, e.g., MoveSelectionMode, often also handle a related gesture in addition, e.g., adding selected nodes to a group node. Most commonly, a user can trigger the related gesture by holding down a modifier key (for example, the SHIFT key).
The mechanism to recognize if a specific modifier key is pressed is defined in class ViewMode:
Protected method isModifierPressed(java.awt.event.MouseEvent) in conjunction with setModifierMask(int) handle modifier keys in view mode implementations.
The latter method allows to easily change the modifier key by simply setting another modifier mask.
For example, by means of the following line the ALT key is used as the modifier. myViewMode.setModifierMask(InputEvent.ALT_MASK);
Using Extended Modifiers
However, setting another modifier mask does not support extended modifiers, which are recommended when looking at the Javadoc comments in class InputEvent.
In order to use these, the isModifierPressed method needs to be appropriately customized.
The following source code example shows how this can be done.
MoveSelectionMode myMSM = new MoveSelectionMode() {
protected boolean isModifierPressed(MouseEvent me) {
return ((me.getModifiersEx() & me.ALT_DOWN_MASK) != 0);
}
};
The attached tutorial demo code AlternativeModifiersDemo27.java presents this and additionally shows how to register a key listener with the canvas component of the Graph2DView that displays the graph in order to use the key combination ALT-M as a modifier.