/** * 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 void testEnumTypes() throws Exception { Object o = Reflections.evalEnum(TestEnum.class, "FOO"); assertTrue("Object is not a ModelUpdate instance",ModelUpdate.class.isAssignableFrom(o.getClass())); } }
public static Binding bindJToggleButtonToEnum(final String value, final Class<?> enumType, final Mutator mutator, final AbstractButton button) { final Object enumValue = Reflections.evalEnum(enumType, value); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { mutator.set(enumValue); } catch (Exception ex) { Wiring.logger.error("exception in JToggleButton binding", ex); //$NON-NLS-1$ } } }); Binding binding = new Binding() { public <T extends Enum<?> & ModelUpdate> void update(T... changed) { try { button.setSelected(mutator.get() == enumValue); } catch (Exception ex) { Wiring.logger.error("exception in JToggleButton binding", ex); //$NON-NLS-1$ } } }; mutator.getModel().bind(binding); return binding; } }