Issue 5518 Fix: Panel Alignment Attributes Have No Effect
Fix Alignment Attribute Parsing for HasAlignment
Review at http://gwt-code-reviews.appspot.com/1121801
Review by: rjrjr@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9257 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..c490ec8
--- /dev/null
+++ b/user/src/com/google/gwt/uibinder/elementparsers/HasAlignmentParser.java
@@ -0,0 +1,64 @@
+/*
+ * 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 {
+ public void parse(XMLElement elem, String fieldName, JClassType type,
+ UiBinderWriter writer) throws UnableToCompleteException {
+
+ // Get fully qualified class name for horizontal alignment
+ JClassType hAlignConstantType = writer.getOracle().findType(
+ HorizontalAlignmentConstant.class.getCanonicalName());
+ // Get horizontal alignment value
+ String horizontalAlignment = elem.consumeAttributeWithDefault(
+ "horizontalAlignment", null, hAlignConstantType);
+ // Set horizontal alignment if not null
+ if (horizontalAlignment != null) {
+ writer.addStatement("%s.setHorizontalAlignment(%s);", fieldName,
+ horizontalAlignment);
+ }
+
+ // Get fully qualified class name for vertical alignment
+ JClassType vAlignConstantType = writer.getOracle().findType(
+ VerticalAlignmentConstant.class.getCanonicalName());
+ // Get vertical alignment value
+ String verticalAlignment = elem.consumeAttributeWithDefault(
+ "verticalAlignment", null, vAlignConstantType);
+ // Set vertical alignment if not null
+ if (verticalAlignment != null) {
+ writer.addStatement("%s.setVerticalAlignment(%s);", fieldName,
+ verticalAlignment);
+ }
+ }
+}
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..e3120c8
--- /dev/null
+++ b/user/test/com/google/gwt/uibinder/elementparsers/HasAlignmentParserTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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.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 {
+ 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 {
+ 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.setHorizontalAlignment(com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_LEFT);",
+ "fieldName.setVerticalAlignment(com.google.gwt.user.client.ui.HasVerticalAlignment.ALIGN_MIDDLE);");
+ }
+
+ 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..7201ea0 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,23 @@
"<td>Lately, anyway.</td>");
}
+ public void testAlignmentAttributes() {
+ assertEquals(
+ widgetUi.myHorizontalPanel.getHorizontalAlignment().getTextAlignString(),
+ "left");
+ assertEquals(
+ widgetUi.myHorizontalPanel.getVerticalAlignment().getVerticalAlignString(),
+ "middle");
+
+ final String innerHtml =
+ widgetUi.myHorizontalPanel.getElement().getInnerHTML();
+ assertInOrder(innerHtml, "vertical-align: middle",
+ "a stackpanel");
+
+ final String innerHtml2 = innerHtml.replace("\"", "");
+ assertInOrder(innerHtml2, "align=left", "a stackpanel");
+ }
+
/**
* 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>