Skip to content

Commit

Permalink
dont go ahead if exception while saving document
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmanusta committed Jul 12, 2015
1 parent ceca6f2 commit ca85a32
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/kodcu/other/IOHelper.java
Expand Up @@ -24,6 +24,7 @@
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.stream.Stream;

Expand All @@ -38,12 +39,14 @@ public static void writeToFile(File file, String content, StandardOpenOption...
writeToFile(file.toPath(), content, openOption);
}

public static void writeToFile(Path path, String content, StandardOpenOption... openOption) {
public static Optional<IOException> writeToFile(Path path, String content, StandardOpenOption... openOption) {
try {
Files.write(path, content.getBytes(Charset.forName("UTF-8")), openOption);
} catch (IOException e) {
logger.error("Problem occured while writing to {}", path, e);
return Optional.of(e);
}
return Optional.empty();
}

public static void writeToFile(Path path, byte[] content, StandardOpenOption... openOption) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/kodcu/service/DocumentService.java
Expand Up @@ -18,6 +18,7 @@
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;

Expand Down Expand Up @@ -58,7 +59,12 @@ public void saveDoc() {
if (Objects.isNull(currentPath) || !current.getCurrentTabText().contains(" *"))
return;

IOHelper.writeToFile(currentPath, (String) current.currentEngine().executeScript("editor.getValue();"), TRUNCATE_EXISTING, CREATE);
Optional<IOException> exception =
IOHelper.writeToFile(currentPath, current.currentEditorValue(), TRUNCATE_EXISTING, CREATE);

if(exception.isPresent())
return;

current.setCurrentTabText(currentPath.getFileName().toString());
ObservableList<String> recentFiles = controller.getRecentFilesList();
recentFiles.remove(currentPath.toString());
Expand Down

0 comments on commit ca85a32

Please sign in to comment.