Autopsy  4.12.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CompoundFilterState.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2018-2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.timeline.ui.filtering.datamodel;
20 
21 import com.google.common.collect.Lists;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Objects;
25 import java.util.stream.Collectors;
26 import javafx.collections.FXCollections;
27 import javafx.collections.ListChangeListener;
28 import javafx.collections.ObservableList;
29 import org.openide.util.Exceptions;
30 import org.sleuthkit.datamodel.TimelineFilter;
31 import org.sleuthkit.datamodel.TimelineFilter.CompoundFilter;
32 
41 public class CompoundFilterState<SubFilterType extends TimelineFilter, FilterType extends CompoundFilter<SubFilterType>> extends SqlFilterState<FilterType> {
42 
43  private final ObservableList< FilterState<? extends SubFilterType>> subFilterStates = FXCollections.observableArrayList();
44 
51  CompoundFilterState(FilterType filter) {
52  super(filter);
53  filter.getSubFilters().forEach(newSubFilter -> {
54  //add the appropriate filter type: default or compound
55  if (newSubFilter instanceof CompoundFilter<?>) {
56  @SuppressWarnings(value = "unchecked")
57  FilterState<SubFilterType> compoundFilterState = (FilterState<SubFilterType>) new CompoundFilterState<>((CompoundFilter<?>) newSubFilter);
58  addSubFilterStateInternal(compoundFilterState);
59  } else {
61  }
62  });
63 
65  }
66 
76  CompoundFilterState(FilterType filter, Collection< FilterState<? extends SubFilterType>> subFilterStates) {
77  super(filter);
78  subFilterStates.forEach(this::addSubFilterStateInternal);
79 
81  }
82 
83  private void configureListeners() {
84  try {
85  //Add a new subfilterstate whenever the underlying subfilters change.
86  getFilter().getSubFilters().addListener((ListChangeListener.Change<? extends SubFilterType> change) -> {
87  while (change.next()) {
88  change.getAddedSubList().forEach((SubFilterType newSubFilter) -> {
89  //if there is not already a state for this filter
90  if (getSubFilterStates().stream().map(FilterState::getFilter).noneMatch(newSubFilter::equals)) {
91  //add the appropriate filter type: default or compound
92  if (newSubFilter instanceof CompoundFilter<?>) {
93  @SuppressWarnings("unchecked")
94  FilterState<SubFilterType> compoundFilterState = (FilterState<SubFilterType>) new CompoundFilterState<>((CompoundFilter<?>) newSubFilter);
95  addSubFilterStateInternal(compoundFilterState);
96 
97  } else {
98  addSubFilterStateInternal(new SqlFilterState<>(newSubFilter));
99  }
100 
101  }
102  });
103  }
104  });
105  } catch (Exception e) {
106  Exceptions.printStackTrace(e);
107  }
108  activeProperty().addListener(activeProperty -> disableSubFiltersIfNotActive());
110 
115  selectedProperty().addListener(selectedProperty -> {
116  if (isSelected() && getSubFilterStates().stream().noneMatch(FilterState::isSelected)) {
117  subFilterStates.forEach(subFilterState -> subFilterState.setSelected(true));
118  }
119  });
120  }
121 
128  boolean inactive = isActive() == false;
129 
130  subFilterStates.forEach(subFilterState -> subFilterState.setDisabled(inactive));
131  }
132 
141  protected void addSubFilterState(FilterState< ? extends SubFilterType> newSubFilterState) {
142  SubFilterType filter = newSubFilterState.getFilter();
143  if (getSubFilterStates().stream().map(FilterState::getFilter).noneMatch(filter::equals)) {
144 
145  //add the state first, and then the actual filter which will check for an existing state before adding another one.
146  addSubFilterStateInternal(newSubFilterState);
147  getFilter().getSubFilters().add(filter);
148  }
149  }
150 
152  if (subFilterStates.contains(newSubFilterState) == false) {
153  subFilterStates.add(newSubFilterState);
154  newSubFilterState.selectedProperty().addListener(selectedProperty -> {
155  //set this compound filter state selected af any of the subfilters are selected.
156  setSelected(subFilterStates.stream().anyMatch(FilterState::isSelected));
157  });
158  newSubFilterState.setDisabled(isActive() == false);
159  }
160  }
161 
162  public ObservableList< FilterState< ? extends SubFilterType>> getSubFilterStates() {
163  return subFilterStates;
164  }
165 
166  @Override
168  @SuppressWarnings("unchecked")
170  = new CompoundFilterState<>((FilterType) getFilter().copyOf(),
171  Lists.transform(subFilterStates, FilterState::copyOf));
172 
173  copy.setSelected(isSelected());
174  copy.setDisabled(isDisabled());
175  return copy;
176  }
177 
178  @Override
179  @SuppressWarnings("unchecked")
180  public FilterType getActiveFilter() {
181  if (isActive() == false) {
182  return null;
183  }
184 
185  List<SubFilterType> activeSubFilters = subFilterStates.stream()
186  .filter(filterState -> filterState.isActive())
187  .map(filterState -> filterState.getActiveFilter())
188  .collect(Collectors.toList());
189  FilterType copy = (FilterType) getFilter().copyOf();
190  copy.getSubFilters().clear();
191  copy.getSubFilters().addAll(activeSubFilters);
192 
193  return copy;
194  }
195 
196  @Override
197  public int hashCode() {
198  int hash = super.hashCode();
199  hash = 41 * hash + Objects.hashCode(this.subFilterStates);
200  return hash;
201  }
202 
203 
204 
205  @Override
206  public boolean equals(Object obj) {
207  if (this == obj) {
208  return true;
209  }
210  if (obj == null) {
211  return false;
212  }
213  if (getClass() != obj.getClass()) {
214  return false;
215  }
216 
218  if (!Objects.equals(this.getFilter(), other.getFilter())) {
219  return false;
220  }
221  if (!Objects.equals(this.isSelected(), other.isSelected())) {
222  return false;
223  }
224  if (!Objects.equals(this.isDisabled(), other.isDisabled())) {
225  return false;
226  }
227  return Objects.equals(this.subFilterStates, other.subFilterStates);
228  }
229 
230  @Override
231  public String toString() {
232  return "CompoundFilterState{ selected=" + isSelected() + ", disabled=" + isDisabled() + ", activeProp=" + isActive() + ",subFilterStates=" + subFilterStates + '}';
233  }
234 
235 }
void addSubFilterState(FilterState< ?extends SubFilterType > newSubFilterState)
void addSubFilterStateInternal(FilterState< ?extends SubFilterType > newSubFilterState)
final ObservableList< FilterState<?extends SubFilterType > > subFilterStates
ObservableList< FilterState< ?extends SubFilterType > > getSubFilterStates()

Copyright © 2012-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.