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/02 18:40:37 UTC
svn commit: r960051 - in /pivot/trunk: core/src/org/apache/pivot/beans/
core/src/org/apache/pivot/json/ core/test/org/apache/pivot/json/test/
tests/src/org/apache/pivot/tests/
tutorials/src/org/apache/pivot/tutorials/explorer/tools/
wtk/src/org/apache/...
Author: gbrown
Date: Fri Jul 2 16:40:36 2010
New Revision: 960051
URL: http://svn.apache.org/viewvc?rev=960051&view=rev
Log:
Allow instance attributes on <bxml:includes>; support non-parameterized sequence and dictionary types in JSONSerializer.
Modified:
pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java
pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java
pivot/trunk/tests/src/org/apache/pivot/tests/include_test.bxml
pivot/trunk/tests/src/org/apache/pivot/tests/include_test.content.bxml
pivot/trunk/tutorials/src/org/apache/pivot/tutorials/explorer/tools/ComponentStyleInspectorSkin.java
pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java
pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentStyleListener.java
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=960051&r1=960050&r2=960051&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 2 16:40:36 2010
@@ -700,8 +700,7 @@ public class BXMLSerializer implements S
ArrayList<Attribute> staticPropertyAttributes = new ArrayList<Attribute>();
if (element.type == Element.Type.INCLUDE) {
- // Process attributes looking for src, resources, and static property
- // setters only
+ // Process attributes looking for src, resources, and property setters
String src = null;
Resources resources = this.resources;
boolean inline = false;
@@ -713,14 +712,10 @@ public class BXMLSerializer implements S
resources = new Resources(resources, attribute.value);
} else if (attribute.localName.equals(INCLUDE_INLINE_ATTRIBUTE)) {
inline = Boolean.parseBoolean(attribute.value);
- } else {
- if (!Character.isUpperCase(attribute.localName.charAt(0))) {
- throw new SerializationException("Instance property setters are not"
- + " supported for " + internalNamespacePrefix + ":" + INCLUDE_TAG
- + " " + " tag.");
- }
-
+ } else if (Character.isUpperCase(attribute.localName.charAt(0))) {
staticPropertyAttributes.add(attribute);
+ } else {
+ instancePropertyAttributes.add(attribute);
}
}
@@ -748,7 +743,7 @@ public class BXMLSerializer implements S
element.value = serializer.readObject(new URL(location, src));
}
} else {
- // Process attributes looking for all property setters
+ // Process attributes looking for property setters
for (Attribute attribute : element.attributes) {
if (Character.isUpperCase(attribute.localName.charAt(0))) {
staticPropertyAttributes.add(attribute);
@@ -789,35 +784,6 @@ public class BXMLSerializer implements S
dictionary.put(attribute.localName, resolve(attribute.value));
}
- // If the element's parent has a default property, use it; otherwise, if the
- // parent is a sequence or a listener list, add the element to it
- if (element.parent != null
- && element.parent.value != null) {
- Class<?> parentType = element.parent.value.getClass();
- DefaultProperty defaultProperty = parentType.getAnnotation(DefaultProperty.class);
-
- if (defaultProperty == null) {
- if (element.parent.value instanceof Sequence<?>) {
- Sequence<Object> sequence = (Sequence<Object>)element.parent.value;
- sequence.add(element.value);
- } else if (element.parent.value instanceof ListenerList<?>) {
- ListenerList<Object> listenerList = (ListenerList<Object>)element.parent.value;
- listenerList.add(element.value);
- }
- } else {
- String defaultPropertyName = defaultProperty.value();
- BeanAdapter beanAdapter = new BeanAdapter(element.parent.value);
- Object defaultPropertyValue = beanAdapter.get(defaultPropertyName);
-
- if (defaultPropertyValue instanceof Sequence<?>) {
- Sequence<Object> sequence = (Sequence<Object>)defaultPropertyValue;
- sequence.add(element.value);
- } else {
- beanAdapter.put(defaultPropertyName, element.value);
- }
- }
- }
-
// Apply static attributes
if (element.value instanceof Dictionary<?, ?>) {
if (staticPropertyAttributes.getLength() > 0) {
@@ -901,11 +867,40 @@ public class BXMLSerializer implements S
}
}
- // If the parent element is a writable property, set this as its
- // value; it will be applied later in the parent's closing tag
- if (element.parent != null
- && element.parent.type == Element.Type.WRITABLE_PROPERTY) {
- element.parent.value = element.value;
+ if (element.parent != null) {
+ // If the parent element has a default property, use it; otherwise, if the
+ // parent is a sequence or a listener list, add the element to it
+ if (element.parent.value != null) {
+ Class<?> parentType = element.parent.value.getClass();
+ DefaultProperty defaultProperty = parentType.getAnnotation(DefaultProperty.class);
+
+ if (defaultProperty == null) {
+ if (element.parent.value instanceof Sequence<?>) {
+ Sequence<Object> sequence = (Sequence<Object>)element.parent.value;
+ sequence.add(element.value);
+ } else if (element.parent.value instanceof ListenerList<?>) {
+ ListenerList<Object> listenerList = (ListenerList<Object>)element.parent.value;
+ listenerList.add(element.value);
+ }
+ } else {
+ String defaultPropertyName = defaultProperty.value();
+ BeanAdapter beanAdapter = new BeanAdapter(element.parent.value);
+ Object defaultPropertyValue = beanAdapter.get(defaultPropertyName);
+
+ if (defaultPropertyValue instanceof Sequence<?>) {
+ Sequence<Object> sequence = (Sequence<Object>)defaultPropertyValue;
+ sequence.add(element.value);
+ } else {
+ beanAdapter.put(defaultPropertyName, element.value);
+ }
+ }
+ }
+
+ // If the parent element is a writable property, set this as its
+ // value; it will be applied later in the parent's closing tag
+ if (element.parent.type == Element.Type.WRITABLE_PROPERTY) {
+ element.parent.value = element.value;
+ }
}
break;
Modified: pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java?rev=960051&r1=960050&r2=960051&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java Fri Jul 2 16:40:36 2010
@@ -452,11 +452,25 @@ public class JSONSerializer implements S
// Get the target item type
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
itemType = actualTypeArguments[0];
- } else if (type == Object.class) {
- sequence = new ArrayList<Object>();
- itemType = Object.class;
} else {
- throw new IllegalArgumentException("Cannot convert array to " + type + ".");
+ Class<?> classType = (Class<?>)type;
+
+ if (Sequence.class.isAssignableFrom(classType)) {
+ try {
+ sequence = (Sequence<Object>)classType.newInstance();
+ } catch (InstantiationException exception) {
+ throw new IllegalArgumentException(exception);
+ } catch (IllegalAccessException exception) {
+ throw new IllegalArgumentException(exception);
+ }
+
+ itemType = Object.class;
+ } else if (type == Object.class) {
+ sequence = new ArrayList<Object>();
+ itemType = Object.class;
+ } else {
+ throw new IllegalArgumentException("Cannot convert array to " + type + ".");
+ }
}
// Move to the next character after '['
@@ -511,20 +525,33 @@ public class JSONSerializer implements S
// Get the target value type
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
valueType = actualTypeArguments[1];
- } else if (type == Object.class){
- dictionary = new HashMap<String, Object>();
- valueType = Object.class;
} else {
- Class<?> beanType = (Class<?>)type;
- try {
- dictionary = new BeanAdapter(beanType.newInstance());
- } catch (InstantiationException exception) {
- throw new RuntimeException(exception);
- } catch (IllegalAccessException exception) {
- throw new RuntimeException(exception);
- }
+ Class<?> classType = (Class<?>)type;
+ if (Dictionary.class.isAssignableFrom(classType)) {
+ try {
+ dictionary = (Dictionary<String, Object>)classType.newInstance();
+ } catch (InstantiationException exception) {
+ throw new IllegalArgumentException(exception);
+ } catch (IllegalAccessException exception) {
+ throw new IllegalArgumentException(exception);
+ }
- valueType = null;
+ valueType = Object.class;
+ } else if (type == Object.class){
+ dictionary = new HashMap<String, Object>();
+ valueType = Object.class;
+ } else {
+ Class<?> beanType = (Class<?>)type;
+ try {
+ dictionary = new BeanAdapter(beanType.newInstance());
+ } catch (InstantiationException exception) {
+ throw new RuntimeException(exception);
+ } catch (IllegalAccessException exception) {
+ throw new RuntimeException(exception);
+ }
+
+ valueType = null;
+ }
}
// Move to the next character after '{'
Modified: pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java?rev=960051&r1=960050&r2=960051&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/BindTest.java Fri Jul 2 16:40:36 2010
@@ -19,7 +19,11 @@ package org.apache.pivot.json.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
+import java.io.StringReader;
+import org.apache.pivot.collections.ArrayList;
+import org.apache.pivot.collections.HashMap;
+import org.apache.pivot.collections.List;
import org.apache.pivot.json.JSON;
import org.apache.pivot.json.JSONSerializer;
import org.apache.pivot.serialization.SerializationException;
@@ -27,6 +31,7 @@ import org.junit.Test;
public class BindTest {
@Test
+ @SuppressWarnings("unchecked")
public void testBind() throws IOException, SerializationException {
JSONSerializer objectSerializer = new JSONSerializer();
Object sampleObject = objectSerializer.readObject(getClass().getResourceAsStream("sample.json"));
@@ -40,5 +45,13 @@ public class BindTest {
assertEquals(sampleBean.getD(), JSON.get(sampleObject, "d"));
assertEquals(sampleBean.getE(), JSON.get(sampleObject, "e"));
assertEquals(sampleBean.getI().getA(), JSON.get(sampleObject, "i.a"));
+
+ JSONSerializer listSerializer = new JSONSerializer(ArrayList.class);
+ List<?> list = (List<?>)listSerializer.readObject(new StringReader("[1, 2, 3, 4, 5]"));
+ assertEquals(list.get(0), 1);
+
+ JSONSerializer mapSerializer = new JSONSerializer(HashMap.class);
+ HashMap<String, ?> map = (HashMap<String, ?>)mapSerializer.readObject(new StringReader("{a:1, b:2, c:3}"));
+ assertEquals(map.get("a"), 1);
}
}
Modified: pivot/trunk/tests/src/org/apache/pivot/tests/include_test.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/include_test.bxml?rev=960051&r1=960050&r2=960051&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/include_test.bxml (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/include_test.bxml Fri Jul 2 16:40:36 2010
@@ -29,5 +29,5 @@ limitations under the License.
</bxml:script>
</windowStateListeners>
- <bxml:include src="include_test.content.bxml" inline="true"/>
+ <bxml:include src="include_test.content.bxml" text="Hello, World!" inline="true"/>
</Window>
Modified: pivot/trunk/tests/src/org/apache/pivot/tests/include_test.content.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/include_test.content.bxml?rev=960051&r1=960050&r2=960051&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/include_test.content.bxml (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/include_test.content.bxml Fri Jul 2 16:40:36 2010
@@ -16,7 +16,7 @@ See the License for the specific languag
limitations under the License.
-->
-<Label bxml:id="label" text="Hello, World!"
+<Label bxml:id="label"
styles="{horizontalAlignment:'center', verticalAlignment:'center'}"
xmlns:bxml="http://pivot.apache.org/bxml"
xmlns="org.apache.pivot.wtk"/>
Modified: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/explorer/tools/ComponentStyleInspectorSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/explorer/tools/ComponentStyleInspectorSkin.java?rev=960051&r1=960050&r2=960051&view=diff
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/explorer/tools/ComponentStyleInspectorSkin.java (original)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/explorer/tools/ComponentStyleInspectorSkin.java Fri Jul 2 16:40:36 2010
@@ -24,7 +24,7 @@ import org.apache.pivot.wtk.ComponentSty
import org.apache.pivot.wtk.Form;
class ComponentStyleInspectorSkin extends ComponentInspectorSkin {
- private ComponentStyleListener componentStyleHandler = new ComponentStyleListener.Adapter() {
+ private ComponentStyleListener componentStyleHandler = new ComponentStyleListener() {
@Override
public void styleUpdated(Component component, String key, Object previousValue) {
Component.StyleDictionary styles = component.getStyles();
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java?rev=960051&r1=960050&r2=960051&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Component.java Fri Jul 2 16:40:36 2010
@@ -416,20 +416,6 @@ public abstract class Component implemen
listener.styleUpdated(component, styleKey, previousValue);
}
}
-
- @Override
- public void styleClassNameAdded(Component component, String styleClassName) {
- for (ComponentStyleListener listener : this) {
- listener.styleClassNameAdded(component, styleClassName);
- }
- }
-
- @Override
- public void styleClassNameRemoved(Component component, String styleClassName) {
- for (ComponentStyleListener listener : this) {
- listener.styleClassNameRemoved(component, styleClassName);
- }
- }
}
private static class ComponentMouseListenerList extends ListenerList<ComponentMouseListener>
@@ -643,7 +629,7 @@ public abstract class Component implemen
// The component's name
private String name = null;
- // The component's styles and style class names
+ // The component's styles
private BeanAdapter styles = null;
private StyleDictionary styleDictionary = new StyleDictionary();
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentStyleListener.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentStyleListener.java?rev=960051&r1=960050&r2=960051&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentStyleListener.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ComponentStyleListener.java Fri Jul 2 16:40:36 2010
@@ -21,20 +21,6 @@ package org.apache.pivot.wtk;
*/
public interface ComponentStyleListener {
/**
- * Component style listener adapter.
- */
- public static class Adapter implements ComponentStyleListener {
- public void styleUpdated(Component component, String styleKey, Object previousValue) {
- }
-
- public void styleClassNameAdded(Component component, String styleClassName) {
- }
-
- public void styleClassNameRemoved(Component component, String styleClassName) {
- }
- }
-
- /**
* Called when a component style has been updated.
*
* @param component
@@ -42,20 +28,4 @@ public interface ComponentStyleListener
* @param previousValue
*/
public void styleUpdated(Component component, String styleKey, Object previousValue);
-
- /**
- * Called when a style class name has been added.
- *
- * @param component
- * @param styleClassName
- */
- public void styleClassNameAdded(Component component, String styleClassName);
-
- /**
- * Called when a style class name has been removed.
- *
- * @param component
- * @param styleClassName
- */
- public void styleClassNameRemoved(Component component, String styleClassName);
}