diff --git a/src/main/java/cn/reddragon/eportal/Authenticator.java b/src/main/java/cn/reddragon/eportal/Authenticator.java index 2f5745a..41e9582 100644 --- a/src/main/java/cn/reddragon/eportal/Authenticator.java +++ b/src/main/java/cn/reddragon/eportal/Authenticator.java @@ -2,9 +2,12 @@ import cn.reddragon.eportal.utils.HttpUtils; +import java.awt.*; import java.io.IOException; import java.io.PrintWriter; import java.net.HttpURLConnection; +import java.net.NoRouteToHostException; +import java.net.SocketTimeoutException; import java.util.List; import java.util.Map; import java.util.Objects; @@ -77,7 +80,39 @@ public static boolean isOnline() { online = false; return false; } - } catch (Exception e) { + } catch (SocketTimeoutException e) { + if (SystemTray.isSupported()) { + SystemTray tray = SystemTray.getSystemTray(); + Image image = Toolkit.getDefaultToolkit().createImage("icon.png"); + TrayIcon icon = new TrayIcon(image, "Error"); + icon.setImageAutoSize(true); + try { + tray.add(icon); + } catch (AWTException e1) { + throw new RuntimeException(e1); + } + icon.displayMessage("Error", "Connection timeout!", TrayIcon.MessageType.ERROR); + tray.remove(icon); + System.exit(0); + } + return false; + } catch (NoRouteToHostException e) { + if (SystemTray.isSupported()) { + SystemTray tray = SystemTray.getSystemTray(); + Image image = Toolkit.getDefaultToolkit().createImage("icon.png"); + TrayIcon icon = new TrayIcon(image, "Error"); + icon.setImageAutoSize(true); + try { + tray.add(icon); + } catch (AWTException e1) { + throw new RuntimeException(e1); + } + icon.displayMessage("Error", "No Internet connection!", TrayIcon.MessageType.ERROR); + tray.remove(icon); + System.exit(0); + } + return false; + } catch (IOException e) { online = false; return false; } diff --git a/src/main/java/cn/reddragon/eportal/Config.java b/src/main/java/cn/reddragon/eportal/Config.java index ea02089..3ad4f78 100644 --- a/src/main/java/cn/reddragon/eportal/Config.java +++ b/src/main/java/cn/reddragon/eportal/Config.java @@ -1,6 +1,9 @@ package cn.reddragon.eportal; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -47,7 +50,6 @@ public static String[] read() throws IOException { } String password = new String(b1); stream.close(); - System.out.println(username + " " + password); return new String[]{username, password}; } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/cn/reddragon/eportal/HelloApplication.java b/src/main/java/cn/reddragon/eportal/HelloApplication.java index 7f1643a..aea1bc3 100644 --- a/src/main/java/cn/reddragon/eportal/HelloApplication.java +++ b/src/main/java/cn/reddragon/eportal/HelloApplication.java @@ -12,14 +12,19 @@ import javafx.scene.control.TextField; import javafx.stage.Stage; -import java.io.IOException; import java.awt.*; +import java.io.IOException; public class HelloApplication extends Application { private static Label statusLabel; private static Button button; public static final Thread askThread = new Thread(() -> { while (true) { + try { + Thread.sleep(2500L); + } catch (InterruptedException e) { + //throw new RuntimeException(e); + } Authenticator.isOnline(); Platform.runLater(() -> { statusLabel.setText("Current status:" + (Authenticator.online ? "Online" : "Offline")); @@ -29,11 +34,6 @@ public class HelloApplication extends Application { button.setText("Login"); } }); - try { - Thread.sleep(2500L); - } catch (InterruptedException e) { - //throw new RuntimeException(e); - } } }); @@ -84,7 +84,6 @@ public static void main(String[] args) { e.printStackTrace(); System.exit(0); } catch (IOException ignored) { - } launch(); System.exit(0); diff --git a/src/main/java/cn/reddragon/eportal/HelloController.java b/src/main/java/cn/reddragon/eportal/HelloController.java index ed51f3c..6e49584 100644 --- a/src/main/java/cn/reddragon/eportal/HelloController.java +++ b/src/main/java/cn/reddragon/eportal/HelloController.java @@ -3,7 +3,8 @@ import cn.reddragon.eportal.utils.HttpUtils; import cn.reddragon.eportal.utils.IOUtils; import cn.reddragon.eportal.utils.URIEncoder; -import com.google.gson.*; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.ChoiceBox; @@ -29,7 +30,7 @@ public class HelloController { @FXML protected void onLoginButtonClick() { - if (Authenticator.isOnline()) { + if (Authenticator.online) { if (button.getText().equals("Login")) { resultText.setText("Already logged in!"); button.setText("Logout");