public void bindWithoutUpdate(Object object) { BindingContext context = new BindingContext(object); bindings.addAll(createBindings(context)); }
private static Collection<Binding> wire(BoundExtent bound, BindingContext context, Field field) throws IntrospectionException { String target = bound.to(); final ObjectFieldMethod setter = context.findSetter(target); final ObjectFieldMethod getter = context.findGetter(target); if (setter == null || getter == null) { throw new IllegalArgumentException("could not find setter/getter for @BoundExtent: " + field); } final BindableModel model1 = context.getFieldObject(setter.getField(), BindableModel.class); final BindableModel model2 = context.getFieldObject(getter.getField(), BindableModel.class); assert model1 == model2; if (Component.class.isAssignableFrom(field.getType())) { final JSlider comp = context.getFieldObject(field, JSlider.class); return bindSlider(bound, context, comp, setter, getter, model1); } else { throw new IllegalArgumentException("don't know how to wire up @BoundExtent field: " + field.getName()); } }
public Collection<Binding> wire(BindingContext context) throws IllegalArgumentException { List<Field> boundFields = context.getAnnotatedFields(Bound.class); List<Binding> bindings = Lists.newArrayList(); for (Field field : boundFields) { Bound bound = field.getAnnotation(Bound.class); bindings.addAll(wire(bound, context, field)); } return bindings; }
/** * Indexes all bindable models within the binding context. If there are two bindable models * in a class hierarchy with identical names then they are indexed as * "DeclaringClass.modelFieldName". If this is not unique then one of them will win * non-deterministically, don't do this. * @return the index */ private Map<String, Field> indexBindableModels() { return dotIndex(getBindableModelFields(), Reflections.FIELD_TO_CONTAINING_CLASS_NAME, Reflections.FIELD_TO_NAME); }
private Map<String, ObjectFieldMethod> indexBindableMethods() throws IllegalArgumentException { // Get all fields marked @Bindable List<Field> bindables = getAnnotatedFields(Bindable.class); if (Iterables.any(bindables, Predicates.not(Reflections.IS_FIELD_FINAL))) { throw new BindingException("all @Bindables have to be final"); } // Add all BindableModels bindables.addAll(getBindableModelFields()); // Index those methods. List<ObjectFieldMethod> methods = getParameterlessMethodsOnFieldTypes(object, bindables); // Add methods for classes marked @Bindable if (Reflections.isClassAnnotatedForClassHierarchy(object, Bindable.class)) { methods.addAll(Reflections.getParameterlessMethodsForClassHierarchy(object)); } return indexMethods(methods); }
/** * Resolves a string reference, as specified in the <code>on</code> parameter of * a {@link Bound} annotation to an Enum object in this runtime. * @param on <code>on</code> parameter from a {@link Bound} annotation. * @param model * @return the resolved object * @throws IllegalArgumentException if the referenced object can't be found. */ public static ModelUpdate findOnObject(final String on, final BindableModel model) { ModelUpdate onObject = null; if (!isNullOrBlank(on)) { final List<Class<?>> updateClasses = findModelUpdateClass(model); for (Class<?> updateClass : updateClasses) { try { onObject = (ModelUpdate)Reflections.evalEnum(updateClass, on); return onObject; } catch (IllegalArgumentException e) { // swallow this if we don't find the enum on one of the // classes, continue to next class. } } throw new IllegalArgumentException("could not find \"on\" parameter " + on); } return onObject; }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { String target = bound.to(); JProgressBar bar = context.getFieldObject(field, JProgressBar.class); // ObjectFieldMethod setter = context.findSetter(target); ObjectFieldMethod getter = context.findGetter(target); if (getter == null) { throw new IllegalArgumentException("could not find getter/setter for " + target); } BindableModel model = context.getFieldObject(getter.getField(), BindableModel.class); // verify type parameters return bindJProgressBar(model, bar, getter.getMethod()); }
public BindableModel apply(Field from) { return getFieldObject(from, BindableModel.class); } };
IllegalAccessException, InvocationTargetException { Method aalMethod = field.getType().getMethod("addActionListener", ActionListener.class); Object actionObject = context.getFieldObject(field, Object.class); final ObjectFieldMethod ofm = context.getBindableMethod(call); if (ofm == null) { throw new BindingException("could not find bindable method: " + call);
public void testModel() { BindingContext context = new BindingContext(this); assertEquals(model, context.getBindableModel("model")); }
private static Collection<Binding> wire(final CallOnUpdate callOnUpdate, final BindingContext context, final ObjectFieldMethod method) { final String to = callOnUpdate.model(); final BindableModel model; if (Strings.isNullOrEmpty(to)) { Set<BindableModel> models = context.getBindableModels(); if (models.size() != 1) { throw new BindingException("more than one bindable model for empty 'to'"); //$NON-NLS-1$ } model = models.iterator().next(); } else { model = context.getBindableModel(to); if (model == null) { throw new BindingException("can't find method to bind to: " + to); //$NON-NLS-1$ } } final String[] ons = callOnUpdate.on(); List<Object> onObjects = BindingContext.getOnObjects(ons, model); Binding binding = makeBinding(method, onObjects); model.bind(binding); return ImmutableList.of(binding); }
public void testConstants() { BindingContext context = new BindingContext(this); assertEquals("string1", context.getBindableConstant("STRING_1")); assertEquals(Boolean.TRUE, context.getBindableConstant("BOOLEAN")); assertEquals("privateString", context.getBindableConstant("PRIVATE_STRING")); assertEquals("#0", context.getBindableConstant("FUNCTION_STRING")); assertEquals(null, context.getBindableConstant("NOT_FOUND")); }
public Collection<Binding> wire(BindingContext context) { List<Field> loggedModels = context.getAnnotatedFields(LoggedModel.class); List<Binding> bindings = Lists.newArrayList(); for (Field field : loggedModels) { final LoggedModel annotation = field.getAnnotation(LoggedModel.class); final String loggerParam = annotation.logger(); final String fieldName = field.getName(); if (BindableModel.class.isAssignableFrom(field.getType())) { BindableModel model = context.getFieldObject(field, BindableModel.class); Binding binding = new Binding() { public <T extends Enum<?> & ModelUpdate> void update(T... changed) { for (T t : changed){ Logger logger = LoggerFactory.getLogger(loggerParam); if (logger.isInfoEnabled()) { logger.info(t.toString() + ": " + fieldName); } } } }; model.bind(binding); bindings.add(binding); } else { throw new BindingException("Can only log a BindableModel."); } } return bindings; } }
public void testGetterIndex() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { model1.setSimpleString("string1"); model2.setSimpleString("string2"); model.setOtherString("other"); BindingContext context = new BindingContext(this); assertNull(context.findGetter("simpleString")); ObjectFieldMethod ofm = context.findGetter("model1.simpleString"); assertEquals("string1", ofm.getMethod().invoke(ofm.getObject(), (Object[])null)); ofm = context.findGetter("model2.simpleString"); assertEquals("string2", ofm.getMethod().invoke(ofm.getObject(), (Object[])null)); ofm = context.findGetter("otherString"); assertEquals("other", ofm.getMethod().invoke(ofm.getObject(), (Object[])null)); }
public void testSetterIndex() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { BindingContext context = new BindingContext(this); assertNull(context.findSetter("simpleString")); ObjectFieldMethod ofm = context.findSetter("model1.simpleString"); ofm.getMethod().invoke(ofm.getObject(), new Object[] { "set1" }); assertEquals("set1", model1.getSimpleString()); ofm = context.findSetter("model2.simpleString"); ofm.getMethod().invoke(ofm.getObject(), new Object[] { "set2" }); assertEquals("set2", model2.getSimpleString()); ofm = context.findSetter("otherString"); ofm.getMethod().invoke(ofm.getObject(), new Object[] { "setOther" }); assertEquals("setOther", model.getOtherString()); } }
private Map<String, ObjectFieldMethod> indexBindableProperties(Function<PropertyDescriptor, Method> methodFn) throws IntrospectionException { final Map<ObjectFieldMethod, String> getterOfms = Maps.newHashMap(); for (Field field : Sets.newHashSet(bindableModels.values())) { BeanInfo beanInfo = Introspector.getBeanInfo(field.getType()); PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor descriptor : props) { Method method = methodFn.apply(descriptor); if (method == null) { continue; } BindableModel model = getFieldObject(field, BindableModel.class); getterOfms.put(new ObjectFieldMethod(model, field, method), descriptor.getName()); } } return dotIndex(getterOfms.keySet(), ObjectFieldMethod.TO_FIELD_NAME, Functions.forMap(getterOfms)); }
/** * If the provided nullValue is null, empty, or just whitespace then the returned value * will be null. Otherwise, this function will look in the provided {@link BindingContext} * to find a constant with the name of the nullValue string and return that. If there is * no constant then the nullValue string itself will be returned. * @see JComboBoxWiringHarness * @see BoundSelection.Wiring */ public static Object getNullValue(BindingContext context, String nullValue) { final String finalNullValue; if (isNullOrBlank(nullValue)) { finalNullValue = null; } else { Object constant = context.getBindableConstant(nullValue); if (constant != null) { finalNullValue = constant.toString(); } else { finalNullValue = nullValue; } } return finalNullValue; }
final Object onObject = context.evalOnObject(on, model1);
throw new BindingException("no setEnabled call on EnabledIf field: " + field); final Object setEnabledObject = context.getFieldObject(field, Object.class); final ObjectFieldMethod getter = context.findGetter(to); if (getter == null) { throw new BindingException("could not find bindable property: " + to);
public BindableModel getBindableModel(String key) { Field field = bindableModels.get(key); if (field == null) { return null; } return getFieldObject(field, BindableModel.class); }