1 /* 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.com.sun.javafx.test; 27 28 import java.lang.reflect.Method; 29 30 import javafx.beans.property.SimpleStringProperty; 31 import javafx.beans.value.ObservableStringValue; 32 import javafx.beans.value.WritableObjectValue; 33 34 import test.com.sun.javafx.test.binding.BindingProxy; 35 import test.com.sun.javafx.test.binding.BindingProxyRefImpl; 36 import test.com.sun.javafx.test.binding.ReflectionHelper; 37 38 public final class BindingHelper { 39 private BindingHelper() { 40 } 41 42 public static Object getPropertyModel( 43 final Object bean, 44 final PropertyReference propertyReference) { 45 final Method modelMethod = ReflectionHelper.getMethod( 46 bean.getClass(), 47 propertyReference.getPropertyName() 48 + "Property"); 49 50 return ReflectionHelper.invokeMethod(bean, modelMethod); 51 } 52 53 public static Object createVariable(final Object value) { 54 final Class<?> typeClass = (value != null) ? value.getClass() 55 : Object.class; 56 final Object variable = createVariable(typeClass); 57 setWritableValue(typeClass, variable, value); 58 return variable; 59 } 60 61 public static Object createVariable(final Class<?> typeClass) { 62 final BindingProxy bindingProxy = getBindingProxy(typeClass); 63 return bindingProxy.getVariableFactory().createVariable(); 64 } 65 66 public static Object getObservableValue(final Class<?> typeClass, 67 final Object observableValue) { 68 final BindingProxy bindingProxy = getBindingProxy(typeClass); 69 return bindingProxy.getObservableValueProxy().getValue(observableValue); 70 } 71 72 public static void setWritableValue(final Object writableValue, 73 final Object value) { 74 setWritableValue(value.getClass(), writableValue, value); 75 } 76 77 public static void setWritableValue(final Class<?> typeClass, 78 final Object writableValue, 79 final Object value) { 80 final BindingProxy bindingProxy = getBindingProxy(typeClass); 81 bindingProxy.getWritableValueProxy().setValue(writableValue, value); 82 } 83 84 public static void bind(final Object bean, 85 final PropertyReference propertyReference, 86 final Object observableValue) { 87 final Object propertyModel = getPropertyModel(bean, propertyReference); 88 bind(propertyReference.getValueType(), propertyModel, observableValue); 89 } 90 91 public static void bind( 92 final Class<?> typeClass, 93 final Object propertyModel, 94 final Object observableValue) { 95 final BindingProxy bindingProxy = getBindingProxy(typeClass); 96 bindingProxy.getPropertyModelProxy().bind(propertyModel, 97 observableValue); 98 } 99 100 public static void unbind(final Object bean, 101 final PropertyReference propertyReference) { 102 final Object propertyModel = getPropertyModel(bean, propertyReference); 103 unbind(propertyReference.getValueType(), propertyModel); 104 } 105 106 public static void unbind(final Class<?> typeClass, 107 final Object propertyModel) { 108 final BindingProxy bindingProxy = getBindingProxy(typeClass); 109 bindingProxy.getPropertyModelProxy().unbind(propertyModel); 110 } 111 112 private static final BindingProxy[] BINDING_PROXY_LIST = { 113 BindingProxyRefImpl.autoLookup("Boolean"), 114 BindingProxyRefImpl.autoLookup("Integer"), 115 BindingProxyRefImpl.autoLookup("Long"), 116 BindingProxyRefImpl.autoLookup("Float"), 117 BindingProxyRefImpl.autoLookup("Double"), 118 // special handling of String 119 // (WritableStringValue is not defined) 120 new BindingProxyRefImpl(String.class, 121 ObservableStringValue.class, 122 WritableObjectValue.class, 123 SimpleStringProperty.class, 124 SimpleStringProperty.class), 125 BindingProxyRefImpl.autoLookup("Object") 126 }; 127 128 private static BindingProxy getBindingProxy( 129 Class<?> typeClass) { 130 if (typeClass.isPrimitive()) { 131 typeClass = getWrapperClassForPrimitiveType(typeClass); 132 } 133 134 for (final BindingProxy proxy: BINDING_PROXY_LIST) { 135 if (proxy.getTypeClass().isAssignableFrom(typeClass)) { 136 return proxy; 137 } 138 } 139 140 // won't happen 141 throw new RuntimeException(); 142 } 143 144 private static Class<?> getWrapperClassForPrimitiveType( 145 final Class<?> typeClass) { 146 if (typeClass == boolean.class) { 147 return Boolean.class; 148 } else if (typeClass == int.class) { 149 return Integer.class; 150 } else if (typeClass == long.class) { 151 return Long.class; 152 } else if (typeClass == float.class) { 153 return Float.class; 154 } else if (typeClass == double.class) { 155 return Double.class; 156 } else { 157 return null; 158 } 159 } 160 }