From 0ca71ad17438732b96c621467efa07602d2cf800 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Wed, 16 Jan 2019 18:22:03 +0100 Subject: [PATCH] async connect button --- .../java/gearth/ui/connection/Connection.java | 77 ++++++++++--------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/G-Earth/src/main/java/gearth/ui/connection/Connection.java b/G-Earth/src/main/java/gearth/ui/connection/Connection.java index 768ab02..58dc0d8 100644 --- a/G-Earth/src/main/java/gearth/ui/connection/Connection.java +++ b/G-Earth/src/main/java/gearth/ui/connection/Connection.java @@ -23,8 +23,6 @@ public class Connection extends SubForm { public CheckBox cbx_autodetect; public TextField txtfield_hotelversion; - private boolean isBusy = false; - public void initialize() { inpPort.getEditor().textProperty().addListener(observable -> { updateInputUI(); @@ -77,53 +75,60 @@ public class Connection extends SubForm { public void onParentSet(){ getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(() -> { txtfield_hotelversion.setText(getHConnection().getHotelVersion()); - if (newState == HConnection.State.NOT_CONNECTED) { - updateInputUI(); - lblState.setText("Not connected"); - btnConnect.setText("Connect"); - outHost.setText(""); - outPort.setText(""); - } - else if (oldState == HConnection.State.NOT_CONNECTED) { - updateInputUI(); - btnConnect.setText("Abort"); - } + Platform.runLater(() -> { + if (newState == HConnection.State.NOT_CONNECTED) { + updateInputUI(); + lblState.setText("Not connected"); + btnConnect.setText("Connect"); + outHost.setText(""); + outPort.setText(""); + } + else if (oldState == HConnection.State.NOT_CONNECTED) { + updateInputUI(); + btnConnect.setText("Abort"); + } + + if (newState == HConnection.State.CONNECTED) { + lblState.setText("Connected"); + outHost.setText(getHConnection().getDomain()); + outPort.setText(getHConnection().getPort()+""); + } + if (newState == HConnection.State.WAITING_FOR_CLIENT) { + lblState.setText("Waiting for connection"); + } + }); - if (newState == HConnection.State.CONNECTED) { - lblState.setText("Connected"); - outHost.setText(getHConnection().getDomain()); - outPort.setText(getHConnection().getPort()+""); - } - if (newState == HConnection.State.WAITING_FOR_CLIENT) { - lblState.setText("Waiting for connection"); - } })); } public void btnConnect_clicked(ActionEvent actionEvent) { - if (!isBusy) { - isBusy = true; - if (cbx_autodetect.isSelected()) { - getHConnection().prepare(); - } - else { - getHConnection().prepare(inpHost.getEditor().getText(), Integer.parseInt(inpPort.getEditor().getText())); - } - - if (HConnection.DEBUG) System.out.println("connecting"); + if (getHConnection().getState() == HConnection.State.NOT_CONNECTED) { + btnConnect.setDisable(true); new Thread(() -> { - try { - getHConnection().start(); - } catch (IOException e) { - e.printStackTrace(); + if (cbx_autodetect.isSelected()) { + getHConnection().prepare(); } + else { + getHConnection().prepare(inpHost.getEditor().getText(), Integer.parseInt(inpPort.getEditor().getText())); + } + Platform.runLater(() -> btnConnect.setDisable(false)); + + if (HConnection.DEBUG) System.out.println("connecting"); + + new Thread(() -> { + try { + getHConnection().start(); + } catch (IOException e) { + e.printStackTrace(); + } + }).start(); }).start(); + } else { getHConnection().abort(); - isBusy = false; } }