diff --git a/src/main/java/de/danielluedecke/zettelkasten/ZettelkastenView.java b/src/main/java/de/danielluedecke/zettelkasten/ZettelkastenView.java index 7421f8fa..a054e44e 100644 --- a/src/main/java/de/danielluedecke/zettelkasten/ZettelkastenView.java +++ b/src/main/java/de/danielluedecke/zettelkasten/ZettelkastenView.java @@ -36,7 +36,7 @@ import com.explodingpixels.macwidgets.*; import com.explodingpixels.widgets.TableUtils; import de.danielluedecke.zettelkasten.database.*; -import de.danielluedecke.zettelkasten.history.HistoryManager; +import de.danielluedecke.zettelkasten.history.History; import de.danielluedecke.zettelkasten.mac.MacSourceList; import de.danielluedecke.zettelkasten.mac.MacSourceTree; import de.danielluedecke.zettelkasten.mac.MacToolbarButton; @@ -112,7 +112,7 @@ public class ZettelkastenView extends FrameView implements WindowListener, DropT public SearchRequests searchRequests; private Daten data; - private HistoryManager historyManager; + private History historyManager; private final TasksData taskinfo; public final Bookmarks bookmarks; private final BibTeX bibtex; @@ -417,7 +417,6 @@ public ZettelkastenView(SingleFrameApplication app, Settings st, TasksData td) t bookmarks = new Bookmarks(this, settings); bibtex = new BibTeX(this, settings); data = new Daten(this, settings, settings.getSynonyms(), bibtex); - historyManager = new HistoryManager(this); display = new Display(this, historyManager); } else { // Handle the case where settings is null @@ -15160,11 +15159,11 @@ public Zettel getDisplayedZettel() { return null; } - public void setHistoryManager(HistoryManager historyManager) { + public void setHistoryManager(History historyManager) { this.historyManager = historyManager; } - public HistoryManager getHistoryManager() { + public History getHistoryManager() { return historyManager; } diff --git a/src/main/java/de/danielluedecke/zettelkasten/database/Daten.java b/src/main/java/de/danielluedecke/zettelkasten/database/Daten.java index 062a0eb5..b441b5ec 100644 --- a/src/main/java/de/danielluedecke/zettelkasten/database/Daten.java +++ b/src/main/java/de/danielluedecke/zettelkasten/database/Daten.java @@ -35,7 +35,7 @@ import de.danielluedecke.zettelkasten.CMakeFormImage; import de.danielluedecke.zettelkasten.EntryID; import de.danielluedecke.zettelkasten.ZettelkastenView; -import de.danielluedecke.zettelkasten.history.HistoryManager; +import de.danielluedecke.zettelkasten.history.History; import de.danielluedecke.zettelkasten.settings.Settings; import de.danielluedecke.zettelkasten.util.classes.Comparer; import de.danielluedecke.zettelkasten.util.Constants; @@ -126,14 +126,14 @@ public class Daten { * Indicates whether saving the data file was OK, or whether an error occurred. */ private boolean saveOk; + /** * This array stores all watched entries in the order the user "surfed" through * the entries, so we have a history-function. The user can then go back to * previously accessed entries and so on... */ - private int[] history; - - private HistoryManager historyManager; + //private int[] history; + private History history; /** * Stores the files which we want to retrieve from the main data file @@ -386,7 +386,6 @@ public Daten(ZettelkastenView zkn, Settings s, Synonyms syn, BibTeX bib) { bibtexObj = bib; activatedEntryNumber = 1; reset(); - historyManager = new HistoryManager(); } /** @@ -420,12 +419,7 @@ public final void reset() { keywordFile = null; metainfFile = null; zknFileExport = null; - - // Initialize the HistoryManager - historyManager = new HistoryManager(); - historyManager.addToHistory(1); - - + // no update to the tabbed panes in the main window when nothing is loaded keywordlistUpToDate = true; authorlistUpToDate = true; @@ -4978,23 +4972,23 @@ private void addToHistory() { public void addToHistory(int entryNr) { - historyManager.addToHistory(entryNr); + history.addToHistory(entryNr); } public boolean canHistoryBack() { - return historyManager.canHistoryBack(); + return history.canHistoryBack(); } public boolean canHistoryFore() { - return historyManager.canHistoryFore(); + return history.canHistoryFore(); } public int historyBack() { - return historyManager.historyBack(); + return history.historyBack(); } public int historyForward() { - return historyManager.navigateForwardInHistory(); + return history.navigateForwardInHistory(); } public void displayHistory(int[] history, int historyCount) { diff --git a/src/main/java/de/danielluedecke/zettelkasten/history/HistoryManager.java b/src/main/java/de/danielluedecke/zettelkasten/history/History.java similarity index 57% rename from src/main/java/de/danielluedecke/zettelkasten/history/HistoryManager.java rename to src/main/java/de/danielluedecke/zettelkasten/history/History.java index 897e8083..bbf0867e 100644 --- a/src/main/java/de/danielluedecke/zettelkasten/history/HistoryManager.java +++ b/src/main/java/de/danielluedecke/zettelkasten/history/History.java @@ -1,44 +1,39 @@ package de.danielluedecke.zettelkasten.history; -import de.danielluedecke.zettelkasten.ZettelkastenView; import de.danielluedecke.zettelkasten.util.Constants; import de.danielluedecke.zettelkasten.view.Display; +import ch.dreyeck.zettelkasten.xml.Zettel; /** * Manages the history of entries in the program. */ -public class HistoryManager implements HistoryNavigationListener { +public class History implements HistoryNavigationListener { private static final int HISTORY_MAX = 100; // Adjust as needed private int[] history; private int historyPosition; private int historyCount; private int activatedEntryNumber; - - // Constructor without parameters - public HistoryManager() { - this.history = new int[HISTORY_MAX]; - this.historyPosition = -1; - this.historyCount = 0; - } + private Display display; - // Constructor with ZettelkastenView parameter (if needed later) - public HistoryManager(ZettelkastenView zkn) { - this(); // Call the default constructor - // Additional initialization if needed - } - - public HistoryManager(Display display) { + public History(Display display) { this.history = new int[HISTORY_MAX]; this.historyPosition = -1; // Initialize to -1 to indicate no history yet this.historyCount = 0; + this.display = display; } /** - * Adds the given entry number to the history. + * Adds the given entry number to the history if a Zettel is displayed. * * @param entryNr the number of the entry to be added to the history */ public void addToHistory(int entryNr) { + Zettel currentZettel = display.getDisplayedZettel(); + if (currentZettel == null) { + Constants.zknlogger.info("No Zettel displayed. Entry not added to history: " + entryNr); + return; + } + // Avoid duplicates if (historyPosition >= 0 && history[historyPosition] == entryNr) { return; @@ -52,33 +47,18 @@ public void addToHistory(int entryNr) { historyPosition = HISTORY_MAX - 1; } historyCount = Math.min(historyCount + 1, HISTORY_MAX); + activatedEntryNumber = entryNr; // Update activated entry number Constants.zknlogger.info("Added to history: " + entryNr); - //display.displayHistory(history, historyCount); } - /** - * Checks if history back navigation is possible. - * - * @return {@code true} if history back navigation is enabled, {@code false} otherwise - */ public boolean canHistoryBack() { return (historyPosition > 0); } - /** - * Checks if history forward navigation is possible. - * - * @return {@code true} if history forward navigation is enabled, {@code false} otherwise - */ public boolean canHistoryFore() { return (historyPosition >= 0 && historyPosition < (historyCount - 1)); } - /** - * Moves back through the history and returns the activated entry number. - * - * @return the activated entry number after navigating back in history - */ public int historyBack() { if (canHistoryBack()) { activatedEntryNumber = history[--historyPosition]; @@ -86,11 +66,6 @@ public int historyBack() { return activatedEntryNumber; } - /** - * Moves forward through the history and returns the activated entry number. - * - * @return the activated entry number after navigating forward in history - */ public int historyFore() { if (canHistoryFore()) { activatedEntryNumber = history[++historyPosition]; @@ -103,8 +78,17 @@ public int navigateForwardInHistory() { return historyFore(); } - @Override - public void navigateBackwardInHistory() { - historyBack(); - } + @Override + public void navigateBackwardInHistory() { + historyBack(); + } + + // Getters for testing purposes + public int getHistoryCount() { + return historyCount; + } + + public int[] getHistory() { + return history; + } } diff --git a/src/main/java/de/danielluedecke/zettelkasten/history/HistoryManagerEventSourceExample.java b/src/main/java/de/danielluedecke/zettelkasten/history/HistoryManagerEventSourceExample.java deleted file mode 100644 index a0d12653..00000000 --- a/src/main/java/de/danielluedecke/zettelkasten/history/HistoryManagerEventSourceExample.java +++ /dev/null @@ -1,29 +0,0 @@ -package de.danielluedecke.zettelkasten.history; - -import java.util.ArrayList; -import java.util.List; - -public class HistoryManagerEventSourceExample { - private List listeners = new ArrayList<>(); - - // Method to add a listener - public void addHistoryNavigationListener(HistoryNavigationListener listener) { - listeners.add(listener); - } - - // Method to notify all listeners - public void notifyNavigateForward() { - for (HistoryNavigationListener listener : listeners) { - listener.navigateForwardInHistory(); - } - } - - // Example method that triggers the event - public void historyFore() { - // Logic to go forward in history - // ... - - // Notify all listeners about the navigation event - notifyNavigateForward(); - } -} diff --git a/src/main/java/de/danielluedecke/zettelkasten/view/Display.java b/src/main/java/de/danielluedecke/zettelkasten/view/Display.java index 1fc290f8..69b49a72 100644 --- a/src/main/java/de/danielluedecke/zettelkasten/view/Display.java +++ b/src/main/java/de/danielluedecke/zettelkasten/view/Display.java @@ -2,21 +2,22 @@ import de.danielluedecke.zettelkasten.ZettelkastenView; import ch.dreyeck.zettelkasten.xml.Zettel; -import de.danielluedecke.zettelkasten.history.HistoryManager; +import de.danielluedecke.zettelkasten.history.History; public class Display { private ZettelkastenView zettelkastenView; - private HistoryManager historyManager; + private History history; - public Display(ZettelkastenView zettelkastenView, HistoryManager historyManager) { + public Display(ZettelkastenView zettelkastenView, History history) { this.zettelkastenView = zettelkastenView; - this.historyManager = historyManager; + this.history = history; } - public void getDisplayedZettel() { + public Zettel getDisplayedZettel() { Zettel zettel = zettelkastenView.getDisplayedZettel(); if (zettel != null) { - historyManager.addToHistory(123); // Assuming 123 is the entryNr + history.addToHistory(123); // Assuming 123 is the entryNr } + return zettel; } } diff --git a/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenViewTest.java b/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenViewTest.java index 7627b1da..756db18e 100644 --- a/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenViewTest.java +++ b/src/test/java/de/danielluedecke/zettelkasten/ZettelkastenViewTest.java @@ -7,14 +7,14 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import de.danielluedecke.zettelkasten.history.HistoryManager; +import de.danielluedecke.zettelkasten.history.History; public class ZettelkastenViewTest { private ZettelkastenView instance; @Mock - private HistoryManager mockHistoryManager; + private History mockHistoryManager; @BeforeMethod public void setUp() { diff --git a/src/test/java/de/danielluedecke/zettelkasten/history/HistoryManagerTest.java b/src/test/java/de/danielluedecke/zettelkasten/history/HistoryTest.java similarity index 87% rename from src/test/java/de/danielluedecke/zettelkasten/history/HistoryManagerTest.java rename to src/test/java/de/danielluedecke/zettelkasten/history/HistoryTest.java index 43e843a7..7c108610 100644 --- a/src/test/java/de/danielluedecke/zettelkasten/history/HistoryManagerTest.java +++ b/src/test/java/de/danielluedecke/zettelkasten/history/HistoryTest.java @@ -1,6 +1,7 @@ package de.danielluedecke.zettelkasten.history; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; @@ -9,15 +10,19 @@ import org.testng.annotations.Test; import de.danielluedecke.zettelkasten.view.Display; +import ch.dreyeck.zettelkasten.xml.Zettel; -public class HistoryManagerTest { - private HistoryManager historyManager; +public class HistoryTest { + private History historyManager; private Display mockDisplay; + private Zettel mockZettel; @BeforeMethod public void setUp() { mockDisplay = mock(Display.class); - historyManager = new HistoryManager(mockDisplay); + mockZettel = mock(Zettel.class); + when(mockDisplay.getDisplayedZettel()).thenReturn(mockZettel); // Ensure a Zettel is displayed + historyManager = new History(mockDisplay); } @Test @@ -70,7 +75,6 @@ public void testNavigateForwardInHistory() { historyManager.addToHistory(1); historyManager.addToHistory(2); historyManager.historyBack(); - historyManager.navigateForwardInHistory(); assertEquals(historyManager.navigateForwardInHistory(), 2, "navigateForwardInHistory should call historyForward and return the next entry"); } } diff --git a/src/test/java/de/danielluedecke/zettelkasten/view/DisplayTest.java b/src/test/java/de/danielluedecke/zettelkasten/view/DisplayTest.java index b12808ec..6fe8749e 100644 --- a/src/test/java/de/danielluedecke/zettelkasten/view/DisplayTest.java +++ b/src/test/java/de/danielluedecke/zettelkasten/view/DisplayTest.java @@ -13,7 +13,7 @@ import de.danielluedecke.zettelkasten.ZettelkastenView; import ch.dreyeck.zettelkasten.xml.Zettel; -import de.danielluedecke.zettelkasten.history.HistoryManager; +import de.danielluedecke.zettelkasten.history.History; public class DisplayTest { private Display display; @@ -21,7 +21,7 @@ public class DisplayTest { // Annotate historyManagerMock with @Mock @Mock - private HistoryManager historyManagerMock; + private History historyManagerMock; @BeforeMethod