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;
134 protected abstract List<T>
makeKeys();
139 protected abstract void onAdd();
154 if (!isPageChangeEvent && !isPageSizeChangeEvent) {
157 pagingSupport.splitKeysIntoPages(allKeys.stream().filter(filter).collect(Collectors.toList()));
160 toPopulate.addAll(pagingSupport.getCurrentPage());
163 isPageChangeEvent =
false;
164 isPageSizeChangeEvent =
false;
184 pageNumber = newPageNumber;
200 pageCount = newPageCount;
216 pageSize = newPageSize;
246 pages =
new ArrayList<>();
269 List<T> getCurrentPage() {
270 if (!pages.isEmpty()) {
271 return pages.get(currentPage - 1);
274 return Collections.emptyList();
282 void splitKeysIntoPages(List<T> keys) {
283 int oldPageCount = pages.size();
289 if (keys.isEmpty() && !pages.isEmpty()) {
296 pages =
new ArrayList<>();
298 pages = Lists.partition(keys, pageSize > 0 ? pageSize : keys.size());
301 if (pages.size() != oldPageCount) {
304 post(nodeName,
new PageCountChangeEvent(pages.size()));
305 }
catch (NoSuchEventBusException ex) {
306 logger.log(Level.WARNING,
"Failed to post page change event.", ex);
320 currentPage =
event.getPageNumber();
321 isPageChangeEvent =
true;
335 int newPageSize =
event.getPageSize();
336 if (pageSize == newPageSize) {
341 pageSize = newPageSize;
342 splitKeysIntoPages(pages.stream().flatMap(List::stream).collect(Collectors.toList()));
345 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)