Commit de0db904 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

De første to punktene av oppgave #4

parent 68d84b1d
{
"java.configuration.updateBuildConfiguration": "automatic"
}
\ No newline at end of file
"java.configuration.updateBuildConfiguration": "automatic",
"java.configuration.runtimes": [
{
"name" : "JavaSE-13",
"path" : "/home/gitpod/.sdkman/candidates/java/14.0.2.j9-adpt",
"default": true
}
]
}
......@@ -29,6 +29,13 @@
<version>2.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>14.0.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
......@@ -51,13 +58,30 @@
<configuration>
<encoding>UTF-8</encoding>
<release>14</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<options>
<option>--enable-preview</option>
</options>
<mainClass>todolist.ui.TodoApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
</project>
\ No newline at end of file
package todolist.ui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TodoApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("Todo.fxml"));
stage.setScene(new Scene(parent));
stage.show();
}
public static void main(String[] args) {
launch(TodoApp.class, args);
}
}
\ No newline at end of file
package todolist.ui;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import todolist.core.TodoItem;
import todolist.core.TodoList;
import todolist.json.TodoModule;
public class TodoController {
private final static String todoListWithTwoItems =
"{\"items\":[{\"text\":\"item2\",\"checked\":true},{\"text\":\"item1\",\"checked\":false},{\"text\":\"item3\",\"checked\":true}]}";
private TodoList todoList;
private ObjectMapper mapper = new ObjectMapper();
public TodoController() {
// setter opp data
mapper.registerModule(new TodoModule());
try {
todoList = mapper.readValue(todoListWithTwoItems, TodoList.class);
} catch (JsonProcessingException e) {
}
}
@FXML
TextField newTodoItemText;
@FXML
ListView<TodoItem> todoListView;
@FXML
public void initialize() {
// kobler data til list-controll
updateTodoListView();
}
protected void updateTodoListView() {
List<TodoItem> viewList = todoListView.getItems();
viewList.clear();
viewList.addAll(todoList.getUncheckedTodoItems());
viewList.addAll(todoList.getCheckedTodoItems());
}
@FXML
public void handleNewTodoItemAction() {
TodoItem item = new TodoItem();
item.setText(newTodoItemText.getText());
todoList.addTodoItem(item);
updateTodoListView();
}
@FXML
public void handleDeleteItemAction() {
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
if (item != null) {
todoList.removeTodoItem(item);
updateTodoListView();
}
}
@FXML
public void handleCheckItemAction() {
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
item.setChecked(true);
updateTodoListView();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ListView?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="todolist.ui.TodoController">
<HBox>
<Button fx:id="newTodoItemButton" text="New Item" onAction="#handleNewTodoItemAction"/>
<TextField fx:id="newTodoItemText" promptText="Skriv inn tekst her" onAction="#handleNewTodoItemAction"/>
</HBox>
<ListView fx:id="todoListView">
</ListView>
<HBox>
<Button fx:id="deleteTodoItemButton" text="Delete Item" onAction="#handleDeleteItemAction"/>
<Button fx:id="checkTodoItemButton" text="Check Item" onAction="#handleCheckItemAction"/>
</HBox>
</VBox>
......@@ -30,7 +30,20 @@ public class TodoModuleTest {
mapper.registerModule(new TodoModule());
}
private final static String todoListWithTwoItems = "{\"items\":[{\"text\":\"item1\",\"checked\":false},{\"text\":\"item2\",\"checked\":true}]}";
private final static String todoListWithTwoItems = """
{
"items": [
{
"text": "item1",
"checked" : false
},
{
"text": "item2",
"checked" : true
}
]
}
""";
@Test
public void testSerializers() {
......@@ -43,7 +56,7 @@ public class TodoModuleTest {
list.addTodoItem(item1);
list.addTodoItem(item2);
try {
assertEquals(todoListWithTwoItems, mapper.writeValueAsString(list));
assertEquals(todoListWithTwoItems.replaceAll("\\s+", ""), mapper.writeValueAsString(list));
} catch (JsonProcessingException e) {
fail();
}
......
Supports Markdown
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