Issue 5518 Fix: Panel Alignment Attributes Have No Effect Review at http://gwt-code-reviews.appspot.com/1109801 Review by: jgw@google.com git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9241 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/uibinder/elementparsers/HasAlignmentParser.java b/user/src/com/google/gwt/uibinder/elementparsers/HasAlignmentParser.java new file mode 100644 index 0000000..031088f --- /dev/null +++ b/user/src/com/google/gwt/uibinder/elementparsers/HasAlignmentParser.java
@@ -0,0 +1,79 @@ +/* + * Copyright 2010 Google Inc. + * + * 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.google.gwt.uibinder.elementparsers; + +import com.google.gwt.core.ext.UnableToCompleteException; +import com.google.gwt.core.ext.typeinfo.JClassType; +import com.google.gwt.uibinder.rebind.UiBinderWriter; +import com.google.gwt.uibinder.rebind.XMLElement; +import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant; +import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant; + +/** + * Parses widgets that inherit from {@link com.google.gwt.user.client.ui.HasAlignment}. + * This class is needed to resolve the parse order of alignment attributes for these + * classes. + * <p> + * + * See {@link "http://code.google.com/p/google-web-toolkit/issues/detail?id=5518"} for + * issue details. + */ +public class HasAlignmentParser implements ElementParser { + + /** + * Parses widgets that inherit from {@link com.google.gwt.user.client.ui.HasHorizontalAlignment}. + */ + private class HasHorizontalAlignmentParser implements ElementParser { + + public void parse(XMLElement elem, String fieldName, JClassType type, + UiBinderWriter writer) throws UnableToCompleteException { + JClassType hAlignConstantType = writer.getOracle().findType( + HorizontalAlignmentConstant.class.getCanonicalName()); + + String horizontalAlignment = elem.consumeAttributeWithDefault("horizontalAlignment", + null, hAlignConstantType); + + if (horizontalAlignment != null) { + writer.addStatement("%s.setHorizontalAlignment(%s);", fieldName, horizontalAlignment); + } + } + } + + /** + * Parses widgets that inherit from {@link com.google.gwt.user.client.ui.HasVerticalAlignment}. + */ + private class HasVerticalAlignmentParser implements ElementParser { + + public void parse(XMLElement elem, String fieldName, JClassType type, + UiBinderWriter writer) throws UnableToCompleteException { + JClassType vAlignConstantType = writer.getOracle().findType( + VerticalAlignmentConstant.class.getCanonicalName()); + + String verticalAlignment = elem.consumeAttributeWithDefault("verticalAlignment", + null, vAlignConstantType); + + if (verticalAlignment != null) { + writer.addStatement("%s.setVerticalAlignment(%s);", fieldName, verticalAlignment); + } + } + } + + public void parse(XMLElement elem, String fieldName, JClassType type, + UiBinderWriter writer) throws UnableToCompleteException { + new HasVerticalAlignmentParser().parse(elem, fieldName, type, writer); + new HasHorizontalAlignmentParser().parse(elem, fieldName, type, writer); + } +}
diff --git a/user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java b/user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java index 2ae08f1..e807ac6 100644 --- a/user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java +++ b/user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java
@@ -997,6 +997,7 @@ addWidgetParser("Image"); addWidgetParser("ListBox"); addWidgetParser("Grid"); + addWidgetParser("HasAlignment"); } /**
diff --git a/user/test/com/google/gwt/uibinder/elementparsers/HasAlignmentParserTest.java b/user/test/com/google/gwt/uibinder/elementparsers/HasAlignmentParserTest.java new file mode 100644 index 0000000..39bc0f4 --- /dev/null +++ b/user/test/com/google/gwt/uibinder/elementparsers/HasAlignmentParserTest.java
@@ -0,0 +1,97 @@ +/* + * Copyright 2010 Google Inc. + * + * 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.google.gwt.uibinder.elementparsers; + +import com.google.gwt.core.ext.UnableToCompleteException; + +import junit.framework.TestCase; + +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import java.io.IOException; +import java.util.Iterator; + +/** + * A unit test. Guess what of. + */ +public class HasAlignmentParserTest extends TestCase { + private static final String PARSED_TYPE = "com.google.gwt.user.client.ui.DockLayoutPanel"; + + private ElementParserTester tester; + + @Override + public void setUp() throws Exception { + super.setUp(); + tester = new ElementParserTester(PARSED_TYPE, new HasAlignmentParser()); + } + + public void testInvalidVerticalAlignment() throws SAXException, IOException { + StringBuffer b = new StringBuffer(); + b.append("<g:DockLayoutPanel verticalAlignment='FOO'>"); + b.append("</g:DockLayoutPanel>"); + + try { + tester.parse(b.toString()); + fail(); + } catch (UnableToCompleteException e) { + assertTrue("Expect vertical alignment parse error", + tester.logger.died.contains("Cannot parse value: \"FOO\"")); + } + } + + public void testInvalidHorizontalAlignment() throws SAXException, IOException { + StringBuffer b = new StringBuffer(); + b.append("<g:DockLayoutPanel horizontalAlignment='BAR'>"); + b.append("</g:DockLayoutPanel>"); + + try { + tester.parse(b.toString()); + fail(); + } catch (UnableToCompleteException e) { + assertTrue("Expect horizontal alignment parse error", + tester.logger.died.contains("Cannot parse value: \"BAR\"")); + } + } + + public void testNoAlignmentArgs() throws UnableToCompleteException, SAXParseException { + StringBuffer b = new StringBuffer(); + b.append("<g:DockLayoutPanel>"); + b.append("</g:DockLayoutPanel>"); + + tester.parse(b.toString()); + assertTrue(tester.writer.statements.isEmpty()); + } + + public void testValidArgs() throws UnableToCompleteException, SAXParseException { + StringBuffer b = new StringBuffer(); + b.append("<g:DockLayoutPanel verticalAlignment='ALIGN_MIDDLE' horizontalAlignment='ALIGN_LEFT'>"); + b.append("</g:DockLayoutPanel>"); + + tester.parse(b.toString()); + assertStatements("fieldName.setVerticalAlignment(com.google.gwt.user.client.ui.HasVerticalAlignment.ALIGN_MIDDLE);", + "fieldName.setHorizontalAlignment(com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_LEFT);"); + } + + private void assertStatements(String... expected) { + Iterator<String> i = tester.writer.statements.iterator(); + for (String e : expected) { + assertEquals(e, i.next()); + } + assertFalse(i.hasNext()); + assertNull(tester.logger.died); + } +}
diff --git a/user/test/com/google/gwt/uibinder/test/client/UiBinderTest.java b/user/test/com/google/gwt/uibinder/test/client/UiBinderTest.java index 7eee4f4..56ba5c4 100644 --- a/user/test/com/google/gwt/uibinder/test/client/UiBinderTest.java +++ b/user/test/com/google/gwt/uibinder/test/client/UiBinderTest.java
@@ -573,6 +573,12 @@ "<td>Lately, anyway.</td>"); } + public void testAlignmentAttributes() { + assertInOrder(widgetUi.myHorizontalPanel.getElement().getInnerHTML(), + "<td style=\"vertical-align: middle;\" align=\"left\">", + "class=\"gwt-StackPanelItem"); + } + /** * Assert that the expect strings are found in body, and in the order given. * WARNING: both body and expected are normalized to lower case, to get around
diff --git a/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.java b/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.java index b076e93..766ce98 100644 --- a/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.java +++ b/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.java
@@ -38,6 +38,7 @@ import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.HasHTML; +import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Image; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.ListBox; @@ -97,6 +98,7 @@ @UiField RadioButton myRadioAble; @UiField RadioButton myRadioBaker; @UiField StackPanel myStackPanel; + @UiField HorizontalPanel myHorizontalPanel; @UiField Widget myStackPanelItem; @UiField DisclosurePanel myDisclosurePanel; @UiField Widget myDisclosurePanelItem;
diff --git a/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.ui.xml b/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.ui.xml index f210d36..b4d3710 100644 --- a/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.ui.xml +++ b/user/test/com/google/gwt/uibinder/test/client/WidgetBasedUi.ui.xml
@@ -303,7 +303,7 @@ <ui:attribute name="text" description="radio button name"/> </demo:PointlessRadioButtonSubclass> - <gwt:HorizontalPanel horizontalAlignment="ALIGN_LEFT"> + <gwt:HorizontalPanel ui:field="myHorizontalPanel" horizontalAlignment="ALIGN_LEFT" verticalAlignment="ALIGN_MIDDLE"> <gwt:Cell><gwt:HTMLPanel> <p> ... a StackPanel ... </p>