Autopsy  4.16.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ResultViewerPersistence.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-17 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.corecomponents;
20 
21 import java.util.ArrayList;
22 import java.util.LinkedHashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeMap;
26 import java.util.prefs.Preferences;
27 import javax.swing.SortOrder;
28 import org.openide.nodes.Children;
29 import org.openide.nodes.Node;
30 import org.openide.util.NbPreferences;
31 
32 final class ResultViewerPersistence {
33 
34  private ResultViewerPersistence() {
35  }
36 
46  static String getColumnPositionKey(TableFilterNode node, String propName) {
47  return getColumnKeyBase(node, propName) + ".column";
48  }
49 
59  static String getColumnSortOrderKey(TableFilterNode node, String propName) {
60  return getColumnKeyBase(node, propName) + ".sortOrder";
61  }
62 
72  static String getColumnSortRankKey(TableFilterNode node, String propName) {
73  return getColumnKeyBase(node, propName) + ".sortRank";
74  }
75 
85  static String getColumnHiddenKey(TableFilterNode node, String propName) {
86  return getColumnKeyBase(node, propName) + ".hidden";
87  }
88 
89  private static String getColumnKeyBase(TableFilterNode node, String propName) {
90  return stripNonAlphanumeric(node.getColumnOrderKey()) + "." + stripNonAlphanumeric(propName);
91  }
92 
93  private static String stripNonAlphanumeric(String str) {
94  return str.replaceAll("[^a-zA-Z0-9_]", "");
95  }
96 
109  static List<Node.Property<?>> getAllChildProperties(Node node, int maxRows) {
110  // This is a set because we add properties of up to 100 child nodes, and we want unique properties
111  Set<Node.Property<?>> propertiesAcc = new LinkedHashSet<>();
112  getAllChildPropertiesHelper(node, maxRows, propertiesAcc);
113  return new ArrayList<>(propertiesAcc);
114  }
115 
126  static private void getAllChildPropertiesHelper(Node node, int maxRows, Set<Node.Property<?>> propertiesAcc) {
127  Children children = node.getChildren();
128  int childCount = 0;
129  for (Node child : children.getNodes()) {
130  childCount++;
131  if (childCount > maxRows) {
132  return;
133  }
134  for (Node.PropertySet ps : child.getPropertySets()) {
135  final Node.Property<?>[] props = ps.getProperties();
136  final int propsNum = props.length;
137  for (int j = 0; j < propsNum; ++j) {
138  propertiesAcc.add(props[j]);
139  }
140  }
141  getAllChildPropertiesHelper(child, maxRows, propertiesAcc);
142  }
143  }
144 
155  static List< SortCriterion> loadSortCriteria(TableFilterNode node) {
156  List<Node.Property<?>> availableProperties = ResultViewerPersistence.getAllChildProperties(node, 100);
157  final Preferences preferences = NbPreferences.forModule(DataResultViewerTable.class);
158  java.util.SortedMap<Integer, SortCriterion> criteriaMap = new TreeMap<>();
159  availableProperties.forEach(prop -> {
160  //if the sort rank is undefined, it will be defaulted to 0 => unsorted.
161  Integer sortRank = preferences.getInt(ResultViewerPersistence.getColumnSortRankKey(node, prop.getName()), 0);
162 
163  if (sortRank != 0) {
164  final SortOrder sortOrder = preferences.getBoolean(ResultViewerPersistence.getColumnSortOrderKey(node, prop.getName()), true)
165  ? SortOrder.ASCENDING
166  : SortOrder.DESCENDING;
167  final SortCriterion sortCriterion = new SortCriterion(prop, sortOrder, sortRank);
168  criteriaMap.put(sortRank, sortCriterion);
169  }
170  });
171  return new ArrayList<>(criteriaMap.values());
172  }
173 
177  static class SortCriterion {
178 
179  private final Node.Property<?> prop;
180  private final SortOrder order;
181  private final int rank;
182 
183  int getSortRank() {
184  return rank;
185  }
186 
187  Node.Property<?> getProperty() {
188  return prop;
189  }
190 
191  SortOrder getSortOrder() {
192  return order;
193  }
194 
195  SortCriterion(Node.Property<?> prop, SortOrder order, int rank) {
196  this.prop = prop;
197  this.order = order;
198  this.rank = rank;
199  }
200 
201  @Override
202  public String toString() {
203  return getSortRank() + ". "
204  + getProperty().getName() + " "
205  + (getSortOrder() == SortOrder.ASCENDING
206  ? "\u25B2" // /\
207  : "\u25BC");// \/
208  }
209  }
210 }

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.