forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request JabRef#9647 from MaryJml/record_search_history
Record search history
- Loading branch information
Showing
7 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/org/jabref/gui/search/SearchFieldRightClickMenu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.jabref.gui.search; | ||
|
||
import javafx.scene.control.ContextMenu; | ||
import javafx.scene.control.Menu; | ||
import javafx.scene.control.MenuItem; | ||
import javafx.scene.control.SeparatorMenuItem; | ||
|
||
import org.jabref.gui.StateManager; | ||
import org.jabref.gui.actions.ActionFactory; | ||
import org.jabref.gui.actions.SimpleCommand; | ||
import org.jabref.gui.actions.StandardActions; | ||
import org.jabref.gui.edit.EditAction; | ||
import org.jabref.gui.keyboard.KeyBindingRepository; | ||
import org.jabref.logic.l10n.Localization; | ||
|
||
import org.controlsfx.control.textfield.CustomTextField; | ||
|
||
public class SearchFieldRightClickMenu { | ||
public static ContextMenu create(KeyBindingRepository keyBindingRepository, | ||
StateManager stateManager, | ||
CustomTextField searchField) { | ||
ActionFactory factory = new ActionFactory(keyBindingRepository); | ||
ContextMenu contextMenu = new ContextMenu(); | ||
|
||
contextMenu.getItems().addAll( | ||
factory.createMenuItem(StandardActions.UNDO, new EditAction(StandardActions.UNDO, null, stateManager)), | ||
factory.createMenuItem(StandardActions.REDO, new EditAction(StandardActions.REDO, null, stateManager)), | ||
factory.createMenuItem(StandardActions.CUT, new EditAction(StandardActions.CUT, null, stateManager)), | ||
factory.createMenuItem(StandardActions.COPY, new EditAction(StandardActions.COPY, null, stateManager)), | ||
factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, null, stateManager)), | ||
factory.createMenuItem(StandardActions.DELETE, new EditAction(StandardActions.DELETE, null, stateManager)), | ||
|
||
new SeparatorMenuItem(), | ||
|
||
factory.createMenuItem(StandardActions.SELECT_ALL, new EditAction(StandardActions.SELECT_ALL, null, stateManager)), | ||
createSearchFromHistorySubMenu(factory, stateManager, searchField) | ||
); | ||
|
||
return contextMenu; | ||
} | ||
|
||
private static Menu createSearchFromHistorySubMenu(ActionFactory factory, | ||
StateManager stateManager, | ||
CustomTextField searchField) { | ||
Menu searchFromHistorySubMenu = factory.createMenu(() -> Localization.lang("Search from history...")); | ||
|
||
int num = stateManager.getLastSearchHistory(10).size(); | ||
if (num == 0) { | ||
MenuItem item = new MenuItem(Localization.lang("your search history is empty")); | ||
searchFromHistorySubMenu.getItems().addAll(item); | ||
} else { | ||
for (int i = 0; i < num; i++) { | ||
int finalI = i; | ||
MenuItem item = factory.createMenuItem(() -> stateManager.getLastSearchHistory(10).get(finalI), new SimpleCommand() { | ||
@Override | ||
public void execute() { | ||
searchField.setText(stateManager.getLastSearchHistory(10).get(finalI)); | ||
} | ||
}); | ||
searchFromHistorySubMenu.getItems().addAll(item); | ||
} | ||
MenuItem clear = factory.createMenuItem(() -> Localization.lang("Clear history"), new SimpleCommand() { | ||
@Override | ||
public void execute() { | ||
stateManager.clearSearchHistory(); | ||
} | ||
}); | ||
searchFromHistorySubMenu.getItems().addAll(new SeparatorMenuItem(), clear); | ||
} | ||
return searchFromHistorySubMenu; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/org/jabref/gui/search/GetLastSearchHistoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.jabref.gui.search; | ||
|
||
import java.util.List; | ||
|
||
import javafx.stage.Stage; | ||
|
||
import org.jabref.gui.StateManager; | ||
import org.jabref.testutils.category.GUITest; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.testfx.framework.junit5.ApplicationExtension; | ||
import org.testfx.framework.junit5.Start; | ||
|
||
@GUITest | ||
@ExtendWith(ApplicationExtension.class) | ||
public class GetLastSearchHistoryTest { | ||
@Start | ||
void onStart(Stage stage) { | ||
// Needed to init JavaFX thread | ||
stage.show(); | ||
} | ||
|
||
@Test | ||
void testGetLastSearchHistory() { | ||
StateManager stateManager = new StateManager(); | ||
stateManager.addSearchHistory("test1"); | ||
stateManager.addSearchHistory("test2"); | ||
stateManager.addSearchHistory("test3"); | ||
List<String> lastSearchHistory = stateManager.getLastSearchHistory(2); | ||
List<String> expected = List.of("test2", "test3"); | ||
|
||
Assertions.assertEquals(expected, lastSearchHistory); | ||
} | ||
|
||
@Test | ||
void testduplicateSearchHistory() { | ||
StateManager stateManager = new StateManager(); | ||
stateManager.addSearchHistory("test1"); | ||
stateManager.addSearchHistory("test2"); | ||
stateManager.addSearchHistory("test3"); | ||
stateManager.addSearchHistory("test1"); | ||
List<String> lastSearchHistory = stateManager.getWholeSearchHistory(); | ||
List<String> expected = List.of("test2", "test3", "test1"); | ||
|
||
Assertions.assertEquals(expected, lastSearchHistory); | ||
} | ||
|
||
@Test | ||
void testclearSearchHistory() { | ||
StateManager stateManager = new StateManager(); | ||
stateManager.addSearchHistory("test1"); | ||
stateManager.addSearchHistory("test2"); | ||
stateManager.addSearchHistory("test3"); | ||
List<String> lastSearchHistory = stateManager.getWholeSearchHistory(); | ||
List<String> expected = List.of("test1", "test2", "test3"); | ||
Assertions.assertEquals(expected, lastSearchHistory); | ||
stateManager.clearSearchHistory(); | ||
lastSearchHistory = stateManager.getWholeSearchHistory(); | ||
expected = List.of(); | ||
Assertions.assertEquals(expected, lastSearchHistory); | ||
} | ||
} |