blob: fc80b8b8b72e6a5f0ab854def6c7a0e0f4afa01e [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2012, 2021 Original NatTable authors and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0.
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Original NatTable authors and others - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ecommons.waltable.ui.mode;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.statet.ecommons.waltable.NatTable;
import org.eclipse.statet.ecommons.waltable.edit.EditUtils;
import org.eclipse.statet.ecommons.waltable.ui.NatEventData;
import org.eclipse.statet.ecommons.waltable.ui.action.IDragMode;
import org.eclipse.statet.ecommons.waltable.ui.action.IKeyAction;
import org.eclipse.statet.ecommons.waltable.ui.action.IMouseAction;
import org.eclipse.statet.ecommons.waltable.ui.binding.UiBindingRegistry;
public class ConfigurableModeEventHandler extends AbstractModeEventHandler {
private final NatTable natTable;
public ConfigurableModeEventHandler(final ModeSupport modeSupport, final NatTable natTable) {
super(modeSupport);
this.natTable= natTable;
}
// Event handling /////////////////////////////////////////////////////////
@Override
public void keyPressed(final KeyEvent event) {
final IKeyAction keyAction= this.natTable.getUiBindingRegistry().getKeyEventAction(event);
if (keyAction != null) {
this.natTable.forceFocus();
keyAction.run(this.natTable, event);
}
}
@Override
public void mouseDown(final MouseEvent event) {
if (EditUtils.commitAndCloseActiveEditor()) {
final IMouseAction mouseDownAction= this.natTable.getUiBindingRegistry().getMouseDownAction(event);
if (mouseDownAction != null) {
event.data= NatEventData.createInstanceFromEvent(event);
mouseDownAction.run(this.natTable, event);
}
final IMouseAction singleClickAction= getUiBindingRegistry().getSingleClickAction(event);
final IMouseAction doubleClickAction= getUiBindingRegistry().getDoubleClickAction(event);
final IDragMode dragMode= this.natTable.getUiBindingRegistry().getDragMode(event);
if (singleClickAction != null || doubleClickAction != null || dragMode != null) {
switchMode(new MouseModeEventHandler(getModeSupport(), this.natTable, event, singleClickAction, doubleClickAction, dragMode));
}
}
}
@Override
public synchronized void mouseMove(final MouseEvent event) {
if (event.x >= 0 && event.y >= 0) {
final IMouseAction mouseMoveAction= getUiBindingRegistry().getMouseMoveAction(event);
if (mouseMoveAction != null) {
event.data= NatEventData.createInstanceFromEvent(event);
mouseMoveAction.run(this.natTable, event);
} else {
this.natTable.setCursor(null);
}
}
}
@Override
public synchronized void mouseHover(final MouseEvent event) {
if (event.x >= 0 && event.y >= 0) {
final IMouseAction mouseHoverAction= getUiBindingRegistry().getMouseHoverAction(event);
if (mouseHoverAction != null) {
event.data= NatEventData.createInstanceFromEvent(event);
mouseHoverAction.run(this.natTable, event);
}
}
}
@Override
public synchronized void mouseEnter(final MouseEvent event) {
if (event.x >= 0 && event.y >= 0) {
final IMouseAction mouseEnterAction= getUiBindingRegistry().getMouseEnterAction(event);
if (mouseEnterAction != null) {
event.data= NatEventData.createInstanceFromEvent(event);
mouseEnterAction.run(this.natTable, event);
} else {
this.natTable.setCursor(null);
}
}
}
@Override
public synchronized void mouseExit(final MouseEvent event) {
if (event.x >= 0 && event.y >= 0) {
final IMouseAction mouseExitAction= getUiBindingRegistry().getMouseExitAction(event);
if (mouseExitAction != null) {
event.data= NatEventData.createInstanceFromEvent(event);
mouseExitAction.run(this.natTable, event);
} else {
this.natTable.setCursor(null);
}
}
}
private UiBindingRegistry getUiBindingRegistry() {
return this.natTable.getUiBindingRegistry();
}
}