Skip to content

Commit

Permalink
'#2163 Implements restore filters of MetadataPanel filterer.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdalla committed Jun 3, 2024
1 parent fbc1804 commit c2eea39
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ public Query getQuery() {
return query;
}

public String getFilterField() {
return filterField;
}

public Set<ValueCount> getValues() {
return values;
}

}
178 changes: 167 additions & 11 deletions iped-app/src/main/java/iped/app/ui/MetadataPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -273,16 +278,78 @@ public void run() {
}.start();
}

class FuturePopulateList implements Future<Boolean> {
Boolean listPopulatedEnded = false;
Semaphore sem = new Semaphore(1);
ArrayList<Runnable> actions = new ArrayList<Runnable>();

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<MultiSearchResult> future = null;
if (updateResult) {
updatingResult = true;
future = App.get().appletListener.futureUpdateFileListing();
}
Future<MultiSearchResult> finalfuture = future;

ms.setLogScale(scale.getValue() == 1);
ms.setNoRanges(scale.getValue() == -1);

Expand Down Expand Up @@ -310,7 +377,9 @@ public void run() {
countValues();

} catch (IOException e) {
e.printStackTrace();
if (futurePopulateList != null) {
futurePopulateList.cancel(true);
}
} finally {
setWaitVisible(false);
}
Expand All @@ -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<ValueCount> selection = list.getSelectedValuesList();
HashSet<ValueCount> selSet = new HashSet<ValueCount>();
Expand All @@ -337,6 +406,7 @@ public void run() {
list.setSelectedIndices(selIdx);
}
updatingList = false;
futurePopulateList.finish();

// System.out.println("finish");
updateTabColor();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -654,4 +719,95 @@ public boolean hasFiltersApplied() {
return isFiltering();
}

@Override
public void restoreDefinedFilters(List<IFilter> 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<ValueCount> 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<ListSelectionListener> temp = new ArrayList<ListSelectionListener>();
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);
}
}
}

0 comments on commit c2eea39

Please sign in to comment.