Menu list button with sort submenu

This commit is contained in:
frosch95
2013-09-28 00:11:37 +02:00
parent da0060a989
commit c73f6476a2
11 changed files with 145 additions and 26 deletions

View File

@@ -30,8 +30,11 @@ import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.scenicview.ScenicView;
import java.util.ResourceBundle;
@@ -48,6 +51,12 @@ public class GeoFroggerFXMain extends Application {
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
scene.setOnKeyPressed(keyEvent -> {
if (isScenicViewShortcutPressed(keyEvent)) {
ScenicView.show(scene);
}
});
}
private void loadCustomFonts() {
@@ -63,6 +72,10 @@ public class GeoFroggerFXMain extends Application {
Font.loadFont(GeoFroggerFXMain.class.getResource("/fonts/FiraSansOT-RegularItalic.otf").toExternalForm(), 12);
}
private boolean isScenicViewShortcutPressed(final KeyEvent keyEvent) {
return keyEvent.isAltDown() && keyEvent.isControlDown() && keyEvent.getCode().equals(KeyCode.V);
}
@Override
public void stop() throws Exception {
System.out.println("stop");

View File

@@ -25,17 +25,22 @@
*/
package de.frosch95.geofrogger.fx.cachelist;
import de.frosch95.geofrogger.application.ServiceManager;
import de.frosch95.geofrogger.application.SessionContext;
import de.frosch95.geofrogger.application.SessionContextListener;
import de.frosch95.geofrogger.fx.components.CacheListCell;
import de.frosch95.geofrogger.fx.components.IconManager;
import de.frosch95.geofrogger.model.Cache;
import de.frosch95.geofrogger.service.CacheService;
import de.frosch95.geofrogger.service.CacheSortField;
import de.frosch95.geofrogger.service.SortDirection;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.util.Callback;
import java.net.URL;
@@ -43,6 +48,8 @@ import java.util.List;
import java.util.ResourceBundle;
import static de.frosch95.geofrogger.fx.utils.JavaFXUtils.addClasses;
import static de.frosch95.geofrogger.service.CacheSortField.*;
import static de.frosch95.geofrogger.service.SortDirection.*;
/**
* FXML Controller class
@@ -53,6 +60,9 @@ public class CacheListController implements Initializable, SessionContextListene
private static final String CACHE_LIST_ACTION_ICONS = "cache-list-action-icons";
private final SessionContext sessionContext = SessionContext.getInstance();
private final CacheService cacheService = ServiceManager.getInstance().getCacheService();
private CacheSortField currentSortField = NAME;
private SortDirection currentSortDirection = ASC;
@FXML
private ListView cacheListView;
@@ -61,10 +71,7 @@ public class CacheListController implements Initializable, SessionContextListene
private Label cacheNumber;
@FXML
private Label filterIcon;
@FXML
private Label sortIcon;
private MenuButton menuIcon;
/**
* Initializes the controller class.
@@ -73,6 +80,7 @@ public class CacheListController implements Initializable, SessionContextListene
* @param rb
*/
@Override
@SuppressWarnings("unchecked")
public void initialize(URL url, ResourceBundle rb) {
setSessionListener();
setCellFactory();
@@ -82,11 +90,11 @@ public class CacheListController implements Initializable, SessionContextListene
sessionContext.setData("current-cache", newValue)
);
addClasses(filterIcon, CACHE_LIST_ACTION_ICONS);
addClasses(sortIcon, CACHE_LIST_ACTION_ICONS);
initListMenuButton(rb);
}
@Override
@SuppressWarnings("unchecked")
public void sessionContextChanged() {
List<Cache> caches = (List<Cache>) sessionContext.getData("cache-list");
Platform.runLater(() -> {
@@ -105,4 +113,33 @@ public class CacheListController implements Initializable, SessionContextListene
sessionContext.addListener("cache-list", this);
}
private void initListMenuButton(final ResourceBundle rb) {
addClasses(menuIcon, CACHE_LIST_ACTION_ICONS);
menuIcon.setGraphic(new ImageView(IconManager.getIcon("/icons/iconmonstr-menu-icon.png", IconManager.IconSize.SMALL)));
menuIcon.getItems().addAll(createSortMenu(rb));
}
private Menu createSortMenu(final ResourceBundle rb) {
Menu sortMenu = new Menu(rb.getString("menu.title.sort"));
sortMenu.getItems().addAll(
createSortButton(rb, NAME),
createSortButton(rb, TYPE),
createSortButton(rb, DIFFICULTY),
createSortButton(rb, TERRAIN),
createSortButton(rb, OWNER),
createSortButton(rb, PLACEDBY));
return sortMenu;
}
private MenuItem createSortButton(final ResourceBundle rb, final CacheSortField field) {
MenuItem button = new MenuItem(rb.getString("sort.cache."+field.getFieldName()));
button.setOnAction(actionEvent -> {
currentSortDirection = (field.equals(currentSortField) && currentSortDirection.equals(ASC)) ? DESC : ASC;
currentSortField = field;
sessionContext.setData("cache-list", cacheService.getAllCaches(currentSortField, currentSortDirection));
});
return button;
}
}

View File

@@ -12,18 +12,11 @@
<children>
<Label alignment="CENTER_LEFT" minWidth="60.0" prefWidth="-1.0" style="&#10;" text="%label.text.cache.list"
textAlignment="LEFT" textFill="WHITE" wrapText="false">
<font>
<Font size="16.0" fx:id="x1"/>
</font>
</Label>
<Label fx:id="cacheNumber" alignment="CENTER_LEFT" minWidth="60.0" prefWidth="-1.0" maxWidth="Infinity"
style="&#10;" text="(0)" textAlignment="LEFT" textFill="WHITE" wrapText="false" HBox.hgrow="ALWAYS">
<font>
<Font size="16.0" fx:id="x1"/>
</font>
</Label>
<Label fx:id="sortIcon"/>
<Label fx:id="filterIcon"/>
<MenuButton fx:id="menuIcon"/>
</children>
<padding>
<Insets left="16.0" right="16.0" fx:id="x2"/>

View File

@@ -58,4 +58,12 @@
.slider:disabled {
-fx-opacity: 0.8;
}
.menu-button.cache-list-action-icons {
-fx-padding: 0 0 0 0;
}
.menu-button.cache-list-action-icons .label{
-fx-padding: 2 2 2 2;
}

View File

@@ -31,6 +31,8 @@ import de.frosch95.geofrogger.application.SessionContext;
import de.frosch95.geofrogger.gpx.GPXReader;
import de.frosch95.geofrogger.model.Cache;
import de.frosch95.geofrogger.service.CacheService;
import de.frosch95.geofrogger.service.CacheSortField;
import de.frosch95.geofrogger.service.SortDirection;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
@@ -54,6 +56,9 @@ import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import static de.frosch95.geofrogger.service.CacheSortField.*;
import static de.frosch95.geofrogger.service.SortDirection.*;
/**
* FXML Controller class
*
@@ -170,7 +175,7 @@ public class GeofroggerController implements Initializable {
@Override
protected Void call() throws Exception {
updateStatus("Load caches from database.", ProgressIndicator.INDETERMINATE_PROGRESS);
sessionContext.setData("cache-list", cacheService.getAllCaches());
sessionContext.setData("cache-list", cacheService.getAllCaches(NAME, ASC));
updateStatus("All caches loaded.", 0);
return null;
}
@@ -209,7 +214,7 @@ public class GeofroggerController implements Initializable {
updateStatus("All caches are stored in database", 0);
updateStatus("Load caches from database.", ProgressIndicator.INDETERMINATE_PROGRESS);
sessionContext.setData("cache-list", cacheService.getAllCaches());
sessionContext.setData("cache-list", cacheService.getAllCaches(NAME, ASC));
updateStatus("All caches loaded.", 0);
}
} catch (IOException ex) {

View File

@@ -4,6 +4,9 @@ menu.title.quit = Beenden
menu.title.help = Hilfe
menu.title.about = \u00dcber GeoFroggerFX
menu.title.sort = Sortieren
menu.title.filter = Filtern
label.text.cache.list=Caches
label.text.name=Name:
label.text.difficulty=Schwierigkeit:
@@ -18,4 +21,11 @@ label.text.htmldescription=HTML Beschreibung
label.text.longdescription=Lange Beschreibung:
tab.text.descriptions=Beschreibungen
tab.text.general=Allgemein
tab.text.general=Allgemein
sort.cache.name = GC Code
sort.cache.type = Art
sort.cache.difficulty = Schwierigkeitsgrad
sort.cache.terrain = Gel\u00E4nde
sort.cache.placedBy = Platziert von
sort.cache.owner = Betreut von

View File

@@ -4,6 +4,9 @@ menu.title.quit = Quit
menu.title.help = Help
menu.title.about = About GeoFroggerFX
menu.title.sort = Sort
menu.title.filter = Filter
label.text.cache.list=Caches
label.text.name=Name:
label.text.difficulty=Difficulty:
@@ -18,4 +21,11 @@ label.text.htmldescription=HTML Description
label.text.longdescription=Long description:
tab.text.descriptions=Descriptions
tab.text.general=General
tab.text.general=General
sort.cache.name = GC Code
sort.cache.type = Type
sort.cache.difficulty = Difficulty
sort.cache.terrain = Terrain
sort.cache.placedBy = Placed by
sort.cache.owner = Owner

View File

@@ -36,7 +36,7 @@ import java.util.List;
public interface CacheService {
void storeCaches(List<Cache> caches);
List<Cache> getAllCaches();
List<Cache> getAllCaches(CacheSortField sortField, SortDirection direction);
void addListener(ProgressListener listener);
}

View File

@@ -74,13 +74,20 @@ public class CacheServiceImpl implements CacheService {
}
@Override
public List<Cache> getAllCaches() {
@SuppressWarnings("unchecked")
public List<Cache> getAllCaches(CacheSortField sortField, SortDirection direction) {
EntityManager em = dbService.getEntityManager();
List<Cache> caches = new ArrayList<>();
List<Cache> result = em.createQuery("select c from Cache c").getResultList();
if (result != null) {
caches = result;
try {
EntityManager em = dbService.getEntityManager();
String query = "select c from Cache c order by c."+sortField.getFieldName()+" "+direction.toString();
System.out.println("query: "+query);
List<Cache> result = em.createQuery(query).getResultList();
if (result != null) {
caches = result;
}
} catch (Exception e) {
e.printStackTrace();
}
return caches;

View File

@@ -0,0 +1,25 @@
package de.frosch95.geofrogger.service;
/**
* Sort fields on cache object
*
* @author abi
*/
public enum CacheSortField {
NAME("name"),
TYPE("type"),
DIFFICULTY("difficulty"),
TERRAIN("terrain"),
PLACEDBY("placedBy"),
OWNER("owner");
private String fieldName;
private CacheSortField(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldName() {
return fieldName;
}
}

View File

@@ -0,0 +1,11 @@
package de.frosch95.geofrogger.service;
/**
* direction of sorting
*
* @author abi
*/
public enum SortDirection {
ASC,
DESC
}