Skip to content
Snippets Groups Projects
Commit 7ef3d014 authored by jez04's avatar jez04
Browse files

feat: assignment lab 10

parent b0e832b1
No related merge requests found
...@@ -4,4 +4,5 @@ ...@@ -4,4 +4,5 @@
.project .project
.classpath .classpath
*.mv.db *.mv.db
*.trace.db *.trace.db
\ No newline at end of file .idea/
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>cz.vsb.fei.java2</groupId> <groupId>cz.vsb.fei.java2</groupId>
<artifactId>java2-lab09-v3</artifactId> <artifactId>java2-lab10-v3</artifactId>
<version>0.0.1-SNAPHOST</version> <version>0.0.1-SNAPHOST</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>java2-lab09-v3</name> <name>java2-lab10-v3</name>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source> <maven.compiler.source>21</maven.compiler.source>
......
StackSultan;66;MEDIUM
SnapMaster;167;MEDIUM
TikTokTornado;210;EASY
ScriptSensei;158;EASY
SnapMasteraaa;167000;null
ScriptSenseidddd;1580000;null
...@@ -2,4 +2,13 @@ package lab.data; ...@@ -2,4 +2,13 @@ package lab.data;
public enum Level { public enum Level {
EASY, MEDIUM, HARD; EASY, MEDIUM, HARD;
public static Level from(String s) {
for(Level l :values()) {
if(l.toString().equals(s)) {
return l;
}
}
return null;
}
} }
...@@ -13,9 +13,12 @@ import javafx.scene.Scene; ...@@ -13,9 +13,12 @@ import javafx.scene.Scene;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType; import javafx.scene.control.ButtonType;
import javafx.stage.Modality;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent; import javafx.stage.WindowEvent;
import lab.Setup; import lab.Setup;
import lab.data.Score;
import lab.storage.FileStorage; import lab.storage.FileStorage;
/** /**
...@@ -28,7 +31,7 @@ public class App extends Application { ...@@ -28,7 +31,7 @@ public class App extends Application {
private static Logger log = LogManager.getLogger(App.class); private static Logger log = LogManager.getLogger(App.class);
private GameController gameController; private GameController gameController;
private MainScreenController menuController;
private boolean viewMode; private boolean viewMode;
private Stage primaryStage; private Stage primaryStage;
...@@ -74,13 +77,32 @@ public class App extends Application { ...@@ -74,13 +77,32 @@ public class App extends Application {
// Construct a main window with a canvas. // Construct a main window with a canvas.
FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("/lab/gui/mainScreen.fxml")); FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("/lab/gui/mainScreen.fxml"));
Parent root = menuLoader.load(); Parent root = menuLoader.load();
MainScreenController menuController = menuLoader.getController(); menuController = menuLoader.getController();
menuController.setApp(this); menuController.setApp(this);
Scene scene = new Scene(root); Scene scene = new Scene(root);
URL cssUrl = getClass().getResource("application.css"); URL cssUrl = getClass().getResource("application.css");
scene.getStylesheets().add(cssUrl.toString()); scene.getStylesheets().add(cssUrl.toString());
primaryStage.setScene(scene); primaryStage.setScene(scene);
} }
public Stage createDialogStage(Score score) throws IOException {
Stage dialog = new Stage();
dialog.initStyle(StageStyle.UTILITY);
FXMLLoader editLoader = new FXMLLoader(getClass().getResource("/lab/gui/edit.fxml"));
Parent root = editLoader.load();
EditController editController = editLoader.getController();
editController.setStage(dialog);
editController.setObjectToEdit(score);
editController.setMainScreenController(menuController);
Scene scene = new Scene(root);
URL cssUrl = getClass().getResource("application.css");
scene.getStylesheets().add(cssUrl.toString());
dialog.setScene(scene);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(primaryStage);
return dialog;
}
@Override @Override
public void stop() throws Exception { public void stop() throws Exception {
......
package lab.gui;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.HPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lab.Setup;
import lab.data.Score;
import lab.game.Difficult;
import lombok.Setter;
/**
*
*/
public class EditController {
private static Logger log = LogManager.getLogger(EditController.class);
@FXML
private Button btnOk;
@FXML
private Button btnCancel;
@FXML
private Label txtTitle;
@FXML
private GridPane content;
private Object data;
private App app;
private Map<String, TextField> nameToTextField = new HashMap<>();
@Setter
private Stage stage;
@Setter
private MainScreenController mainScreenController;
@FXML
void btnOkAction(ActionEvent event) {
//TODO: add code to read values from text fields
mainScreenController.updateData((Score)data);
stage.hide();
}
@FXML
void btnCancelAction(ActionEvent event) {
stage.hide();
}
@FXML
void initialize() {
log.info("Screen initialized.");
}
public void setObjectToEdit(Object data) {
this.data = data;
log.info("data set {}", data);
content.getChildren().clear();
//TODO: code to detect properties and add rows to dialog
}
private void addDialogRow(int rowNumber, String name, String descriptionName, String stringValue,
boolean editable) {
Label label = new Label(descriptionName);
TextField textField = new TextField(stringValue);
textField.setEditable(editable);
nameToTextField.put(name, textField);
content.addRow(rowNumber, label, textField);
GridPane.setHalignment(label, HPos.RIGHT);
}
}
...@@ -10,14 +10,23 @@ import org.apache.logging.log4j.Logger; ...@@ -10,14 +10,23 @@ import org.apache.logging.log4j.Logger;
import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.SelectionMode; import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView; import javafx.scene.control.TableView;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup; import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lab.Setup; import lab.Setup;
import lab.data.Score; import lab.data.Score;
import lab.game.Difficult; import lab.game.Difficult;
...@@ -41,7 +50,6 @@ public class MainScreenController { ...@@ -41,7 +50,6 @@ public class MainScreenController {
@FXML @FXML
private Button btnDelete; private Button btnDelete;
@FXML @FXML
private TableView<Score> scores; private TableView<Score> scores;
...@@ -97,7 +105,7 @@ public class MainScreenController { ...@@ -97,7 +105,7 @@ public class MainScreenController {
void btnLoadFirstTenAction(ActionEvent event) { void btnLoadFirstTenAction(ActionEvent event) {
updateScoreTable(Setup.getInstance().getScoreStorageInterface().getFirstTen()); updateScoreTable(Setup.getInstance().getScoreStorageInterface().getFirstTen());
} }
@FXML @FXML
void btnDeleteAction(ActionEvent event) { void btnDeleteAction(ActionEvent event) {
List<Score> selectedScores = new ArrayList<>(scores.getSelectionModel().getSelectedItems()); List<Score> selectedScores = new ArrayList<>(scores.getSelectionModel().getSelectedItems());
...@@ -110,6 +118,11 @@ public class MainScreenController { ...@@ -110,6 +118,11 @@ public class MainScreenController {
this.scores.getItems().addAll(scores); this.scores.getItems().addAll(scores);
} }
public void updateData(Score score) {
Setup.getInstance().getScoreStorageInterface().save(score);
updateScoreTable(Setup.getInstance().getScoreStorageInterface().getAll());
}
@FXML @FXML
void initialize() { void initialize() {
assert difficult != null : "fx:id=\"difficult\" was not injected: check your FXML file 'mainScreen.fxml'."; assert difficult != null : "fx:id=\"difficult\" was not injected: check your FXML file 'mainScreen.fxml'.";
...@@ -123,10 +136,23 @@ public class MainScreenController { ...@@ -123,10 +136,23 @@ public class MainScreenController {
difficultColumn.setCellValueFactory(new PropertyValueFactory<>("level")); difficultColumn.setCellValueFactory(new PropertyValueFactory<>("level"));
btnDelete.setDisable(true); btnDelete.setDisable(true);
scores.setRowFactory(tableView -> {
TableRow<Score> row = new TableRow<>();
row.setOnMouseClicked(event -> {
if (event.getClickCount() >= 2 && event.getButton() == MouseButton.PRIMARY && row.getItem() != null) {
try {
MainScreenController.this.app.createDialogStage(row.getItem()).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
return row;
});
scores.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); scores.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
scores.getSelectionModel().getSelectedItems() scores.getSelectionModel().getSelectedItems()
.addListener((ListChangeListener.Change<? extends Score> change) -> .addListener((ListChangeListener.Change<? extends Score> change) -> btnDelete
btnDelete.setDisable(change.getList().isEmpty())); .setDisable(change.getList().isEmpty()));
initStorage(); initStorage();
log.info("Screeen initialized."); log.info("Screeen initialized.");
......
...@@ -10,6 +10,7 @@ import java.util.Comparator; ...@@ -10,6 +10,7 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import lab.data.Level;
import lab.data.Score; import lab.data.Score;
public class FileStorage implements ScoreStorageInterface { public class FileStorage implements ScoreStorageInterface {
...@@ -21,7 +22,7 @@ public class FileStorage implements ScoreStorageInterface { ...@@ -21,7 +22,7 @@ public class FileStorage implements ScoreStorageInterface {
if (Files.exists(Paths.get(SCORE_FILE_NAME))) { if (Files.exists(Paths.get(SCORE_FILE_NAME))) {
try (Stream<String> lines = Files.lines(Paths.get(SCORE_FILE_NAME))) { try (Stream<String> lines = Files.lines(Paths.get(SCORE_FILE_NAME))) {
List<Score> result = lines.map(line -> line.split(";")) List<Score> result = lines.map(line -> line.split(";"))
.map(parts -> new Score(null, parts[0], Integer.parseInt(parts[1]), null)).toList(); .map(parts -> new Score(null, parts[0], Integer.parseInt(parts[1]), Level.from(parts[2]))).toList();
return new ArrayList<>(result); return new ArrayList<>(result);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -34,7 +35,7 @@ public class FileStorage implements ScoreStorageInterface { ...@@ -34,7 +35,7 @@ public class FileStorage implements ScoreStorageInterface {
public List<Score> getFirstTen() { public List<Score> getFirstTen() {
List<Score> all = getAll(); List<Score> all = getAll();
Collections.sort(all, Comparator.comparing(Score::getPoints).reversed()); Collections.sort(all, Comparator.comparing(Score::getPoints).reversed());
return all.subList(0, 10); return all.subList(0, Math.min(all.size(), 10));
} }
@Override @Override
...@@ -51,9 +52,9 @@ public class FileStorage implements ScoreStorageInterface { ...@@ -51,9 +52,9 @@ public class FileStorage implements ScoreStorageInterface {
} }
private void storeAll(List<Score> all) { private void storeAll(List<Score> all) {
List<String> lines = all.stream().map(s -> String.format("%s;%d", s.getName(), s.getPoints())).toList(); List<String> lines = all.stream().map(s -> String.format("%s;%d;%s", s.getName(), s.getPoints(), s.getLevel())).toList();
try { try {
Files.write(Paths.get(SCORE_FILE_NAME), lines, StandardOpenOption.TRUNCATE_EXISTING); Files.write(Paths.get(SCORE_FILE_NAME), lines, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -8,6 +8,7 @@ module lab05_module { ...@@ -8,6 +8,7 @@ module lab05_module {
requires com.h2database; requires com.h2database;
requires jakarta.persistence; requires jakarta.persistence;
requires org.hibernate.orm.core; requires org.hibernate.orm.core;
requires java.desktop;
opens lab.gui to javafx.fxml; opens lab.gui to javafx.fxml;
opens lab.data to javafx.base,org.hibernate.orm.core; opens lab.data to javafx.base,org.hibernate.orm.core;
......
...@@ -26,6 +26,22 @@ Label { ...@@ -26,6 +26,22 @@ Label {
-fx-background-size: stretch; -fx-background-size: stretch;
} }
#content {
-fx-grid-lines-visible: true;
}
#btnOk {
-fx-background-color: RGBA(0.0,255.0,0.0,0.5);
}
#btnCancel {
-fx-background-color: RGBA(150.0,150.0,0.0,0.5);
}
.actionButton:hover {
-fx-text-fill: white;
}
#playButton { #playButton {
-fx-background-color: RGBA(0.0,0.0,255.0,0.5); -fx-background-color: RGBA(0.0,0.0,255.0,0.5);
} }
......
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<BorderPane fx:id="menuPanel" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lab.gui.EditController">
<bottom>
<HBox BorderPane.alignment="CENTER">
<children>
<Button fx:id="btnCancel" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#btnCancelAction" styleClass="actionButton" text="Cancle" HBox.hgrow="ALWAYS">
<font>
<Font name="System Bold" size="51.0" />
</font>
</Button>
<Button fx:id="btnOk" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#btnOkAction" styleClass="actionButton" text="Ok" HBox.hgrow="ALWAYS">
<font>
<Font name="System Bold" size="51.0" />
</font>
</Button>
</children>
</HBox>
</bottom>
<center>
<GridPane fx:id="content" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="NEVER" />
</rowConstraints>
</GridPane>
</center>
<top>
<Label fx:id="txtTitle" text="Title" BorderPane.alignment="CENTER">
<font>
<Font name="System Bold" size="48.0" />
</font>
</Label>
</top>
</BorderPane>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment