Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

load/save markdown files implemented

Author Karl Tauber <email>
Date 2015-07-23 15:18:11 GMT+0200
Commit a0a29a70dad2c489940a3d44c304718030409547
Parent c135f24
src/main/java/org/markdownwriterfx/FileEditor.java
package org.markdownwriterfx;
+import java.io.IOException;
+import java.nio.file.Files;
import java.nio.file.Path;
+import javafx.beans.property.ObjectProperty;
+import javafx.beans.property.SimpleObjectProperty;
+import javafx.scene.control.Alert;
+import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
FileEditor(Path path) {
+ this.path.set(path);
+ this.path.addListener((observable, oldPath, newPath) -> updateTab());
+
// avoid that this is GCed
tab.setUserData(this);
- tab.setText((path != null) ? path.getFileName().toString() : "New Document");
- tab.setTooltip((path != null) ? new Tooltip(path.toString()) : null);
+ updateTab();
tab.setOnSelectionChanged(e -> {
if(tab.isSelected())
activated();
});
}
Tab getTab() {
return tab;
+ }
+
+ // path property
+ private final ObjectProperty<Path> path = new SimpleObjectProperty<>();
+ Path getPath() { return path.get(); }
+ void setPath(Path path) { this.path.set(path); }
+ ObjectProperty<Path> pathProperty() { return path; }
+
+ private void updateTab() {
+ Path path = this.path.get();
+ tab.setText((path != null) ? path.getFileName().toString() : "Untitled");
+ tab.setTooltip((path != null) ? new Tooltip(path.toString()) : null);
}
markdownPreviewPane = new MarkdownPreviewPane();
- //TODO
- markdownEditorPane.setMarkdown("# h1\n\n## h2\n\nsome **bold** text\n\n* ul 1\n* ul 2\n* ul 3");
+ load();
// bind preview to editor
markdownPreviewPane.markdownASTProperty().bind(markdownEditorPane.markdownASTProperty());
SplitPane splitPane = new SplitPane(markdownEditorPane.getNode(), markdownPreviewPane.getNode());
tab.setContent(splitPane);
+ }
+
+ void load() {
+ Path path = this.path.get();
+ if (path == null)
+ return;
+
+ try {
+ String markdown = new String(Files.readAllBytes(path));
+ markdownEditorPane.setMarkdown(markdown);
+ } catch (IOException ex) {
+ Alert alert = new Alert(AlertType.ERROR);
+ alert.setTitle("Load");
+ alert.setHeaderText(null);
+ alert.setContentText(String.format(
+ "Failed to load '%s'.\n\nReason: %s", path, ex.getMessage()));
+ alert.showAndWait();
+ }
+ }
+
+ boolean save() {
+ String markdown = markdownEditorPane.getMarkdown();
+ try {
+ Files.write(path.get(), markdown.getBytes());
+ return true;
+ } catch (IOException ex) {
+ Alert alert = new Alert(AlertType.ERROR);
+ alert.setTitle("Save");
+ alert.setHeaderText(null);
+ alert.setContentText(String.format(
+ "Failed to save '%s'.\n\nReason: %s", path.get(), ex.getMessage()));
+ alert.showAndWait();
+ return false;
+ }
}
}
src/main/java/org/markdownwriterfx/MainWindow.java
private void fileOpen() {
- FileChooser fileChooser = new FileChooser();
- fileChooser.setTitle("Open Markdown File");
- fileChooser.getExtensionFilters().addAll(
- new ExtensionFilter("Markdown Files", "*.md", "*.txt"),
- new ExtensionFilter("All Files", "*.*"));
-
+ FileChooser fileChooser = createFileChooser("Open Markdown File");
List<File> selectedFiles = fileChooser.showOpenMultipleDialog(getScene().getWindow());
if(selectedFiles == null)
private void fileSave() {
- //TODO
+ FileEditor activeFileEditor = (FileEditor) tabPane.getSelectionModel().getSelectedItem().getUserData();
+ if (activeFileEditor == null)
+ return;
+
+ if (activeFileEditor.getPath() == null) {
+ FileChooser fileChooser = createFileChooser("Save Markdown File");
+ File file = fileChooser.showSaveDialog(getScene().getWindow());
+ if (file == null)
+ return;
+
+ activeFileEditor.setPath(file.toPath());
+ }
+
+ activeFileEditor.save();
}
private void fileExit() {
Platform.exit();
+ }
+
+ private FileChooser createFileChooser(String title) {
+ FileChooser fileChooser = new FileChooser();
+ fileChooser.setTitle(title);
+ fileChooser.getExtensionFilters().addAll(
+ new ExtensionFilter("Markdown Files", "*.md", "*.markdown", "*.txt"),
+ new ExtensionFilter("All Files", "*.*"));
+ fileChooser.setInitialDirectory(new File("."));
+ return fileChooser;
}
Delta 82 lines added, 11 lines removed, 71-line increase