blob: b60feb1c3639a867c243f6fdf6e1cf7a0e850d50 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.vaadin.common.shortcuts;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.vaadin.ui.Component;
// TODO: Auto-generated Javadoc
/**
* Handles ShortcutHandlers.
*/
public class ShortcutManager {
/** The handlers. */
private Map<Component, Set<ShortcutHandler>> handlers = new HashMap<Component, Set<ShortcutHandler>>();
/** The active component. */
private Component activeComponent;
/**
* Registers a new handler for the component.
*
* @param component
* the component
* @param handler
* the handler
*/
public void register(Component component, ShortcutHandler handler) {
if (!handlers.containsKey(component)) {
handlers.put(component, new HashSet<ShortcutHandler>());
}
Set<ShortcutHandler> temp = handlers.get(component);
temp.add(handler);
}
/**
* Unregisters the handler for the component. The handler will be
* deactivated.
*
* @param component
* the component
* @param handler
* the handler
*/
public void unregister(Component component, ShortcutHandler handler) {
if (!handlers.containsKey(component)) {
return;
}
Set<ShortcutHandler> temp = handlers.get(component);
temp.remove(handler);
// deactivate handler
handler.deactivate();
}
/**
* Unregisters all handlers for the component. Handlers will be deactivated.
*
* @param component
* the component
*/
public void unregister(Component component) {
Set<ShortcutHandler> temp = handlers.remove(component);
for (ShortcutHandler handler : temp) {
handler.deactivate();
}
}
/**
* Activates all handlers for the component.
*
* @param component
* the component
*/
public void activate(Component component) {
if (this.activeComponent != null) {
deactivate(this.activeComponent);
}
this.activeComponent = component;
if (!handlers.containsKey(component)) {
return;
}
Set<ShortcutHandler> temp = handlers.get(component);
for (ShortcutHandler handler : temp) {
handler.activate();
}
}
/**
* Deactivates all handlers for the component.
*
* @param component
* the component
*/
public void deactivate(Component component) {
if (this.activeComponent == component) {
this.activeComponent = null;
}
if (!handlers.containsKey(component)) {
return;
}
Set<ShortcutHandler> temp = handlers.get(component);
for (ShortcutHandler handler : temp) {
handler.deactivate();
}
}
/**
* Checks if is handled.
*
* @param component
* the component
* @return true, if is handled
*/
public boolean isHandled(Component component) {
return handlers.containsKey(component);
}
}