19 package org.sleuthkit.autopsy.datamodel;
21 import com.google.common.collect.Lists;
22 import com.google.common.eventbus.EventBus;
23 import com.google.common.eventbus.Subscribe;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.function.Predicate;
30 import java.util.logging.Level;
32 import java.util.prefs.PreferenceChangeEvent;
33 import java.util.stream.Collectors;
34 import org.openide.nodes.ChildFactory;
35 import org.openide.util.NbBundle.Messages;
45 public abstract class BaseChildFactory<T
extends Content> extends ChildFactory.Detachable<T> {
62 "# {0} - node name",
"BaseChildFactory.NoSuchEventBusException.message=No event bus for node: {0}"
67 super(Bundle.BaseChildFactory_NoSuchEventBusException_message(nodeName));
78 public static void register(String nodeName, Object subscriber) {
79 EventBus bus = nodeNameToEventBusMap.get(nodeName);
81 bus =
new EventBus(nodeName);
82 nodeNameToEventBusMap.put(nodeName, bus);
84 bus.register(subscriber);
97 EventBus bus = nodeNameToEventBusMap.get(nodeName);
108 this(nodeName, x ->
true);
112 pagingSupport =
new PagingSupport(nodeName);
113 pagingSupport.initialize();
114 isPageChangeEvent =
false;
115 isPageSizeChangeEvent =
false;
135 protected abstract List<T>
makeKeys();
140 protected abstract void onAdd();
155 if (!isPageChangeEvent && !isPageSizeChangeEvent) {
158 pagingSupport.splitKeysIntoPages(allKeys.stream().filter(filter).collect(Collectors.toList()));
161 toPopulate.addAll(pagingSupport.getCurrentPage());
164 isPageChangeEvent =
false;
165 isPageSizeChangeEvent =
false;
185 pageNumber = newPageNumber;
201 pageCount = newPageCount;
217 pageSize = newPageSize;
247 pages =
new ArrayList<>();
270 List<T> getCurrentPage() {
271 if (!pages.isEmpty()) {
272 return pages.get(currentPage - 1);
275 return Collections.emptyList();
283 void splitKeysIntoPages(List<T> keys) {
284 int oldPageCount = pages.size();
290 if (keys.isEmpty() && !pages.isEmpty()) {
297 pages =
new ArrayList<>();
299 pages = Lists.partition(keys, pageSize > 0 ? pageSize : keys.size());
302 if (pages.size() != oldPageCount) {
305 post(nodeName,
new PageCountChangeEvent(pages.size()));
306 }
catch (NoSuchEventBusException ex) {
307 logger.log(Level.WARNING,
"Failed to post page change event.", ex);
321 currentPage =
event.getPageNumber();
322 isPageChangeEvent =
true;
336 int newPageSize =
event.getPageSize();
337 if (pageSize == newPageSize) {
342 pageSize = newPageSize;
343 splitKeysIntoPages(pages.stream().flatMap(List::stream).collect(Collectors.toList()));
346 isPageSizeChangeEvent =
true;
boolean isPageSizeChangeEvent
static Map< String, EventBus > nodeNameToEventBusMap
void subscribeToPageChange(PageChangeEvent event)
boolean createKeys(List< T > toPopulate)
void subscribeToPageSizeChange(PageSizeChangeEvent event)
NoSuchEventBusException(String nodeName)
BaseChildFactory(String nodeName)
PageSizeChangeEvent(int newPageSize)
final PagingSupport pagingSupport
static final String RESULTS_TABLE_PAGE_SIZE
boolean isPageChangeEvent
static final Logger logger
static void post(String nodeName, Object event)
static int getResultsTablePageSize()
PageChangeEvent(int newPageNumber)
synchronized static Logger getLogger(String name)
static void addChangeListener(PreferenceChangeListener listener)
PageCountChangeEvent(int newPageCount)
BaseChildFactory(String nodeName, Predicate< T > filter)
abstract List< T > makeKeys()
void subscribeToRefreshKeys(RefreshKeysEvent event)