Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open "near-by" .bib file #12172

Merged
merged 12 commits into from
Nov 13, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added an importer for SSRN URLs. [#12021](https://github.com/JabRef/jabref/pull/12021)
- We added a compare button to the duplicates in the citation relations tab to open the "Possible duplicate entries" window. [#11192](https://github.com/JabRef/jabref/issues/11192)
- We added automatic browser extension install on Windows for Chrome and Edge. [#6076](https://github.com/JabRef/jabref/issues/6076)
- We added support to automatically open a `.bib` file in the current/parent folder if no other library is opened. [koppor#377](https://github.com/koppor/jabref/issues/377)
- We added a search bar for filtering keyboard shortcuts. [#11686](https://github.com/JabRef/jabref/issues/11686)
- By double clicking on a local citation in the Citation Relations Tab you can now jump the linked entry. [#11955](https://github.com/JabRef/jabref/pull/11955)
- We use the menu icon for background tasks as a progress indicator to visualise an import's progress when dragging and dropping several PDF files into the main table. [#12072](https://github.com/JabRef/jabref/pull/12072)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public ArgumentProcessor(String[] args,
}

/**
* Will open a file (like {@link #importFile(String)}, but will also request JabRef to focus on this database.
* Will open a file (like {@link #importFile(String)}, but will also request JabRef to focus on this library.
*
* @return ParserResult with setToOpenTab(true)
*/
Expand Down
47 changes: 46 additions & 1 deletion src/main/java/org/jabref/gui/frame/JabRefFrameViewModel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jabref.gui.frame;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -30,6 +32,7 @@
import org.jabref.logic.UiCommand;
import org.jabref.logic.ai.AiService;
import org.jabref.logic.importer.ImportCleanup;
import org.jabref.logic.importer.OpenDatabase;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.shared.DatabaseNotSupportedException;
Expand Down Expand Up @@ -146,7 +149,26 @@ public boolean close() {
public void handleUiCommands(List<UiCommand> uiCommands) {
LOGGER.debug("Handling UI commands {}", uiCommands);
if (uiCommands.isEmpty()) {
return;
if (tabContainer.getLibraryTabs().isEmpty()) {
Optional<Path> firstBibFile = firstBibFile();
if (firstBibFile.isPresent()) {
ParserResult parserResult;
try {
parserResult = OpenDatabase.loadDatabase(
firstBibFile.get(),
preferences.getImportFormatPreferences(),
fileUpdateMonitor);
} catch (IOException e) {
LOGGER.error("Could not open bib file {}", firstBibFile.get(), e);
return;
}
uiCommands = List.of(new UiCommand.OpenDatabases(new ArrayList<>(List.of(parserResult))));
} else {
return;
}
} else {
return;
}
}

// Handle blank workspace
Expand Down Expand Up @@ -180,6 +202,29 @@ public void handleUiCommands(List<UiCommand> uiCommands) {
});
}

private Optional<Path> firstBibFile() {
Path currentDir = Path.of("").toAbsolutePath();

while (currentDir != null) {
try {
Optional<Path> bibFile = Files.list(currentDir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks horrible. Did you use chatgpt? Better use Files.find that does all this under the hood already
https://mkyong.com/java/java-files-find-examples/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also climbing up!!!

I search x/y/z. If there not found, go one dir up.

I can also do recursion...

I do NOT want list all files form root (e.g.., / or c:) and then filter. This takes too long!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I search x/y/z. If there not found, go one dir up.

This does not make sense.
I assume a scenario with JabRef portable. You have the bib file next to JabRef.exe and it will load it. This is fine.
But not randomly searching folders up and up or down

And additionally, this features makes so sense under Mac as you have the Appplications folder and all .app apps.
You would never place a file there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My use case:

image

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

.filter(path -> path.toString().endsWith(".bib"))
.findFirst();
if (bibFile.isPresent()) {
return bibFile;
}
currentDir = currentDir.getParent();
} catch (IOException e) {
LOGGER.error("Could not crawl for first bib file {}", currentDir, e);
return Optional.empty();
}
}
return Optional.empty();
}

/// Opens the libraries given in `parserResults`. This list needs to be modifiable, because invalidDatabases are removed.
///
/// @param parserResults A modifiable list of parser results
private void openDatabases(List<ParserResult> parserResults) {
final List<ParserResult> toOpenTab = new ArrayList<>();

Expand Down
Loading