diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java index 3f2f5a6..460fc14 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java @@ -7,10 +7,13 @@ package pp.battleship.client; +import com.simsilica.lemur.Button; import com.simsilica.lemur.Container; import com.simsilica.lemur.Label; import com.simsilica.lemur.TextField; import com.simsilica.lemur.component.SpringGridLayout; + +import pp.battleship.client.sever.BattleshipServer; import pp.dialog.Dialog; import pp.dialog.DialogBuilder; import pp.dialog.SimpleDialog; @@ -33,6 +36,7 @@ class NetworkDialog extends SimpleDialog { private final NetworkSupport network; private final TextField host = new TextField(LOCALHOST); private final TextField port = new TextField(DEFAULT_PORT); + private final Button serverButton = new Button(lookup("client.server-start")); private String hostname; private int portNumber; private Future connectionFuture; @@ -65,6 +69,9 @@ class NetworkDialog extends SimpleDialog { .setOkClose(false) .setNoClose(false) .build(this); + + //Add the button to start the sever + addChild(serverButton).addClickCommands(s -> ifTopDialog(this::startServerInThread)); } /** @@ -150,4 +157,21 @@ class NetworkDialog extends SimpleDialog { network.getApp().errorDialog(lookup("server.connection.failed")); network.getApp().setInfoText(e.getLocalizedMessage()); } + + /** + * Starts the server in a separate thread. + */ + private void startServerInThread() { + serverButton.setEnabled(false); + Thread serverThread = new Thread(() -> { + try { + BattleshipServer.main(null); + } catch (Exception e) { + serverButton.setEnabled(true); + LOGGER.log(Level.ERROR, "Server could not be started", e); + network.getApp().errorDialog("Could not start server: " + e.getMessage()); + } + }); + serverThread.start(); + } }