Skip to content

Commit

Permalink
Merge branch 'main' into fix-issue-11189-part-00-refactor-citation-re…
Browse files Browse the repository at this point in the history
…lation-tab-logic
  • Loading branch information
Alexandre CREMIEUX authored and Alexandre CREMIEUX committed Feb 7, 2025
2 parents 7af20c3 + 4c9c821 commit eb3dfd8
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

### Added

- We added a feature for copying entries to libraries, available via the context menu, with an option to include cross-references. [#12374](https://github.com/JabRef/jabref/pull/12374)
- We introduced a settings parameters to manage citations' relations local storage time-to-live with a default value set to 30 days. [#11189](https://github.com/JabRef/jabref/issues/11189)

### Changed

- We moved the "Generate a new key for imported entries" option from the "Web search" tab to the "Citation key generator" tab in preferences. [#12436](https://github.com/JabRef/jabref/pull/12436)
- We improved the offline parsing of BibTeX data from PDF-documents. [#12278](https://github.com/JabRef/jabref/issues/12278)
- The tab bar is now hidden when only one library is open. [#9971](https://github.com/JabRef/jabref/issues/9971)
- We improved the citations relations caching by implementing a two levels cache aside strategy including offline storage. [#11189](https://github.com/JabRef/jabref/issues/11189)
Expand All @@ -28,6 +30,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where removing the sort from the table did not restore the original order. [#12371](https://github.com/JabRef/jabref/pull/12371)
- We fixed an issue where JabRef icon merges with dark background [#7771](https://github.com/JabRef/jabref/issues/7771)
- We fixed an issue where an entry's group was no longer highlighted on selection [#12413](https://github.com/JabRef/jabref/issues/12413)
- We fixed an issue where BibTeX Strings were not included in the backup file [#12462](https://github.com/JabRef/jabref/issues/12462)

### Removed

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ plugins {

id 'idea'

id 'org.openrewrite.rewrite' version '7.0.0'
id 'org.openrewrite.rewrite' version '7.0.5'

id "org.itsallcode.openfasttrace" version "3.0.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.BibtexString;
import org.jabref.model.metadata.SaveOrder;
import org.jabref.model.metadata.SelfContainedSaveOrder;

Expand All @@ -59,7 +60,7 @@ public class BackupManager {

private static final int DELAY_BETWEEN_BACKUP_ATTEMPTS_IN_SECONDS = 19;

private static Set<BackupManager> runningInstances = new HashSet<>();
private static final Set<BackupManager> RUNNING_INSTANCES = new HashSet<>();

private final BibDatabaseContext bibDatabaseContext;
private final CliPreferences preferences;
Expand Down Expand Up @@ -109,7 +110,7 @@ static Optional<Path> getLatestBackupPath(Path originalPath, Path backupDir) {
public static BackupManager start(LibraryTab libraryTab, BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, CliPreferences preferences) {
BackupManager backupManager = new BackupManager(libraryTab, bibDatabaseContext, entryTypesManager, preferences);
backupManager.startBackupTask(preferences.getFilePreferences().getBackupDirectory());
runningInstances.add(backupManager);
RUNNING_INSTANCES.add(backupManager);
return backupManager;
}

Expand All @@ -119,7 +120,7 @@ public static BackupManager start(LibraryTab libraryTab, BibDatabaseContext bibD
* @param bibDatabaseContext Associated {@link BibDatabaseContext}
*/
public static void discardBackup(BibDatabaseContext bibDatabaseContext, Path backupDir) {
runningInstances.stream().filter(instance -> instance.bibDatabaseContext == bibDatabaseContext).forEach(backupManager -> backupManager.discardBackup(backupDir));
RUNNING_INSTANCES.stream().filter(instance -> instance.bibDatabaseContext == bibDatabaseContext).forEach(backupManager -> backupManager.discardBackup(backupDir));
}

/**
Expand All @@ -130,8 +131,8 @@ public static void discardBackup(BibDatabaseContext bibDatabaseContext, Path bac
* @param backupDir The path to the backup directory
*/
public static void shutdown(BibDatabaseContext bibDatabaseContext, Path backupDir, boolean createBackup) {
runningInstances.stream().filter(instance -> instance.bibDatabaseContext == bibDatabaseContext).forEach(backupManager -> backupManager.shutdown(backupDir, createBackup));
runningInstances.removeIf(instance -> instance.bibDatabaseContext == bibDatabaseContext);
RUNNING_INSTANCES.stream().filter(instance -> instance.bibDatabaseContext == bibDatabaseContext).forEach(backupManager -> backupManager.shutdown(backupDir, createBackup));
RUNNING_INSTANCES.removeIf(instance -> instance.bibDatabaseContext == bibDatabaseContext);
}

/**
Expand Down Expand Up @@ -268,6 +269,9 @@ void performBackup(Path backupPath) {
.map(BibEntry.class::cast)
.toList();
BibDatabase bibDatabaseClone = new BibDatabase(list);
bibDatabaseContext.getDatabase().getStringValues().stream().map(BibtexString::clone)
.map(BibtexString.class::cast)
.forEach(bibDatabaseClone::addString);
BibDatabaseContext bibDatabaseContextClone = new BibDatabaseContext(bibDatabaseClone, bibDatabaseContext.getMetaData());

Charset encoding = bibDatabaseContext.getMetaData().getEncoding().orElse(StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private void addToGroups(List<BibEntry> entries, Collection<GroupTreeNode> group
* @param entries entries to generate keys for
*/
private void generateKeys(List<BibEntry> entries) {
if (!preferences.getImporterPreferences().isGenerateNewKeyOnImport()) {
if (!preferences.getImporterPreferences().shouldGenerateNewKeyOnImport()) {
return;
}
CitationKeyGenerator keyGenerator = new CitationKeyGenerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</padding>
</CheckBox>
<CheckBox fx:id="generateOnSave" text="%Generate keys before saving (for entries without a key)"/>
<CheckBox fx:id="generateNewKeyOnImport" text="%Generate a new key for imported entries (overwriting their default)"/>

<Label text="%Letters after duplicate generated keys"/>
<VBox spacing="10.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CitationKeyPatternTab extends AbstractPreferenceTabView<CitationKey
@FXML private CheckBox overwriteAllow;
@FXML private CheckBox overwriteWarning;
@FXML private CheckBox generateOnSave;
@FXML private CheckBox generateNewKeyOnImport;
@FXML private RadioButton letterStartA;
@FXML private RadioButton letterStartB;
@FXML private RadioButton letterAlwaysAdd;
Expand All @@ -45,11 +46,12 @@ public String getTabName() {
}

public void initialize() {
this.viewModel = new CitationKeyPatternTabViewModel(preferences.getCitationKeyPatternPreferences());
this.viewModel = new CitationKeyPatternTabViewModel(preferences.getCitationKeyPatternPreferences(), preferences.getImporterPreferences());

overwriteAllow.selectedProperty().bindBidirectional(viewModel.overwriteAllowProperty());
overwriteWarning.selectedProperty().bindBidirectional(viewModel.overwriteWarningProperty());
generateOnSave.selectedProperty().bindBidirectional(viewModel.generateOnSaveProperty());
generateNewKeyOnImport.selectedProperty().bindBidirectional(viewModel.generateKeyOnImportProperty());
letterStartA.selectedProperty().bindBidirectional(viewModel.letterStartAProperty());
letterStartB.selectedProperty().bindBidirectional(viewModel.letterStartBProperty());
letterAlwaysAdd.selectedProperty().bindBidirectional(viewModel.letterAlwaysAddProperty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import org.jabref.gui.preferences.PreferenceTabViewModel;
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.citationkeypattern.GlobalCitationKeyPatterns;
import org.jabref.logic.importer.ImporterPreferences;

public class CitationKeyPatternTabViewModel implements PreferenceTabViewModel {

private final BooleanProperty overwriteAllowProperty = new SimpleBooleanProperty();
private final BooleanProperty overwriteWarningProperty = new SimpleBooleanProperty();
private final BooleanProperty generateOnSaveProperty = new SimpleBooleanProperty();
private final BooleanProperty generateKeyOnImportProperty = new SimpleBooleanProperty();
private final BooleanProperty letterStartAProperty = new SimpleBooleanProperty();
private final BooleanProperty letterStartBProperty = new SimpleBooleanProperty();
private final BooleanProperty letterAlwaysAddProperty = new SimpleBooleanProperty();
Expand All @@ -36,15 +38,25 @@ public class CitationKeyPatternTabViewModel implements PreferenceTabViewModel {

private final CitationKeyPatternPreferences keyPatternPreferences;

public CitationKeyPatternTabViewModel(CitationKeyPatternPreferences keyPatternPreferences) {
/**
* The preference for whether to use the key generator on import is different from how it is configured.
* In the UI, there is no better place to put the option than the Citation Key Generator tab.
* However, shifting the preference to {@link CitationKeyPatternPreferences} would break the abstraction or hierarchy.
* Hence, we keep the preference in {@link ImporterPreferences}, but for the UI, we initialize it here.
*/
private final ImporterPreferences importerPreferences;

public CitationKeyPatternTabViewModel(CitationKeyPatternPreferences keyPatternPreferences, ImporterPreferences importerPreferences) {
this.keyPatternPreferences = keyPatternPreferences;
this.importerPreferences = importerPreferences;
}

@Override
public void setValues() {
overwriteAllowProperty.setValue(!keyPatternPreferences.shouldAvoidOverwriteCiteKey());
overwriteWarningProperty.setValue(keyPatternPreferences.shouldWarnBeforeOverwriteCiteKey());
generateOnSaveProperty.setValue(keyPatternPreferences.shouldGenerateCiteKeysBeforeSaving());
generateKeyOnImportProperty.setValue(importerPreferences.shouldGenerateNewKeyOnImport());

if (keyPatternPreferences.getKeySuffix()
== CitationKeyPatternPreferences.KeySuffix.ALWAYS) {
Expand Down Expand Up @@ -97,6 +109,7 @@ public void storeSettings() {
keyPatternPreferences.setAvoidOverwriteCiteKey(!overwriteAllowProperty.getValue());
keyPatternPreferences.setWarnBeforeOverwriteCiteKey(overwriteWarningProperty.getValue());
keyPatternPreferences.setGenerateCiteKeysBeforeSaving(generateOnSaveProperty.getValue());
importerPreferences.setGenerateNewKeyOnImport(generateKeyOnImportProperty.getValue());
keyPatternPreferences.setKeySuffix(keySuffix);
keyPatternPreferences.setKeyPatternRegex(keyPatternRegexProperty.getValue());
keyPatternPreferences.setKeyPatternReplacement(keyPatternReplacementProperty.getValue());
Expand All @@ -116,6 +129,10 @@ public BooleanProperty generateOnSaveProperty() {
return generateOnSaveProperty;
}

public BooleanProperty generateKeyOnImportProperty() {
return generateKeyOnImportProperty;
}

public BooleanProperty letterStartAProperty() {
return letterStartAProperty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

<Label styleClass="sectionHeader" text="%General"/>
<CheckBox fx:id="enableWebSearch" text="%Enable web search"/>
<CheckBox fx:id="generateNewKeyOnImport" text="%Generate a new key for imported entries (overwriting their default)"/>
<CheckBox fx:id="warnAboutDuplicatesOnImport" text="%Warn about duplicates on import"/>
<CheckBox fx:id="downloadLinkedOnlineFiles" text="%Download linked online files"/>
<CheckBox fx:id="keepDownloadUrl" text="%Store url for downloaded file" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
public class WebSearchTab extends AbstractPreferenceTabView<WebSearchTabViewModel> implements PreferencesTab {

@FXML private CheckBox enableWebSearch;
@FXML private CheckBox generateNewKeyOnImport;
@FXML private CheckBox warnAboutDuplicatesOnImport;
@FXML private CheckBox downloadLinkedOnlineFiles;
@FXML private CheckBox keepDownloadUrl;
Expand Down Expand Up @@ -75,7 +74,6 @@ public void initialize() {
this.viewModel = new WebSearchTabViewModel(preferences, dialogService, refAiEnabled);

enableWebSearch.selectedProperty().bindBidirectional(viewModel.enableWebSearchProperty());
generateNewKeyOnImport.selectedProperty().bindBidirectional(viewModel.generateKeyOnImportProperty());
warnAboutDuplicatesOnImport.selectedProperty().bindBidirectional(viewModel.warnAboutDuplicatesOnImportProperty());
downloadLinkedOnlineFiles.selectedProperty().bindBidirectional(viewModel.shouldDownloadLinkedOnlineFiles());
keepDownloadUrl.selectedProperty().bindBidirectional(viewModel.shouldKeepDownloadUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

public class WebSearchTabViewModel implements PreferenceTabViewModel {
private final BooleanProperty enableWebSearchProperty = new SimpleBooleanProperty();
private final BooleanProperty generateKeyOnImportProperty = new SimpleBooleanProperty();
private final BooleanProperty warnAboutDuplicatesOnImportProperty = new SimpleBooleanProperty();
private final BooleanProperty shouldDownloadLinkedOnlineFiles = new SimpleBooleanProperty();
private final BooleanProperty shouldkeepDownloadUrl = new SimpleBooleanProperty();
Expand Down Expand Up @@ -130,7 +129,6 @@ private void setupPlainCitationParsers(CliPreferences preferences) {
@Override
public void setValues() {
enableWebSearchProperty.setValue(importerPreferences.areImporterEnabled());
generateKeyOnImportProperty.setValue(importerPreferences.isGenerateNewKeyOnImport());
warnAboutDuplicatesOnImportProperty.setValue(importerPreferences.shouldWarnAboutDuplicatesOnImport());
shouldDownloadLinkedOnlineFiles.setValue(filePreferences.shouldDownloadLinkedFiles());
shouldkeepDownloadUrl.setValue(filePreferences.shouldKeepDownloadUrl());
Expand Down Expand Up @@ -163,7 +161,6 @@ public void setValues() {
@Override
public void storeSettings() {
importerPreferences.setImporterEnabled(enableWebSearchProperty.getValue());
importerPreferences.setGenerateNewKeyOnImport(generateKeyOnImportProperty.getValue());
importerPreferences.setWarnAboutDuplicatesOnImport(warnAboutDuplicatesOnImportProperty.getValue());
filePreferences.setDownloadLinkedFiles(shouldDownloadLinkedOnlineFiles.getValue());
filePreferences.setKeepDownloadUrl(shouldkeepDownloadUrl.getValue());
Expand All @@ -190,10 +187,6 @@ public BooleanProperty enableWebSearchProperty() {
return enableWebSearchProperty;
}

public BooleanProperty generateKeyOnImportProperty() {
return generateKeyOnImportProperty;
}

public ListProperty<PlainCitationParserChoice> plainCitationParsers() {
return plainCitationParsers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Response<AiMessage> generate(List<ChatMessage> list) {
.uri(URLUtil.createUri(fullUrl))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.timeout(Duration.ofSeconds(60))
.timeout(Duration.ofMinutes(1))
.build();

HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void setImporterEnabled(boolean importerEnabled) {
this.importerEnabled.set(importerEnabled);
}

public boolean isGenerateNewKeyOnImport() {
public boolean shouldGenerateNewKeyOnImport() {
return generateNewKeyOnImport.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,21 +354,18 @@ private void parseJabRefComment(Map<String, String> meta) {
// We remove all line breaks in the metadata
// These have been inserted to prevent too long lines when the file was saved, and are not part of the data.
String comment = buffer.toString().replaceAll("[\\x0d\\x0a]", "");
if (MetaData.META_FLAG.equals(comment.substring(0, Math.min(comment.length(), MetaData.META_FLAG.length())))) {
if (comment.startsWith(MetaData.META_FLAG)) {
String rest = comment.substring(MetaData.META_FLAG.length());
if (comment.startsWith(MetaData.META_FLAG)) {
String rest = comment.substring(MetaData.META_FLAG.length());

int pos = rest.indexOf(':');
int pos = rest.indexOf(':');

if (pos > 0) {
meta.put(rest.substring(0, pos), rest.substring(pos + 1));
if (pos > 0) {
meta.put(rest.substring(0, pos), rest.substring(pos + 1));

// meta comments are always re-written by JabRef and not stored in the file
dumpTextReadSoFarToString();
}
// meta comments are always re-written by JabRef and not stored in the file
dumpTextReadSoFarToString();
}
} else if (MetaData.ENTRYTYPE_FLAG
.equals(comment.substring(0, Math.min(comment.length(), MetaData.ENTRYTYPE_FLAG.length())))) {
} else if (comment.startsWith(MetaData.ENTRYTYPE_FLAG)) {
// A custom entry type can also be stored in a
// "@comment"
Optional<BibEntryType> typ = MetaDataParser.parseCustomEntryType(comment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void setUp() {
when(preferences.getImportFormatPreferences()).thenReturn(importFormatPreferences);

ImporterPreferences importerPreferences = mock(ImporterPreferences.class, Answers.RETURNS_DEEP_STUBS);
when(importerPreferences.isGenerateNewKeyOnImport()).thenReturn(false);
when(importerPreferences.shouldGenerateNewKeyOnImport()).thenReturn(false);
when(preferences.getImporterPreferences()).thenReturn(importerPreferences);

FieldPreferences fieldPreferences = mock(FieldPreferences.class);
Expand Down

0 comments on commit eb3dfd8

Please sign in to comment.