Skip to content

Commit

Permalink
refactor 'updateDisplay' within `SearchResultsFrame #533
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfBarkow committed Oct 19, 2024
1 parent 888f50d commit 0c0b329
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 76 deletions.
154 changes: 86 additions & 68 deletions src/main/java/de/danielluedecke/zettelkasten/SearchResultsFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ private void initAcceleratorTable() {
*/
@Action
public void showEntryImmediately() {
Constants.zknlogger.info(data.toString());
data.getSettings().setShowSearchEntry(!data.getSettings().getShowSearchEntry());
}

Expand Down Expand Up @@ -1269,74 +1270,91 @@ private void prepareResultList(int searchrequestnr) {
}

/**
* This method updates the display, i.e. it retrieves the selected entry from
* the jTableResults and fills the textfields with content (displaying the
* entry).
*/
private void updateDisplay() {
// get selected row
int row = jTableResults.getSelectedRow();
// if we have any selections, go on
if (row != -1) {
// retrieve the value...
Object o = jTableResults.getValueAt(row, 0);
try {
// ...and try to convert it to an integer value
int selection = Integer.parseInt(o.toString());
// prepare array for search terms which might be highlighted
String[] sts = getHighlightSearchterms();
displayZettelContent(selection, sts);
//
// Here we set up the keywordlist for the JList
//
// retrieve the keywords of the selected entry
String[] kws = data.getData().getKeywords(selection);
// prepare the JList which will display the keywords
data.getKeywordListModel().clear();
// check whether any keywords have been found
if (kws != null) {
// Sort the array
if (kws.length > 0)
Arrays.sort(kws);
// iterate the string array and add its content to the list model
for (String kw : kws)
data.getKeywordListModel().addElement(kw);
}
// if we have any search terms, we want to select the related keywords...
if (sts != null) {
// create an integer list
LinkedList<Integer> l = new LinkedList<>();
// iterate all search terms
for (String s : sts) {
// try to find the keyword in the jList
for (int cnt = 0; cnt < data.getKeywordListModel().getSize(); cnt++)
if (s.equalsIgnoreCase(data.getKeywordListModel().get(cnt).toString()))
l.add(cnt);
}
// create int-array
int[] selections = new int[l.size()];
// copy all elements of the list to the array
for (int cnt = 0; cnt < l.size(); cnt++)
selections[cnt] = l.get(cnt);
// Set selected indices for the jList
jListKeywords.setSelectedIndices(selections);
}
// if we don't have highlighting, clear selection
else
jListKeywords.clearSelection();
// if we want to update the entry immediately, show entry in mainframe as well
if (data.getSettings().getShowSearchEntry())
data.getMainFrame().setNewActivatedEntryAndUpdateDisplay(selection);
// finally, set desktop selected
// setDesktopEntrySelected(desktopObj.isEntryInAnyDesktop(selection));
} catch (NumberFormatException e) {
Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
}
} else {
jEditorPaneSearchEntry.setText("");
data.getKeywordListModel().clear();
}
}
* Updates the display with content from the selected entry in jTableResults
* and updates the related keyword list and highlights.
*/
private void updateDisplay() {
int row = jTableResults.getSelectedRow();

// If a row is selected, proceed with display update
if (row != -1) {
Object selectedObject = jTableResults.getValueAt(row, 0);
Constants.zknlogger.info("Selected object: " + selectedObject);

try {
int selection = parseSelection(selectedObject);
updateZettelContent(selection);
updateKeywordList(selection);
highlightKeywords(selection);
updateMainFrameDisplay(selection);
} catch (NumberFormatException e) {
Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
}
} else {
clearDisplay();
}
}

// Helper method to parse the selected object into an integer
private int parseSelection(Object selectedObject) throws NumberFormatException {
return Integer.parseInt(selectedObject.toString());
}

// Updates the main Zettel content display
private void updateZettelContent(int selection) {
String[] searchTerms = getHighlightSearchterms();
displayZettelContent(selection, searchTerms);
}

// Updates the keyword list with the keywords from the selected Zettel
private void updateKeywordList(int selection) {
String[] keywords = data.getData().getKeywords(selection);
data.getKeywordListModel().clear();

if (keywords != null && keywords.length > 0) {
Arrays.sort(keywords);
for (String keyword : keywords) {
data.getKeywordListModel().addElement(keyword);
}
}
}

// Highlights the search terms in the keyword list, if any
private void highlightKeywords(int selection) {
String[] searchTerms = getHighlightSearchterms();

if (searchTerms != null) {
List<Integer> selectedIndices = new LinkedList<>();

for (String searchTerm : searchTerms) {
for (int i = 0; i < data.getKeywordListModel().getSize(); i++) {
if (searchTerm.equalsIgnoreCase(data.getKeywordListModel().get(i).toString())) {
selectedIndices.add(i);
}
}
}

// Convert list to int[] and set selected indices
int[] selections = selectedIndices.stream().mapToInt(Integer::intValue).toArray();
jListKeywords.setSelectedIndices(selections);
} else {
jListKeywords.clearSelection();
}
}

// Updates the main frame display if necessary
private void updateMainFrameDisplay(int selection) {
if (data.getSettings().getShowSearchEntry()) {
data.getMainFrame().setNewActivatedEntryAndUpdateDisplay(selection);
}
}

// Clears the display fields and keyword list when no row is selected
private void clearDisplay() {
jEditorPaneSearchEntry.setText("");
data.getKeywordListModel().clear();
}


public void updateDisplayAfterEditing() {
// get selected row
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/de/danielluedecke/zettelkasten/ZettelkastenView.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import static de.danielluedecke.zettelkasten.data.History.*;

/**
* The application's main frame.
*/
Expand Down Expand Up @@ -2722,14 +2724,28 @@ public final void updateDisplay() {
}

public final void updateDisplay(UpdateDisplayOptions options) {
if (!data.hasZettelID(displayedZettel)) {
if (data == null) {
throw new IllegalStateException("Data cannot be null.");
}

// Ensure the currently displayed Zettel is valid, otherwise use the activated entry.
if (!isValidDisplayedZettel()) {
displayedZettel = data.getActivatedEntryNumber();
Constants.zknlogger.info("Displayed Zettel: " + displayedZettel);
}

updateFrameTitle();
updateEntryPaneAndKeywordsPane(displayedZettel);
updateToolbarAndMenu();
updateTabbedPane(options);

if (options != null) {
updateTabbedPane(options);
}
}

// Helper method to encapsulate the logic for checking displayed Zettel validity
private boolean isValidDisplayedZettel() {
return data.hasZettelID(displayedZettel);
}

/**
Expand Down Expand Up @@ -8553,12 +8569,14 @@ public void setNewActivatedEntryAndUpdateDisplay(int entryNumber) {
*/
public void setNewActivatedEntryAndUpdateDisplay(int entryNumber, UpdateDisplayOptions options) {
if (data.activateEntry(entryNumber)) {
Constants.zknlogger.info("Update display with Zettel: " + String.valueOf(entryNumber));
updateDisplay(options);
} else {
// Log a warning if the entry number is invalid
Constants.zknlogger.log(Level.WARNING,
"setNewActivatedEntryAndUpdateDisplay was called with invalid entry number: {0}", entryNumber);
}
History.logCurrentHistory();
}

/**
Expand Down Expand Up @@ -10660,7 +10678,7 @@ public void searchKeywordsFromListLogOr() {
private void updateDisplayAfterOpen() {
// this is the typical stuff we need to do when a file is opened
// or imported. first of all, all the views of the tabbed pane are not
// uptodate, because we have new data. thus, we set all values to false,
// up-to-date, because we have new data. thus, we set all values to false,
// indicating that all lists ar <b>not</b> up to date and all tables need
// to be re-filled.
data.setKeywordlistUpToDate(false);
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/de/danielluedecke/zettelkasten/data/History.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import de.danielluedecke.zettelkasten.util.Constants;
import de.danielluedecke.zettelkasten.view.Display;

import java.util.logging.Level;

/**
* Manages the history of entries in the program.
*/
public class History implements NavigationListener {
private static final int HISTORY_MAX = 100; // Adjust as needed
private int[] history;
private int historyPosition;
private static int[] history;
private static int historyPosition;
private int historyCount;
private int activatedEntryNumber;
private int[] displayedEntries;
Expand Down Expand Up @@ -62,7 +64,7 @@ public void addToHistory(int entryNr) {
/**
* Logs the current history.
*/
private void logCurrentHistory() {
public static void logCurrentHistory() {
StringBuilder historyBuilder = new StringBuilder("Current history: [");
for (int i = 0; i <= historyPosition; i++) {
historyBuilder.append(history[i]);
Expand Down Expand Up @@ -101,9 +103,11 @@ public boolean canHistoryForward() {
* @return the activated entry number after navigating back in history
*/
public int historyBack() {
logCurrentHistory();
if (canHistoryBack()) {
activatedEntryNumber = history[--historyPosition];
}
Constants.zknlogger.log(Level.INFO, "Activated entry number:", String.valueOf(activatedEntryNumber));
return activatedEntryNumber;
}

Expand All @@ -112,16 +116,18 @@ public int historyBack() {
*
* @return the activated entry number after navigating forward in history
*/
public int historyFore() {
public int historyForward() {
logCurrentHistory();
if (canHistoryForward()) {
activatedEntryNumber = history[++historyPosition];
}
Constants.zknlogger.log(Level.INFO, "Activated entry number:", String.valueOf(activatedEntryNumber));
return activatedEntryNumber;
}

@Override
public int navigateForwardInHistory() {
return historyFore();
return historyForward();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4980,6 +4980,7 @@ public boolean canHistoryForward() {
}

public int historyBack() {
Constants.zknlogger.info(String.valueOf(history.historyBack()));
return history.historyBack();
}

Expand Down Expand Up @@ -5530,6 +5531,7 @@ private boolean setAuthorBibKeyValue(int pos, String key) {
* @return number of the currently <i>activated</i> entry
*/
public int getActivatedEntryNumber() {
Constants.zknlogger.info("Activated entry number: " + activatedEntryNumber);
return activatedEntryNumber;
}

Expand Down

0 comments on commit 0c0b329

Please sign in to comment.