Skip to content

Commit

Permalink
Resolve "If SearchBar in config view is used - changes are not applied"
Browse files Browse the repository at this point in the history
Closes #1990

See merge request main/Sumatra!1890

sumatra-commit: 9d516aeb6cc35ec6194be1089ce33c947447a6da
  • Loading branch information
Mk-arc authored and TIGERs GitLab committed Jul 16, 2024
1 parent 7ba31b4 commit bdb672e
Showing 1 changed file with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package edu.tigers.sumatra.config;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import me.xdrop.fuzzywuzzy.FuzzySearch;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.tree.ConfigurationNode;
Expand All @@ -13,9 +15,13 @@
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreePath;
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.Serial;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
Expand Down Expand Up @@ -122,7 +128,9 @@ private ConfigXMLTreeTableModel filterModel(HierarchicalConfiguration config, Co
var filteredConfig = new HierarchicalConfiguration();
var filteredRoot = filterConfigNode(config.getRootNode(), model, false).map(FilterResult::node);
filteredRoot.ifPresent(filteredConfig::setRootNode);
return new ConfigXMLTreeTableModel(filteredConfig);
var filteredModel = new ConfigXMLTreeTableModel(filteredConfig);
filteredModel.addTreeModelListener(new TreeChangeForwarder(model, filteredModel));
return filteredModel;
}


Expand Down Expand Up @@ -221,4 +229,77 @@ private void applySearch()
}

}

@RequiredArgsConstructor
private static class TreeChangeForwarder implements TreeModelListener
{
@NonNull
private final ConfigXMLTreeTableModel originalModel;
@NonNull
private final ConfigXMLTreeTableModel filteredModel;


@Override
public void treeNodesChanged(TreeModelEvent treeModelEvent)
{
var filteredObj = (ConfigurationNode) treeModelEvent.getTreePath().getLastPathComponent();
for (var child : filteredObj.getChildren())
{
var originalChild = convertPath(treeModelEvent.getTreePath(), child).getLastPathComponent();
var value = filteredModel.getValueAt(child, 1);
originalModel.setValueAt(value, originalChild, 1);
}
}


private TreePath convertPath(TreePath oldTreePath, ConfigurationNode interestingChild)
{
var originalPath = new ArrayList<>(oldTreePath.getPathCount());

var originalNode = (ConfigurationNode) originalModel.getRoot();
@SuppressWarnings("squid:S6204") // Immutable on intention and sonar is too dumb to detect the list changes
var oldPath = Arrays.stream(oldTreePath.getPath()).collect(Collectors.toList());
// Handle root node manually
oldPath.removeFirst();
oldPath.addLast(interestingChild);
originalPath.addFirst(originalNode);


for (var filteredNodeObj : oldPath)
{
var filteredNode = (ConfigurationNode) filteredNodeObj;
for (var child : originalNode.getChildren())
{
if (child.getName().equals(filteredNode.getName()))
{
originalPath.addLast(child);
originalNode = child;
break;
}
}
}
return new TreePath(originalPath.toArray());
}


@Override
public void treeNodesInserted(TreeModelEvent treeModelEvent)
{
// Unused
}


@Override
public void treeNodesRemoved(TreeModelEvent treeModelEvent)
{
// Unused
}


@Override
public void treeStructureChanged(TreeModelEvent treeModelEvent)
{
// Unused
}
}
}

0 comments on commit bdb672e

Please sign in to comment.