You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/07/09 21:25:29 UTC
svn commit: r962657 - in /pivot/trunk:
core/src/org/apache/pivot/beans/BXMLSerializer.java
demos/src/org/apache/pivot/demos/binding/NamespaceBindingDemo.java
demos/src/org/apache/pivot/demos/binding/namespace_binding_demo.bxml
Author: gbrown
Date: Fri Jul 9 19:25:28 2010
New Revision: 962657
URL: http://svn.apache.org/viewvc?rev=962657&view=rev
Log:
Post-resolve all object references in BXMLSerializer (this allows a caller to reference a variable before it is defined, similar to a class member in Java); re-integrate namespace binding support.
Modified:
pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
pivot/trunk/demos/src/org/apache/pivot/demos/binding/NamespaceBindingDemo.java
pivot/trunk/demos/src/org/apache/pivot/demos/binding/namespace_binding_demo.bxml
Modified: pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java?rev=962657&r1=962656&r2=962657&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java Fri Jul 9 19:25:28 2010
@@ -418,19 +418,60 @@ public class BXMLSerializer implements S
// Resolve object references
for (Attribute attribute : objectReferenceAttributes) {
- Object value = JSON.get(namespace, (String)attribute.value);
+ Element element = attribute.element;
+ String reference = (String)attribute.value;
- if (attribute.propertyClass == null) {
- Dictionary<String, Object> dictionary;
- if (attribute.element.value instanceof Dictionary<?, ?>) {
- dictionary = (Dictionary<String, Object>)attribute.element.value;
- } else {
- dictionary = new BeanAdapter(attribute.element.value);
- }
+ if (reference.startsWith(Character.toString(NAMESPACE_BINDING_PREFIX))
+ && reference.endsWith(Character.toString(NAMESPACE_BINDING_SUFFIX))) {
+ reference = reference.substring(1, reference.length() - 1);
+
+ switch (element.type) {
+ case INSTANCE:
+ case INCLUDE: {
+ // Bind to <element ID>.<attribute name>
+ if (element.id == null) {
+ throw new SerializationException("Bind target does not have an ID.");
+ }
+
+ String targetPath = element.id + "." + attribute.name;
+ NamespaceBinding namespaceBinding = new NamespaceBinding(namespace, reference, targetPath);
+ namespaceBinding.bind();
+
+ break;
+ }
+
+ case READ_ONLY_PROPERTY: {
+ // Bind to <parent element ID>.<element name>.<attribute name>
+ if (element.parent.id == null) {
+ throw new SerializationException("Bind target does not have an ID.");
+ }
- dictionary.put(attribute.name, value);
+ String targetPath = element.parent.id + "." + element.name + "." + attribute.name;
+ NamespaceBinding namespaceBinding = new NamespaceBinding(namespace, reference, targetPath);
+ namespaceBinding.bind();
+
+ break;
+ }
+ }
} else {
- setStaticProperty(element.value, attribute.propertyClass, attribute.name, value);
+ if (!JSON.containsKey(namespace, reference)) {
+ throw new SerializationException("Value \"" + reference + "\" does not exist.");
+ }
+
+ Object value = JSON.get(namespace, reference);
+
+ if (attribute.propertyClass == null) {
+ Dictionary<String, Object> dictionary;
+ if (element.value instanceof Dictionary<?, ?>) {
+ dictionary = (Dictionary<String, Object>)element.value;
+ } else {
+ dictionary = new BeanAdapter(element.value);
+ }
+
+ dictionary.put(attribute.name, value);
+ } else {
+ setStaticProperty(element.value, attribute.propertyClass, attribute.name, value);
+ }
}
}
@@ -608,7 +649,7 @@ public class BXMLSerializer implements S
}
elementType = Element.Type.INSTANCE;
- name = "<" + prefix + ":" + localName + ">";
+ name = "<" + ((prefix == null) ? "" : prefix + ":") + localName + ">";
String className = namespaceURI + "." + localName.replace('.', '$');
@@ -878,9 +919,15 @@ public class BXMLSerializer implements S
if (value.charAt(0) == OBJECT_REFERENCE_PREFIX) {
attribute.value = value;
} else {
- if (attribute.propertyClass != null
- && attribute.propertyClass.isInterface()) {
- throw new SerializationException("An object reference is not valid in this context.");
+ if (attribute.propertyClass != null) {
+ if (attribute.propertyClass.isInterface()) {
+ throw new SerializationException("An object reference is not valid in this context.");
+ }
+
+ if (value.startsWith(Character.toString(NAMESPACE_BINDING_PREFIX))
+ && value.endsWith(Character.toString(NAMESPACE_BINDING_SUFFIX))) {
+ throw new SerializationException("Cannot bind to a static property.");
+ }
}
attribute.value = value;
Modified: pivot/trunk/demos/src/org/apache/pivot/demos/binding/NamespaceBindingDemo.java
URL: http://svn.apache.org/viewvc/pivot/trunk/demos/src/org/apache/pivot/demos/binding/NamespaceBindingDemo.java?rev=962657&r1=962656&r2=962657&view=diff
==============================================================================
--- pivot/trunk/demos/src/org/apache/pivot/demos/binding/NamespaceBindingDemo.java (original)
+++ pivot/trunk/demos/src/org/apache/pivot/demos/binding/NamespaceBindingDemo.java Fri Jul 9 19:25:28 2010
@@ -27,6 +27,7 @@ import org.apache.pivot.wtk.Window;
public class NamespaceBindingDemo extends Window implements Bindable {
@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
+ // Manually bind list button selection to label text
NamespaceBinding namespaceBinding = new NamespaceBinding(namespace,
"listButton.selectedIndex", "listButtonLabel.text");
namespaceBinding.bind();
Modified: pivot/trunk/demos/src/org/apache/pivot/demos/binding/namespace_binding_demo.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/demos/src/org/apache/pivot/demos/binding/namespace_binding_demo.bxml?rev=962657&r1=962656&r2=962657&view=diff
==============================================================================
--- pivot/trunk/demos/src/org/apache/pivot/demos/binding/namespace_binding_demo.bxml (original)
+++ pivot/trunk/demos/src/org/apache/pivot/demos/binding/namespace_binding_demo.bxml Fri Jul 9 19:25:28 2010
@@ -26,7 +26,6 @@ limitations under the License.
<Label bxml:id="textInputLabel" Form.label="Text" text="${textInput.text}"/>
</Form.Section>
- <!--
<Form.Section heading="Two-Way Binding">
<TextInput bxml:id="textInput1" Form.label="Text Input 1" text="${textInput2.text}"/>
<TextInput bxml:id="textInput2" Form.label="Text Input 2" text="${textInput1.text}"/>
@@ -35,13 +34,13 @@ limitations under the License.
<Form.Section heading="Style Binding">
<ColorChooserButton bxml:id="colorChooserButton" Form.label="Color Chooser Button"
selectedColor="#000000"/>
- <Label Form.label="Selected Color" text="${colorChooserButton.selectedColor}">
+ <Label bxml:id="colorChooserLabel" Form.label="Selected Color"
+ text="${colorChooserButton.selectedColor}">
<styles color="${colorChooserButton.selectedColor}"/>
</Label>
</Form.Section>
- -->
- <Form.Section>
+ <Form.Section heading="Manual Binding">
<ListButton bxml:id="listButton" Form.label="List Button"
listData="['Zero', 'One', 'Two', 'Three']" selectedIndex="0"/>
<Label bxml:id="listButtonLabel" Form.label="Selected Index"/>