async connect button

This commit is contained in:
sirjonasxx 2019-01-16 18:22:03 +01:00
parent c522db40da
commit 0ca71ad174

View File

@ -23,8 +23,6 @@ public class Connection extends SubForm {
public CheckBox cbx_autodetect; public CheckBox cbx_autodetect;
public TextField txtfield_hotelversion; public TextField txtfield_hotelversion;
private boolean isBusy = false;
public void initialize() { public void initialize() {
inpPort.getEditor().textProperty().addListener(observable -> { inpPort.getEditor().textProperty().addListener(observable -> {
updateInputUI(); updateInputUI();
@ -77,53 +75,60 @@ public class Connection extends SubForm {
public void onParentSet(){ public void onParentSet(){
getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(() -> { getHConnection().addStateChangeListener((oldState, newState) -> Platform.runLater(() -> {
txtfield_hotelversion.setText(getHConnection().getHotelVersion()); txtfield_hotelversion.setText(getHConnection().getHotelVersion());
if (newState == HConnection.State.NOT_CONNECTED) { Platform.runLater(() -> {
updateInputUI(); if (newState == HConnection.State.NOT_CONNECTED) {
lblState.setText("Not connected"); updateInputUI();
btnConnect.setText("Connect"); lblState.setText("Not connected");
outHost.setText(""); btnConnect.setText("Connect");
outPort.setText(""); outHost.setText("");
} outPort.setText("");
else if (oldState == HConnection.State.NOT_CONNECTED) { }
updateInputUI(); else if (oldState == HConnection.State.NOT_CONNECTED) {
btnConnect.setText("Abort"); 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) { public void btnConnect_clicked(ActionEvent actionEvent) {
if (!isBusy) { if (getHConnection().getState() == HConnection.State.NOT_CONNECTED) {
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");
btnConnect.setDisable(true);
new Thread(() -> { new Thread(() -> {
try { if (cbx_autodetect.isSelected()) {
getHConnection().start(); getHConnection().prepare();
} catch (IOException e) {
e.printStackTrace();
} }
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(); }).start();
} }
else { else {
getHConnection().abort(); getHConnection().abort();
isBusy = false;
} }
} }