Pulled handling of initial settings into ComponentContext interface and

simplified its usage
This commit is contained in:
Steffen Schaefer committed 2017-02-20 08:28:35 +01:00
1 parent 88eeceefea
commit cf43a62842
8 files changed
+111 -134

No files matched your search

@@ -58,4 +58,6 @@ public interface ComponentContext<L extends ComponentLifecycle<S, ?>, S extends
boolean hasMakeCustomDefaultSettingsSupport(Component<?> component);
void initInitialSettings(OnSettingsLoadedCallback<S> onInitialSettingsLoaded);
void initInitialSettings();
}
@@ -1,9 +1,7 @@
package com.sap.sse.gwt.client.shared.perspective;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONObject;
@@ -48,18 +46,6 @@ public class ComponentContextWithSettingsStorage<L extends ComponentLifecycle<S,
*/
private final SettingsStorageManager<S> settingsStorageManager;
/**
* Contains {@link SettingsReceiverCallback}s which are waiting to receive the initial settings of the root
* component.
*/
private Queue<SettingsReceiverCallback<S>> settingsReceiverCallbacks = new LinkedList<>();
/**
* Current initial/default settings for the whole settings tree which corresponds to the root component and its
* subcomponents.
*/
private S currentDefaultSettings = null;
/**
*
* @param rootLifecycle
@@ -72,73 +58,11 @@ public class ComponentContextWithSettingsStorage<L extends ComponentLifecycle<S,
super(rootLifecycle);
this.settingsStorageManager = settingsStorageManager;
}
/**
* Initialises the instance with initial settings. This method may be called only once during the whole lifecycle of
* this instance. The call of this method is mandatory, otherwise it will not be possible to obtain initial
* settings.
*
* @see #initInitialSettings(OnSettingsLoadedCallback)
*/
public void initInitialSettings() {
initInitialSettings(null);
}
/**
* Initializes the instance with initial settings. This method may be called only once during the whole lifecycle of
* this instance. The call of this method is mandatory, otherwise it will not be possible to obtain initial
* settings.
*
* @param onInitialSettingsLoaded
* Callback to be called when the settings initialisation finishes
* @see #initInitialSettings()
*/
@Override
public void initInitialSettings(final OnSettingsLoadedCallback<S> onInitialSettingsLoaded) {
if (currentDefaultSettings != null) {
throw new IllegalStateException(
"Settings have been already initialized. You may only call this method once.");
}
protected void loadDefaultSettings(OnSettingsLoadedCallback<S> callback) {
S systemDefaultSettings = rootLifecycle.createDefaultSettings();
settingsStorageManager.retrieveDefaultSettings(systemDefaultSettings, new OnSettingsLoadedCallback<S>() {
@Override
public void onError(Throwable caught, S fallbackDefaultSettings) {
if (onInitialSettingsLoaded != null) {
onInitialSettingsLoaded.onError(caught, fallbackDefaultSettings);
}
}
@Override
public void onSuccess(S result) {
currentDefaultSettings = result;
if (onInitialSettingsLoaded != null) {
onInitialSettingsLoaded.onSuccess(result);
}
SettingsReceiverCallback<S> callback;
while ((callback = settingsReceiverCallbacks.poll()) != null) {
callback.receiveSettings(result);
}
}
});
}
/**
* Retrieve settings for the root component managed by this context. The provided callback gets called when
* initial/default settings are available. That means if the initialisation of this instance is finished, the
* provided callback is called immediately. If initialisation is not done yet, then the callback gets called when
* the initialisation gets finished and thus, initial settings are available. Make sure to call
* {@link #initInitialSettings()} when using this method, otherwise the provided callback will be never called.
*
* @param settingsReceiverCallback
* The callback which supplies the caller with initial settings
*/
public void receiveInitialSettings(SettingsReceiverCallback<S> settingsReceiverCallback) {
if (currentDefaultSettings == null) {
settingsReceiverCallbacks.add(settingsReceiverCallback);
} else {
settingsReceiverCallback.receiveSettings(currentDefaultSettings);
}
settingsStorageManager.retrieveDefaultSettings(systemDefaultSettings, callback);
}
/**
@@ -150,23 +74,6 @@ public class ComponentContextWithSettingsStorage<L extends ComponentLifecycle<S,
return settingsStorageManager.getLastError();
}
/**
* Gets the current default {@link Settings} of the root component managed by this {@link ComponentContext}. The
* returned {@link Settings} should contain all settings for the root component and its subcomponents.
*
* @return The {@link Settings} of the root component
* @throws IllegalStateException
* When the instance has not been initialised yet
* @see #initInitialSettings()
*/
@Override
public S getDefaultSettings() {
if (currentDefaultSettings == null) {
throw new IllegalStateException("Settings have not been initialized yet.");
}
return currentDefaultSettings;
}
/**
* {@inheritDoc}
*/
@@ -0,0 +1,11 @@
package com.sap.sse.gwt.client.shared.perspective;
import com.sap.sse.common.settings.Settings;
public abstract class DefaultOnSettingsLoadedCallback<S extends Settings> implements OnSettingsLoadedCallback<S> {
@Override
public void onError(Throwable caught, S fallbackDefaultSettings) {
onSuccess(fallbackDefaultSettings);
}
}
@@ -1,15 +0,0 @@
package com.sap.sse.gwt.client.shared.perspective;
import com.sap.sse.common.settings.Settings;
/**
* Receives initial/default settings when {@link ComponentContextWithSettingsStorage} have been initialised.
*
* @author Vladislav Chumak
*
* @param <S> The {@link Settings} type of the settings of the root component/perspective containing all the settings for itself and its subcomponents
* @see ComponentContextWithSettingsStorage
*/
public interface SettingsReceiverCallback<S extends Settings> {
void receiveSettings(S initialSettings);
}
@@ -1,5 +1,8 @@
package com.sap.sse.gwt.client.shared.perspective;
import java.util.LinkedList;
import java.util.Queue;
import com.sap.sse.common.settings.Settings;
import com.sap.sse.gwt.client.shared.components.Component;
import com.sap.sse.gwt.client.shared.components.ComponentLifecycle;
@@ -25,6 +28,20 @@ public class SimpleComponentContext<L extends ComponentLifecycle<S, ?>, S extend
protected final L rootLifecycle;
/**
* Contains {@link SettingsReceiverCallback}s which are waiting to receive the initial settings of the root
* component.
*/
private Queue<OnSettingsLoadedCallback<S>> settingsReceiverCallbacks = new LinkedList<>();
/**
* Current initial/default settings for the whole settings tree which corresponds to the root component and its
* subcomponents.
*/
private S currentDefaultSettings = null;
private boolean loadingDefaultSettings = false;
/**
*
* @param rootLifecycle
@@ -68,8 +85,68 @@ public class SimpleComponentContext<L extends ComponentLifecycle<S, ?>, S extend
return false;
}
/**
* Initializes the instance with initial settings. This method may be called only once during the whole lifecycle of
* this instance. The call of this method is mandatory, otherwise it will not be possible to obtain initial
* settings.
*
* @see #initInitialSettings(OnSettingsLoadedCallback)
*/
public void initInitialSettings() {
loadDefaultSettingsIfNecessary();
}
/**
* Initializes the instance with initial settings. This method may be called only once during the whole lifecycle of
* this instance. The call of this method is mandatory, otherwise it will not be possible to obtain initial
* settings.
*
* @param onInitialSettingsLoaded
* Callback to be called when the settings initialization finishes
* @see #initInitialSettings()
*/
@Override
public void initInitialSettings(OnSettingsLoadedCallback<S> onInitialSettingsLoaded) {
onInitialSettingsLoaded.onSuccess(getDefaultSettings());
public void initInitialSettings(final OnSettingsLoadedCallback<S> onInitialSettingsLoaded) {
settingsReceiverCallbacks.add(onInitialSettingsLoaded);
loadDefaultSettingsIfNecessary();
}
private void loadDefaultSettingsIfNecessary() {
if (loadingDefaultSettings) {
return;
}
if (currentDefaultSettings == null) {
loadingDefaultSettings = true;
loadDefaultSettings(new OnSettingsLoadedCallback<S>() {
@Override
public void onError(Throwable caught, S fallbackDefaultSettings) {
S fallbackSettings = getDefaultSettings();
OnSettingsLoadedCallback<S> callback;
while ((callback = settingsReceiverCallbacks.poll()) != null) {
callback.onError(caught, fallbackSettings);
}
}
@Override
public void onSuccess(S result) {
currentDefaultSettings = result;
OnSettingsLoadedCallback<S> callback;
while ((callback = settingsReceiverCallbacks.poll()) != null) {
callback.onSuccess(result);
}
}
});
} else {
OnSettingsLoadedCallback<S> callback;
while ((callback = settingsReceiverCallbacks.poll()) != null) {
callback.onSuccess(currentDefaultSettings);
}
}
}
protected void loadDefaultSettings(OnSettingsLoadedCallback<S> callback) {
callback.onSuccess(getDefaultSettings());
}
}