Skip to content

Commit

Permalink
feat: improve logic for when filemonitor is added (JabRef#10585)
Browse files Browse the repository at this point in the history
Made sure that filemonitor first is added when the user navigates to LaTeX Citation for the first time for each .bib tab, also added logic to remove the old listener whenever the directory target is changed

However, an issue is that listener seemingly is still watching over the old directory even though the listener has been removed
  • Loading branch information
VinterSallad committed Feb 28, 2024
1 parent c2370b8 commit 4eaf252
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ enum Status {
private LatexParserResult latexParserResult;
private BibEntry currentEntry;
private final FileUpdateMonitor fileUpdateMonitor;
private boolean hasListener;

public LatexCitationsTabViewModel(BibDatabaseContext databaseContext,
PreferencesService preferencesService,
Expand All @@ -74,6 +75,7 @@ public LatexCitationsTabViewModel(BibDatabaseContext databaseContext,
this.searchError = new SimpleStringProperty("");

this.fileUpdateMonitor = Globals.getFileUpdateMonitor();
this.hasListener = false;
}

public void init(BibEntry entry) {
Expand All @@ -82,6 +84,16 @@ public void init(BibEntry entry) {
currentEntry = entry;
Optional<String> citeKey = entry.getCitationKey();

// make sure that we only add the listener once at the beginning
if (!hasListener) {
try {
fileUpdateMonitor.addListenerForFile(directory.get(), this::refreshLatexDirectory);
hasListener = true;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

if (citeKey.isPresent()) {
startSearch(citeKey.get());
} else {
Expand Down Expand Up @@ -135,7 +147,6 @@ private Collection<Citation> searchAndParse(String citeKey) throws IOException {
.orElse(FileUtil.getInitialDirectory(databaseContext, preferencesService.getFilePreferences().getWorkingDirectory()));

if (latexParserResult == null || !newDirectory.equals(directory.get())) {
fileUpdateMonitor.addListenerForFile(newDirectory, this::refreshLatexDirectory);
directory.set(newDirectory);

if (!newDirectory.toFile().exists()) {
Expand Down Expand Up @@ -170,8 +181,16 @@ public void setLatexDirectory() {
DirectoryDialogConfiguration directoryDialogConfiguration = new DirectoryDialogConfiguration.Builder()
.withInitialDirectory(directory.get()).build();

dialogService.showDirectorySelectionDialog(directoryDialogConfiguration).ifPresent(selectedDirectory ->
databaseContext.getMetaData().setLatexFileDirectory(preferencesService.getFilePreferences().getUserAndHost(), selectedDirectory.toAbsolutePath()));
fileUpdateMonitor.removeListener(directory.get(), this::refreshLatexDirectory);

dialogService.showDirectorySelectionDialog(directoryDialogConfiguration).ifPresent(selectedDirectory -> {
databaseContext.getMetaData().setLatexFileDirectory(preferencesService.getFilePreferences().getUserAndHost(), selectedDirectory.toAbsolutePath());
try {
fileUpdateMonitor.addListenerForFile(selectedDirectory, this::refreshLatexDirectory);
} catch (IOException e) {
throw new RuntimeException(e);
}
});

init(currentEntry);
}
Expand Down

0 comments on commit 4eaf252

Please sign in to comment.