Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CheckBoxListPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019-2020 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.guiutils;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Enumeration;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Objects;
27 import java.util.Set;
28 import javax.swing.DefaultListModel;
29 import javax.swing.Icon;
30 import javax.swing.event.ListSelectionEvent;
31 import javax.swing.event.ListSelectionListener;
32 
36 public final class CheckBoxListPanel<T> extends javax.swing.JPanel {
37 
38  private static final long serialVersionUID = 1L;
39 
40  private final DefaultListModel<ObjectCheckBox<T>> model = new DefaultListModel<>();
42 
46  public CheckBoxListPanel() {
48 
49  checkboxList = new CheckBoxJList<>();
50  checkboxList.setModel(model);
51  scrollPane.setViewportView(checkboxList);
52  }
53 
61  public void addElement(String displayName, Icon icon, T obj) {
62  ObjectCheckBox<T> newCheckBox = new ObjectCheckBox<>(displayName, icon, true, obj);
63 
64  if (!model.contains(newCheckBox)) {
65  model.addElement(newCheckBox);
66  }
67  }
68 
72  public void clearList() {
73  model.removeAllElements();
74  }
75 
76  public boolean isEmpty() {
77  return model.isEmpty();
78  }
79 
80  @Override
81  public void setEnabled(boolean enabled) {
82  checkboxList.setEnabled(enabled);
83  checkButton.setEnabled(enabled);
84  uncheckButton.setEnabled(enabled);
85  checkboxList.setEnabled(enabled);
86  }
87 
93  public List<T> getSelectedElements() {
94  List<T> selectedElements = new ArrayList<>();
95  Enumeration<ObjectCheckBox<T>> elements = model.elements();
96 
97  while (elements.hasMoreElements()) {
98  ObjectCheckBox<T> element = elements.nextElement();
99  if (element.isChecked()) {
100  selectedElements.add(element.getObject());
101  }
102  }
103 
104  return selectedElements;
105  }
106 
114  public void setSelectedElements(List<T> selected) {
115  Set<T> toSelect = selected == null ? Collections.emptySet() : new HashSet<>(selected);
116  for (int i = 0; i < model.size(); i++) {
117  ObjectCheckBox<T> item = model.get(i);
118  boolean shouldBeSelected = toSelect.contains(item.getObject());
119  if (item.isChecked() != shouldBeSelected) {
120  item.setChecked(shouldBeSelected);
121  model.set(i, item);
122  }
123  }
124  checkboxList.repaint();
125  checkboxList.revalidate();
126  }
127 
133  public void setSetAllSelected(boolean selected) {
134  Enumeration<ObjectCheckBox<T>> enumeration = model.elements();
135  while (enumeration.hasMoreElements()) {
136  ObjectCheckBox<T> element = enumeration.nextElement();
137  element.setChecked(selected);
138  checkboxList.repaint();
139  checkboxList.revalidate();
140  }
141  }
142 
148  public void setPanelTitle(String title) {
149  titleLabel.setText(title);
150  }
151 
157  public void setPanelTitleIcon(Icon icon) {
158  titleLabel.setIcon(icon);
159  }
160 
167  public void addListSelectionListener(ListSelectionListener listener) {
168  checkboxList.addListSelectionListener(listener);
169  }
170 
176  @SuppressWarnings("unchecked")
177  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
178  private void initComponents() {
179  java.awt.GridBagConstraints gridBagConstraints;
180 
181  titleLabel = new javax.swing.JLabel();
182  uncheckButton = new javax.swing.JButton();
183  checkButton = new javax.swing.JButton();
184  scrollPane = new javax.swing.JScrollPane();
185 
186  setLayout(new java.awt.GridBagLayout());
187 
188  org.openide.awt.Mnemonics.setLocalizedText(titleLabel, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.titleLabel.text")); // NOI18N
189  gridBagConstraints = new java.awt.GridBagConstraints();
190  gridBagConstraints.gridx = 0;
191  gridBagConstraints.gridy = 0;
192  gridBagConstraints.gridwidth = 3;
193  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
194  add(titleLabel, gridBagConstraints);
195 
196  org.openide.awt.Mnemonics.setLocalizedText(uncheckButton, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.uncheckButton.text")); // NOI18N
197  uncheckButton.addActionListener(new java.awt.event.ActionListener() {
198  public void actionPerformed(java.awt.event.ActionEvent evt) {
200  }
201  });
202  gridBagConstraints = new java.awt.GridBagConstraints();
203  gridBagConstraints.gridx = 1;
204  gridBagConstraints.gridy = 2;
205  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
206  gridBagConstraints.weightx = 1.0;
207  gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 9);
208  add(uncheckButton, gridBagConstraints);
209 
210  org.openide.awt.Mnemonics.setLocalizedText(checkButton, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.checkButton.text")); // NOI18N
211  checkButton.addActionListener(new java.awt.event.ActionListener() {
212  public void actionPerformed(java.awt.event.ActionEvent evt) {
214  }
215  });
216  gridBagConstraints = new java.awt.GridBagConstraints();
217  gridBagConstraints.gridx = 2;
218  gridBagConstraints.gridy = 2;
219  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
220  add(checkButton, gridBagConstraints);
221  gridBagConstraints = new java.awt.GridBagConstraints();
222  gridBagConstraints.gridx = 0;
223  gridBagConstraints.gridy = 1;
224  gridBagConstraints.gridwidth = 3;
225  gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
226  gridBagConstraints.weightx = 1.0;
227  gridBagConstraints.weighty = 1.0;
228  gridBagConstraints.insets = new java.awt.Insets(5, 0, 9, 0);
229  add(scrollPane, gridBagConstraints);
230  }// </editor-fold>//GEN-END:initComponents
231 
232  private void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uncheckButtonActionPerformed
233  setSetAllSelected(false);
234  }//GEN-LAST:event_uncheckButtonActionPerformed
235 
236  private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkButtonActionPerformed
237  setSetAllSelected(true);
238  }//GEN-LAST:event_checkButtonActionPerformed
239 
240 
241  // Variables declaration - do not modify//GEN-BEGIN:variables
242  private javax.swing.JButton checkButton;
243  private javax.swing.JScrollPane scrollPane;
244  private javax.swing.JLabel titleLabel;
245  private javax.swing.JButton uncheckButton;
246  // End of variables declaration//GEN-END:variables
247 
253  final class ObjectCheckBox<T> implements CheckBoxJList.CheckboxListItem {
254 
255  private static final long serialVersionUID = 1L;
256 
257  private final T object;
258  private final String displayName;
259  private final Icon icon;
260  private boolean checked;
261 
270  ObjectCheckBox(String displayName, Icon icon, boolean initialState, T object) {
271  this.displayName = displayName;
272  this.icon = icon;
273  this.object = object;
274  this.checked = initialState;
275  }
276 
277  T getObject() {
278  return object;
279  }
280 
281  @Override
282  public boolean isChecked() {
283  return checked;
284  }
285 
286  @Override
287  public void setChecked(boolean checked) {
288  if (this.checked != checked) {
289  this.checked = checked;
290  //notify the list that an items checked status changed
291  if (!isEmpty()) {
292  for (ListSelectionListener listener : checkboxList.getListSelectionListeners()) {
293  listener.valueChanged(new ListSelectionEvent(checkboxList, 0, model.getSize() - 1, false));
294  }
295  }
296  }
297  }
298 
299  @Override
300  public String getDisplayName() {
301  return displayName;
302  }
303 
304  @Override
305  public boolean hasIcon() {
306  return icon != null;
307  }
308 
309  @Override
310  public Icon getIcon() {
311  return icon;
312  }
313 
314  @Override
315  public boolean equals(Object obj) {
316  if (obj instanceof ObjectCheckBox) {
317  return object.equals(((ObjectCheckBox) obj).object);
318  }
319  return false;
320  }
321 
322  @Override
323  public int hashCode() {
324  int hash = 7;
325  hash = 31 * hash + Objects.hashCode(this.object);
326  return hash;
327  }
328  }
329 
330 }
final CheckBoxJList< ObjectCheckBox< T > > checkboxList
void checkButtonActionPerformed(java.awt.event.ActionEvent evt)
void addListSelectionListener(ListSelectionListener listener)
void addElement(String displayName, Icon icon, T obj)
void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt)
final DefaultListModel< ObjectCheckBox< T > > model

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.