package controller;

import java.util.List;

import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;

import connection.TerminalConnection;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;

public class ConnectionPaneController {

	@FXML
	private ComboBox<CardTerminal> terminalCombo;

	@FXML
	private AnchorPane childConnection;

	private TitledPane parentView;

	@FXML
	protected void handleConnectAction(ActionEvent event) {
		try {
			if (TerminalConnection.INSTANCE.connect()) {
				parentView.setText("Verbunden!");
				parentView.setTextFill(Color.GREEN);
			}
		} catch (CardException e) {
			parentView.setText("ERROR!");
		}
	}

	@FXML
	protected void handleDisconnectAction(ActionEvent event) {
		try {
			if (TerminalConnection.INSTANCE.disconnect()) {
				parentView.setText("Nicht verbunden!");
				parentView.setTextFill(Color.RED);
			}
		} catch (CardException e) {
			parentView.setText("ERROR!");
		}
	}

	@FXML
	protected void handleComboShowing() {
		try {
			List<CardTerminal> terminals = TerminalConnection.getTerminals();
			terminalCombo.getItems().clear();
			terminals.forEach(t -> terminalCombo.getItems().add(t));
		} catch (CardException e) {
			parentView.setText("ERROR!");
		}
	}

	@FXML
	protected void handleComboClicked() {
		try {
			CardTerminal terminal = (CardTerminal) terminalCombo.getSelectionModel().getSelectedItem();
			TerminalConnection.INSTANCE.chooseTerminal(terminal);
		} catch (CardException e) {
			parentView.setText("ERROR!");
		}
	}

	public void setParentView(TitledPane parentView) {
		this.parentView = parentView;
	}
}

@@ -631,32 +631,186 @@
 nt) %7B%0D%0A%09%09try %7B%0D%0A
+%09%09%09CardTerminal terminal = (CardTerminal) terminalCombo.getSelectionModel().getSelectedItem();%0D%0A%09%09%09TerminalConnection.INSTANCE.chooseTerminal(terminal);%0D%0A
 %09%09%09if (TerminalC
@@ -919,24 +919,72 @@
 or.GREEN);%0D%0A
+%09%09%09%7D else %7B%0D%0A%09%09%09%09parentView.setText(%22ERROR!%22);%0D%0A
 %09%09%09%7D%0D%0A%09%09%7D ca
@@ -1016,34 +1016,19 @@
 %09%09%09p
-arentView.setText(%22ERROR!%22
+rintError(e
 );%0D%0A
@@ -1298,34 +1298,19 @@
 %09%09%09p
-arentView.setText(%22ERROR!%22
+rintError(e
 );%0D%0A
@@ -1585,34 +1585,19 @@
 %09%09%09p
-arentView.setText(%22ERROR!%22
+rintError(e
 );%0D%0A
@@ -1661,201 +1661,213 @@
 %0D%0A%09%09
-try %7B%0D%0A%09%09%09CardTerminal terminal = (CardTerminal) terminalCombo.getSelectionModel().getSelectedItem();%0D%0A%09%09%09TerminalConnection.INSTANCE.chooseTerminal(terminal);%0D%0A%09%09%7D catch (CardException e) %7B
+// Nothing To do%0D%0A%09%7D%0D%0A%0D%0A%09public void setParentView(TitledPane parentView) %7B%0D%0A%09%09this.parentView = parentView;%0D%0A%09%7D%0D%0A%0D%0A%09private void printError(Throwable e) %7B%0D%0A%09%09System.err.println(e.getLocalizedMessage());
 %0D%0A
-%09
 %09%09pa
@@ -1901,100 +1901,41 @@
 %0D%0A%09%09
-%7D%0D%0A%09%7D%0D%0A%0D%0A%09public void setParentView(TitledPane parentView) %7B%0D%0A%09%09this.parentView = parentView
+parentView.setTextFill(Color.RED)
 ;%0D%0A%09

Add console print out for errors