/** * Convenience method that subclasses can call upon to fire off property changes back to the * models. This method used reflection to inspect each of the model classes to determine if it is * the owner of the property in question. If it isn't, a NoSuchMethodException is thrown (which * the method ignores). * * @param propertyName The name of the property * @param newValue An object that represents the new value of the property. */ protected void setModelProperty(String propertyName, Object newValue) { for (AbstractModel model : registeredModels) { try { Method method = model.getClass().getMethod("set" + propertyName, new Class[] {newValue.getClass()}); method.invoke(model, newValue); } catch (Exception ex) { // Handle exception } } }
/** * Unbinds a model from this controller. * * @param model The model to be removed */ public void removeModel(AbstractModel model) { registeredModels.remove(model); model.removePropertyChangeListener(this); }
/** * Binds a model to this controller. Once added, the controller will listen for all model property * changes and propagate them on to registered views. In addition, it is also responsible for * resetting the model properties when a view changes state. * * @param model The model to be added */ public void addModel(AbstractModel model) { registeredModels.add(model); model.addPropertyChangeListener(this); }