package edu.chl.proton.control;

import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.input.KeyCode;

import java.io.File;

/**
 * @author Stina werme
 * Created by stinawerme on 20/05/17.
 *
 * Extends java class TreeCell to make the tree editable for the user.
 */
final class EditableTreeCell extends TreeCell<File> {

    private TextField textField;

    public EditableTreeCell() {
    }

    @Override
    public void startEdit() {
        super.startEdit();

        if (textField == null) {
            createTextField();
        }
        setText(null);
        setGraphic(textField);
        textField.selectAll();
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        File item = getItem();


        item.getName();
        setText(item.getName());
        setGraphic(getTreeItem().getGraphic());
    }


    @Override
    public void updateItem(File item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                setGraphic(getTreeItem().getGraphic());
            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setOnKeyReleased(keyReleaseEventObject -> {
            if (keyReleaseEventObject.getCode() == KeyCode.ENTER) {
                String name = textField.getText();
                File newFileName = new File(getItem().getParentFile(), name);
                getItem().renameTo(newFileName);
                commitEdit(newFileName);

            } else if (keyReleaseEventObject.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        });
    }

    private String getString() {
        return getItem() == null ? "" : getItem().getName();
    }
}

@@ -44,31 +44,53 @@
 afx.
-scene.control.TextField
+event.Event;%0Aimport javafx.event.EventHandler
 ;%0Aim
@@ -115,24 +115,17 @@
 control.
-TreeCell
+*
 ;%0Aimport
@@ -411,40 +411,456 @@
 ld;%0A
-%0A    public EditableTreeCell() %7B
+    private ContextMenu addMenu = new ContextMenu();%0A%0A    public EditableTreeCell() %7B%0A%0A        MenuItem addMenuItem = new MenuItem(%22Add File%22);%0A        addMenu.getItems().add(addMenuItem);%0A        addMenuItem.setOnAction(new EventHandler() %7B%0A            public void handle(Event t) %7B%0A                TreeItem newFile =%0A                        new TreeItem%3CFile%3E();%0A                getTreeItem().getChildren().add(newFile);%0A            %7D%0A        %7D);
 %0A   
@@ -1847,24 +1847,168 @@
 Graphic());%0A
+                if (!getTreeItem().isLeaf()&&getTreeItem().getParent()!= null) %7B%0A                    setContextMenu(addMenu);%0A                %7D%0A
             

Make it possible to add tree item