Add notifications and some basic usage
Since I can't rely on platform specific notifications, I'll just have to open a custom window that stays until dismissed.
This commit is contained in:
parent
3aea6f1281
commit
cb3f0b0938
@ -16,6 +16,7 @@ import java.util.Date;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.Optional;
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
@ -25,6 +26,8 @@ import javafx.stage.Stage;
|
||||
import org.apache.commons.cli.*;
|
||||
|
||||
public class Main extends Application {
|
||||
public record StartParameters (Optional<NotificationController.NotificationParameters> notification) {}
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Main.class.getName());
|
||||
// TODO: get git info
|
||||
private static final String VERSION = "0.0.1";
|
||||
@ -44,14 +47,28 @@ public class Main extends Application {
|
||||
return statePath;
|
||||
}
|
||||
|
||||
private static StartParameters startParams = new StartParameters(Optional.empty());
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
try {
|
||||
if (startParams.notification().isPresent()){
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("NotificationView.fxml"));
|
||||
Parent root = fxmlLoader.load();
|
||||
|
||||
NotificationController controller = fxmlLoader.getController();
|
||||
controller.setNotification(startParams.notification().get());
|
||||
|
||||
primaryStage.setTitle("Numbers Station");
|
||||
primaryStage.setScene(new Scene(root));
|
||||
primaryStage.show();
|
||||
} else {
|
||||
Parent root = FXMLLoader.load(getClass().getResource("MainView.fxml"));
|
||||
primaryStage.setTitle("Numbers Station");
|
||||
primaryStage.setScene(new Scene(root));
|
||||
primaryStage.show();
|
||||
logger.info("Application started");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Failed to load main view", e);
|
||||
}
|
||||
@ -98,18 +115,36 @@ public class Main extends Application {
|
||||
setupLogger();
|
||||
|
||||
if (parsedArgs.getStationName() != null) {
|
||||
// TODO: errors in runStation should trigger a notification
|
||||
try {
|
||||
runStation(parsedArgs);
|
||||
} catch (StationRunException e) {
|
||||
// TODO: Add time of run
|
||||
var notification = new NotificationController.NotificationParameters("Error running station '" + parsedArgs.getStationName() + "'",
|
||||
e.getMessage(),
|
||||
NotificationController.NotificationType.ERROR,
|
||||
Optional.empty());
|
||||
startParams = new StartParameters(Optional.of(notification));
|
||||
|
||||
launch(args);
|
||||
System.exit(1);
|
||||
}
|
||||
} else {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
|
||||
public static void runStation(MainParsedArguments parsedArgs) {
|
||||
public static class StationRunException extends Exception {
|
||||
public StationRunException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void runStation(MainParsedArguments parsedArgs) throws StationRunException {
|
||||
if (parsedArgs.getStationName() == null || parsedArgs.getStationName() == "") {
|
||||
logger.log(Level.SEVERE, "Station name must be provided and not empty");
|
||||
// TODO: Notification
|
||||
System.exit(1);
|
||||
String message = "Station name must be provided and not empty";
|
||||
logger.log(Level.SEVERE, message);
|
||||
|
||||
throw new StationRunException(message);
|
||||
}
|
||||
|
||||
Result<MainSettings, Exception> result = MainSettings.load();
|
||||
@ -126,9 +161,10 @@ public class Main extends Application {
|
||||
.orElse(null);
|
||||
|
||||
if (loadedStation == null) {
|
||||
logger.log(Level.SEVERE, "Unable to find station " + parsedArgs.getStationName());
|
||||
// TODO: Notification
|
||||
System.exit(1);
|
||||
String message = "Unable to find station '" + parsedArgs.getStationName() + "'";
|
||||
logger.log(Level.SEVERE, message);
|
||||
|
||||
throw new StationRunException(message);
|
||||
}
|
||||
|
||||
logger.info("Loaded station " + parsedArgs.getStationName());
|
||||
|
@ -0,0 +1,84 @@
|
||||
package name.nathanmcrae.numbersstation;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.Optional;
|
||||
import javafx.event.Event;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.DialogPane;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class NotificationController {
|
||||
public enum NotificationType {
|
||||
INFO,
|
||||
ERROR
|
||||
}
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Main.class.getName());
|
||||
|
||||
private static final String informationString = "🛈";
|
||||
private static final String errorString = "⚠";
|
||||
|
||||
private Runnable openCallback;
|
||||
|
||||
@FXML
|
||||
TextArea descriptionTextArea;
|
||||
|
||||
@FXML
|
||||
Label iconLabel;
|
||||
|
||||
@FXML
|
||||
Button openButton;
|
||||
|
||||
@FXML
|
||||
Label titleLabel;
|
||||
|
||||
@FXML
|
||||
private ImageView iconImageView;
|
||||
|
||||
public void handleOpenButtonPressed() {
|
||||
if (openCallback != null) {
|
||||
openCallback.run();
|
||||
}
|
||||
}
|
||||
|
||||
public void handleDismissButtonPressed(Event event) {
|
||||
Node node = (Node) event.getSource();
|
||||
Stage stage = (Stage) node.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
|
||||
public record NotificationParameters(String title,
|
||||
String description,
|
||||
NotificationType type,
|
||||
Optional<Runnable> openAction) {}
|
||||
|
||||
public void setNotification(NotificationParameters params) throws IllegalArgumentException {
|
||||
titleLabel.setText(params.title());
|
||||
descriptionTextArea.setText(params.description());
|
||||
switch (params.type()) {
|
||||
case INFO:
|
||||
iconLabel.setText(informationString);
|
||||
iconLabel.setTextFill(Color.color(0.2, 0.47, 0.73));
|
||||
break;
|
||||
case ERROR:
|
||||
iconLabel.setText(errorString);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid notification type: " + params.type());
|
||||
}
|
||||
|
||||
if (params.openAction().isPresent()) {
|
||||
openButton.setVisible(true);
|
||||
this.openCallback = params.openAction().get();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
Copyright (c) 2015, 2019, Gluon and/or its affiliates.
|
||||
All rights reserved. Use is subject to license terms.
|
||||
|
||||
This file is available and licensed under the following license:
|
||||
|
||||
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.
|
||||
- Neither the name of Oracle Corporation nor the names of its
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
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
|
||||
OWNER 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.
|
||||
-->
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextArea?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.text.Font?>
|
||||
|
||||
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="250.0" prefWidth="395.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="name.nathanmcrae.numbersstation.NotificationController">
|
||||
<children>
|
||||
<Label fx:id="titleLabel" layoutX="86.0" layoutY="24.0" text="Notification title (this sure is a title)">
|
||||
<font>
|
||||
<Font name="System Bold" size="16.0" />
|
||||
</font>
|
||||
</Label>
|
||||
<Button fx:id="dismissButton" layoutX="325.0" layoutY="139.0" mnemonicParsing="false" onMousePressed="#handleDismissButtonPressed" text="Dismiss" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="14.0" />
|
||||
<Button fx:id="openButton" layoutX="270.0" layoutY="139.0" mnemonicParsing="false" onMousePressed="#handleOpenButtonPressed" text="Open" visible="false" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="79.5" />
|
||||
<Label fx:id="iconLabel" alignment="CENTER" contentDisplay="CENTER" layoutX="14.0" layoutY="5.0" prefHeight="60.0" prefWidth="65.0" text="⚠" textAlignment="CENTER" textFill="RED">
|
||||
<font>
|
||||
<Font size="42.0" />
|
||||
</font>
|
||||
</Label>
|
||||
<TextArea fx:id="descriptionTextArea" editable="false" layoutX="14.0" layoutY="67.0" prefHeight="62.0" prefWidth="367.0" style="-fx-background-color: EEEEEE;" text="Quidem repellendus perferendis quos ipsum. Nihil nobis esse aperiam. Modi optio quia repudiandae quaerat fugit accusamus. Qui sint qui cupiditate rerum quisquam aspernatur illum. Deleniti voluptatum nam alias fugiat.Ab in iste dolores. Ad nostrum quia quisquam deleniti dolorum sequi. Est ut id et enim.Repellat nemo velit modi sunt provident neque neque. Est consequatur est beatae voluptates tempore commodi voluptas. Dicta asperiores deserunt veritatis quia officia. Reiciendis et qui placeat est minus." wrapText="true" AnchorPane.bottomAnchor="49.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="67.0" />
|
||||
</children>
|
||||
</AnchorPane>
|
Loading…
x
Reference in New Issue
Block a user