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..8a4a04a 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 @@ -11,6 +11,8 @@ import com.simsilica.lemur.Container; import com.simsilica.lemur.Label; import com.simsilica.lemur.TextField; import com.simsilica.lemur.component.SpringGridLayout; +import com.simsilica.lemur.Button; +import pp.battleship.server.BattleshipServer; import pp.dialog.Dialog; import pp.dialog.DialogBuilder; import pp.dialog.SimpleDialog; @@ -33,6 +35,8 @@ 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-star")); + private final Button serverButton = new Button(lookup("client.server-start")); private String hostname; private int portNumber; private Future connectionFuture; @@ -65,6 +69,10 @@ 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 +158,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(); + } }