2013-09-18 20:44:20 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013, Andreas Billmann <abi@geofroggerfx.de>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
2014-05-17 07:18:37 +02:00
|
|
|
package de.geofroggerfx.fx.cachelist;
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2014-05-17 07:18:37 +02:00
|
|
|
import de.geofroggerfx.application.SessionContext;
|
|
|
|
|
import de.geofroggerfx.application.SessionContextListener;
|
|
|
|
|
import de.geofroggerfx.fx.components.CacheListCell;
|
|
|
|
|
import de.geofroggerfx.fx.components.IconManager;
|
|
|
|
|
import de.geofroggerfx.fx.components.SortingMenuItem;
|
|
|
|
|
import de.geofroggerfx.model.Cache;
|
|
|
|
|
import de.geofroggerfx.service.CacheService;
|
|
|
|
|
import de.geofroggerfx.service.CacheSortField;
|
2013-09-18 20:44:20 +02:00
|
|
|
import javafx.application.Platform;
|
|
|
|
|
import javafx.beans.value.ChangeListener;
|
|
|
|
|
import javafx.beans.value.ObservableValue;
|
|
|
|
|
import javafx.fxml.FXML;
|
|
|
|
|
import javafx.fxml.Initializable;
|
2014-05-23 10:24:33 +02:00
|
|
|
import javafx.scene.control.Label;
|
|
|
|
|
import javafx.scene.control.ListView;
|
|
|
|
|
import javafx.scene.control.Menu;
|
|
|
|
|
import javafx.scene.control.MenuButton;
|
2013-09-28 00:11:37 +02:00
|
|
|
import javafx.scene.image.ImageView;
|
2013-09-18 20:44:20 +02:00
|
|
|
import javafx.util.Callback;
|
|
|
|
|
|
2014-05-23 10:24:33 +02:00
|
|
|
import javax.inject.Inject;
|
2013-09-18 20:44:20 +02:00
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.ResourceBundle;
|
|
|
|
|
|
2014-05-17 07:18:37 +02:00
|
|
|
import static de.geofroggerfx.fx.utils.JavaFXUtils.addClasses;
|
|
|
|
|
import static de.geofroggerfx.service.CacheSortField.*;
|
2013-09-18 20:44:20 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* FXML Controller class
|
|
|
|
|
*
|
|
|
|
|
* @author Andreas
|
|
|
|
|
*/
|
|
|
|
|
public class CacheListController implements Initializable, SessionContextListener {
|
|
|
|
|
|
|
|
|
|
private static final String CACHE_LIST_ACTION_ICONS = "cache-list-action-icons";
|
2014-03-15 00:35:49 +01:00
|
|
|
|
2014-05-23 10:24:33 +02:00
|
|
|
@Inject
|
|
|
|
|
private SessionContext sessionContext;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
private CacheService cacheService;
|
|
|
|
|
|
2014-03-15 00:35:49 +01:00
|
|
|
private SortingMenuItem currentSortingButton = null;
|
2013-09-18 20:44:20 +02:00
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
|
private ListView cacheListView;
|
|
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
|
private Label cacheNumber;
|
|
|
|
|
|
|
|
|
|
@FXML
|
2013-09-28 00:11:37 +02:00
|
|
|
private MenuButton menuIcon;
|
2013-09-18 20:44:20 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes the controller class.
|
|
|
|
|
*
|
|
|
|
|
* @param url
|
|
|
|
|
* @param rb
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2013-09-28 00:11:37 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2013-09-18 20:44:20 +02:00
|
|
|
public void initialize(URL url, ResourceBundle rb) {
|
|
|
|
|
setSessionListener();
|
|
|
|
|
setCellFactory();
|
|
|
|
|
|
|
|
|
|
cacheListView.getSelectionModel().selectedItemProperty().addListener(
|
|
|
|
|
(ChangeListener<Cache>) (ObservableValue<? extends Cache> ov, Cache oldValue, Cache newValue) ->
|
2013-09-27 20:28:44 +02:00
|
|
|
sessionContext.setData("current-cache", newValue)
|
2013-09-18 20:44:20 +02:00
|
|
|
);
|
|
|
|
|
|
2013-09-28 00:11:37 +02:00
|
|
|
initListMenuButton(rb);
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-09-28 00:11:37 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2013-09-18 20:44:20 +02:00
|
|
|
public void sessionContextChanged() {
|
|
|
|
|
List<Cache> caches = (List<Cache>) sessionContext.getData("cache-list");
|
|
|
|
|
Platform.runLater(() -> {
|
|
|
|
|
cacheNumber.setText("(" + caches.size() + ")");
|
|
|
|
|
cacheListView.getItems().setAll(caches);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setCellFactory() {
|
|
|
|
|
cacheListView.setCellFactory(
|
|
|
|
|
(Callback<ListView<Cache>, CacheListCell>)
|
2013-09-27 20:28:44 +02:00
|
|
|
(ListView<Cache> p) -> new CacheListCell());
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setSessionListener() {
|
|
|
|
|
sessionContext.addListener("cache-list", this);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-28 00:11:37 +02:00
|
|
|
private void initListMenuButton(final ResourceBundle rb) {
|
|
|
|
|
addClasses(menuIcon, CACHE_LIST_ACTION_ICONS);
|
2014-01-05 01:33:25 +01:00
|
|
|
menuIcon.setGraphic(new ImageView(IconManager.getIcon("iconmonstr-menu-icon.png", IconManager.IconSize.SMALL)));
|
2013-09-28 00:11:37 +02:00
|
|
|
menuIcon.getItems().addAll(createSortMenu(rb));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Menu createSortMenu(final ResourceBundle rb) {
|
|
|
|
|
Menu sortMenu = new Menu(rb.getString("menu.title.sort"));
|
2014-03-15 00:35:49 +01:00
|
|
|
currentSortingButton = createSortButton(rb, NAME);
|
|
|
|
|
currentSortingButton.setSelected(true);
|
2013-09-28 00:11:37 +02:00
|
|
|
sortMenu.getItems().addAll(
|
2014-03-15 00:35:49 +01:00
|
|
|
currentSortingButton,
|
2013-09-28 00:11:37 +02:00
|
|
|
createSortButton(rb, TYPE),
|
|
|
|
|
createSortButton(rb, DIFFICULTY),
|
|
|
|
|
createSortButton(rb, TERRAIN),
|
|
|
|
|
createSortButton(rb, OWNER),
|
|
|
|
|
createSortButton(rb, PLACEDBY));
|
|
|
|
|
return sortMenu;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-15 00:35:49 +01:00
|
|
|
private SortingMenuItem createSortButton(final ResourceBundle rb, final CacheSortField field) {
|
|
|
|
|
SortingMenuItem button = new SortingMenuItem(rb.getString("sort.cache."+field.getFieldName()), field);
|
2013-09-28 00:11:37 +02:00
|
|
|
button.setOnAction(actionEvent -> {
|
2014-03-15 00:35:49 +01:00
|
|
|
|
|
|
|
|
// if there was another button selected, change the selection
|
|
|
|
|
if (!currentSortingButton.equals(button)) {
|
|
|
|
|
currentSortingButton.setSelected(false);
|
|
|
|
|
currentSortingButton = button;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentSortingButton.setSelected(true);
|
|
|
|
|
sessionContext.setData("cache-list", cacheService.getAllCaches(currentSortingButton.getField(), currentSortingButton.getSortDirection()));
|
2013-09-28 00:11:37 +02:00
|
|
|
});
|
|
|
|
|
return button;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-03-15 00:35:49 +01:00
|
|
|
|
|
|
|
|
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|