Skip to content

Commit

Permalink
'#2163 Design method for restoration of defined filters of a filter from
Browse files Browse the repository at this point in the history
another source. Implements the restoration for Category filterer
CategoryTreeListener.
  • Loading branch information
patrickdalla committed May 29, 2024
1 parent f58d09b commit 8eef37f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 27 deletions.
92 changes: 68 additions & 24 deletions iped-app/src/main/java/iped/app/ui/CategoryTreeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedHashSet;
import java.util.List;

import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
Expand Down Expand Up @@ -130,35 +131,78 @@ public void recursiveCategoryQuery(Category cat, StringBuffer buff) {
}
}

class CategoryFilter implements IQueryFilter {
private Category category;

public CategoryFilter(Category category) {
this.category = category;

}

@Override
public Query getQuery() {
String name = IndexItem.normalize(category.getName(), true);
StringBuffer queryStr = new StringBuffer();
queryStr.append(" category:\"");
queryStr.append(name);
queryStr.append("\"");
recursiveCategoryQuery(category, queryStr);

Query query;
try {
query = new QueryBuilder(App.get().appCase).getQuery(queryStr.toString());
return query;
} catch (ParseException | QueryNodeException e) {
e.printStackTrace();
}
return null;
}

public String toString() {
return IndexItem.normalize(category.getName(), true);
}

public Category getCategory() {
return category;
}
}

@Override
public void restoreDefineFilters(List<IFilter> filtersToRestore) {
if (filtersToRestore == null) {
return;
}
categoryList.clear();
selection.clear();

for (IFilter filter : filtersToRestore) {
if (filter instanceof CategoryFilter) {
CategoryFilter catFilter = (CategoryFilter) filter;
categoryList.add(catFilter.getCategory());
select(catFilter.getCategory());
}
}
}

private void select(Category category) {
JTree tree = (JTree) App.get().categoryTree;
int rowCount = tree.getRowCount();
for (int i = 0; i < rowCount; i++) {
TreePath path = tree.getPathForRow(i);
if (path.getLastPathComponent().equals(category)) {
tree.addSelectionRow(i);
selection.add(path);
break;
}
}
}

@Override
public List<IFilter> getDefinedFilters() {
CategoryTreeListener self = this;
List<IFilter> result = new ArrayList<IFilter>();
for (Category category : categoryList) {
result.add(new IQueryFilter() {
@Override
public Query getQuery() {
String name = IndexItem.normalize(category.getName(), true);
StringBuffer queryStr = new StringBuffer();
queryStr.append(" category:\"");
queryStr.append(name);
queryStr.append("\"");
recursiveCategoryQuery(category, queryStr);

Query query;
try {
query = new QueryBuilder(App.get().appCase).getQuery(queryStr.toString());
return query;
} catch (ParseException | QueryNodeException e) {
e.printStackTrace();
}
return null;
}

public String toString() {
return IndexItem.normalize(category.getName(), true);
}
});
result.add(new CategoryFilter(category));
}
return result;
}
Expand Down
16 changes: 13 additions & 3 deletions iped-app/src/main/java/iped/app/ui/FiltersPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.function.Predicate;

import javax.swing.DropMode;
Expand Down Expand Up @@ -65,6 +67,8 @@ public class FiltersPanel extends JPanel

private volatile TreePath lastClickedPath;

HashMap<IFilterer, List<IFilter>> lastFilters = new HashMap<IFilterer, List<IFilter>>();

public FiltersPanel() {
invertUrl = this.getClass().getResource("negative.png");
invertIcon = new ImageIcon(invertUrl);
Expand Down Expand Up @@ -121,16 +125,22 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
return result;
}
};

treeCellRenderer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
IFilterer filterer = (IFilterer) e.getSource();
if (filterer.hasFilters()) {
filterManager.setFilterEnabled(filterer, !filterManager.isFiltererEnabled(filterer));
App.get().filtersPanel.updateUI();
lastFilters.put(filterer, filterer.getDefinedFilters());
filterer.clearFilter();
App.get().getAppListener().updateFileListing();
} else {
App.get().filtersPanel.updateUI();
List<IFilter> clastFilters = lastFilters.get(filterer);
if (clastFilters != null) {
filterer.restoreDefineFilters(clastFilters);
App.get().getAppListener().updateFileListing();
App.get().filtersPanel.updateUI();
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public interface IFilterer extends ClearFilterListener, ActionListenerControl {
*/
public boolean hasFiltersApplied();

/**
* Restores internal state elements to represent the defined filters passed as
* parameter
*
* @return
*/
default public void restoreDefineFilters(List<IFilter> filtersToRestore) {
return;
}

default public String getFilterName() {
try {
return Messages.get(this.getClass().getName().replace("$", ".") + ".filtererName");
Expand Down

0 comments on commit 8eef37f

Please sign in to comment.