diff --git a/src/main/java/com/kodedu/controller/ApplicationController.java b/src/main/java/com/kodedu/controller/ApplicationController.java index b027f039e..ea3d7ffed 100644 --- a/src/main/java/com/kodedu/controller/ApplicationController.java +++ b/src/main/java/com/kodedu/controller/ApplicationController.java @@ -612,6 +612,7 @@ public void initializeApp() { Arrays.asList(htmlPane, slidePane, liveReloadPane).forEach(viewPanel -> VBox.getVgrow(viewPanel)); threadService.runTaskLater(() -> { + // Main loop for the app while (true) { try { renderLoop(); diff --git a/src/main/java/com/kodedu/service/impl/DirectoryServiceImpl.java b/src/main/java/com/kodedu/service/impl/DirectoryServiceImpl.java index e51d72974..fe4a2862f 100644 --- a/src/main/java/com/kodedu/service/impl/DirectoryServiceImpl.java +++ b/src/main/java/com/kodedu/service/impl/DirectoryServiceImpl.java @@ -5,9 +5,7 @@ import com.kodedu.helper.IOHelper; import com.kodedu.other.Current; import com.kodedu.service.DirectoryService; -import com.kodedu.service.FileWatchService; import com.kodedu.service.PathMapper; -import com.kodedu.service.PathResolverService; import com.kodedu.service.ThreadService; import com.kodedu.service.ui.FileBrowseService; import javafx.application.Platform; @@ -22,12 +20,12 @@ import java.nio.file.Files; import java.nio.file.FileSystems; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; -import java.util.stream.Collectors; import java.util.stream.Stream; import static java.util.Arrays.asList; @@ -38,43 +36,31 @@ @Component public class DirectoryServiceImpl implements DirectoryService { - private final ApplicationController controller; - private final FileBrowseService fileBrowser; - private final Current current; - private final PathResolverService pathResolver; - private final StoredConfigBean storedConfigBean; + @Autowired + private ApplicationController controller; + @Autowired + private FileBrowseService fileBrowser; + @Autowired + private ThreadService threadService; + @Autowired + private PathMapper pathMapper; + @Autowired + private Current current; + @Autowired + private StoredConfigBean storedConfigBean; private final Logger logger = LoggerFactory.getLogger(DirectoryService.class); private Optional workingDirectory = Optional.of(IOHelper.getPath(System.getProperty("user.home"))); private Optional initialDirectory = Optional.empty(); - private Supplier pathSaveSupplier; - private final FileWatchService fileWatchService; - private final ThreadService threadService; - private final PathMapper pathMapper; - - - @Autowired - public DirectoryServiceImpl(final ApplicationController controller, final FileBrowseService fileBrowser, final Current current, PathResolverService pathResolver, StoredConfigBean storedConfigBean, FileWatchService fileWatchService, ThreadService threadService, PathMapper pathMapper) { - this.controller = controller; - this.fileBrowser = fileBrowser; - this.current = current; - this.pathResolver = pathResolver; - this.storedConfigBean = storedConfigBean; - this.fileWatchService = fileWatchService; - this.threadService = threadService; - this.pathMapper = pathMapper; - - pathSaveSupplier = () -> { - final FileChooser chooser = newFileChooser("Save Document"); - chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Asciidoc", "*.adoc", "*.asciidoc", "*.asc", "*.ad", "*.txt", "*.*")); - chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Markdown", "*.md", "*.markdown", "*.txt", "*.*")); - File file = chooser.showSaveDialog(null); - return Objects.nonNull(file) ? file.toPath() : null; - }; - - } + private Supplier pathSaveSupplier = () -> { + final FileChooser chooser = newFileChooser("Save Document"); + chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Asciidoc", "*.adoc", "*.asciidoc", "*.asc", "*.ad", "*.txt", "*.*")); + chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Markdown", "*.md", "*.markdown", "*.txt", "*.*")); + File file = chooser.showSaveDialog(null); + return Objects.nonNull(file) ? file.toPath() : null; + }; @Override public DirectoryChooser newDirectoryChooser(String title) { @@ -314,13 +300,13 @@ public Path findPathInCurrentOrWorkDir(String uri) { .map(path -> path.resolve(uri)) .filter(Files::exists) .findFirst() - .orElseGet(() -> null); + .orElse(null); } @Override public Path findPathInPublic(String finalUri) { - List uris = asList(finalUri, finalUri.replaceFirst("/", "")).stream().collect(Collectors.toList()); + List uris = new ArrayList<>(asList(finalUri, finalUri.replaceFirst("/", ""))); Path result = null; for (String uri : uris) { Path configPath = controller.getConfigPath().resolve("public"); @@ -360,21 +346,11 @@ public Path findPathInWorkdirOrLookup(Path uri) { } if (optional.isPresent()) { - Path currentParent = optional.get(); - - while (true) { - - if (Objects.nonNull(currentParent)) { - Path candidate = currentParent.resolve(uri); - if (Files.exists(candidate)) { - return candidate; - } else { - currentParent = currentParent.getParent(); - } - } else { - break; + for (Path currentParent = optional.get(); Objects.nonNull(currentParent); currentParent = currentParent.getParent()) { + Path candidate = currentParent.resolve(uri); + if (Files.exists(candidate)) { + return candidate; } - } } diff --git a/src/main/java/com/kodedu/service/impl/FileWatchServiceImpl.java b/src/main/java/com/kodedu/service/impl/FileWatchServiceImpl.java index 477777ef6..ac5733473 100644 --- a/src/main/java/com/kodedu/service/impl/FileWatchServiceImpl.java +++ b/src/main/java/com/kodedu/service/impl/FileWatchServiceImpl.java @@ -95,6 +95,8 @@ private void watchPathChanges() { reCreateWatchService(); } + // This should keep running to keep track of files changes + // Must be implemented as an active check due to the way Java handles listening to file changes while (true) { if (Objects.isNull(watcher)) { @@ -118,6 +120,7 @@ private void watchPathChanges() { } List> watchEvents = watchKey.pollEvents(); + watchKey.reset(); boolean updateFsView = false; for (WatchEvent event : watchEvents) { @@ -137,14 +140,9 @@ private void watchPathChanges() { } } } - watchKey.reset(); - } else if (kind == ENTRY_MODIFY && event.count() > 1) { - watchKey.reset(); - } else { + } else if (kind != ENTRY_MODIFY || (kind == ENTRY_MODIFY && event.count() == 0)) { updateFsView = true; - watchKey.reset(); } - } if (updateFsView) { @@ -159,7 +157,6 @@ private void watchPathChanges() { } fileBrowseService.refreshPathToTree(path, changedPath); } - } } diff --git a/src/main/java/com/kodedu/service/ui/impl/FileBrowseServiceImpl.java b/src/main/java/com/kodedu/service/ui/impl/FileBrowseServiceImpl.java index a43b46469..eaf6176cd 100644 --- a/src/main/java/com/kodedu/service/ui/impl/FileBrowseServiceImpl.java +++ b/src/main/java/com/kodedu/service/ui/impl/FileBrowseServiceImpl.java @@ -285,33 +285,26 @@ public void searchUpAndSelect(String text) { ListIterator> listIterator = foundItems.listIterator(); - while (true) { - - if (Objects.isNull(searchFoundItem)) { - if (listIterator.hasNext()) { - searchFoundItem = listIterator.next(); - } - break; - } - + if (Objects.isNull(searchFoundItem)) { if (listIterator.hasNext()) { - TreeItem next = listIterator.next(); - if (next.getValue().equals(searchFoundItem.getValue())) { - if (listIterator.hasNext()) { - TreeItem nexted = listIterator.next(); + searchFoundItem = listIterator.next(); + } + } + while (listIterator.hasNext()) { + TreeItem next = listIterator.next(); + if (next.getValue().equals(searchFoundItem.getValue())) { + if (listIterator.hasNext()) { + TreeItem nexted = listIterator.next(); - if (next == nexted) { - if (listIterator.hasNext()) { - nexted = listIterator.next(); - } + if (next == nexted) { + if (listIterator.hasNext()) { + nexted = listIterator.next(); } - - searchFoundItem = nexted; - break; } + + searchFoundItem = nexted; + break; } - } else { - break; } } @@ -332,34 +325,23 @@ public void searchDownAndSelect(String text) { ListIterator> listIterator = foundItems.listIterator(); - while (true) { - - if (Objects.isNull(searchFoundItem)) { - if (listIterator.hasPrevious()) { - searchFoundItem = listIterator.previous(); - } - - break; + if (Objects.isNull(searchFoundItem)) { + if (listIterator.hasNext()) { + searchFoundItem = listIterator.next(); } + } else { - if (listIterator.hasNext()) { + while (listIterator.hasNext()) { TreeItem next = listIterator.next(); - if (next.getValue().equals(searchFoundItem.getValue())) { - if (listIterator.hasPrevious()) { - TreeItem previous = listIterator.previous(); - if (next == previous) { - if (listIterator.hasPrevious()) { - previous = listIterator.previous(); - } - } - searchFoundItem = previous; - break; + if (next.getValue().equals(searchFoundItem.getValue()) && listIterator.hasPrevious()) { + TreeItem previous = listIterator.previous(); + if (next == previous && listIterator.hasPrevious()) { + previous = listIterator.previous(); } + searchFoundItem = previous; + break; } - } else { - break; } - } focusFoundItem(searchFoundItem); @@ -439,7 +421,6 @@ public void searchAndSelect(String text) { ListIterator> listIterator = foundItems.listIterator(); - if (Objects.isNull(searchFoundItem)) { if (listIterator.hasNext()) { searchFoundItem = listIterator.next();