Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AbstractLoadableComponent.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 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.datasourcesummary.uiutils;
20 
21 import java.util.Collection;
22 import java.util.logging.Level;
23 import javax.swing.JPanel;
24 import org.openide.util.NbBundle;
26 
30 @NbBundle.Messages({
31  "AbstractLoadableComponent_loadingMessage_defaultText=Loading results...",
32  "AbstractLoadableComponent_errorMessage_defaultText=There was an error loading results.",
33  "AbstractLoadableComponent_noDataExists_defaultText=No data exists.",})
34 public abstract class AbstractLoadableComponent<T> extends JPanel implements LoadableComponent<T> {
35 
36  private static final long serialVersionUID = 1L;
37 
41  public static final String DEFAULT_LOADING_MESSAGE = Bundle.AbstractLoadableComponent_loadingMessage_defaultText();
42 
46  public static final String DEFAULT_ERROR_MESSAGE = Bundle.AbstractLoadableComponent_errorMessage_defaultText();
47 
51  public static final String DEFAULT_NO_RESULTS_MESSAGE = Bundle.AbstractLoadableComponent_noDataExists_defaultText();
52 
53  private static final Logger logger = Logger.getLogger(AbstractLoadableComponent.class.getName());
54 
58  public static String getDefaultErrorMessage() {
59  return DEFAULT_ERROR_MESSAGE;
60  }
61 
65  public static String getDefaultNoResultsMessage() {
66  return DEFAULT_NO_RESULTS_MESSAGE;
67  }
68 
75  public synchronized void showMessage(String message) {
76  setResults(null);
77  setMessage(true, message);
78  repaint();
79  }
80 
85  public void showDefaultLoadingMessage() {
86  showMessage(DEFAULT_LOADING_MESSAGE);
87  }
88 
96  public synchronized void showResults(T data) {
97  setMessage(false, null);
98  setResults(data);
99  repaint();
100  }
101 
114  public void showDataFetchResult(DataFetchResult<T> result, String errorMessage, String noResultsMessage) {
115  if (result == null) {
116  logger.log(Level.SEVERE, "Null data fetch result received.");
117  return;
118  }
119 
120  switch (result.getResultType()) {
121  case SUCCESS:
122  T data = result.getData();
123  if (data == null || (data instanceof Collection<?> && ((Collection<?>) data).isEmpty())) {
124  showMessage(noResultsMessage);
125  } else {
126  showResults(data);
127  }
128  break;
129  case ERROR:
130  // if there is an error, log accordingly, set result list to
131  // empty and display error message
132  logger.log(Level.WARNING, "An exception was caused while results were loaded.", result.getException());
133  showMessage(errorMessage);
134  break;
135  default:
136  // an unknown loading state was specified. log accordingly.
137  logger.log(Level.SEVERE, "No known loading state was found in result.");
138  break;
139  }
140  }
141 
151  showDataFetchResult(result, DEFAULT_ERROR_MESSAGE, DEFAULT_NO_RESULTS_MESSAGE);
152  }
153 
161  protected abstract void setMessage(boolean visible, String message);
162 
169  protected abstract void setResults(T data);
170 }
void showDataFetchResult(DataFetchResult< T > result, String errorMessage, String noResultsMessage)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.