From 75f42d32229c2a4c5198a8d2b5ae5b2d90a7f8ff Mon Sep 17 00:00:00 2001 From: Mikael Grankvist Date: Tue, 1 Jul 2014 14:11:05 +0300 Subject: [PATCH] DelegateToWidget will now be run even for parent states for extending states (#14059) Updated the code to encompass Leif's suggestion. Change-Id: I70c0a4a93b9fe9ee8b2c458d666a1fec791f20b4 --- .../ConnectorBundleLoaderFactory.java | 19 ++++-- .../metadata/ConnectorBundle.java | 27 ++++---- .../metadata/WidgetInitVisitor.java | 2 +- .../ExtraSuperTextAreaConnector.java | 15 +++++ .../superText/ExtraSuperTextAreaState.java | 7 ++ .../superText/SuperTextAreaConnector.java | 19 ++++++ .../client/superText/SuperTextAreaState.java | 11 +++ .../widgetset/server/ExtraSuperTextArea.java | 16 +++++ .../server/OverriddenDecendants.java | 67 +++++++++++++++++++ .../server/OverriddenDecendantsTest.java | 50 ++++++++++++++ .../tests/widgetset/server/SuperTextArea.java | 16 +++++ 11 files changed, 229 insertions(+), 20 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/widgetset/client/superText/ExtraSuperTextAreaConnector.java create mode 100644 uitest/src/com/vaadin/tests/widgetset/client/superText/ExtraSuperTextAreaState.java create mode 100644 uitest/src/com/vaadin/tests/widgetset/client/superText/SuperTextAreaConnector.java create mode 100644 uitest/src/com/vaadin/tests/widgetset/client/superText/SuperTextAreaState.java create mode 100644 uitest/src/com/vaadin/tests/widgetset/server/ExtraSuperTextArea.java create mode 100644 uitest/src/com/vaadin/tests/widgetset/server/OverriddenDecendants.java create mode 100644 uitest/src/com/vaadin/tests/widgetset/server/OverriddenDecendantsTest.java create mode 100644 uitest/src/com/vaadin/tests/widgetset/server/SuperTextArea.java diff --git a/client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java b/client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java index 5519dd1aae..a6ca690a8a 100644 --- a/client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java +++ b/client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java @@ -639,13 +639,18 @@ public class ConnectorBundleLoaderFactory extends Generator { private void writeDelegateToWidget(TreeLogger logger, SplittingSourceWriter w, ConnectorBundle bundle) { - Set needsDelegateToWidget = bundle.getNeedsDelegateToWidget(); - for (Property property : needsDelegateToWidget) { - w.println("store.setDelegateToWidget(%s, \"%s\", \"%s\");", - getClassLiteralString(property.getBeanType()), - property.getName(), - property.getAnnotation(DelegateToWidget.class).value()); - + Map> needsDelegateToWidget = bundle + .getNeedsDelegateToWidget(); + for (Entry> entry : needsDelegateToWidget + .entrySet()) { + JClassType beanType = entry.getKey(); + for (Property property : entry.getValue()) { + w.println( + "store.setDelegateToWidget(%s, \"%s\", \"%s\");", + getClassLiteralString(beanType),// property.getBeanType()), + property.getName(), + property.getAnnotation(DelegateToWidget.class).value()); + } w.splitIfNeeded(); } } diff --git a/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorBundle.java b/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorBundle.java index 8bbcac4ecb..e8a384298f 100644 --- a/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorBundle.java +++ b/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorBundle.java @@ -1,12 +1,12 @@ /* * Copyright 2000-2014 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 @@ -38,6 +38,7 @@ import com.google.gwt.core.ext.typeinfo.JType; import com.google.gwt.core.ext.typeinfo.NotFoundException; import com.google.gwt.core.ext.typeinfo.TypeOracle; import com.google.gwt.json.client.JSONValue; +import com.google.gwt.thirdparty.guava.common.collect.Sets; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.ComponentConnector; import com.vaadin.client.ServerConnector; @@ -72,7 +73,7 @@ public class ConnectorBundle { private final Map> needsOnStateChange = new HashMap>(); private final Set needsProperty = new HashSet(); - private final Set needsDelegateToWidget = new HashSet(); + private final Map> needsDelegateToWidget = new HashMap>(); private ConnectorBundle(String name, ConnectorBundle previousBundle, Collection visitors, @@ -567,23 +568,25 @@ public class ConnectorBundle { } } - public void setNeedsDelegateToWidget(Property property) { - if (!isNeedsDelegateToWidget(property)) { - needsDelegateToWidget.add(property); + public void setNeedsDelegateToWidget(Property property, JClassType type) { + if (!isNeedsDelegateToWidget(type)) { + needsDelegateToWidget.put(type, Sets.newHashSet(property)); + } else if (!needsDelegateToWidget.get(type).contains(property)) { + needsDelegateToWidget.get(type).add(property); } } - private boolean isNeedsDelegateToWidget(Property property) { - if (needsDelegateToWidget.contains(property)) { + private boolean isNeedsDelegateToWidget(JClassType type) { + if (needsDelegateToWidget.containsKey(type)) { return true; } else { return previousBundle != null - && previousBundle.isNeedsDelegateToWidget(property); + && previousBundle.isNeedsDelegateToWidget(type); } } - public Set getNeedsDelegateToWidget() { - return Collections.unmodifiableSet(needsDelegateToWidget); + public Map> getNeedsDelegateToWidget() { + return Collections.unmodifiableMap(needsDelegateToWidget); } public void setNeedsOnStateChangeHandler(JClassType type, JMethod method) { diff --git a/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/WidgetInitVisitor.java b/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/WidgetInitVisitor.java index e3fee8d9ee..a77b523d14 100644 --- a/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/WidgetInitVisitor.java +++ b/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/WidgetInitVisitor.java @@ -59,7 +59,7 @@ public class WidgetInitVisitor extends TypeVisitor { .getAnnotation(DelegateToWidget.class); if (delegateToWidget != null) { // Generate meta data required for @DelegateToWidget - bundle.setNeedsDelegateToWidget(property); + bundle.setNeedsDelegateToWidget(property, stateType); // Find the delegate target method String methodName = DelegateToWidget.Helper diff --git a/uitest/src/com/vaadin/tests/widgetset/client/superText/ExtraSuperTextAreaConnector.java b/uitest/src/com/vaadin/tests/widgetset/client/superText/ExtraSuperTextAreaConnector.java new file mode 100644 index 0000000000..b9037208f9 --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/client/superText/ExtraSuperTextAreaConnector.java @@ -0,0 +1,15 @@ +package com.vaadin.tests.widgetset.client.superText; + +import com.vaadin.client.ui.textarea.TextAreaConnector; +import com.vaadin.shared.ui.Connect; +import com.vaadin.tests.widgetset.server.ExtraSuperTextArea; + +@Connect(ExtraSuperTextArea.class) +public class ExtraSuperTextAreaConnector extends TextAreaConnector { + + // @DelegateToWidget will not work with overridden state + @Override + public ExtraSuperTextAreaState getState() { + return (ExtraSuperTextAreaState) super.getState(); + } +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/widgetset/client/superText/ExtraSuperTextAreaState.java b/uitest/src/com/vaadin/tests/widgetset/client/superText/ExtraSuperTextAreaState.java new file mode 100644 index 0000000000..44456b27ba --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/client/superText/ExtraSuperTextAreaState.java @@ -0,0 +1,7 @@ +package com.vaadin.tests.widgetset.client.superText; + +import com.vaadin.shared.ui.textarea.TextAreaState; + +public class ExtraSuperTextAreaState extends TextAreaState { + +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/widgetset/client/superText/SuperTextAreaConnector.java b/uitest/src/com/vaadin/tests/widgetset/client/superText/SuperTextAreaConnector.java new file mode 100644 index 0000000000..fda1fc6f0a --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/client/superText/SuperTextAreaConnector.java @@ -0,0 +1,19 @@ +package com.vaadin.tests.widgetset.client.superText; + +import com.vaadin.client.ui.textarea.TextAreaConnector; +import com.vaadin.shared.ui.Connect; +import com.vaadin.tests.widgetset.server.SuperTextArea; + +/** + * @author artamonov + * @version $Id$ + */ +@Connect(SuperTextArea.class) +public class SuperTextAreaConnector extends TextAreaConnector { + + // @DelegateToWidget will not work with overridden state + @Override + public SuperTextAreaState getState() { + return (SuperTextAreaState) super.getState(); + } +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/widgetset/client/superText/SuperTextAreaState.java b/uitest/src/com/vaadin/tests/widgetset/client/superText/SuperTextAreaState.java new file mode 100644 index 0000000000..005075429c --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/client/superText/SuperTextAreaState.java @@ -0,0 +1,11 @@ +package com.vaadin.tests.widgetset.client.superText; + +import com.vaadin.shared.ui.textarea.TextAreaState; + +/** + * @author artamonov + * @version $Id$ + */ +public class SuperTextAreaState extends TextAreaState { + +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/widgetset/server/ExtraSuperTextArea.java b/uitest/src/com/vaadin/tests/widgetset/server/ExtraSuperTextArea.java new file mode 100644 index 0000000000..b741c099b5 --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/server/ExtraSuperTextArea.java @@ -0,0 +1,16 @@ +package com.vaadin.tests.widgetset.server; + +import com.vaadin.tests.widgetset.client.superText.SuperTextAreaState; +import com.vaadin.ui.TextArea; + +/** + * @author artamonov + * @version $Id$ + */ +public class ExtraSuperTextArea extends TextArea { + + @Override + public SuperTextAreaState getState() { + return (SuperTextAreaState) super.getState(); + } +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/widgetset/server/OverriddenDecendants.java b/uitest/src/com/vaadin/tests/widgetset/server/OverriddenDecendants.java new file mode 100644 index 0000000000..aadabb3fcc --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/server/OverriddenDecendants.java @@ -0,0 +1,67 @@ +/* + * Copyright 2000-2014 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.widgetset.server; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.widgetset.TestingWidgetSet; +import com.vaadin.ui.TextArea; + +/** + * UI for testing that @DelegateToWidget works on derived widget states. + * + * @since + * @author Vaadin Ltd + */ +@Widgetset(TestingWidgetSet.NAME) +public class OverriddenDecendants extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + + TextArea normalTextArea = new TextArea(); + normalTextArea.setRows(10); + normalTextArea.setWordwrap(true); + + getLayout().addComponent(normalTextArea); + + // @DelegateToWidget will not work with overridden state in connector + SuperTextArea superTextArea = new SuperTextArea(); + superTextArea.setRows(10); + superTextArea.setWordwrap(true); + + getLayout().addComponent(superTextArea); + + // @DelegateToWidget will not work with overridden state in connector + ExtraSuperTextArea extraSuperTextArea = new ExtraSuperTextArea(); + extraSuperTextArea.setRows(10); + extraSuperTextArea.setWordwrap(true); + + getLayout().addComponent(extraSuperTextArea); + } + + @Override + protected String getTestDescription() { + return "@DelegateToWidget does not work for widget descendants with overridden getState"; + } + + @Override + protected Integer getTicketNumber() { + return 14059; + } + +} diff --git a/uitest/src/com/vaadin/tests/widgetset/server/OverriddenDecendantsTest.java b/uitest/src/com/vaadin/tests/widgetset/server/OverriddenDecendantsTest.java new file mode 100644 index 0000000000..aa29284010 --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/server/OverriddenDecendantsTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2014 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.widgetset.server; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Test; + +import com.vaadin.testbench.elements.TextAreaElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Class for unit testing that @DelegateToWidget works on derived widget states. + * + * @since + * @author Vaadin Ltd + */ +public class OverriddenDecendantsTest extends MultiBrowserTest { + + @Test + public void allExtendingFieldsShouldGetRowsFromTextAreaStateAnnotation() + throws InterruptedException { + openTestURL(); + + List textAreas = $(TextAreaElement.class).all(); + + assertEquals("Did not contain all 3 text areas", 3, textAreas.size()); + + for (TextAreaElement area : textAreas) { + assertEquals("Text area was missing rows", "10", + area.getAttribute("rows")); + } + + } +} diff --git a/uitest/src/com/vaadin/tests/widgetset/server/SuperTextArea.java b/uitest/src/com/vaadin/tests/widgetset/server/SuperTextArea.java new file mode 100644 index 0000000000..6e73915e44 --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/server/SuperTextArea.java @@ -0,0 +1,16 @@ +package com.vaadin.tests.widgetset.server; + +import com.vaadin.tests.widgetset.client.superText.SuperTextAreaState; +import com.vaadin.ui.TextArea; + +/** + * @author artamonov + * @version $Id$ + */ +public class SuperTextArea extends TextArea { + + @Override + public SuperTextAreaState getState() { + return (SuperTextAreaState) super.getState(); + } +} \ No newline at end of file -- 2.39.5