19 package org.sleuthkit.autopsy.corecomponents;
21 import java.util.ArrayList;
22 import java.util.LinkedHashSet;
23 import java.util.List;
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;
32 final class ResultViewerPersistence {
34 private ResultViewerPersistence() {
46 static String getColumnPositionKey(TableFilterNode node, String propName) {
47 return getColumnKeyBase(node, propName) +
".column";
59 static String getColumnSortOrderKey(TableFilterNode node, String propName) {
60 return getColumnKeyBase(node, propName) +
".sortOrder";
72 static String getColumnSortRankKey(TableFilterNode node, String propName) {
73 return getColumnKeyBase(node, propName) +
".sortRank";
85 static String getColumnHiddenKey(TableFilterNode node, String propName) {
86 return getColumnKeyBase(node, propName) +
".hidden";
89 private static String getColumnKeyBase(TableFilterNode node, String propName) {
90 return stripNonAlphanumeric(node.getColumnOrderKey()) +
"." + stripNonAlphanumeric(propName);
93 private static String stripNonAlphanumeric(String str) {
94 return str.replaceAll(
"[^a-zA-Z0-9_]",
"");
109 static List<Node.Property<?>> getAllChildProperties(Node node,
int maxRows) {
111 Set<Node.Property<?>> propertiesAcc =
new LinkedHashSet<>();
112 getAllChildPropertiesHelper(node, maxRows, propertiesAcc);
113 return new ArrayList<>(propertiesAcc);
126 static private void getAllChildPropertiesHelper(Node node,
int maxRows, Set<Node.Property<?>> propertiesAcc) {
127 Children children = node.getChildren();
129 for (Node child : children.getNodes(
true)) {
131 if (childCount > maxRows) {
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]);
141 getAllChildPropertiesHelper(child, maxRows, propertiesAcc);
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 -> {
161 Integer sortRank = preferences.getInt(ResultViewerPersistence.getColumnSortRankKey(node, prop.getName()), 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);
171 return new ArrayList<>(criteriaMap.values());
177 static class SortCriterion {
179 private final Node.Property<?> prop;
180 private final SortOrder order;
181 private final int rank;
187 Node.Property<?> getProperty() {
191 SortOrder getSortOrder() {
195 SortCriterion(Node.Property<?> prop, SortOrder order,
int rank) {
202 public String toString() {
203 return getSortRank() +
". "
204 + getProperty().getName() +
" "
205 + (getSortOrder() == SortOrder.ASCENDING