Added javafx stuff.

This commit is contained in:
Andrew Lalis 2021-05-28 23:46:30 +02:00
parent 7bd89c0e50
commit 2e78b95588
10 changed files with 152 additions and 8 deletions

View File

@ -4,8 +4,11 @@ import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import nl.andrewlalis.crystalkeep.control.MainViewController;
import nl.andrewlalis.crystalkeep.model.Cluster;
import nl.andrewlalis.crystalkeep.model.Model;
import nl.andrewlalis.crystalkeep.model.Shard;
import nl.andrewlalis.crystalkeep.model.serialization.ClusterLoader;
import nl.andrewlalis.crystalkeep.model.serialization.ClusterSerializer;
import nl.andrewlalis.crystalkeep.model.shards.LoginCredentialsShard;
import nl.andrewlalis.crystalkeep.model.shards.TextShard;
@ -17,7 +20,7 @@ import java.time.LocalDateTime;
import java.util.Arrays;
public class CrystalKeep extends Application {
public static void main(String[] args) throws IOException {
public static void main(String[] args) {
launch(args);
}
@ -25,10 +28,28 @@ public class CrystalKeep extends Application {
public void start(Stage stage) throws Exception {
URL url = CrystalKeep.class.getClassLoader().getResource("ui/crystalkeep.fxml");
FXMLLoader loader = new FXMLLoader(url);
Model model = new Model();
loader.setController(new MainViewController(model));
var scene = new Scene(loader.load());
stage.setScene(scene);
stage.setTitle("CrystalKeep");
stage.sizeToScene();
ClusterLoader clusterLoader = new ClusterLoader();
Cluster rootCluster;
try {
rootCluster = clusterLoader.loadDefault();
System.out.println("Loaded existing root cluster.");
} catch (IOException e) {
rootCluster = new Cluster("Root");
rootCluster.addShard(new TextShard(rootCluster, "Example Shard", LocalDateTime.now(), "Hello world!"));
clusterLoader.saveDefault(rootCluster);
System.out.println("Saved root cluster on first load.");
}
model.setActiveCluster(rootCluster);
System.out.println(rootCluster);
stage.show();
}

View File

@ -0,0 +1,9 @@
package nl.andrewlalis.crystalkeep.control;
import javafx.fxml.FXML;
import javafx.scene.control.TreeView;
public class ClusterTreeViewController {
@FXML
TreeView<String> clusterTreeView;
}

View File

@ -0,0 +1,26 @@
package nl.andrewlalis.crystalkeep.control;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import nl.andrewlalis.crystalkeep.model.Model;
import nl.andrewlalis.crystalkeep.model.serialization.ClusterLoader;
import java.io.IOException;
public class MainViewController {
private final Model model;
public MainViewController(Model model) {
this.model = model;
}
@FXML
public void exit(ActionEvent event) {
System.out.println("Exiting...");
try {
new ClusterLoader().saveDefault(model.getActiveCluster());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,4 @@
package nl.andrewlalis.crystalkeep.control;
public class ShardDetailController {
}

View File

@ -0,0 +1,12 @@
package nl.andrewlalis.crystalkeep.model;
import lombok.Getter;
@Getter
public class Model {
private Cluster activeCluster;
public void setActiveCluster(Cluster activeCluster) {
this.activeCluster = activeCluster;
}
}

View File

@ -0,0 +1,25 @@
package nl.andrewlalis.crystalkeep.model.serialization;
import nl.andrewlalis.crystalkeep.model.Cluster;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
public class ClusterLoader {
private static final Path CLUSTER_PATH = Path.of("clusters");
private static final Path DEFAULT_CLUSTER = CLUSTER_PATH.resolve("default.cts");
public Cluster loadDefault() throws IOException {
InputStream is = new FileInputStream(DEFAULT_CLUSTER.toFile());
return ClusterSerializer.clusterFromBytes(is, null);
}
public void saveDefault(Cluster cluster) throws IOException {
Files.createDirectories(CLUSTER_PATH);
byte[] bytes = ClusterSerializer.toBytes(cluster);
Files.write(DEFAULT_CLUSTER, bytes);
}
}

View File

@ -19,6 +19,10 @@ import java.util.Map;
import static nl.andrewlalis.crystalkeep.model.serialization.ByteUtils.toInt;
/**
* This serializer class offers some methods for reading and writing clusters
* from and to byte arrays.
*/
public class ClusterSerializer {
private static final Map<ShardType, ShardSerializer<?>> serializers = new HashMap<>();
static {
@ -29,6 +33,7 @@ public class ClusterSerializer {
/**
* Serializes a cluster to a byte array, including all shards and nested
* clusters.
* TODO: Use output stream instead of byte array.
* @param cluster The cluster to serialize.
* @return The byte array representing the cluster.
* @throws IOException If an error occurs while writing the cluster.

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="nl.andrewlalis.crystalkeep.control.ClusterTreeViewController"
prefWidth="200.0"
>
<top>
<Label text="Clusters" BorderPane.alignment="TOP_CENTER"/>
</top>
<center>
<TreeView fx:id="clusterTreeView" prefWidth="200.0"/>
</center>
</BorderPane>

View File

@ -1,10 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.andrewlalis.crystalkeep.control.MainViewController">
<MenuBar>
<Menu text="File">
<MenuItem text="Exit" onAction="#exit" />
</Menu>
</MenuBar>
<BorderPane>
<left>
<fx:include source="clusters_view.fxml" />
</left>
<center>
<fx:include source="shard_detail.fxml" />
</center>
</BorderPane>
</VBox>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<left>
<TreeView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</left>
</BorderPane>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="nl.andrewlalis.crystalkeep.control.ShardDetailController"
prefHeight="400.0" prefWidth="600.0">
</AnchorPane>