Replaced the AsyncCallbacks with (backtracking) method calls

This commit is contained in:
Lennart Hensler committed 2015-04-28 14:59:25 +02:00
1 parent b5f712c114
commit 39e08f8472
4 files changed
+49 -56

No files matched your search

@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -104,12 +105,7 @@ class DimensionFilterSelectionProvider {
public void onValueChange(ValueChangeEvent<FunctionDTO> event) {
final FunctionDTO dimension = event.getValue();
if (dimension != null) {
fetchAndDisplayAvailableData(false, new AsyncCallback<Boolean>() {
@Override
public void onFailure(Throwable caught) { }
@Override
public void onSuccess(Boolean result) { }
});
fetchAndSetAvailableData();
if (firstChange && retrieverLevelSelectionProvider.canAddDimensionSelectionProvider()) {
retrieverLevelSelectionProvider.createAndAddDimensionSelectionProvider();
}
@@ -119,7 +115,7 @@ class DimensionFilterSelectionProvider {
firstChange = false;
} else {
selectionTable.clearSelection();
selectionTable.setContent(new ArrayList<>());
selectionTable.setContent(new ArrayList<>(), true);
selectionTable.setVisible(false);
toggleFilterButton.setVisible(false);
@@ -129,6 +125,14 @@ class DimensionFilterSelectionProvider {
}
}
private void fetchAndSetAvailableData() {
fetchAndDisplayAvailableData(false, true, null);
}
void fetchAndUpdateAvailableData(final Iterator<DimensionFilterSelectionProvider> retrieverLevelSelectionProviderIterator) {
fetchAndDisplayAvailableData(true, false, retrieverLevelSelectionProviderIterator);
}
/**
* Fetches the dimension values from the server and displays it. The data is filtered by the current filter
* selection (of all retriever levels).
@@ -137,7 +141,8 @@ class DimensionFilterSelectionProvider {
* <code>false</code>, if it should override the data and clear the selection.
* @param selectionChangedCallback An {@link AsyncCallback} to react, if the selection has been changed or not.
*/
void fetchAndDisplayAvailableData(final boolean isUpdate, final AsyncCallback<Boolean> selectionChangedCallback) {
private void fetchAndDisplayAvailableData(final boolean isUpdate, final boolean notifyListenersWhenSelectionChanged,
final Iterator<DimensionFilterSelectionProvider> retrieverLevelSelectionProviderIterator) {
final FunctionDTO dimension = getSelectedDimension();
Collection<FunctionDTO> dimensionDTOs = new ArrayList<>();
dimensionDTOs.add(dimension);
@@ -166,28 +171,36 @@ class DimensionFilterSelectionProvider {
boolean selectionChanged;
if (isUpdate) {
selectionChanged = selectionTable.updateContent(content);
selectionChanged = selectionTable.updateContent(content, notifyListenersWhenSelectionChanged);
} else {
selectionChanged = selectionTable.setContent(content);
selectionChanged = selectionTable.setContent(content, notifyListenersWhenSelectionChanged);
}
if (selectionToBeApplied != null) {
selectionTable.setSelection(selectionToBeApplied);
selectionToBeApplied = null;
}
busyIndicator.setVisible(false);
selectionTable.setVisible(true);
toggleFilterButton.setVisible(true);
selectionChangedCallback.onSuccess(selectionChanged);
if (retrieverLevelSelectionProviderIterator != null) {
if (selectionChanged) {
//Update the complete retriever level, because the selection changed
// retrieverLevelSelectionProvider.updateAvailableData(getSelectedDimension());
//TODO This is currently done with a selection changed notification of the inner selection table
//FIXME Enable, after the notifications can be blocked.
} else {
//Continue with the update of the retriever level selection provider
retrieverLevelSelectionProvider.updateAvailableData(getSelectedDimension(), retrieverLevelSelectionProviderIterator);
}
}
}
busyIndicator.setVisible(false);
}
@Override
public void onFailure(Throwable caught) {
errorReporter.reportError("Error fetching the dimension values of " + dimension + ": "
+ caught.getMessage());
selectionChangedCallback.onFailure(caught);
}
});
}
@@ -95,7 +95,7 @@ public class FilterableSelectionTable<ContentType extends Serializable> extends
* @param newContent
*/
@SuppressWarnings("unchecked") //You can't use instanceof for generic type parameters
public boolean setContent(Collection<?> newContent) {
public boolean setContent(Collection<?> newContent, boolean notifyListenersWhenSelectionChanged) {
Collection<ContentType> specificNewContent = null;
try {
specificNewContent = (Collection<ContentType>) newContent;
@@ -104,7 +104,13 @@ public class FilterableSelectionTable<ContentType extends Serializable> extends
}
internalSetContent(specificNewContent);
Set<ContentType> previousSelection = selectionModel.getSelectedSet();
selectionModel.clear();
if (notifyListenersWhenSelectionChanged) {
clearSelection();
} else {
blockSelectionChangeNotification = true;
clearSelection();
blockSelectionChangeNotification = false;
}
return !previousSelection.isEmpty();
}
@@ -115,7 +121,7 @@ public class FilterableSelectionTable<ContentType extends Serializable> extends
* @return <code>true</code>, if previously selected elements have been removed.
*/
@SuppressWarnings("unchecked") //You can't use instanceof for generic type parameters
public boolean updateContent(Collection<?> newContent) {
public boolean updateContent(Collection<?> newContent, boolean notifyListenersWhenSelectionChanged) {
Collection<ContentType> specificNewContent = null;
try {
specificNewContent = (Collection<ContentType>) newContent;
@@ -123,7 +129,7 @@ public class FilterableSelectionTable<ContentType extends Serializable> extends
return false;
}
internalSetContent(specificNewContent);
return cleanSelection();
return cleanSelection(notifyListenersWhenSelectionChanged);
}
private void internalSetContent(Collection<ContentType> specificNewContent) {
@@ -138,7 +144,7 @@ public class FilterableSelectionTable<ContentType extends Serializable> extends
* Unselects the selected elements, that aren't in the data anymore.
* @return <code>true</code>, if elements have been unselected;
*/
private boolean cleanSelection() {
private boolean cleanSelection(boolean notifyListenersWhenSelectionChanged) {
boolean selectionChanged = false;
blockSelectionChangeNotification = true;
@@ -158,7 +164,7 @@ public class FilterableSelectionTable<ContentType extends Serializable> extends
}
blockSelectionChangeNotification = false;
if (selectionChanged) {
if (selectionChanged && notifyListenersWhenSelectionChanged) {
notifyListeners();
}
return selectionChanged;
@@ -219,33 +219,19 @@ public class ListRetrieverChainFilterSelectionProvider implements FilterSelectio
updateFilterSelectionProviders(retrieverLevelFilterSelectionProvider.getRetrieverLevel(), dimensionFilterSelectionProvider.getSelectedDimension());
}
private void updateFilterSelectionProviders(int beginningWithLevel, final FunctionDTO exceptForDimension) {
void updateFilterSelectionProviders(int beginningWithLevel, final FunctionDTO exceptForDimension) {
LocalizedTypeDTO retrievedDataType = null;
int nextRetrieverLevel = 0;
for (int retrieverLevel = beginningWithLevel; retrieverLevel < retrieverChain.size(); retrieverLevel++) {
retrievedDataType = retrieverChain.getRetrievedDataType(retrieverLevel);
if (selectionProvidersMappedByRetrievedDataType.containsKey(retrievedDataType)) {
nextRetrieverLevel = retrieverLevel + 1;
break;
}
}
final int finalNextRetrievalLevel = nextRetrieverLevel;
if (retrievedDataType != null) {
selectionProvidersMappedByRetrievedDataType.get(retrievedDataType).updateAvailableData(exceptForDimension,
new AsyncCallback<Boolean>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(Boolean selectionChanged) {
if (selectionChanged != null && !selectionChanged) {
updateFilterSelectionProviders(finalNextRetrievalLevel, exceptForDimension);
}
}
});
selectionProvidersMappedByRetrievedDataType.get(retrievedDataType).updateAvailableData(exceptForDimension);
} else {
//The update of the whole retriever chain is completed. Notify the listeners.
mainPanel.setWidgetHidden(selectionPresenterScrollPanel, getSelection().isEmpty());
notifyListeners();
}
@@ -9,7 +9,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ProvidesResize;
import com.google.gwt.user.client.ui.RequiresResize;
@@ -134,33 +133,22 @@ public class RetrieverLevelFilterSelectionProvider implements Component<Abstract
* Updates the dimension values except for the given dimension.<br>
* Aborts the update, if it caused a selection change.
*/
void updateAvailableData(FunctionDTO exceptForDimension, AsyncCallback<Boolean> selectionChangedCallback) {
updateAvailableData(exceptForDimension, selectionChangedCallback, dimensionSelectionProviders.iterator());
void updateAvailableData(FunctionDTO exceptForDimension) {
updateAvailableData(exceptForDimension, dimensionSelectionProviders.iterator());
}
private void updateAvailableData(final FunctionDTO exceptForDimension, final AsyncCallback<Boolean> selectionChangedCallback, final Iterator<DimensionFilterSelectionProvider> selectionProviderIterator) {
void updateAvailableData(final FunctionDTO exceptForDimension, final Iterator<DimensionFilterSelectionProvider> selectionProviderIterator) {
if (selectionProviderIterator.hasNext()) {
DimensionFilterSelectionProvider selectionProvider = selectionProviderIterator.next();
FunctionDTO selectedDimension = selectionProvider.getSelectedDimension();
if (selectedDimension != null && !selectedDimension.equals(exceptForDimension)) {
selectionProvider.fetchAndDisplayAvailableData(true, new AsyncCallback<Boolean>() {
@Override
public void onFailure(Throwable caught) { }
@Override
public void onSuccess(Boolean selectionChanged) {
if (selectionChanged != null && !selectionChanged) {
updateAvailableData(exceptForDimension, selectionChangedCallback, selectionProviderIterator);
} else {
selectionChangedCallback.onSuccess(true);
}
}
});
selectionProvider.fetchAndUpdateAvailableData(selectionProviderIterator);
} else {
updateAvailableData(exceptForDimension, selectionChangedCallback, selectionProviderIterator);
updateAvailableData(exceptForDimension, selectionProviderIterator);
}
} else {
selectionChangedCallback.onSuccess(false);
// Update for this level is completed. Continue with the next level.
retrieverChainSelectionProvider.updateFilterSelectionProviders(getRetrieverLevel() + 1, exceptForDimension);
}
}