Commits

shemnon  committed 1649207

some more menu commands

  • Participants
  • Parent commits 03ddf7a

Comments (0)

Files changed (4)

File flowdown-app/src/main/java/FlowDown/FLowDownModel.java

 package FlowDown;
 
-import javafx.beans.property.ObjectProperty;
-import javafx.beans.property.SimpleObjectProperty;
-import javafx.beans.property.SimpleStringProperty;
-import javafx.beans.property.StringProperty;
+import javafx.beans.property.*;
 
 import java.io.File;
 
 
     StringProperty cssFile = new SimpleStringProperty("");
 
+    BooleanProperty dirty = new SimpleBooleanProperty(false);
+
 }

File flowdown-app/src/main/java/FlowDown/FlowDownController.java

 
 import javafx.application.Platform;
 import javafx.event.ActionEvent;
+import javafx.stage.FileChooser;
 import javafx.stage.WindowEvent;
 
+import java.io.*;
+
 /**
  * Created with IntelliJ IDEA.
  * User: shemnon
 
     FlowDownModel model;
     FlowDownView view;
+    FileChooser fileChooser;
 
-    public void newDoc(ActionEvent e) {
+    public FlowDownController() {
+        fileChooser = new FileChooser();
+        fileChooser.getExtensionFilters().add(
+                new FileChooser.ExtensionFilter("Markdown Documents", "*.md", "*.markdown")
+        );
+    }
 
+
+    public void newDoc(ActionEvent e) {
+        if (model.dirty.get()) {
+            //TODO ask to save dialog
+        }
+        model.content.set("");
     }
 
     public void open(ActionEvent e) {
-
+        File f = fileChooser.showOpenDialog(view.getWindow());
+        System.out.println(f);
+        new Thread(() -> {
+            if (f != null) {
+                // This is way simpler in groovy
+                try (
+                    FileReader fr = new FileReader(f);
+                    StringWriter sw = new StringWriter()
+                ) {
+                    char[] buffer = new char[4096];
+                    int size = 0;
+                    while ((size = fr.read(buffer)) > 0) {
+                        sw.write(buffer, 0, size);
+                    }
+                    sw.close();
+                    fr.close();
+                    Platform.runLater(() -> model.content.set(sw.toString()));
+                    model.fileProp.set(f);
+                } catch (IOException ignore) {}
+            }
+        }).start();
     }
 
     public void save(ActionEvent e) {
-
+        if (model.fileProp.get() == null) {
+            saveAs(e);
+        } else {
+            saveFile(model.fileProp.get());
+        }
     }
 
     public void saveAs(ActionEvent e) {
+        saveFile(fileChooser.showSaveDialog(view.getWindow()));
+
+    }
 
+    protected void saveFile(final File f) {
+        if (f != null) {
+            new Thread(() -> {
+                try (
+                    StringReader stringReader = new StringReader(model.content.get());
+                    FileWriter fw = new FileWriter(f)
+                ) {
+                    char[] buffer = new char[4096];
+                    int size = 0;
+                    while ((size = stringReader.read(buffer)) > 0) {
+                        fw.write(buffer, 0, size);
+                    }
+                    stringReader.close();
+                    fw.close();
+                } catch (IOException ignore) {}
+
+            }).start();
+        }
     }
 
     public void quit(ActionEvent e) {

File flowdown-app/src/main/java/FlowDown/FlowDownView.java

 import java.net.URL;
 import java.util.ResourceBundle;
 
+import javafx.stage.Window;
 import org.bitbucket.shemnon.mdnode.MarkdownNode;
 import org.bitbucket.shemnon.mdnode.MarkdownToNodeGenerator;
 import com.sun.javafx.Utils;
         popup.show(anchor, point.getX(), point.getY());
     }
 
+    public Window getWindow() {
+        return contentScrollPane.getScene().getWindow();
+    }
 
 }
 

File markdown-node/src/main/resources/org/bitbucket/shemnon/mdnode/FlowDown.css

 
  Text {
   -fx-font-smoothing-type: lcd;
-  -fx-font-family: "DejaVu Sans Mono";
   -fx-font-size: 14px;
  }