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

Add option to clear recent libraries #10027

Merged
merged 10 commits into from
Jun 25, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added a cleanup activity that identifies a URL or a last-visited-date in the `note` field and moves it to the `url` and `urldate` field respectively. [koppor#216](https://github.com/koppor/jabref/issues/216)
- We enabled the user to change the name of a field in a custom entry type by double-clicking on it. [#9840](https://github.com/JabRef/jabref/issues/9840)
- We integrated two mail actions ("As Email" and "To Kindle") under a new "Send" option in the right-click & Tools menus. The Kindle option creates an email targeted to the user's Kindle email, which can be set in preferences under "External programs" [#6186](https://github.com/JabRef/jabref/issues/6186)
- We added an option to clear recent libraries' history. [#10003](https://github.com/JabRef/jabref/issues/10003)

### Changed

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/jabref/gui/menus/FileHistoryMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.input.KeyEvent;

import org.jabref.gui.DialogService;
Expand All @@ -14,13 +15,18 @@

public class FileHistoryMenu extends Menu {

protected final MenuItem clearRecentLibraries;
private final FileHistory history;
private final DialogService dialogService;
private final OpenDatabaseAction openDatabaseAction;

public FileHistoryMenu(FileHistory fileHistory, DialogService dialogService, OpenDatabaseAction openDatabaseAction) {
setText(Localization.lang("Recent libraries"));

this.clearRecentLibraries = new MenuItem();
clearRecentLibraries.setText(Localization.lang("Clear recent libraries"));
clearRecentLibraries.setOnAction(event -> clearLibrariesHistory());

this.history = fileHistory;
this.dialogService = dialogService;
this.openDatabaseAction = openDatabaseAction;
Expand Down Expand Up @@ -65,6 +71,10 @@ private void setItems() {
for (int index = 0; index < history.size(); index++) {
addItem(history.get(index), index + 1);
}
getItems().addAll(
new SeparatorMenuItem(),
clearRecentLibraries
);
}

private void addItem(Path file, int num) {
Expand All @@ -91,4 +101,9 @@ public void openFile(Path file) {
}
openDatabaseAction.openFile(file);
}

public void clearLibrariesHistory() {
history.clear();
setDisable(true);
Copy link
Member

Choose a reason for hiding this comment

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

This would disable the button when recent libraries are empty, but I don't see any code that would re-enable it when recent library item is added.

Copy link
Contributor Author

@dkokkotas dkokkotas Jun 24, 2023

Choose a reason for hiding this comment

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

I assume that FileHistoryMenu#newFile(Path file) invocation within the OpenDatabaseAction class, will re-enable the item as the aforementioned method declares : setDisable(false). To my understanding, the menu Recent Libraries shall be disabled unless the user opens a library.

I also tested it manually and the system's behavior is as expected. When we open a new library the Recent libraries menu will be correctly enabled and the recent libraries will be listed.

}
}
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,7 @@ New\ sublibrary\ based\ on\ AUX\ file=New sublibrary based on AUX file
Push\ entries\ to\ external\ application\ (%0)=Push entries to external application (%0)
Quit=Quit
Recent\ libraries=Recent libraries
Clear\ recent\ libraries=Clear recent libraries
Set\ up\ general\ fields=Set up general fields
View\ change\ log=View change log
View\ event\ log=View event log
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/jabref/gui/menus/FileHistoryMenuTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jabref.gui.menus;

import java.nio.file.Path;

import org.jabref.gui.DialogService;
import org.jabref.gui.importer.actions.OpenDatabaseAction;
import org.jabref.logic.util.io.FileHistory;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.testfx.framework.junit5.ApplicationExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.MockitoAnnotations.openMocks;

@ExtendWith(ApplicationExtension.class)
public class FileHistoryMenuTest {
private static final String BIBTEX_LIBRARY_PATH = "src/test/resources/org/jabref/";

private FileHistoryMenu fileHistoryMenu;
@Mock
private FileHistory fileHistory;
@Mock
private DialogService dialogService;
@Mock
private OpenDatabaseAction openDatabaseAction;

@BeforeEach
public void setUp() {
openMocks(this);
fileHistoryMenu = new FileHistoryMenu(fileHistory, dialogService, openDatabaseAction);
}

@Test
void recentLibrariesAreCleared() {
fileHistoryMenu.newFile(Path.of(BIBTEX_LIBRARY_PATH.concat("bibtexFiles/test.bib")));

fileHistoryMenu.clearLibrariesHistory();
assertTrue(fileHistoryMenu.isDisable());
assertEquals(0, fileHistory.size());
}
}