Skip to content

Commit d890eea

Browse files
committed
Refactor lists into class
1 parent fbb37fa commit d890eea

File tree

2 files changed

+88
-34
lines changed

2 files changed

+88
-34
lines changed

jabgui/src/main/java/org/jabref/gui/LibraryTab.java

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.nio.file.Path;
5-
import java.util.ArrayList;
65
import java.util.List;
76
import java.util.Objects;
87
import java.util.Optional;
@@ -119,9 +118,7 @@ public class LibraryTab extends Tab implements CommandSelectionTab {
119118
private final BibEntryTypesManager entryTypesManager;
120119
private final BooleanProperty changedProperty = new SimpleBooleanProperty(false);
121120
private final BooleanProperty nonUndoableChangeProperty = new SimpleBooleanProperty(false);
122-
private final List<BibEntry> previousEntries = new ArrayList<>();
123-
private final List<BibEntry> nextEntries = new ArrayList<>();
124-
private BibEntry currentlyShowing;
121+
private final NavigationHistory navigationHistory = new NavigationHistory();
125122
private final BooleanProperty canGoBackProperty = new SimpleBooleanProperty(false);
126123
private final BooleanProperty canGoForwardProperty = new SimpleBooleanProperty(false);
127124
private boolean backOrForwardNavigationActionTriggered = false;
@@ -983,56 +980,36 @@ public void resetChangedProperties() {
983980
}
984981

985982
public void back() {
986-
navigateToEntry(previousEntries, nextEntries);
983+
navigationHistory.back().ifPresent(this::navigateToEntry);
987984
}
988985

989986
public void forward() {
990-
navigateToEntry(nextEntries, previousEntries);
987+
navigationHistory.forward().ifPresent(this::navigateToEntry);
991988
}
992989

993-
private void navigateToEntry(List<BibEntry> sourceHistory, List<BibEntry> destinationHistory) {
994-
if (!sourceHistory.isEmpty()) {
995-
BibEntry toShow = sourceHistory.getLast();
996-
sourceHistory.removeLast();
997-
998-
// add current entry to destination history
999-
if (currentlyShowing != null) {
1000-
destinationHistory.add(currentlyShowing);
1001-
}
1002-
1003-
backOrForwardNavigationActionTriggered = true;
1004-
clearAndSelect(toShow);
1005-
updateNavigationState();
1006-
}
990+
private void navigateToEntry(BibEntry entry) {
991+
backOrForwardNavigationActionTriggered = true;
992+
clearAndSelect(entry);
993+
updateNavigationState();
1007994
}
1008995

1009996
public boolean canGoBack() {
1010-
return !previousEntries.isEmpty();
997+
return navigationHistory.canGoBack();
1011998
}
1012999

10131000
public boolean canGoForward() {
1014-
return !nextEntries.isEmpty();
1001+
return navigationHistory.canGoForward();
10151002
}
10161003

10171004
private void newEntryShowing(BibEntry entry) {
10181005
// skip history updates if this is from a back/forward operation
10191006
if (backOrForwardNavigationActionTriggered) {
1020-
currentlyShowing = entry;
10211007
backOrForwardNavigationActionTriggered = false;
1022-
updateNavigationState();
10231008
return;
10241009
}
10251010

1026-
nextEntries.clear(); // clear forward history when new entry selected in existing chronological sequence
1027-
1028-
if (!Objects.equals(entry, currentlyShowing)) {
1029-
// add the entry we are leaving to the history
1030-
if (currentlyShowing != null) {
1031-
previousEntries.add(currentlyShowing);
1032-
}
1033-
currentlyShowing = entry;
1034-
updateNavigationState();
1035-
}
1011+
navigationHistory.add(entry);
1012+
updateNavigationState();
10361013
}
10371014

10381015
/**
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.jabref.gui;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import java.util.Optional;
7+
8+
import org.jabref.model.entry.BibEntry;
9+
10+
/**
11+
* Manages the navigation history of viewed entries using two stacks.
12+
* This class encapsulates the logic of moving back and forward by maintaining a "back" stack for past entries
13+
* and a "forward" stack for future entries.
14+
*/
15+
public class NavigationHistory {
16+
private final List<BibEntry> previousEntries = new ArrayList<>();
17+
private final List<BibEntry> nextEntries = new ArrayList<>();
18+
private BibEntry currentEntry;
19+
20+
/**
21+
* Sets a new entry as the current one, clearing the forward history.
22+
* The previously current entry is moved to the back stack.
23+
*
24+
* @param entry The BibEntry to add to the history.
25+
*/
26+
public void add(BibEntry entry) {
27+
if (Objects.equals(currentEntry, entry)) {
28+
return;
29+
}
30+
31+
// a new selection invalidates the forward history
32+
nextEntries.clear();
33+
34+
if (currentEntry != null) {
35+
previousEntries.add(currentEntry);
36+
}
37+
currentEntry = entry;
38+
}
39+
40+
/**
41+
* Moves to the previous entry in the history.
42+
* The current entry is pushed to the forward stack, and the last entry from the back stack becomes current.
43+
*
44+
* @return An Optional containing the previous BibEntry, or an empty Optional if there's no history to go back to.
45+
*/
46+
public Optional<BibEntry> back() {
47+
if (canGoBack()) {
48+
nextEntries.add(currentEntry);
49+
currentEntry = previousEntries.removeLast();
50+
return Optional.of(currentEntry);
51+
}
52+
return Optional.empty();
53+
}
54+
55+
/**
56+
* Moves to the next entry in the history.
57+
* The current entry is pushed to the back stack, and the last entry from the forward stack becomes current.
58+
*
59+
* @return An Optional containing the next BibEntry, or an empty Optional if there is no "forward" history.
60+
*/
61+
public Optional<BibEntry> forward() {
62+
if (canGoForward()) {
63+
previousEntries.add(currentEntry);
64+
currentEntry = nextEntries.removeLast();
65+
return Optional.of(currentEntry);
66+
}
67+
return Optional.empty();
68+
}
69+
70+
public boolean canGoBack() {
71+
return !previousEntries.isEmpty();
72+
}
73+
74+
public boolean canGoForward() {
75+
return !nextEntries.isEmpty();
76+
}
77+
}

0 commit comments

Comments
 (0)