
import java.io.File; 
import java.util.concurrent.Callable; 

import javafx.beans.binding.Bindings; 
import javafx.beans.binding.StringBinding; 
import javafx.beans.property.ObjectProperty; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.control.Button; 
import javafx.scene.control.ContextMenu; 
import javafx.scene.control.Label; 
import javafx.scene.control.ListCell; 
import javafx.scene.control.ListView; 
import javafx.scene.control.MenuItem; 
import javafx.scene.control.Separator; 
import javafx.scene.layout.VBox; 
import javafx.stage.FileChooser; 
import javafx.stage.Stage; 

public class FileUploaderVBox extends VBox { 

	ListView<File> filesToUpload = new ListView<>(); 

	public FileUploaderVBox(Stage primaryStage) { 
		setAlignment(Pos.TOP_CENTER); 

		Label l = new Label("Select Files to Upload"); 
		l.setStyle("-fx-font: 12 arial; -fx-font-weight: bold;"); 
		setMargin(l, new Insets(25,0,20,0)); 

		Separator horizSeparator1 = new Separator(); 
		horizSeparator1.prefWidthProperty().bind(widthProperty()); 


		filesToUpload.setCellFactory(lv -> { 

			ListCell<File> cell = new ListCell<>(); 

			ContextMenu contextMenu = new ContextMenu(); 


			MenuItem deleteItem = new MenuItem(); 
			deleteItem.textProperty().bind(Bindings.format("Delete \"%s\"", cell.itemProperty())); 
			deleteItem.setOnAction(event -> filesToUpload.getItems().remove(cell.getItem())); 

			contextMenu.getItems().addAll(deleteItem); 

			// cell.textProperty().bind(cell.itemProperty()); 
			// cell.textProperty().bind(cell.itemProperty().asString()); 

			ObjectProperty<File> itemProperty = cell.itemProperty(); 
			// StringBinding sb = Bindings.createStringBinding(new Callable<String>() { 
			// 
			//	@Override 
			//	public String call() throws Exception { 
			//	
			//	if (null == itemProperty.getValue()) 
			//	return ""; 
			//	else 
			//	return itemProperty.getValue().toString(); 
			//	} 
			// 
			// }, itemProperty); 

			StringBinding sb = Bindings.createStringBinding(new Callable<String>() { 

				@Override 
				public String call() throws Exception { 
					if (null == itemProperty) 
						return ""; 
					else 
						return itemProperty.toString(); 
				} 

			}, itemProperty); 
			cell.textProperty().bind(sb); 


			cell.emptyProperty().addListener((obs, wasEmpty, isNowEmpty) -> { 
				if (isNowEmpty) { 
					cell.setContextMenu(null); 
				} else { 
					cell.setContextMenu(contextMenu); 
				} 
			}); 

			return cell ; 
		}); 

		Separator horizSeparator2 = new Separator(); 
		horizSeparator2.prefWidthProperty().bind(widthProperty()); 

		Button chooseFileButton = new Button("Choose File"); 

		chooseFileButton.setOnAction(new EventHandler<ActionEvent> () { 

			@Override 
			public void handle(ActionEvent event) { 

				FileChooser fileChooser = new FileChooser(); 
				File f = fileChooser.showOpenDialog(primaryStage); 
				if (null != f) 
					filesToUpload.getItems().add(f); 
			} 
		}); 

		getChildren().addAll(l, horizSeparator1, filesToUpload, horizSeparator2, chooseFileButton); 

	} 

} 