package seng302.Controllers; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.layout.AnchorPane; import seng302.RaceConnection; import java.io.IOException; import java.net.Socket; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; /** * Created by cbt24 on 3/05/17. */ public class ConnectionController extends Controller { @FXML private AnchorPane connectionWrapper; @FXML private TableView connectionTable; @FXML private TableColumn hostnameColumn; @FXML private TableColumn statusColumn; @FXML private Button connectButton; private List connections; @Override public void initialize(URL location, ResourceBundle resources) { // TODO - replace with config file connections = new ArrayList<>(); connections.add(new RaceConnection("livedata.americascup.com", 4941)); connections.add(new RaceConnection("localhost", 4942)); connectionTable.setItems(FXCollections.observableArrayList(connections)); hostnameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty()); statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty()); connectionTable.getSelectionModel().selectedItemProperty().addListener((obs, prev, curr) -> { if (curr != null && ((RaceConnection)curr).check()) connectButton.setDisable(false); else connectButton.setDisable(true); }); connectButton.setDisable(true); } /** * Sets current status of all connections. */ public void checkConnections() { for(RaceConnection connection: connections) { connection.check(); } } /** * Connects to host currently selected in table. Button enabled only if host is ready. */ public void connectSocket() { try{ RaceConnection connection = (RaceConnection)connectionTable.getSelectionModel().getSelectedItem(); Socket socket = new Socket(connection.getHostname(), connection.getPort()); connectionWrapper.setVisible(false); parent.enterLobby(socket); } catch (IOException e) { /* Never reached */ } } }