
net.vectorpublish.desktop.vp.pd.official.ActionBinding Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2016, Peter Rader. All rights reserved.
* ___ ___ __ ______ __ __ __ __
* | | |.-----..----.| |_ .-----..----.| __ \.--.--.| |--.| ||__|.-----.| |--.
* | | || -__|| __|| _|| _ || _|| __/| | || _ || || ||__ --|| |
* \_____/ |_____||____||____||_____||__| |___| |_____||_____||__||__||_____||__|__|
*
* http://www.gnu.org/licenses/gpl-3.0.html
*/
package net.vectorpublish.desktop.vp.pd.official;
import java.awt.Event;
import java.awt.event.KeyEvent;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import javax.swing.Action;
import javax.swing.KeyStroke;
/**
*
*/
public class ActionBinding {
private static Set registredActions = new LinkedHashSet<>();
private static Random rnd = new Random(System.currentTimeMillis());
private final KeyStroke stroke;
private final String actionName;
private final Action action;
public ActionBinding(boolean pressed, int keyVal, Action action) {
this.actionName = generateUniqueActionName();
this.action = action;
int mod = 0;
if (keyVal == KeyEvent.VK_CONTROL) {
if (pressed) {
mod += Event.CTRL_MASK;
mod += KeyEvent.CTRL_DOWN_MASK;
}
}
if (keyVal == KeyEvent.VK_ALT) {
if (pressed) {
mod += Event.ALT_MASK;
mod += KeyEvent.ALT_DOWN_MASK;
}
}
if (keyVal == KeyEvent.VK_SHIFT) {
if (pressed) {
mod += Event.SHIFT_MASK;
mod += KeyEvent.SHIFT_DOWN_MASK;
}
}
stroke = KeyStroke.getKeyStroke(keyVal, mod, !pressed);
}
public void bind(DrawPanel drawPanel) {
drawPanel.getInputMap().put(stroke, actionName);
drawPanel.getActionMap().put(actionName, action);
}
public String generateUniqueActionName() {
synchronized (registredActions) {
String name;
do {
name = "act" + rnd.nextInt();
} while (registredActions.contains(name));
return name;
}
}
public Action getAction() {
return action;
}
public String getActionName() {
return actionName;
}
public KeyStroke getStroke() {
return stroke;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy