From c2eea3966a935606edede386f4525da7cf0a1c7a Mon Sep 17 00:00:00 2001 From: "patrick.pdb" Date: Mon, 3 Jun 2024 13:27:22 -0400 Subject: [PATCH] '#2163 Implements restore filters of MetadataPanel filterer. --- .../app/metadata/ValueCountQueryFilter.java | 8 + .../main/java/iped/app/ui/MetadataPanel.java | 178 ++++++++++++++++-- 2 files changed, 175 insertions(+), 11 deletions(-) diff --git a/iped-app/src/main/java/iped/app/metadata/ValueCountQueryFilter.java b/iped-app/src/main/java/iped/app/metadata/ValueCountQueryFilter.java index 8182694b0e..b6513e675f 100644 --- a/iped-app/src/main/java/iped/app/metadata/ValueCountQueryFilter.java +++ b/iped-app/src/main/java/iped/app/metadata/ValueCountQueryFilter.java @@ -102,4 +102,12 @@ public Query getQuery() { return query; } + public String getFilterField() { + return filterField; + } + + public Set getValues() { + return values; + } + } diff --git a/iped-app/src/main/java/iped/app/ui/MetadataPanel.java b/iped-app/src/main/java/iped/app/ui/MetadataPanel.java index b6dfd1d692..0b4857cbdf 100644 --- a/iped-app/src/main/java/iped/app/ui/MetadataPanel.java +++ b/iped-app/src/main/java/iped/app/ui/MetadataPanel.java @@ -18,6 +18,8 @@ import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import javax.swing.Box; @@ -27,6 +29,7 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSlider; +import javax.swing.ListModel; import javax.swing.SwingUtilities; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; @@ -99,11 +102,13 @@ public class MetadataPanel extends JPanel implements ActionListener, ListSelecti ValueCount[] array, filteredArray; - boolean updatingProps = false, updatingList = false, clearing = false; + boolean updatingProps = false, updatingList = false, clearing = false, isRestoringFields = false; volatile boolean updatingResult = false; private MetadataSearch ms = new MetadataSearch(); + private FuturePopulateList futurePopulateList; + private static final long serialVersionUID = 1L; public MetadataPanel() { @@ -273,16 +278,78 @@ public void run() { }.start(); } + class FuturePopulateList implements Future { + Boolean listPopulatedEnded = false; + Semaphore sem = new Semaphore(1); + ArrayList actions = new ArrayList(); + + FuturePopulateList() { + try { + sem.acquire(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + sem.release(); + return false; + } + + @Override + public boolean isCancelled() { + return !listPopulatedEnded; + } + + @Override + public boolean isDone() { + sem.release(); + return listPopulatedEnded; + } + + @Override + public Boolean get() throws InterruptedException, ExecutionException { + try { + sem.acquire(); + sem.release(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return listPopulatedEnded; + } + + @Override + public Boolean get(long timeout, TimeUnit unit) + throws InterruptedException, ExecutionException { + return null; + } + + public void finish() { + listPopulatedEnded = true; + + sem.release(); + + for (Runnable action : actions) { + action.run(); + } + } + + public void addAction(Runnable r) { + actions.add(r); + } + }; + private void populateList() { setWaitVisible(true); - final boolean updateResult = !list.isSelectionEmpty(); + final boolean updateResult = !list.isSelectionEmpty() || isRestoringFields; Future future = null; if (updateResult) { updatingResult = true; future = App.get().appletListener.futureUpdateFileListing(); } Future finalfuture = future; - + ms.setLogScale(scale.getValue() == 1); ms.setNoRanges(scale.getValue() == -1); @@ -310,7 +377,9 @@ public void run() { countValues(); } catch (IOException e) { - e.printStackTrace(); + if (futurePopulateList != null) { + futurePopulateList.cancel(true); + } } finally { setWaitVisible(false); } @@ -322,7 +391,7 @@ public void run() { private void updateList(final ValueCount[] sortedArray) { SwingUtilities.invokeLater(new Runnable() { @Override - public void run() { + public void run() { updatingList = true; List selection = list.getSelectedValuesList(); HashSet selSet = new HashSet(); @@ -337,6 +406,7 @@ public void run() { list.setSelectedIndices(selIdx); } updatingList = false; + futurePopulateList.finish(); // System.out.println("finish"); updateTabColor(); @@ -480,13 +550,13 @@ private String removeIllegalChars(String s) { @Override public void clearFilter() { clearing = true; + groups.setSelectedIndex(-1); list.setListData(new ValueCount[0]); clearing = false; } @Override public void stateChanged(ChangeEvent e) { - if (e.getSource() == sort) { if (!sort.getValueIsAdjusting()) sortAndUpdateList(filteredArray); @@ -612,11 +682,6 @@ public List getDefinedFilters() { if (isFiltering()) { ValueCount sample = selectedValues.iterator().next(); result.add(new ValueCountQueryFilter(field, selectedValues)); - /* - * if(sample instanceof RangeCount) { result.add(new ValueCountQueryFilter(field - * , selectedValues)); }else { result.add(new ValueCountFilter(field , - * selectedValues)); } - */ } return result; } @@ -654,4 +719,95 @@ public boolean hasFiltersApplied() { return isFiltering(); } + @Override + public void restoreDefinedFilters(List filtersToRestore) { + for (IFilter filter : filtersToRestore) { + if (filter instanceof ValueCountQueryFilter) { + ValueCountQueryFilter vcqFilter = (ValueCountQueryFilter) filter; + String field = vcqFilter.getFilterField(); + int i = 0; + fields: for (String[] fieldsInGroup : ColumnsManager.getInstance().fieldGroups) { + for (String fieldInGroup : fieldsInGroup) { + if (fieldInGroup.equals(field)) { + groups.setSelectedIndex(i); + + if (futurePopulateList != null) { + FuturePopulateList oldFuturePopulateList = futurePopulateList; + futurePopulateList = new FuturePopulateList(); + oldFuturePopulateList.cancel(true); + } else { + futurePopulateList = new FuturePopulateList(); + } + + futurePopulateList.addAction(new Runnable() { + @Override + public void run() { + Thread t = new Thread(new Runnable() { + + @Override + public void run() { + try { + if (futurePopulateList.get()) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + int[] is = new int[vcqFilter.getValues().size()]; + ListModel dm = list.getModel(); + int j = 0; + for (int k = 0, c = dm.getSize(); k < c + && j < is.length; k++) { + if (vcqFilter.getValues() + .contains(dm.getElementAt(k))) { + is[j] = k; + j++; + } + } + setSelectedIndices(is); + } + }); + } + futurePopulateList = null; + App.get().filtersPanel.updateUI(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + } + } + }); + t.start(); + } + }); + + isRestoringFields = true; + props.setSelectedItem(field); + isRestoringFields = false; + + break fields; + } + } + i++; + } + break; + } + } + + } + + private void setSelectedIndices(int[] is) { + ArrayList temp = new ArrayList(); + for (ListSelectionListener sl : list.getListSelectionListeners()) { + temp.add(sl); + } + for (ListSelectionListener sl : temp) { + list.removeListSelectionListener(sl); + } + list.setSelectedIndices(is); + for (ListSelectionListener sl : temp) { + list.addListSelectionListener(sl); + } + } }