package edu.chl.proton.control;

import com.jfoenix.controls.JFXTabPane;
import edu.chl.proton.model.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Tab;
import javafx.scene.control.TreeView;
import javafx.scene.control.TreeItem;
import java.io.File;

import java.io.IOException;


/**
 * Anton Levholm
 * Created by antonlevholm on 2017-05-01.
 */
public class MainController {
    private static IFileHandler file;
    private static IDocumentHandler document;
    private FXMLLoader loader;

    @FXML
    private JFXTabPane tabPane;
    @FXML
    private TreeView<File> treeView;


    public void initialize() throws IOException {
        WorkspaceFactory factory = new WorkspaceFactory();
        file = factory.getWorkspace();
        document = factory.getWorkspace();
        loader = new FXMLLoader(getClass().getResource("/edu/chl/proton/view/markdown-tab.fxml"));
        Tab tab = new Tab("Untitled");
        tab.setContent(loader.load());
        tabPane.getTabs().add(tab);

        File currentDir = new File(file.getCurrentDirectory()); // current directory
        findFiles(currentDir, null);
    }


    private void findFiles(File dir, TreeItem<File> parent) {
        TreeItem<File> root = new TreeItem<>(dir);
        root.setExpanded(true);
        try {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.println("directory:" + file.getCanonicalPath());
                    findFiles(file,root);
                } else {
                    System.out.println("     file:" + file.getCanonicalPath());
                    root.getChildren().add(new TreeItem<>(file));
                }

            }
            if(parent == null){
                treeView.setRoot(root);
            } else {
                parent.getChildren().add(root);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    public void addNewTab(String name) throws IOException {
        Tab tab = new Tab(name);
        tab.setContent(loader.load());
        tabPane.getTabs().add(tab);
    }

    @FXML
    public void onClickNewButton(ActionEvent event) throws IOException {
        document.createDocument(DocumentType.MARKDOWN);
        addNewTab("Untitled.md");
    }

    @FXML
    public void onClickOpenButton(ActionEvent event) throws IOException {
        String file = "";
        document.openDocument(file);
    }

    @FXML
    public void onClickSaveButton(ActionEvent event) throws IOException {
        file.saveCurrentDocument();
    }

    @FXML
    public void onClickUndoButton(ActionEvent event) throws IOException {
        // Can be really hard to implement.
    }

    @FXML
    public void onClickRedoButton(ActionEvent event) throws IOException {

    }
}

@@ -186,16 +186,42 @@
 Loader;%0A
+import javafx.scene.Node;%0A
 import j
@@ -321,16 +321,48 @@
 eeItem;%0A
+import javafx.scene.text.Text;%0A%0A
 import j
@@ -1322,22 +1322,16 @@
 TreeItem
-%3CFile%3E
  root = 
@@ -1351,16 +1351,88 @@
 %3E(dir);%0A
+        root.setValue(dir.getName());%0A        if (parent == null) %7B%0A    
         
@@ -1467,18 +1467,64 @@
     
-try
+%7D else
  %7B%0A    
+        root.setExpanded(false);%0A        %7D%0A
     
@@ -1567,20 +1567,16 @@
         
-  
-  
 for (Fil
@@ -1589,28 +1589,24 @@
  : files) %7B%0A
-    
             
@@ -1651,143 +1651,106 @@
     
-    System.out.println(%22directory:%22 + file.getCanonicalPath());%0A                    findFiles(file,root);%0A                %7D else %7B%0A    
+findFiles(file, root);%0A            %7D else %7B%0A                TreeItem item = new TreeItem%3C%3E(file);%0A
     
@@ -1765,72 +1765,39 @@
     
-Sys
+i
 tem.
-out.println(%22     file:%22 + file.getCanonicalPath
+setValue(file.getName
 ());%0A
-    
     
@@ -1835,35 +1835,15 @@
 add(
-new TreeItem%3C%3E(file));%0A    
+item);%0A
     
@@ -1853,36 +1853,32 @@
      %7D%0A%0A        
-    
 %7D%0A            if
@@ -1871,20 +1871,16 @@
         
- 
-   
 if(paren
@@ -1891,20 +1891,16 @@
  null)%7B%0A
-    
         
@@ -1927,36 +1927,32 @@
 (root);%0A        
-    
 %7D else %7B%0A       
@@ -1956,20 +1956,16 @@
         
-   
- 
 parent.g
@@ -1992,89 +1992,8 @@
 t);%0A
-            %7D%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A
     

Add changes to file tree