package de.prob2.ui.project.runconfigurations;

import java.util.ResourceBundle;

import com.google.inject.Inject;
import com.google.inject.Injector;
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
import de.prob2.ui.helpsystem.HelpButton;
import de.prob2.ui.internal.StageManager;
import de.prob2.ui.layout.FontSize;
import de.prob2.ui.prob2fx.CurrentProject;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;

public class RunconfigurationsTab extends Tab {

	@FXML
	private Label runconfigsPlaceholder;
	@FXML
	private Button addRunconfigButton;
	@FXML
	private HelpButton helpButton;
	@FXML
	private ListView<Runconfiguration> runconfigurationsListView;

	private final ResourceBundle bundle;
	private final CurrentProject currentProject;
	private final Injector injector;

	@Inject
	private RunconfigurationsTab(final StageManager stageManager, final ResourceBundle bundle, final CurrentProject currentProject, final Injector injector) {
		this.bundle = bundle;
		this.currentProject = currentProject;
		this.injector = injector;
		stageManager.loadFXML(this, "runconfigurations_tab.fxml");
	}

	@FXML
	public void initialize() {
		helpButton.setHelpContent("HelpMain.html");
		currentProject.machinesProperty().emptyProperty().addListener((observable, from, to) -> {
			if (to) {
				runconfigsPlaceholder.setText(bundle.getString("project.runconfigurations.tab.addMachinesFirst"));
				addRunconfigButton.setDisable(true);
			} else {
				runconfigsPlaceholder.setText(bundle.getString("project.runconfigurations.tab.noRunconfigurations"));
				addRunconfigButton.setDisable(false);
			}
		});
		runconfigurationsListView.itemsProperty().bind(currentProject.runconfigurationsProperty());
		this.selectedProperty().addListener((observable, from, to) -> {
			if (to) {
				runconfigurationsListView.refresh();
			}
		});
		runconfigurationsListView.setCellFactory(listView -> {
			ListCell<Runconfiguration> cell = new ListCell<Runconfiguration>() {
				@Override
				public void updateItem(Runconfiguration runconfiguration, boolean empty) {
					super.updateItem(runconfiguration, empty);
					if (empty) {
						setText(null);
						setGraphic(null);
					} else {
						setText(runconfiguration.toString());
						setGraphic(null);
					}
				}
			};

			final MenuItem removeRunconfigMenuItem = new MenuItem(bundle.getString("project.runconfigurations.tab.remove"));
			removeRunconfigMenuItem.setOnAction(event -> currentProject.removeRunconfiguration(cell.getItem()));
			removeRunconfigMenuItem.disableProperty().bind(cell.emptyProperty());

			cell.setOnMouseClicked(event -> {
				if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) {
					currentProject.startAnimation(cell.getItem());
				}
				runconfigurationsListView.getSelectionModel().select(currentProject.getCurrentRunconfiguration());
			});
			// makes sure that the selected item is always the current
			// runconfiguration. Otherwise the current runconfig would be
			// unselected as long as a mouse button is pressed
			cell.setOnMousePressed(event -> runconfigurationsListView.getSelectionModel()
					.select(currentProject.getCurrentRunconfiguration()));

			cell.setContextMenu(new ContextMenu(removeRunconfigMenuItem));

			return cell;
		});

		FontSize fontsize = injector.getInstance(FontSize.class);
		((FontAwesomeIconView) (addRunconfigButton.getGraphic())).glyphSizeProperty().bind(fontsize.multiply(2.0));
	}

	@FXML
	void addRunconfiguration() {
		injector.getInstance(RunconfigurationsDialog.class).showAndWait()
				.ifPresent(currentProject::addRunconfiguration);
	}
	
	public void disableTable(boolean disable) {
		runconfigurationsListView.setDisable(disable);
	}
}

@@ -2724,24 +2724,59 @@
 t() == 2) %7B%0A
+%09%09%09%09%09if(cell.getItem() != null) %7B%0A%09
 %09%09%09%09%09current
@@ -2811,24 +2811,31 @@
 getItem());%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09%09%09ru

Fix a NPE in RunconfigurationsTab