19 package org.sleuthkit.autopsy.report.infrastructure;
22 import java.awt.Component;
23 import java.awt.event.ItemEvent;
24 import java.awt.event.ItemListener;
25 import java.awt.event.MouseAdapter;
26 import java.awt.event.MouseEvent;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.LinkedHashMap;
32 import java.util.List;
34 import java.util.logging.Level;
35 import javax.swing.JCheckBox;
36 import javax.swing.JLabel;
37 import javax.swing.JList;
38 import javax.swing.ListCellRenderer;
39 import javax.swing.ListModel;
40 import javax.swing.event.ListDataListener;
41 import org.openide.util.NbBundle;
52 @SuppressWarnings(
"PMD.SingularField")
53 class PortableCaseTagsListPanel extends javax.swing.JPanel {
55 private List<TagName> tagNames;
56 private final Map<String, Boolean> tagNameSelections =
new LinkedHashMap<>();
57 private final TagNamesListModel tagsNamesListModel =
new TagNamesListModel();
58 private final TagsNamesListCellRenderer tagsNamesRenderer =
new TagsNamesListCellRenderer();
59 private final Map<String, Long> tagCounts =
new HashMap<>();
61 private final ReportWizardPortableCaseOptionsPanel wizPanel;
62 private final PortableCaseReportModuleSettings settings;
63 private final boolean useCaseSpecificData;
68 PortableCaseTagsListPanel(ReportWizardPortableCaseOptionsPanel wizPanel, PortableCaseReportModuleSettings options,
boolean useCaseSpecificData) {
69 this.wizPanel = wizPanel;
70 this.useCaseSpecificData = useCaseSpecificData;
71 this.settings = options;
73 customizeComponents();
75 jAllTagsCheckBox.setSelected(settings.areAllTagsSelected());
76 tagNamesListBox.setEnabled(!jAllTagsCheckBox.isSelected());
77 selectButton.setEnabled(!jAllTagsCheckBox.isSelected());
78 deselectButton.setEnabled(!jAllTagsCheckBox.isSelected());
79 selectAllTags(jAllTagsCheckBox.isSelected());
81 this.jAllTagsCheckBox.addItemListener(
new ItemListener() {
83 public void itemStateChanged(ItemEvent e) {
84 tagNamesListBox.setEnabled(!jAllTagsCheckBox.isSelected());
85 selectButton.setEnabled(!jAllTagsCheckBox.isSelected());
86 deselectButton.setEnabled(!jAllTagsCheckBox.isSelected());
87 selectAllTags(jAllTagsCheckBox.isSelected());
93 "PortableCaseTagsListPanel.error.errorTitle=Error getting tag names for case",
94 "PortableCaseTagsListPanel.error.noOpenCase=There is no case open",
95 "PortableCaseTagsListPanel.error.errorLoadingTags=Error loading tags",
97 private void customizeComponents() {
99 tagNames =
new ArrayList<>();
100 Map<Long, Long> tagCountsByID =
new HashMap<>();
105 if (useCaseSpecificData) {
106 tagNames = Case.getCurrentCaseThrows().getServices().getTagsManager().getTagNamesInUse();
109 String query =
"tag_name_id, count(1) AS tag_count "
111 +
"SELECT tag_name_id FROM content_tags UNION ALL SELECT tag_name_id FROM blackboard_artifact_tags"
112 +
") tag_ids GROUP BY tag_name_id";
113 GetTagCountsCallback callback =
new GetTagCountsCallback();
114 Case.getCurrentCaseThrows().getSleuthkitCase().getCaseDbAccessManager().select(query, callback);
115 tagCountsByID = callback.getTagCountMap();
121 for (TagName tagName : tagNames) {
122 tagNameSelections.put(tagName.getDisplayName(), Boolean.FALSE);
123 if (tagCountsByID.containsKey(tagName.getId())) {
124 tagCounts.put(tagName.getDisplayName(), tagCountsByID.get(tagName.getId()));
126 tagCounts.put(tagName.getDisplayName(), 0L);
129 }
catch (TskCoreException ex) {
130 Logger.getLogger(ReportWizardPortableCaseOptionsVisualPanel.class.getName()).log(Level.SEVERE,
"Failed to get tag names", ex);
131 }
catch (NoCurrentCaseException ex) {
133 Logger.getLogger(ReportWizardPortableCaseOptionsVisualPanel.class.getName()).log(Level.WARNING,
"Exception while getting open case.", ex);
139 tagNamesListBox.setModel(tagsNamesListModel);
140 tagNamesListBox.setCellRenderer(tagsNamesRenderer);
141 tagNamesListBox.setVisibleRowCount(-1);
142 tagNamesListBox.addMouseListener(
new MouseAdapter() {
144 public void mousePressed(MouseEvent evt) {
145 JList<?> list = (JList) evt.getSource();
146 int index = list.locationToIndex(evt.getPoint());
148 String value = tagsNamesListModel.getElementAt(index);
149 tagNameSelections.put(value, !tagNameSelections.get(value));
160 private void updateTagList() {
161 settings.updateTagNames(getSelectedTagNames());
162 settings.setAllTagsSelected(jAllTagsCheckBox.isSelected());
163 wizPanel.setFinish(settings.isValid());
173 return tagNames.size();
178 return tagNames.get(index).getDisplayName();
196 private static final long serialVersionUID = 1L;
201 setEnabled(list.isEnabled());
202 setSelected(tagNameSelections.get(value));
203 setFont(list.getFont());
204 setBackground(list.getBackground());
205 setForeground(list.getForeground());
206 String text = value +
" (" + tagCounts.get(value) +
")";
208 setToolTipText(text);
220 private List<TagName> getSelectedTagNames() {
221 List<TagName> selectedTagNames =
new ArrayList<>();
222 for (TagName tagName : tagNames) {
223 if (tagNameSelections.get(tagName.getDisplayName())) {
224 selectedTagNames.add(tagName);
227 return selectedTagNames;
233 static class GetTagCountsCallback
implements CaseDbAccessManager.CaseDbAccessQueryCallback {
235 private static final Logger logger = Logger.getLogger(GetTagCountsCallback.class.getName());
236 private final Map<Long, Long> tagCounts =
new HashMap<>();
239 public void process(ResultSet rs) {
243 Long tagCount = rs.getLong(
"tag_count");
244 Long tagID = rs.getLong(
"tag_name_id");
246 tagCounts.put(tagID, tagCount);
248 }
catch (SQLException ex) {
249 logger.log(Level.WARNING,
"Unable to get data_source_obj_id or value from result set", ex);
252 }
catch (SQLException ex) {
253 logger.log(Level.WARNING,
"Failed to get next result for values by datasource", ex);
262 Map<Long, Long> getTagCountMap() {
272 @SuppressWarnings(
"unchecked")
274 private
void initComponents() {
276 jPanel1 =
new javax.swing.JPanel();
277 jScrollPane1 =
new javax.swing.JScrollPane();
278 tagNamesListBox =
new javax.swing.JList<>();
279 descLabel =
new javax.swing.JLabel();
280 selectButton =
new javax.swing.JButton();
281 deselectButton =
new javax.swing.JButton();
282 jAllTagsCheckBox =
new javax.swing.JCheckBox();
284 jScrollPane1.setViewportView(tagNamesListBox);
286 org.openide.awt.Mnemonics.setLocalizedText(descLabel,
org.openide.util.NbBundle.getMessage(PortableCaseTagsListPanel.class,
"PortableCaseTagsListPanel.descLabel.text"));
288 org.openide.awt.Mnemonics.setLocalizedText(selectButton,
org.openide.util.NbBundle.getMessage(PortableCaseTagsListPanel.class,
"PortableCaseTagsListPanel.selectButton.text"));
289 selectButton.setMaximumSize(
new java.awt.Dimension(87, 23));
290 selectButton.setMinimumSize(
new java.awt.Dimension(87, 23));
291 selectButton.addActionListener(
new java.awt.event.ActionListener() {
292 public void actionPerformed(java.awt.event.ActionEvent evt) {
293 selectButtonActionPerformed(evt);
297 org.openide.awt.Mnemonics.setLocalizedText(deselectButton,
org.openide.util.NbBundle.getMessage(PortableCaseTagsListPanel.class,
"PortableCaseTagsListPanel.deselectButton.text"));
298 deselectButton.addActionListener(
new java.awt.event.ActionListener() {
299 public void actionPerformed(java.awt.event.ActionEvent evt) {
300 deselectButtonActionPerformed(evt);
304 org.openide.awt.Mnemonics.setLocalizedText(jAllTagsCheckBox,
org.openide.util.NbBundle.getMessage(PortableCaseTagsListPanel.class,
"PortableCaseTagsListPanel.jAllTagsCheckBox.text"));
305 jAllTagsCheckBox.addActionListener(
new java.awt.event.ActionListener() {
306 public void actionPerformed(java.awt.event.ActionEvent evt) {
307 jAllTagsCheckBoxActionPerformed(evt);
311 javax.swing.GroupLayout jPanel1Layout =
new javax.swing.GroupLayout(jPanel1);
312 jPanel1.setLayout(jPanel1Layout);
313 jPanel1Layout.setHorizontalGroup(
314 jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
315 .addGroup(jPanel1Layout.createSequentialGroup()
317 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
318 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
319 .addGroup(jPanel1Layout.createSequentialGroup()
320 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
321 .addComponent(descLabel)
322 .addGroup(jPanel1Layout.createSequentialGroup()
323 .addComponent(selectButton, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
324 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
325 .addComponent(deselectButton))
326 .addComponent(jAllTagsCheckBox))
327 .addGap(0, 0, Short.MAX_VALUE)))
330 jPanel1Layout.setVerticalGroup(
331 jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
332 .addGroup(jPanel1Layout.createSequentialGroup()
333 .addComponent(descLabel)
334 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
335 .addComponent(jAllTagsCheckBox)
336 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
337 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
338 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
339 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
340 .addComponent(selectButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
341 .addComponent(deselectButton)))
344 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(
this);
345 this.setLayout(layout);
346 layout.setHorizontalGroup(
347 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
348 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
350 layout.setVerticalGroup(
351 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
352 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
356 private void deselectButtonActionPerformed(java.awt.event.ActionEvent evt) {
357 selectAllTags(
false);
360 private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {
364 private void jAllTagsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
368 private void selectAllTags(
boolean select) {
369 Boolean state = Boolean.TRUE;
371 state = Boolean.FALSE;
373 for (TagName tagName : tagNames) {
374 tagNameSelections.put(tagName.getDisplayName(), state);
377 tagNamesListBox.repaint();
381 private javax.swing.JLabel descLabel;
382 private javax.swing.JButton deselectButton;
383 private javax.swing.JCheckBox jAllTagsCheckBox;
384 private javax.swing.JPanel jPanel1;
385 private javax.swing.JScrollPane jScrollPane1;
386 private javax.swing.JButton selectButton;
387 private javax.swing.JList<String> tagNamesListBox;