/** * Bind model to this manager * @param sender Who wants to bind it * @param model The model to bind to this manager. CANNOT be NULL otherwise a runtime */ public void bindModel(Object sender, MODEL model) { super.bindModel(model); }
/** * Get the view model the controller is holding. Don't write but only read the model from view. * Should only controller write the model. * * @return Null if the controller doesn't need to get its model saved and restored automatically * when {@link #modelType()} returns null. */ @Override public MODEL getModel() { return super.getModel(); }
/** * Called when the Bean is injected for the first time or restored when a new instance of * this Bean needs to be instantiated. * * <p>The model of the Bean will be instantiated by model's default no-argument constructor. * However, if the Bean needs to be restored, a new instance of model restored by * {@link #restoreModel(Object)} will replace the model created by this method.</p> */ public void onCreated() { model = instantiateModel(); }
/** * Restores the model of this Bean. * <p> * Note that when {@link #modelType()} returns null, this method will have no effect. * </p> * * @param restoredModel The restored model by {@link StateKeeper} that will be rebound to the * Bean. */ public void restoreModel(MODEL restoredModel) { if (modelType() != null) { this.model = restoredModel; onRestored(); } }
@Test public void should_rebind_state_after_restoring_mvcBean() { Bean<String> bean = new Bean() { @Override public Class modelType() { return String.class; } }; Assert.assertNull(bean.getModel()); bean.restoreModel("A"); Assert.assertEquals("A", bean.getModel()); }
@Override public void onCreated(Provider provider, Object instance) { if (instance instanceof Bean) { final Bean bean = (Bean) instance; bean.onCreated(); logger.trace("+++Bean created - '{}'.", provider.type().getSimpleName()); } } });
@Override public <T> void onDisposed(Provider<T> provider, T instance) { if (instance != null && instance instanceof Bean) { //When the cached instance is still there free and dispose it. Bean bean = (Bean) instance; bean.onDestroy(); logger.trace("---Bean destroyed - '{}'.", provider.type().getSimpleName()); } } });
private MODEL instantiateModel() { Class<MODEL> type = modelType(); if (type == null) { return null; } else { try { return new ReflectUtils.newObjectByType<>(type).newInstance(); } catch (Exception e) { throw new RuntimeException( String.format("Fail to instantiate model of %s by its default constructor", type.getName()), e); } } }
public void onCreated() { super.onCreated(); eventBus2C.register(this); }
/** * Called when the controller is destroyed. This occurs when the controller is de-referenced and * not retained by any objects. */ @Override public void onDestroy() { super.onDestroy(); eventBusC.unregister(this); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_when_bind_null_to_a_mvcBean() { Bean bean = new Bean() { @Override public Class modelType() { return String.class; } }; bean.bindModel(null); }
/** * Called when the controller is created. Note that it could be called either when the * controller is instantiated for the first time or restored by views. * <p/> * <p>The model of the controller will be instantiated by model's default no-argument * constructor here whe {@link #modelType()} doesn't return null.</p> */ public void onCreated() { super.onCreated(); if (uiThreadRunner == null) { //Use mvc graph's default uiThreadRunner uiThreadRunner = Mvc.graph().uiThreadRunner; } eventBusC.register(this); }
@Override public void onDestroy() { super.onDestroy(); eventBus2C.unregister(this); } /**