Browse Source

Find active connector anywhere in the DOM (#9429)

Fixes #9419
tags/8.1.0.beta2
Artur 7 years ago
parent
commit
3d6a25c2f1

+ 5
- 3
client/src/main/java/com/vaadin/client/ApplicationConnection.java View File

@@ -39,6 +39,7 @@ import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ApplicationConfiguration.ErrorMessage;
import com.vaadin.client.ApplicationConnection.ApplicationStoppedEvent;
@@ -1478,16 +1479,17 @@ public class ApplicationConnection implements HasHandlers {
}

/**
* Gets the active connector for focused element in browser.
* Gets the active connector for the focused element in the browser.
*
* @return Connector for focused element or null.
* @return the connector for the focused element or <code>null</code> if
* none found or no element is focused.
*/
private ComponentConnector getActiveConnector() {
Element focusedElement = WidgetUtil.getFocusedElement();
if (focusedElement == null) {
return null;
}
return Util.getConnectorForElement(this, getUIConnector().getWidget(),
return Util.getConnectorForElement(this, RootPanel.get(),
focusedElement);
}


+ 64
- 0
uitest/src/main/java/com/vaadin/tests/components/window/ShortcutInWindow.java View File

@@ -0,0 +1,64 @@
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.components.window;

import com.vaadin.annotations.Widgetset;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.ValueChangeMode;
import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Widgetset("com.vaadin.DefaultWidgetSet")
public class ShortcutInWindow extends AbstractTestUIWithLog {

@Override
protected void setup(VaadinRequest request) {
VerticalLayout content = new VerticalLayout();
HorizontalLayout buttons = new HorizontalLayout();
final TextField name = new TextField("Name");
name.setValueChangeMode(ValueChangeMode.BLUR);
name.setValueChangeTimeout(1000);
name.addValueChangeListener(
(e) -> log("Value Changed: " + e.getValue()));
final Button toggle = new Button(name.getValueChangeMode().toString());
toggle.addClickListener((e) -> {
int o = name.getValueChangeMode().ordinal();
int i = ValueChangeMode.values().length <= o + 1 ? 0 : o + 1;
ValueChangeMode m = ValueChangeMode.values()[i];
toggle.setCaption(m.toString());
name.setValueChangeMode(m);
log("New ValueChangeMode: " + m.toString());
});
final Button submit = new Button("Submit",
(e) -> log("Submitted value: " + name.getValue()));
submit.setClickShortcut(KeyCode.ENTER);
buttons.addComponent(toggle);
buttons.addComponent(submit);
content.addComponent(name);
content.addComponent(buttons);
Window popup = new Window();
popup.center();
popup.setContent(content);
getUI().addWindow(popup);

}

}

+ 34
- 0
uitest/src/test/java/com/vaadin/tests/components/window/ShortcutInWindowTest.java View File

@@ -0,0 +1,34 @@
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.components.window;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Keys;

import com.vaadin.testbench.elements.TextFieldElement;
import com.vaadin.tests.tb3.SingleBrowserTest;

public class ShortcutInWindowTest extends SingleBrowserTest {

@Test
public void shortcutFlushesActiveField() {
openTestURL();
TextFieldElement tf = $(TextFieldElement.class).first();
tf.sendKeys("foo" + Keys.ENTER);
Assert.assertEquals("2. Submitted value: foo", getLogRow(0));
}
}

Loading…
Cancel
Save