Autopsy  4.17.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 
34 public final class CheckBoxListPanel<T> extends javax.swing.JPanel {
35 
36  private static final long serialVersionUID = 1L;
37 
38  private final DefaultListModel<ObjectCheckBox<T>> model = new DefaultListModel<>();
40 
44  public CheckBoxListPanel() {
46 
47  checkboxList = new CheckBoxJList<>();
48  checkboxList.setModel(model);
49  scrollPane.setViewportView(checkboxList);
50  }
51 
59  public void addElement(String displayName, Icon icon, T obj) {
60  ObjectCheckBox<T> newCheckBox = new ObjectCheckBox<>(displayName, icon, true, obj);
61 
62  if (!model.contains(newCheckBox)) {
63  model.addElement(newCheckBox);
64  }
65  }
66 
70  public void clearList() {
71  model.removeAllElements();
72  }
73 
74  public boolean isEmpty() {
75  return model.isEmpty();
76  }
77 
78  @Override
79  public void setEnabled(boolean enabled) {
80  checkboxList.setEnabled(enabled);
81  checkButton.setEnabled(enabled);
82  uncheckButton.setEnabled(enabled);
83  checkboxList.setEnabled(enabled);
84  }
85 
91  public List<T> getSelectedElements() {
92  List<T> selectedElements = new ArrayList<>();
93  Enumeration<ObjectCheckBox<T>> elements = model.elements();
94 
95  while (elements.hasMoreElements()) {
96  ObjectCheckBox<T> element = elements.nextElement();
97  if (element.isChecked()) {
98  selectedElements.add(element.getObject());
99  }
100  }
101 
102  return selectedElements;
103  }
104 
112  public void setSelectedElements(List<T> selected) {
113  Set<T> toSelect = selected == null ? Collections.emptySet() : new HashSet<>(selected);
114  for (int i = 0; i < model.size(); i++) {
115  ObjectCheckBox<T> item = model.get(i);
116  boolean shouldBeSelected = toSelect.contains(item.getObject());
117  if (item.isChecked() != shouldBeSelected) {
118  item.setChecked(shouldBeSelected);
119  model.set(i, item);
120  }
121  }
122 
123  checkboxList.repaint();
124  checkboxList.revalidate();
125  }
126 
132  public void setSetAllSelected(boolean selected) {
133  Enumeration<ObjectCheckBox<T>> enumeration = model.elements();
134  while (enumeration.hasMoreElements()) {
135  ObjectCheckBox<T> element = enumeration.nextElement();
136  element.setChecked(selected);
137  checkboxList.repaint();
138  checkboxList.revalidate();
139 
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 
166  @SuppressWarnings("unchecked")
167  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
168  private void initComponents() {
169  java.awt.GridBagConstraints gridBagConstraints;
170 
171  titleLabel = new javax.swing.JLabel();
172  uncheckButton = new javax.swing.JButton();
173  checkButton = new javax.swing.JButton();
174  scrollPane = new javax.swing.JScrollPane();
175 
176  setLayout(new java.awt.GridBagLayout());
177 
178  org.openide.awt.Mnemonics.setLocalizedText(titleLabel, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.titleLabel.text")); // NOI18N
179  gridBagConstraints = new java.awt.GridBagConstraints();
180  gridBagConstraints.gridx = 0;
181  gridBagConstraints.gridy = 0;
182  gridBagConstraints.gridwidth = 3;
183  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
184  add(titleLabel, gridBagConstraints);
185 
186  org.openide.awt.Mnemonics.setLocalizedText(uncheckButton, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.uncheckButton.text")); // NOI18N
187  uncheckButton.addActionListener(new java.awt.event.ActionListener() {
188  public void actionPerformed(java.awt.event.ActionEvent evt) {
190  }
191  });
192  gridBagConstraints = new java.awt.GridBagConstraints();
193  gridBagConstraints.gridx = 1;
194  gridBagConstraints.gridy = 2;
195  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
196  gridBagConstraints.weightx = 1.0;
197  gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 9);
198  add(uncheckButton, gridBagConstraints);
199 
200  org.openide.awt.Mnemonics.setLocalizedText(checkButton, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.checkButton.text")); // NOI18N
201  checkButton.addActionListener(new java.awt.event.ActionListener() {
202  public void actionPerformed(java.awt.event.ActionEvent evt) {
204  }
205  });
206  gridBagConstraints = new java.awt.GridBagConstraints();
207  gridBagConstraints.gridx = 2;
208  gridBagConstraints.gridy = 2;
209  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
210  add(checkButton, gridBagConstraints);
211  gridBagConstraints = new java.awt.GridBagConstraints();
212  gridBagConstraints.gridx = 0;
213  gridBagConstraints.gridy = 1;
214  gridBagConstraints.gridwidth = 3;
215  gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
216  gridBagConstraints.weightx = 1.0;
217  gridBagConstraints.weighty = 1.0;
218  gridBagConstraints.insets = new java.awt.Insets(5, 0, 9, 0);
219  add(scrollPane, gridBagConstraints);
220  }// </editor-fold>//GEN-END:initComponents
221 
222  private void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uncheckButtonActionPerformed
223  setSetAllSelected(false);
224  }//GEN-LAST:event_uncheckButtonActionPerformed
225 
226  private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkButtonActionPerformed
227  setSetAllSelected(true);
228  }//GEN-LAST:event_checkButtonActionPerformed
229 
230 
231  // Variables declaration - do not modify//GEN-BEGIN:variables
232  private javax.swing.JButton checkButton;
233  private javax.swing.JScrollPane scrollPane;
234  private javax.swing.JLabel titleLabel;
235  private javax.swing.JButton uncheckButton;
236  // End of variables declaration//GEN-END:variables
237 
243  final class ObjectCheckBox<T> implements CheckBoxJList.CheckboxListItem {
244 
245  private static final long serialVersionUID = 1L;
246 
247  private final T object;
248  private final String displayName;
249  private final Icon icon;
250  private boolean checked;
251 
260  ObjectCheckBox(String displayName, Icon icon, boolean initialState, T object) {
261  this.displayName = displayName;
262  this.icon = icon;
263  this.object = object;
264  this.checked = initialState;
265  }
266 
267  T getObject() {
268  return object;
269  }
270 
271  @Override
272  public boolean isChecked() {
273  return checked;
274  }
275 
276  @Override
277  public void setChecked(boolean checked) {
278  this.checked = checked;
279  }
280 
281  @Override
282  public String getDisplayName() {
283  return displayName;
284  }
285 
286  @Override
287  public boolean hasIcon() {
288  return icon != null;
289  }
290 
291  @Override
292  public Icon getIcon() {
293  return icon;
294  }
295 
296  @Override
297  public boolean equals(Object obj) {
298  if (obj instanceof ObjectCheckBox) {
299  return object.equals(((ObjectCheckBox) obj).object);
300  }
301  return false;
302  }
303 
304  @Override
305  public int hashCode() {
306  int hash = 7;
307  hash = 31 * hash + Objects.hashCode(this.object);
308  return hash;
309  }
310  }
311 
312 }
final CheckBoxJList< ObjectCheckBox< T > > checkboxList
void checkButtonActionPerformed(java.awt.event.ActionEvent evt)
void addElement(String displayName, Icon icon, T obj)
void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt)
final DefaultListModel< ObjectCheckBox< T > > model

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.