我在面板中有一个组件DragSourceExtension
,我可以通过DragData
传递任何数据,但我无法通过DragStartEvent
获取relativeX和Y.
DropEvent
获得最终位置的relativeX和Y. 在vaadin 7中,DragAndDropWrapper.WrapperTransferable
(可转让),我们可以在开始拖动时得到relativeX和Y:
transferable.getMouseDownEvent().getClientX()
transferable.getMouseDownEvent().getClientY()
更新:这是代码的一部分:
DropTargetExtension<AbsoluteLayout> absoluteLayoutDropTargetExtension = new DropTargetExtension<>(sceneLayout);
...
absoluteLayoutDropTargetExtension.addDropListener(event -> {
Optional<Object> source = event.getDragData();
int x = event.getMouseEventDetails().getRelativeX();
int y = event.getMouseEventDetails().getRelativeY();
x和y是当前鼠标位置,但我没有开始计算diff
的位置更新2: 通过此文档Component and UI Extension,我测试了扩展示例。
public class CapsLockWarning extends AbstractExtension {
// You could pass it in the constructor
public CapsLockWarning(PasswordField field) {
super.extend(field);
System.out.println("CapsLockWarning");
}
// Or in an extend() method
public void extend(PasswordField field) {
super.extend(field);
}
// Or with a static helper
public static void addTo(PasswordField field) {
new CapsLockWarning(field);
}
}
@Connect(CapsLockWarning.class)
public class CapsLockWarningConnector extends AbstractExtensionConnector {
public CapsLockWarningConnector() {
super();
System.out.println("CapsLockWarningConnector");
}
@Override
protected void extend(ServerConnector target) {
// Get the extended widget
final Widget pw = ((ComponentConnector) target).getWidget();
// Preparations for the added feature
final VOverlay warning = new VOverlay();
warning.setOwner(pw);
warning.add(new HTML("Caps Lock is enabled!"));
Window.alert("ssss");
// Add an event handler
pw.addDomHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (isEnabled() && isCapsLockOn(event)) {
warning.showRelativeTo(pw);
UiUtilities.showMessage("oh");
} else {
warning.hide();
}
}
}, KeyPressEvent.getType());
}
private boolean isCapsLockOn(KeyPressEvent e) {
return e.isShiftKeyDown() ^
Character.isUpperCase(e.getCharCode());
}
}
UI:
txtPassword = new PasswordField(bundle.getString("common.password"));
txtPassword.setRequiredIndicatorVisible(true);
binder.forField(txtPassword).asRequired(MessageFormat.format(bundle.getString("common.error.isRequiredFor"), bundle.getString("common.password")))
.bind(HostEntity::getPassword, HostEntity::setPassword);
txtPassword.addStyleName("inline-label");
new CapsLockWarning(txtPassword);
form.addComponent(txtPassword);
但是,CapsLockWarningConnector
的构造函数确实没有运行,并且UI中没有任何操作