fixed problem with setTickInterval when changing detailType for Leaderboard charts

This commit is contained in:
fmittag committed 2013-10-01 14:09:19 +02:00
1 parent 833aadbae9
commit 4397217454
5 files changed
+109 -103

No files matched your search

@@ -26,7 +26,6 @@ import org.moxieapps.gwt.highcharts.client.plotOptions.LinePlotOptions;
import org.moxieapps.gwt.highcharts.client.plotOptions.Marker;
import org.moxieapps.gwt.highcharts.client.plotOptions.Marker.Symbol;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RequiresResize;
@@ -138,18 +137,28 @@ public abstract class AbstractCompetitorLeaderboardChart<SettingsType> extends A
}
protected void setSelectedDetailType(DetailType newSelectedDetailType) {
boolean oldReversedYAxis = hasReversedYAxis(this.selectedDetailType);
this.selectedDetailType = newSelectedDetailType;
// TODO There is a bug in the highcharts library which prevents to change the reverse property of the YAxis
// Because we need this functionality we need to recreate the chart each time the YAxis changes
if (oldReversedYAxis != hasReversedYAxis(selectedDetailType)) {
chart = createChart();
// TODO: There are some problems with the highcharts library to change all values of a chart dynamically.
// Therefore we need to recreate the chart each time the detail type changes
chart = createChart();
switch(selectedDetailType) {
case REGATTA_RANK:
chart.getYAxis().setReversed(true);
chart.getYAxis().setTickInterval(1.0);
break;
case RACE_TOTAL_POINTS:
chart.getYAxis().setTickInterval(5.0);
chart.getYAxis().setReversed(false);
break;
default:
break;
}
chart.setTitle(new ChartTitle().setText(DetailTypeFormatter.format(selectedDetailType)), null);
final String unit = DetailTypeFormatter.getUnit(getSelectedDetailType());
final String label = unit.isEmpty() ? "" : "[" + unit + "]";
chart.getYAxis().setAxisTitleText(label);
chart.getYAxis().setReversed(isYAxisReversed());
chart.setToolTip(new ToolTip().setEnabled(true).setFormatter(new ToolTipFormatter() {
@Override
@@ -166,14 +175,6 @@ public abstract class AbstractCompetitorLeaderboardChart<SettingsType> extends A
}));
}
private boolean hasReversedYAxis(DetailType detailType) {
return detailType == DetailType.REGATTA_RANK;
}
private boolean isYAxisReversed() {
return hasReversedYAxis(selectedDetailType);
}
protected void clearChart() {
competitorSeries.clear();
chart.removeAllSeries();
@@ -289,19 +290,9 @@ public abstract class AbstractCompetitorLeaderboardChart<SettingsType> extends A
}
}
chart.getYAxis().setMaxPadding(0.5 / maxCompetitorCount).setMinPadding(0.5 / maxCompetitorCount);
setAxisTickInterval(chart.getYAxis().getNativeAxis(), 1.0);
chart.setHeight(maxCompetitorCount * 30 + 100 + "px");
}
/**
* To set tick interval of an axis dynamically we need to access the native javascript object
* @param axis the axis
* @param newTickInterval the new value of the tick interval
*/
private static native void setAxisTickInterval(JavaScriptObject axis, double newTickInterval) /*-{
return axis.options.tickInterval = value;
}-*/;
private void fillTotalPointsSeries(List<Triple<String, List<CompetitorDTO>, List<Double>>> result, List<Series> chartSeries) {
Double maxTotalPoints = 0.0;
Set<Series> unusedSeries = new HashSet<Series>(competitorSeries.values());
@@ -348,7 +339,6 @@ public abstract class AbstractCompetitorLeaderboardChart<SettingsType> extends A
}
}
chart.getYAxis().setMaxPadding(0.5/maxCompetitorCount).setMinPadding(0.5/maxCompetitorCount);
setAxisTickInterval(chart.getYAxis().getNativeAxis(), 5.0);
chart.setHeight(maxCompetitorCount * 30 + 100 + "px");
}
@@ -59,9 +59,11 @@ public abstract class AbstractLeaderboardViewer extends SimplePanel {
}
protected <SettingsType> void addComponentToNavigationMenu(final Component<SettingsType> component, boolean isCheckboxEnabled,
String componentDisplayName) {
String componentDisplayName, final boolean hasSettingsWhenComponentIsInvisible) {
final String componentName = componentDisplayName != null ? componentDisplayName : component.getLocalizedShortName();
final CheckBox checkBox= new CheckBox(componentName);
final Button settingsButton = new Button("");
checkBox.getElement().getStyle().setFloat(Style.Float.LEFT);
checkBox.setEnabled(isCheckboxEnabled);
@@ -79,23 +81,24 @@ public abstract class AbstractLeaderboardViewer extends SimplePanel {
// trigger the component to update its data
((TimeListener) component).timeChanged(timer.getTime());
}
if(component.hasSettings() && !hasSettingsWhenComponentIsInvisible) {
settingsButton.setEnabled(visible);
}
}
});
componentsNavigationPanel.add(checkBox);
Button settingsButton = new Button("");
if(component.hasSettings()) {
settingsButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
new SettingsDialog<SettingsType>(component, stringMessages).show();
}
}
});
} else {
settingsButton.setEnabled(false);
}
}
settingsButton.setEnabled(component.hasSettings() && hasSettingsWhenComponentIsInvisible);
settingsButton.addStyleName(STYLE_VIEWER_TOOLBAR_SETTINGS_BUTTON);
settingsButton.getElement().getStyle().setFloat(Style.Float.LEFT);
settingsButton.setTitle(stringMessages.settingsForComponent(componentName));
@@ -49,8 +49,8 @@ public class LeaderboardViewer extends AbstractLeaderboardViewer {
mainPanel.add(leaderboardPanel);
mainPanel.add(multiCompetitorChart);
addComponentToNavigationMenu(leaderboardPanel, false, null);
addComponentToNavigationMenu(multiCompetitorChart, true, null);
addComponentToNavigationMenu(leaderboardPanel, false, null, /* hasSettingsWhenComponentIsInvisible*/ true);
addComponentToNavigationMenu(multiCompetitorChart, true, null, /* hasSettingsWhenComponentIsInvisible*/ true);
if(showCharts) {
multiCompetitorChart.timeChanged(timer.getTime());
@@ -52,7 +52,7 @@ public class MetaLeaderboardViewer extends AbstractLeaderboardViewer {
multiCompetitorChart.getElement().getStyle().setMarginBottom(10, Unit.PX);
multiLeaderboardPanel = new MultiLeaderboardPanel(sailingService, metaLeaderboardName, asyncActionsExecutor, timer,
leaderboardSettings, preselectedLeaderboardName, preselectedRace, errorReporter, stringMessages,
preselectedLeaderboardName, preselectedRace, errorReporter, stringMessages,
userAgent, showRaceDetails, autoExpandLastRaceColumn);
multiLeaderboardPanel.setVisible(false);
@@ -60,8 +60,8 @@ public class MetaLeaderboardViewer extends AbstractLeaderboardViewer {
mainPanel.add(multiCompetitorChart);
mainPanel.add(multiLeaderboardPanel);
addComponentToNavigationMenu(metaLeaderboardPanel, false, stringMessages.seriesLeaderboard());
addComponentToNavigationMenu(multiCompetitorChart, true, null);
addComponentToNavigationMenu(multiLeaderboardPanel, true, stringMessages.regattaLeaderboards());
addComponentToNavigationMenu(metaLeaderboardPanel, false, stringMessages.seriesLeaderboard(), /* hasSettingsWhenComponentIsInvisible*/ true);
addComponentToNavigationMenu(multiCompetitorChart, true, null, /* hasSettingsWhenComponentIsInvisible*/ true);
addComponentToNavigationMenu(multiLeaderboardPanel, true , stringMessages.regattaLeaderboards(), /* hasSettingsWhenComponentIsInvisible*/ false);
}
}
@@ -2,7 +2,9 @@ package com.sap.sailing.gwt.ui.leaderboard;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.dom.client.Style.Unit;
@@ -28,19 +30,19 @@ import com.sap.sailing.gwt.ui.client.shared.components.AbstractLazyComponent;
import com.sap.sailing.gwt.ui.client.shared.components.SettingsDialogComponent;
/**
* A panel managing multiple {@link LeaderboardPanel}s (e.g. from a series leaderboard) so that the user can switch between them.
* A panel managing multiple {@link LeaderboardPanel}s (e.g. from a meta leaderboard) so that the user can switch between them.
* @author Frank
*/
public class MultiLeaderboardPanel extends AbstractLazyComponent<LeaderboardSettings> implements TimeListener {
private LeaderboardPanel selectedRegattaLeaderboardPanel;
private FlowPanel selectedRegattaLeaderboardFlowPanel;
private LeaderboardPanel selectedLeaderboardPanel;
private FlowPanel selectedLeaderboardFlowPanel;
private final StringMessages stringMessages;
private final ErrorReporter errorReporter;
private final SailingServiceAsync sailingService;
private String selectedRegattaLeaderboardName;
private String selectedLeaderboardName;
private final AsyncActionsExecutor asyncActionsExecutor;
private final UserAgentDetails userAgent;
@@ -48,16 +50,15 @@ public class MultiLeaderboardPanel extends AbstractLazyComponent<LeaderboardSett
private final Timer timer;
private VerticalPanel mainPanel;
private final List<Pair<String, String>> regattaLeaderboardNamesAndDisplayNames;
private final List<Pair<String, String>> leaderboardNamesAndDisplayNames;
private final Map<String, LeaderboardSettings> leaderboardNamesAndSettings;
private LeaderboardSettings selectedRegattaLeaderboardSettings;
private TabPanel regattaLeaderboardsTabPanel;
private Label regattaLeaderboardsLabel;
private TabPanel leaderboardsTabPanel;
private Label leaderboardsLabel;
private final String metaLeaderboardName;
public MultiLeaderboardPanel(SailingServiceAsync sailingService, String metaLeaderboardName, AsyncActionsExecutor asyncActionsExecutor, Timer timer,
LeaderboardSettings leaderboardSettings, String preselectedActLeaderboardName, RaceIdentifier preselectedRace,
String preselectedLeaderboardName, RaceIdentifier preselectedRace,
ErrorReporter errorReporter, StringMessages stringMessages,
UserAgentDetails userAgent, boolean showRaceDetails, boolean autoExpandLastRaceColumn) {
this.stringMessages = stringMessages;
@@ -68,41 +69,51 @@ public class MultiLeaderboardPanel extends AbstractLazyComponent<LeaderboardSett
this.userAgent = userAgent;
this.showRaceDetails = showRaceDetails;
this.timer = timer;
this.selectedRegattaLeaderboardName = preselectedActLeaderboardName;
this.selectedLeaderboardName = preselectedLeaderboardName;
selectedRegattaLeaderboardFlowPanel = null;
selectedRegattaLeaderboardPanel = null;
regattaLeaderboardNamesAndDisplayNames = new ArrayList<Pair<String, String>>();
selectedRegattaLeaderboardSettings = LeaderboardSettingsFactory.getInstance().createNewDefaultSettings(null, null, null, false);
selectedLeaderboardFlowPanel = null;
selectedLeaderboardPanel = null;
leaderboardNamesAndDisplayNames = new ArrayList<Pair<String, String>>();
leaderboardNamesAndSettings = new HashMap<String, LeaderboardSettings>();
}
private LeaderboardSettings getOrCreateLeaderboardSettings(String leaderboardName) {
LeaderboardSettings result = leaderboardNamesAndSettings.get(leaderboardName);
if(result == null) {
result = LeaderboardSettingsFactory.getInstance().createNewDefaultSettings(null, null, null, false);
leaderboardNamesAndSettings.put(leaderboardName, result);
}
return result;
}
@Override
public Widget createWidget() {
mainPanel = new VerticalPanel();
regattaLeaderboardsLabel = new Label(stringMessages.regattaLeaderboards());
regattaLeaderboardsLabel.setVisible(false);
regattaLeaderboardsLabel.getElement().getStyle().setFontWeight(FontWeight.BOLD);
regattaLeaderboardsLabel.getElement().getStyle().setMargin(5, Unit.PX);
mainPanel.add(regattaLeaderboardsLabel);
leaderboardsLabel = new Label(stringMessages.regattaLeaderboards());
leaderboardsLabel.setVisible(false);
leaderboardsLabel.getElement().getStyle().setFontWeight(FontWeight.BOLD);
leaderboardsLabel.getElement().getStyle().setMargin(5, Unit.PX);
mainPanel.add(leaderboardsLabel);
regattaLeaderboardsTabPanel = new TabPanel();
regattaLeaderboardsTabPanel.setVisible(false);
regattaLeaderboardsTabPanel.setAnimationEnabled(false);
regattaLeaderboardsTabPanel.setWidth("100%");
regattaLeaderboardsTabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
leaderboardsTabPanel = new TabPanel();
leaderboardsTabPanel.setVisible(false);
leaderboardsTabPanel.setAnimationEnabled(false);
leaderboardsTabPanel.setWidth("100%");
leaderboardsTabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
@Override
public void onSelection(SelectionEvent<Integer> event) {
Integer tabIndex = event.getSelectedItem();
if(tabIndex >= 0) {
updateSelectedLeaderboard(regattaLeaderboardNamesAndDisplayNames.get(tabIndex).getA(), tabIndex);
updateSelectedLeaderboard(leaderboardNamesAndDisplayNames.get(tabIndex).getA(), tabIndex);
}
}
});
mainPanel.add(regattaLeaderboardsTabPanel);
mainPanel.add(leaderboardsTabPanel);
updateRegattaLeaderboardSelection();
updateLeaderboardSelection();
return mainPanel;
}
@@ -119,19 +130,20 @@ public class MultiLeaderboardPanel extends AbstractLazyComponent<LeaderboardSett
@Override
public SettingsDialogComponent<LeaderboardSettings> getSettingsDialogComponent() {
return selectedRegattaLeaderboardPanel.getSettingsDialogComponent();
return selectedLeaderboardPanel.getSettingsDialogComponent();
}
@Override
public void updateSettings(LeaderboardSettings newSettings) {
selectedRegattaLeaderboardPanel.updateSettings(newSettings);
leaderboardNamesAndSettings.put(selectedLeaderboardName, newSettings);
selectedLeaderboardPanel.updateSettings(newSettings);
}
public void setRegattaLeaderboardNames(List<Pair<String, String>> newLeaderboardNamesAndDisplayNames) {
regattaLeaderboardNamesAndDisplayNames.clear();
regattaLeaderboardNamesAndDisplayNames.addAll(newLeaderboardNamesAndDisplayNames);
public void setLeaderboardNames(List<Pair<String, String>> newLeaderboardNamesAndDisplayNames) {
leaderboardNamesAndDisplayNames.clear();
leaderboardNamesAndDisplayNames.addAll(newLeaderboardNamesAndDisplayNames);
updateRegattaLeaderboardSelection();
updateLeaderboardSelection();
}
private void readAndUpdateLeaderboardsOfMetaleaderboard() {
@@ -139,7 +151,7 @@ public class MultiLeaderboardPanel extends AbstractLazyComponent<LeaderboardSett
@Override
public void onSuccess(List<Pair<String, String>> leaderboardNamesAndDisplayNames) {
setRegattaLeaderboardNames(leaderboardNamesAndDisplayNames);
setLeaderboardNames(leaderboardNamesAndDisplayNames);
}
@Override
@@ -148,52 +160,53 @@ public class MultiLeaderboardPanel extends AbstractLazyComponent<LeaderboardSett
});
}
private void updateRegattaLeaderboardSelection() {
if(regattaLeaderboardsTabPanel != null) {
regattaLeaderboardsTabPanel.clear();
private void updateLeaderboardSelection() {
if(leaderboardsTabPanel != null) {
leaderboardsTabPanel.clear();
int index = 0;
int leaderboardCount = regattaLeaderboardNamesAndDisplayNames.size();
for (Pair<String, String> leaderboardNameAndDisplayName : regattaLeaderboardNamesAndDisplayNames) {
int leaderboardCount = leaderboardNamesAndDisplayNames.size();
for (Pair<String, String> leaderboardNameAndDisplayName : leaderboardNamesAndDisplayNames) {
FlowPanel tabFlowPanel = new FlowPanel();
regattaLeaderboardsTabPanel.add(tabFlowPanel, leaderboardNameAndDisplayName.getB(), false);
leaderboardsTabPanel.add(tabFlowPanel, leaderboardNameAndDisplayName.getB(), false);
if(selectedRegattaLeaderboardName != null && selectedRegattaLeaderboardName.equals(leaderboardNameAndDisplayName.getA())) {
regattaLeaderboardsTabPanel.selectTab(index);
if(selectedLeaderboardName != null && selectedLeaderboardName.equals(leaderboardNameAndDisplayName.getA())) {
leaderboardsTabPanel.selectTab(index);
}
index++;
}
// show the last leaderboard when no leaderboard is selected yet
if(selectedRegattaLeaderboardName == null && leaderboardCount > 0) {
regattaLeaderboardsTabPanel.selectTab(leaderboardCount-1);
if(selectedLeaderboardName == null && leaderboardCount > 0) {
leaderboardsTabPanel.selectTab(leaderboardCount-1);
}
regattaLeaderboardsTabPanel.setVisible(leaderboardCount > 0);
regattaLeaderboardsLabel.setVisible(leaderboardCount > 0);
leaderboardsTabPanel.setVisible(leaderboardCount > 0);
leaderboardsLabel.setVisible(leaderboardCount > 0);
}
}
private void updateSelectedLeaderboard(String selectedLeaderboardName, int newTabIndex) {
private void updateSelectedLeaderboard(String newSelectedLeaderboardName, int newTabIndex) {
this.selectedLeaderboardName = newSelectedLeaderboardName;
if(selectedLeaderboardName != null) {
if(selectedRegattaLeaderboardPanel != null && selectedRegattaLeaderboardFlowPanel != null) {
timer.removeTimeListener(selectedRegattaLeaderboardPanel);
selectedRegattaLeaderboardFlowPanel.remove(selectedRegattaLeaderboardPanel);
selectedRegattaLeaderboardPanel = null;
selectedRegattaLeaderboardFlowPanel = null;
if(selectedLeaderboardPanel != null && selectedLeaderboardFlowPanel != null) {
timer.removeTimeListener(selectedLeaderboardPanel);
selectedLeaderboardFlowPanel.remove(selectedLeaderboardPanel);
selectedLeaderboardPanel = null;
selectedLeaderboardFlowPanel = null;
}
selectedRegattaLeaderboardFlowPanel = (FlowPanel) regattaLeaderboardsTabPanel.getWidget(newTabIndex);
selectedRegattaLeaderboardPanel = new LeaderboardPanel(sailingService, asyncActionsExecutor,
selectedRegattaLeaderboardSettings, /* preselectedRace*/ null, new CompetitorSelectionModel(true), timer,
selectedLeaderboardFlowPanel = (FlowPanel) leaderboardsTabPanel.getWidget(newTabIndex);
selectedLeaderboardPanel = new LeaderboardPanel(sailingService, asyncActionsExecutor,
getOrCreateLeaderboardSettings(selectedLeaderboardName), /* preselectedRace*/ null, new CompetitorSelectionModel(true), timer,
null, selectedLeaderboardName, errorReporter, stringMessages, userAgent,
showRaceDetails, /* raceTimesInfoProvider */null, false, /* adjustTimerDelay */ true);
selectedRegattaLeaderboardFlowPanel.add(selectedRegattaLeaderboardPanel);
selectedLeaderboardFlowPanel.add(selectedLeaderboardPanel);
} else {
if(selectedRegattaLeaderboardPanel != null && selectedRegattaLeaderboardFlowPanel != null) {
timer.removeTimeListener(selectedRegattaLeaderboardPanel);
selectedRegattaLeaderboardFlowPanel.remove(selectedRegattaLeaderboardPanel);
selectedRegattaLeaderboardPanel = null;
selectedRegattaLeaderboardFlowPanel = null;
if(selectedLeaderboardPanel != null && selectedLeaderboardFlowPanel != null) {
timer.removeTimeListener(selectedLeaderboardPanel);
selectedLeaderboardFlowPanel.remove(selectedLeaderboardPanel);
selectedLeaderboardPanel = null;
selectedLeaderboardFlowPanel = null;
}
}
}
@@ -211,8 +224,8 @@ public class MultiLeaderboardPanel extends AbstractLazyComponent<LeaderboardSett
@Override
public void timeChanged(Date date) {
if(selectedRegattaLeaderboardPanel != null) {
selectedRegattaLeaderboardPanel.timeChanged(date);
if(selectedLeaderboardPanel != null) {
selectedLeaderboardPanel.timeChanged(date);
}
}