Skip to content

Commit

Permalink
Add support for jumping in ordered author list by typing letters (#6440)
Browse files Browse the repository at this point in the history
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
  • Loading branch information
leitianjian and calixtus authored May 11, 2020
1 parent 754c7ee commit 100f731
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We created a new install screen for macOS. [#5759](https://github.com/JabRef/jabref/issues/5759)
- We implemented an option to download fulltext files while importing. [#6381](https://github.com/JabRef/jabref/pull/6381)
- We fixed the bug when strike the delete key in the text field. [#6421](https://github.com/JabRef/jabref/issues/6421)
- We added support for jumping to target entry when typing letter/digit after sorting a column in maintable [#6146](https://github.com/JabRef/jabref/issues/6146)

### Changed

Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import javafx.collections.ListChangeListener;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
Expand Down Expand Up @@ -57,11 +58,21 @@ public class MainTable extends TableView<BibEntryTableViewModel> {
private final ImportHandler importHandler;
private final CustomLocalDragboard localDragboard;

private long lastKeyPressTime;
private String columnSearchTerm;

public MainTable(MainTableDataModel model, JabRefFrame frame,
BasePanel panel, BibDatabaseContext database,
MainTablePreferences preferences, ExternalFileTypes externalFileTypes, KeyBindingRepository keyBindingRepository) {
super();

this.setOnKeyTyped(key -> {
if (this.getSortOrder().isEmpty()) {
return;
}
this.jumpToSearchKey(getSortOrder().get(0), key);
});

this.model = model;
this.database = Objects.requireNonNull(database);

Expand Down Expand Up @@ -123,6 +134,40 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
database.getDatabase().registerListener(this);
}

/**
* This is called, if a user starts typing some characters into the keyboard with focus on main table.
* The {@link MainTable} will scroll to the cell with the same starting column value and typed string
*
* @param sortedColumn The sorted column in {@link MainTable}
* @param keyEvent The pressed character
*/

private void jumpToSearchKey(TableColumn<BibEntryTableViewModel, ?> sortedColumn, KeyEvent keyEvent) {
if (keyEvent.getCharacter() == null || sortedColumn == null) {
return;
}

if (System.currentTimeMillis() - lastKeyPressTime < 700) {
columnSearchTerm += keyEvent.getCharacter().toLowerCase();
} else {
columnSearchTerm = keyEvent.getCharacter().toLowerCase();
}

lastKeyPressTime = System.currentTimeMillis();

this.getItems().stream()
.filter(item -> Optional.ofNullable(sortedColumn.getCellObservableValue(item).getValue())
.map(Object::toString)
.orElse("")
.toLowerCase()
.startsWith(columnSearchTerm))
.findFirst()
.ifPresent(item -> {
this.scrollTo(item);
this.clearAndSelect(item.getEntry());
});
}

@Subscribe
public void listen(EntriesAddedEvent event) {
DefaultTaskExecutor.runInJavaFXThread(() -> clearAndSelect(event.getFirstEntry()));
Expand Down

0 comments on commit 100f731

Please sign in to comment.