private List<Field> getBindableModelFields() { List<Field> allModelFields = Reflections.getFieldsOfTypeForClassHierarchy(object.getClass(), BindableModel.class); List<Field> notBindableFields = Reflections.getAnnotatedFieldsForClassHierarchy(object.getClass(), NotBindable.class); allModelFields = ImmutableList.copyOf(Iterables.filter(allModelFields, Predicates.not(Predicates.in(notBindableFields)))); List<Field> nonFinalModelFields = ImmutableList.copyOf(Iterables.filter(allModelFields, Predicates.not(Reflections.IS_FIELD_FINAL))); if (!nonFinalModelFields.isEmpty()) { throw new BindingException("All BindableModels have to be final or marked with @NotBindable, but "+ Iterables.transform(nonFinalModelFields, Reflections.FIELD_TO_NAME)+" are not."); } return allModelFields; }
public Collection<Binding> wire(BindingContext context) { List<Field> actions = context.getAnnotatedFields(OnClick.class); for (Field field : actions) { OnClick onClick = field.getAnnotation(OnClick.class); try { wire(onClick, field, context); } catch (Exception e) { throw new BindingException("could not wire up @OnClick on " + field.getName(), e); } } return ImmutableList.of(); }
public Collection<Binding> wire(BindingContext context) { List<Field> actions = context.getAnnotatedFields(OnChange.class); for (Field field : actions) { OnChange change = field.getAnnotation(OnChange.class); try { wire(change, field, context); } catch (Exception e) { throw new BindingException("could not wire up @OnChange on " + field.getName(), e); } } return ImmutableList.of(); }
private static Collection<Binding> wire(final Bound bound, final BindingContext context, final Field field) { final boolean[] didBind = new boolean[1]; final List<Binding> bindings = Lists.newArrayList(); Reflections.visitClassHierarchy(field.getType(), new Visitor<Class<?>>() { public void visit(Class<?> klass){ if (didBind[0]) { return; } WiringHarness<Bound, Field> wiringHarness = wiringHarnesses.get(klass); if (wiringHarness == null) { return; } try { bindings.addAll(wiringHarness.wire(bound, context, field)); didBind[0] = true; } catch (Exception e) { throw Throwables.throwUncheckedException(e); } } }); if (!didBind[0]) { throw new BindingException("don't know how to wire up @Bound field: " + field.getName()); } return bindings; } }
String call = onClick.call(); if (call == null) { throw new BindingException("call on @OnClick on " + field.getName() + " must be specified"); final ObjectFieldMethod ofm = context.getBindableMethod(call); if (ofm == null) { throw new BindingException("could not find bindable method: " + call);
public Collection<Binding> wire(final BindingContext context) { final List<Field> actions = context.getAnnotatedFields(EnabledIf.class); final List<Binding> bindings = Lists.newArrayList(); for (final Field field : actions) { final EnabledIf action = field.getAnnotation(EnabledIf.class); final String to = action.to(); final boolean invert = (action.type() == Type.INVERTED); try { bindings.addAll(wire(to, field, context, invert)); } catch (final Exception e) { throw new BindingException("could not wire up @EnabledIf on " + field.getName(), e); } } return bindings; }
public Collection<Binding> wire(final BindingContext context) { final List<Field> actions = context.getAnnotatedFields(VisibleIf.class); final List<Binding> bindings = Lists.newArrayList(); for (final Field field : actions) { final VisibleIf action = field.getAnnotation(VisibleIf.class); final String to = action.to(); final boolean invert = (action.type() == Type.INVERTED); try { bindings.addAll(wire(to, field, context, invert)); } catch (final Exception e) { throw new BindingException("could not wire up @VisibleIf on " + field.getName(), e); } } return bindings; }
throw new BindingException("call on @OnChange on " + field.getName() + " must be specified"); final ObjectFieldMethod ofm = context.getBindableMethod(call); if (ofm == null) { throw new BindingException("could not find bindable method: " + call);
final Method setVisibleMethod = field.getType().getMethod("setVisible", boolean.class); if (setVisibleMethod == null) { throw new BindingException("no setVisible call on VisibleIf field: " + field); throw new BindingException("could not find bindable property: " + to); throw new BindingException("VisibleIf binding must return boolean: " + to);
final ObjectFieldMethod ofm = context.getBindableMethod(call); if (ofm == null) { throw new BindingException("could not find bindable method: " + call);
/** * Wires all {@link Action} bindings in the passed context. * Called by {@link Bindings#createBindings(BindingContext)} as part of runtime wiring * process. * * @param context */ public Collection<Binding> wire(BindingContext context) { List<Field> actions = context.getAnnotatedFields(Action.class); for (Field field : actions) { Action action = field.getAnnotation(Action.class); String call = action.call(); try { wire(call, field, context); } catch (Exception e) { throw new BindingException("could not wire up @Action on " + field.getName(), e); } } return ImmutableList.of(); }
final Method setEnabledMethod = field.getType().getMethod("setEnabled", boolean.class); if (setEnabledMethod == null) { throw new BindingException("no setEnabled call on EnabledIf field: " + field); throw new BindingException("could not find bindable property: " + to); throw new BindingException("EnabledIf binding must return boolean: " + to);
lostOFM = context.getBindableMethod(lost); if (lostOFM == null) { throw new BindingException("could not find bindable method: " + lost); gainedOFM = context.getBindableMethod(gained); if (gainedOFM == null) { throw new BindingException("could not find bindable method: " + gained);
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 Collection<Binding> wire(BindingContext context) { List<Field> actions = context.getAnnotatedFields(OnFocusChange.class); for (Field field : actions) { OnFocusChange focusChange = field.getAnnotation(OnFocusChange.class); String lost = normalizeString(focusChange.lost()); String gained = normalizeString(focusChange.gained()); if (Bound.Utilities.isNullOrBlank(lost) && Bound.Utilities.isNullOrBlank(gained)) { throw new BindingException("either lost or gained must be specified on @OnFocusChange on " + field.getName()); } try { wire(lost, gained, field, context); } catch (Exception e) { throw new BindingException("could not wire up @OnFocusChange " + field.getName(), e); } } return ImmutableList.of(); }
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); }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { String target = bound.to(); Mutator mutator = Mutator.create(context, target); JToggleButton toggle = context.getFieldObject(field, JToggleButton.class); Class<?>[] paramTypes = mutator.getSetter().getMethod().getParameterTypes(); if (paramTypes.length == 1 && paramTypes[0].isEnum()) { Class<?> enumType = paramTypes[0]; String value = bound.value(); return ImmutableList.of(bindJToggleButtonToEnum(value, enumType, mutator, toggle)); } else if (paramTypes.length == 1 && paramTypes[0] == boolean.class) { String value = bound.value(); if (Strings.isNullOrEmpty(value)) { return ImmutableList.of(AbstractButtonWiringHarness.bindAbstractButton(mutator, toggle)); } else { return ImmutableList.of(bindJToggleButtonToBoolean(bound.value(), mutator, toggle)); } } else { throw new BindingException("can only bind JToggleButtons to enums or booleans"); //$NON-NLS-1$ } }
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; } }
/** * Create a BindingContext for the given, non-null object. Throws a {@link BindingException} * if there is a problem. * @param object the object - cannot be null */ public BindingContext(Object object) { Preconditions.checkNotNull(object, "object"); this.object = object; try { bindableModels = indexBindableModels(); bindableMethods = indexBindableMethods(); bindableModelMethods = indexBindableModelMethods(); bindableConstants = indexBindableConstants(); bindableGetters = indexBindableProperties(Reflections.getterFunction(PropertyDescriptor.class, Method.class, "readMethod")); bindableSetters = indexBindableProperties(Reflections.getterFunction(PropertyDescriptor.class, Method.class, "writeMethod")); } catch (Exception e) { throw new BindingException("could not create BindingContext", e); } }