-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Open "near-by" .bib file #12172
Changes from 2 commits
14c0f62
5ff5a0d
8469ffd
5c799a4
35e30d1
d0ae6d1
15e9101
114f2f4
60321f0
63f8e28
2199209
26aaad9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This does not make sense. And additionally, this features makes so sense under Mac as you have the Appplications folder and all .app apps. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
.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<>(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loading
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did the other way round 😅