![JAR search and dependency download from the Maven repository](/logo.png)
com.sencha.gxt.state.client.AbstractStateHandler Maven / Gradle / Ivy
/**
* Sencha GXT 3.1.0-beta - Sencha for GWT
* Copyright(c) 2007-2014, Sencha, Inc.
* [email protected]
*
* http://www.sencha.com/products/gxt/license/
*/
package com.sencha.gxt.state.client;
import com.google.gwt.core.client.Callback;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.SimpleEventBus;
import com.sencha.gxt.state.client.BeforeRestoreStateEvent.BeforeRestoreStateHandler;
import com.sencha.gxt.state.client.BeforeRestoreStateEvent.HasBeforeRestoreStateHandlers;
import com.sencha.gxt.state.client.BeforeSaveStateEvent.HasBeforeSaveStateHandlers;
import com.sencha.gxt.state.client.RestoreStateEvent.HasRestoreStateHandlers;
import com.sencha.gxt.state.client.RestoreStateEvent.RestoreStateHandler;
import com.sencha.gxt.state.client.SaveStateEvent.HasSaveStateHandlers;
import com.sencha.gxt.state.client.SaveStateEvent.SaveStateHandler;
/**
* Simple class to add support for reading and writing state. Can be subclassed
* and a concrete type given to O (such as a Store or Component subclass), and a
* particular interface S can be defined to hold the state for this object.
*
* @param the state interface
* @param the concrete type of the object state will be applied to
*/
public abstract class AbstractStateHandler implements HasBeforeRestoreStateHandlers,
HasRestoreStateHandlers, HasBeforeSaveStateHandlers, HasSaveStateHandlers {
private final Class stateType;
private final O object;
private final String key;
private S state;
private SimpleEventBus eventBus;
protected AbstractStateHandler(Class stateType, O object, String key) {
assert stateType.isInterface();
this.stateType = stateType;
this.object = object;
this.key = key;
state = StateManager.get().getDefaultStateInstance(stateType);
}
public HandlerRegistration addBeforeRestoreStateHandler(BeforeRestoreStateHandler handler) {
return ensureHandlers().addHandler(BeforeRestoreStateEvent.getType(), handler);
}
public HandlerRegistration addBeforeSaveStateHandler(BeforeSaveStateEvent.BeforeSaveStateHandler handler) {
return ensureHandlers().addHandler(BeforeSaveStateEvent.getType(), handler);
}
@Override
public HandlerRegistration addRestoreStateHandler(RestoreStateHandler handler) {
return ensureHandlers().addHandler(RestoreStateEvent.getType(), handler);
}
@Override
public HandlerRegistration addSaveStateHandler(SaveStateHandler handler) {
return ensureHandlers().addHandler(SaveStateEvent.getType(), handler);
}
/**
* Applies the currently loaded state to the current stateful object.
*/
public abstract void applyState();
/**
* Returns the target object.
*
* @return the target object
*/
public O getObject() {
return object;
}
/**
* Returns the state.
*
* @return the currently loaded state. The state instance may not be set, so
* this may return null.
*/
public S getState() {
return state;
}
/**
* Starts to load the state for the given object.
*/
public void loadState() {
StateManager.get().get(key, stateType, new Callback() {
@Override
public void onFailure(Throwable reason) {
}
public void onSuccess(S result) {
state = result;
handleLoadState();
}
});
}
/**
* Saves the current state.
*/
public void saveState() {
BeforeSaveStateEvent e = new BeforeSaveStateEvent(state, object);
ensureHandlers().fireEvent(e);
if (!e.isCancelled()) {
StateManager.get().set(key, state);
ensureHandlers().fireEvent(new SaveStateEvent(state, object));
}
}
protected void handleLoadState() {
BeforeRestoreStateEvent e = new BeforeRestoreStateEvent(state, object);
ensureHandlers().fireEvent(e);
if (!e.isCancelled()) {
applyState();
ensureHandlers().fireEvent(new RestoreStateEvent(state, object));
}
}
SimpleEventBus ensureHandlers() {
return eventBus == null ? eventBus = new SimpleEventBus() : eventBus;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy