Skip to content

Commit

Permalink
WIP on main: f926ebe refactor(SearchResultsFrame): names
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfBarkow committed Jun 7, 2024
2 parents f926ebe + 4bc28cb commit 23d6f97
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
26 changes: 10 additions & 16 deletions src/main/java/de/danielluedecke/zettelkasten/database/Daten.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -386,7 +386,6 @@ public Daten(ZettelkastenView zkn, Settings s, Synonyms syn, BibTeX bib) {
bibtexObj = bib;
activatedEntryNumber = 1;
reset();
historyManager = new HistoryManager();
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -52,45 +47,25 @@ 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];
}
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];
Expand All @@ -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;
}
}

This file was deleted.

13 changes: 7 additions & 6 deletions src/main/java/de/danielluedecke/zettelkasten/view/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

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;
private ZettelkastenView zettelkastenViewMock;

// Annotate historyManagerMock with @Mock
@Mock
private HistoryManager historyManagerMock;
private History historyManagerMock;


@BeforeMethod
Expand Down

0 comments on commit 23d6f97

Please sign in to comment.