arrows added to sort menu to show sorting direction

This commit is contained in:
2014-03-15 00:35:49 +01:00
parent 814cff94c8
commit 128311320e
2 changed files with 74 additions and 10 deletions

View File

@@ -30,10 +30,10 @@ import de.frosch95.geofrogger.application.SessionContext;
import de.frosch95.geofrogger.application.SessionContextListener; import de.frosch95.geofrogger.application.SessionContextListener;
import de.frosch95.geofrogger.fx.components.CacheListCell; import de.frosch95.geofrogger.fx.components.CacheListCell;
import de.frosch95.geofrogger.fx.components.IconManager; import de.frosch95.geofrogger.fx.components.IconManager;
import de.frosch95.geofrogger.fx.components.SortingMenuItem;
import de.frosch95.geofrogger.model.Cache; import de.frosch95.geofrogger.model.Cache;
import de.frosch95.geofrogger.service.CacheService; import de.frosch95.geofrogger.service.CacheService;
import de.frosch95.geofrogger.service.CacheSortField; import de.frosch95.geofrogger.service.CacheSortField;
import de.frosch95.geofrogger.service.SortDirection;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
@@ -49,7 +49,6 @@ import java.util.ResourceBundle;
import static de.frosch95.geofrogger.fx.utils.JavaFXUtils.addClasses; import static de.frosch95.geofrogger.fx.utils.JavaFXUtils.addClasses;
import static de.frosch95.geofrogger.service.CacheSortField.*; import static de.frosch95.geofrogger.service.CacheSortField.*;
import static de.frosch95.geofrogger.service.SortDirection.*;
/** /**
* FXML Controller class * FXML Controller class
@@ -59,10 +58,10 @@ import static de.frosch95.geofrogger.service.SortDirection.*;
public class CacheListController implements Initializable, SessionContextListener { public class CacheListController implements Initializable, SessionContextListener {
private static final String CACHE_LIST_ACTION_ICONS = "cache-list-action-icons"; private static final String CACHE_LIST_ACTION_ICONS = "cache-list-action-icons";
private final SessionContext sessionContext = SessionContext.getInstance(); private final SessionContext sessionContext = SessionContext.getInstance();
private final CacheService cacheService = ServiceManager.getInstance().getCacheService(); private final CacheService cacheService = ServiceManager.getInstance().getCacheService();
private CacheSortField currentSortField = NAME; private SortingMenuItem currentSortingButton = null;
private SortDirection currentSortDirection = ASC;
@FXML @FXML
private ListView cacheListView; private ListView cacheListView;
@@ -121,8 +120,10 @@ public class CacheListController implements Initializable, SessionContextListene
private Menu createSortMenu(final ResourceBundle rb) { private Menu createSortMenu(final ResourceBundle rb) {
Menu sortMenu = new Menu(rb.getString("menu.title.sort")); Menu sortMenu = new Menu(rb.getString("menu.title.sort"));
currentSortingButton = createSortButton(rb, NAME);
currentSortingButton.setSelected(true);
sortMenu.getItems().addAll( sortMenu.getItems().addAll(
createSortButton(rb, NAME), currentSortingButton,
createSortButton(rb, TYPE), createSortButton(rb, TYPE),
createSortButton(rb, DIFFICULTY), createSortButton(rb, DIFFICULTY),
createSortButton(rb, TERRAIN), createSortButton(rb, TERRAIN),
@@ -131,15 +132,23 @@ public class CacheListController implements Initializable, SessionContextListene
return sortMenu; return sortMenu;
} }
private MenuItem createSortButton(final ResourceBundle rb, final CacheSortField field) { private SortingMenuItem createSortButton(final ResourceBundle rb, final CacheSortField field) {
MenuItem button = new MenuItem(rb.getString("sort.cache."+field.getFieldName())); SortingMenuItem button = new SortingMenuItem(rb.getString("sort.cache."+field.getFieldName()), field);
button.setOnAction(actionEvent -> { button.setOnAction(actionEvent -> {
currentSortDirection = (field.equals(currentSortField) && currentSortDirection.equals(ASC)) ? DESC : ASC;
currentSortField = field; // if there was another button selected, change the selection
sessionContext.setData("cache-list", cacheService.getAllCaches(currentSortField, currentSortDirection)); if (!currentSortingButton.equals(button)) {
currentSortingButton.setSelected(false);
currentSortingButton = button;
}
currentSortingButton.setSelected(true);
sessionContext.setData("cache-list", cacheService.getAllCaches(currentSortingButton.getField(), currentSortingButton.getSortDirection()));
}); });
return button; return button;
} }
} }

View File

@@ -0,0 +1,55 @@
package de.frosch95.geofrogger.fx.components;
import de.frosch95.geofrogger.service.CacheSortField;
import de.frosch95.geofrogger.service.SortDirection;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.MenuItem;
import static de.frosch95.geofrogger.service.SortDirection.ASC;
import static de.frosch95.geofrogger.service.SortDirection.DESC;
/**
* MenuItem with a ASC and DESC arrow when selected
*/
public class SortingMenuItem extends MenuItem {
private static final String BLACK_UP_POINTING_TRIANGLE = " \u25B2";
private static final String BLACK_DOWN_POINTING_TRIANGLE = " \u25BC";
private boolean oldValue = false;
private ObjectProperty<CacheSortField> field = new SimpleObjectProperty<>();
private ObjectProperty<SortDirection> sortDirection = new SimpleObjectProperty<>(ASC);
public SortingMenuItem(final String text, final CacheSortField field) {
super(text);
this.field.setValue(field);
}
public void setSelected(boolean newValue) {
final StringBuilder text = new StringBuilder(getText());
String triangle = BLACK_UP_POINTING_TRIANGLE;
if (newValue && oldValue) {
sortDirection.set(sortDirection.get().equals(ASC) ? DESC : ASC);
triangle = sortDirection.get().equals(ASC) ? BLACK_UP_POINTING_TRIANGLE : BLACK_DOWN_POINTING_TRIANGLE;
text.delete(text.length() - 2, text.length());
text.append(triangle);
} else if (newValue) {
text.append(triangle);
} else {
text.delete(text.length() - 2, text.length());
}
setText(text.toString());
oldValue = newValue;
}
public CacheSortField getField() {
return field.get();
}
public SortDirection getSortDirection() {
return sortDirection.get();
}
}