Skip to content

Commit

Permalink
Use HostServices to open Browser instead of Desktop.getDesktop().browse
Browse files Browse the repository at this point in the history
  • Loading branch information
patschuh committed Mar 14, 2022
1 parent d3a5ffe commit 3e8e326
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
21 changes: 10 additions & 11 deletions src/main/java/at/esque/kafka/AboutController.java
@@ -1,18 +1,15 @@
package at.esque.kafka;

import at.esque.kafka.alerts.ErrorAlert;
import at.esque.kafka.handlers.VersionInfo;
import at.esque.kafka.handlers.VersionInfoHandler;
import javafx.application.HostServices;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class AboutController {
public ImageView imageView;
public HBox infoContainer;
Expand All @@ -22,7 +19,10 @@ public class AboutController {
public Label buildJvm;
public Label buildTime;

public void setup(VersionInfoHandler versionInfoHandler) {
private HostServices hostServices;

public void setup(VersionInfoHandler versionInfoHandler, HostServices hostServices) {
this.hostServices = hostServices;
final VersionInfo versionInfo = versionInfoHandler.getVersionInfo();
final String tag = versionInfo.getTag();
infoContainer.setVisible(false);
Expand All @@ -39,12 +39,11 @@ public void setup(VersionInfoHandler versionInfoHandler) {
}

public void clickGithubLink(MouseEvent mouseEvent) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
try {
Desktop.getDesktop().browse(new URI("https://github.com/patschuh/KafkaEsque"));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
hostServices.showDocument("https://github.com/patschuh/KafkaEsque");
}catch (Exception e){
ErrorAlert.show(e);
}
}
}

}
11 changes: 9 additions & 2 deletions src/main/java/at/esque/kafka/Controller.java
Expand Up @@ -42,6 +42,7 @@
import com.google.inject.Injector;
import com.opencsv.bean.CsvToBeanBuilder;
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException;
import javafx.application.HostServices;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
Expand Down Expand Up @@ -153,6 +154,8 @@ public class Controller {
@Inject
private Injector injector;

private HostServices hostServices;

//FXML
@FXML
private KafkaEsqueCodeArea keyTextArea;
Expand Down Expand Up @@ -272,7 +275,7 @@ public void setup(Stage controlledStage) {
dummycluster.setIdentifier("Empty");
messageTabPane.getTabs().add(createTab(dummycluster, "Tab"));

versionInfoHandler.showDialogIfUpdateIsAvailable();
versionInfoHandler.showDialogIfUpdateIsAvailable(hostServices);
}

private void setupJsonFormatToggle() {
Expand Down Expand Up @@ -1371,7 +1374,7 @@ public void aboutClick(ActionEvent event) {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/about.fxml"));
Parent root1 = fxmlLoader.load();
AboutController controller = fxmlLoader.getController();
controller.setup(versionInfoHandler);
controller.setup(versionInfoHandler, hostServices);
Stage stage = new Stage();
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/kafkaesque.png")));
stage.initOwner(controlledStage);
Expand All @@ -1385,4 +1388,8 @@ public void aboutClick(ActionEvent event) {
ErrorAlert.show(e, controlledStage);
}
}

public void setHostServices(HostServices hostServices) {
this.hostServices = hostServices;
}
}
3 changes: 3 additions & 0 deletions src/main/java/at/esque/kafka/Main.java
Expand Up @@ -4,6 +4,7 @@
import com.google.inject.Guice;
import com.google.inject.Injector;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -19,8 +20,10 @@ public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = injector.getInstance(FXMLLoader.class);
loader.setLocation(getClass().getResource("/fxml/mainScene.fxml"));
Parent root = loader.load();
final HostServices hostServices = this.getHostServices();
Controller controller = loader.getController();
controller.setup(primaryStage);
controller.setHostServices(hostServices);
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/kafkaesque.png")));
primaryStage.setTitle("Kafkaesque");
primaryStage.setScene(createStyledScene(root, 1600, 900));
Expand Down
Expand Up @@ -17,4 +17,6 @@ public FXMLLoader get() {
return loader;
}



}
19 changes: 6 additions & 13 deletions src/main/java/at/esque/kafka/handlers/VersionInfoHandler.java
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import javafx.application.HostServices;
import javafx.application.Platform;
import okhttp3.Call;
import okhttp3.CipherSuite;
Expand All @@ -15,11 +16,7 @@
import okhttp3.TlsVersion;
import org.gradle.util.VersionNumber;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
Expand Down Expand Up @@ -129,19 +126,15 @@ private Map<String, Object> checkLatestVersion() {
return null;
}

public void showDialogIfUpdateIsAvailable() {
public void showDialogIfUpdateIsAvailable(HostServices hostServices) {
final UpdateInfo updateInfo = availableUpdate();
if (updateInfo != null) {
final boolean openInBrowser = ConfirmationAlert.show("Update Available", "Version " + updateInfo.getTag() + " is available", "Do you want to open the release page?");
if (openInBrowser) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
try {
Desktop.getDesktop().browse(new URI(updateInfo.getReleasePage()));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
hostServices.showDocument(updateInfo.getReleasePage());
}catch (Exception e){
ErrorAlert.show(e);
}
}
}
Expand Down

0 comments on commit 3e8e326

Please sign in to comment.