diff --git a/iped-app/src/main/java/iped/app/ui/CategoryTreeListener.java b/iped-app/src/main/java/iped/app/ui/CategoryTreeListener.java index c9d085a51a..c76b8c533c 100644 --- a/iped-app/src/main/java/iped/app/ui/CategoryTreeListener.java +++ b/iped-app/src/main/java/iped/app/ui/CategoryTreeListener.java @@ -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; @@ -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 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 getDefinedFilters() { CategoryTreeListener self = this; List result = new ArrayList(); 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; } diff --git a/iped-app/src/main/java/iped/app/ui/FiltersPanel.java b/iped-app/src/main/java/iped/app/ui/FiltersPanel.java index b4b103c474..4d3813e9c7 100644 --- a/iped-app/src/main/java/iped/app/ui/FiltersPanel.java +++ b/iped-app/src/main/java/iped/app/ui/FiltersPanel.java @@ -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; @@ -65,6 +67,8 @@ public class FiltersPanel extends JPanel private volatile TreePath lastClickedPath; + HashMap> lastFilters = new HashMap>(); + public FiltersPanel() { invertUrl = this.getClass().getResource("negative.png"); invertIcon = new ImageIcon(invertUrl); @@ -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 clastFilters = lastFilters.get(filterer); + if (clastFilters != null) { + filterer.restoreDefineFilters(clastFilters); + App.get().getAppListener().updateFileListing(); + App.get().filtersPanel.updateUI(); + } } } }); diff --git a/iped-viewers/iped-viewers-api/src/main/java/iped/viewers/api/IFilterer.java b/iped-viewers/iped-viewers-api/src/main/java/iped/viewers/api/IFilterer.java index 04506841b0..cd3094f42b 100644 --- a/iped-viewers/iped-viewers-api/src/main/java/iped/viewers/api/IFilterer.java +++ b/iped-viewers/iped-viewers-api/src/main/java/iped/viewers/api/IFilterer.java @@ -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 filtersToRestore) { + return; + } + default public String getFilterName() { try { return Messages.get(this.getClass().getName().replace("$", ".") + ".filtererName");