Skip to content

Commit

Permalink
Add notifications when Operation finishes
Browse files Browse the repository at this point in the history
  • Loading branch information
patschuh committed Apr 10, 2019
1 parent 9adbf0e commit 461561c
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/main/java/at/esque/kafka/Controller.java
Expand Up @@ -48,7 +48,6 @@
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TableColumn;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/at/esque/kafka/CrossClusterController.java
Expand Up @@ -11,6 +11,7 @@
import at.esque.kafka.handlers.ConsumerHandler;
import at.esque.kafka.handlers.CrossClusterOperationHandler;
import at.esque.kafka.handlers.ProducerHandler;
import at.esque.kafka.handlers.TrayHandler;
import com.google.inject.Inject;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
Expand Down Expand Up @@ -74,6 +75,8 @@ public class CrossClusterController {
private ProducerHandler producerHandler;
@Inject
private ConsumerHandler consumerHandler;
@Inject
private TrayHandler trayHandler;

public void setup() {
ClusterConfigs clusterConfigs = configHandler.loadOrCreateConfigs();
Expand All @@ -96,6 +99,10 @@ public void setup() {
} else {
cell.textProperty().set(MessageFormat.format("{0} / {1} ---> {2} / {3}", newValue.getFromCluster().getIdentifier(), newValue.getFromTopic().getName(), newValue.getToCluster().getIdentifier(), newValue.getToTopic().getName()));
cell.getItem().statusProperty().addListener((observable1, oldValue1, newValue1) -> {
if (newValue1 != null && newValue1.equals("Finished")) {
trayHandler.showInfoNotification("Operation Finished!",
MessageFormat.format("{0} / {1} ---> {2} / {3}", newValue.getFromCluster().getIdentifier(), newValue.getFromTopic().getName(), newValue.getToCluster().getIdentifier(), newValue.getToTopic().getName()));
}
});
cell.graphicProperty().bind(Bindings.createObjectBinding(() -> {
if (cell.getItem() != null) {
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/at/esque/kafka/Main.java
@@ -1,21 +1,34 @@
package at.esque.kafka;

import at.esque.kafka.guice.GuiceEsqueModule;
import at.esque.kafka.handlers.TrayHandler;
import com.google.inject.Guice;
import com.google.inject.Injector;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.util.Arrays;
import java.util.Optional;

public class Main extends Application {

private TrayHandler trayHandler;

@Override
public void start(Stage primaryStage) throws Exception {
Injector injector = Guice.createInjector(new GuiceEsqueModule());
trayHandler = injector.getInstance(TrayHandler.class);
trayHandler.setPrimaryStage(primaryStage);
FXMLLoader loader = injector.getInstance(FXMLLoader.class);
loader.setLocation(getClass().getResource("/fxml/mainScene.fxml"));
Parent root = loader.load();
Expand All @@ -24,9 +37,28 @@ public void start(Stage primaryStage) throws Exception {
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/kafkaesque.png")));
primaryStage.setTitle("Kafkaesque");
primaryStage.setScene(createStyledScene(root, 1600, 900));
primaryStage.getScene().getWindow().addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, this::primaryStageClose);
primaryStage.show();
}

private <T extends Event> void primaryStageClose(T event) {
if (trayHandler.isTraySupported()) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation");
alert.setHeaderText("Should KafkaEsque stay open in the Tray?");

alert.getButtonTypes().setAll(Arrays.asList(ButtonType.YES, ButtonType.NO));

Optional<ButtonType> result = alert.showAndWait();

if (result.orElse(ButtonType.NO) == ButtonType.YES) {
trayHandler.getPrimaryStage().hide();
} else {
Platform.exit();
}
}
}

public static Scene createStyledScene(Parent parent, double width, double height) {
Scene scene = new Scene(parent, width, height);
applyStylesheet(scene);
Expand All @@ -45,4 +77,9 @@ public static void applyIcon(Dialog dialog) {
public static void main(String[] args) {
launch(args);
}

@Override
public void stop() {
trayHandler.shutdown();
}
}
2 changes: 2 additions & 0 deletions src/main/java/at/esque/kafka/handlers/ProducerHandler.java
Expand Up @@ -6,6 +6,7 @@
import at.esque.kafka.serialization.ExtendedJsonDecoder;
import at.esque.kafka.serialization.KafkaEsqueSerializer;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.confluent.kafka.schemaregistry.client.rest.RestService;
import io.confluent.kafka.schemaregistry.client.rest.entities.Schema;
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException;
Expand Down Expand Up @@ -33,6 +34,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Singleton
public class ProducerHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(ProducerHandler.class);

Expand Down
95 changes: 95 additions & 0 deletions src/main/java/at/esque/kafka/handlers/TrayHandler.java
@@ -0,0 +1,95 @@
package at.esque.kafka.handlers;

import at.esque.kafka.alerts.ErrorAlert;
import com.google.inject.Singleton;
import javafx.application.Platform;
import javafx.stage.Stage;

import java.awt.*;

@Singleton
public class TrayHandler {

private final boolean traySupported;
private final TrayIcon trayIcon;
private final SystemTray systemTray;
private Stage primaryStage;

public TrayHandler() {
traySupported = SystemTray.isSupported();
if (traySupported) {

systemTray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/icons/trayIcon.png"));
trayIcon = new TrayIcon(image, "KafkaEsque");
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("KafkaEsque");

PopupMenu popupMenu = new PopupMenu();
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(e -> {
Platform.exit();
});

MenuItem showStageItem = new MenuItem("Open");
showStageItem.addActionListener(e -> {
Platform.runLater(() -> primaryStage.show());
});

MenuItem hideStageItem = new MenuItem("Hide");
hideStageItem.addActionListener(e -> {
Platform.runLater(() -> primaryStage.hide());
});

popupMenu.add(exitItem);
popupMenu.add(showStageItem);
popupMenu.add(hideStageItem);
try {
trayIcon.setPopupMenu(popupMenu);
systemTray.add(trayIcon);
Platform.setImplicitExit(false);
} catch (AWTException e) {
ErrorAlert.show(e);
}
} else {
systemTray = null;
trayIcon = null;
}
}

public void showInfoNotification(String title, String message) {
if (traySupported) {
trayIcon.displayMessage(title, message, TrayIcon.MessageType.INFO);
}
}

public void showWarningNotification(String title, String message) {
if (traySupported) {
trayIcon.displayMessage(title, message, TrayIcon.MessageType.WARNING);
}
}

public void showErrorNotification(String title, String message) {
if (traySupported) {
trayIcon.displayMessage(title, message, TrayIcon.MessageType.ERROR);
}
}

public void shutdown() {
if (traySupported) {
systemTray.remove(trayIcon);
}
}

public Stage getPrimaryStage() {
return primaryStage;
}

public void setPrimaryStage(Stage primaryStage) {
this.primaryStage = primaryStage;
}

public boolean isTraySupported() {
return traySupported;
}
}
Binary file added src/main/resources/icons/trayIcon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 461561c

Please sign in to comment.