Autopsy  4.13.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 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.geolocation;
20 
21 import java.util.ArrayList;
22 import java.util.Enumeration;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.swing.DefaultListModel;
26 import javax.swing.Icon;
27 
31 final class CheckBoxListPanel<T> extends javax.swing.JPanel {
32 
33  private static final long serialVersionUID = 1L;
34 
35  private final DefaultListModel<ObjectCheckBox<T>> model = new DefaultListModel<>();
36  private final CheckBoxJList<ObjectCheckBox<T>> checkboxList;
37 
41  CheckBoxListPanel() {
42  initComponents();
43 
44  checkboxList = new CheckBoxJList<>();
45  checkboxList.setModel(model);
46  scrollPane.setViewportView(checkboxList);
47  }
48 
55  void addElement(String displayName, T obj) {
56  ObjectCheckBox<T> newCheckBox = new ObjectCheckBox<>(displayName, true, obj);
57 
58  if(!model.contains(newCheckBox)) {
59  model.addElement(newCheckBox);
60  }
61  }
62 
66  void clearList() {
67  model.removeAllElements();
68  }
69 
70  @Override
71  public void setEnabled(boolean enabled) {
72  checkboxList.setEnabled(enabled);
73  checkButton.setEnabled(enabled);
74  uncheckButton.setEnabled(enabled);
75  checkboxList.setEnabled(enabled);
76  }
77 
83  List<T> getSelectedElements() {
84  List<T> selectedElements = new ArrayList<>();
85  Enumeration<ObjectCheckBox<T>> elements = model.elements();
86 
87  while (elements.hasMoreElements()) {
88  ObjectCheckBox<T> element = elements.nextElement();
89  if (element.isChecked()) {
90  selectedElements.add(element.getObject());
91  }
92  }
93 
94  return selectedElements;
95  }
96 
102  void setSetAllSelected(boolean selected) {
103  Enumeration<ObjectCheckBox<T>> enumeration = model.elements();
104  while (enumeration.hasMoreElements()) {
105  ObjectCheckBox<T> element = enumeration.nextElement();
106  element.setChecked(selected);
107  checkboxList.repaint();
108  checkboxList.revalidate();
109 
110  }
111  }
112 
118  void setPanelTitle(String title) {
119  titleLabel.setText(title);
120  }
121 
127  void setPanelTitleIcon(Icon icon) {
128  titleLabel.setIcon(icon);
129  }
130 
136  @SuppressWarnings("unchecked")
137  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
138  private void initComponents() {
139  java.awt.GridBagConstraints gridBagConstraints;
140 
141  titleLabel = new javax.swing.JLabel();
142  uncheckButton = new javax.swing.JButton();
143  checkButton = new javax.swing.JButton();
144  scrollPane = new javax.swing.JScrollPane();
145 
146  setLayout(new java.awt.GridBagLayout());
147 
148  org.openide.awt.Mnemonics.setLocalizedText(titleLabel, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.titleLabel.text")); // NOI18N
149  gridBagConstraints = new java.awt.GridBagConstraints();
150  gridBagConstraints.gridx = 0;
151  gridBagConstraints.gridy = 0;
152  gridBagConstraints.gridwidth = 3;
153  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
154  add(titleLabel, gridBagConstraints);
155 
156  org.openide.awt.Mnemonics.setLocalizedText(uncheckButton, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.uncheckButton.text")); // NOI18N
157  uncheckButton.addActionListener(new java.awt.event.ActionListener() {
158  public void actionPerformed(java.awt.event.ActionEvent evt) {
159  uncheckButtonActionPerformed(evt);
160  }
161  });
162  gridBagConstraints = new java.awt.GridBagConstraints();
163  gridBagConstraints.gridx = 1;
164  gridBagConstraints.gridy = 2;
165  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
166  gridBagConstraints.weightx = 1.0;
167  gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 9);
168  add(uncheckButton, gridBagConstraints);
169 
170  org.openide.awt.Mnemonics.setLocalizedText(checkButton, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.checkButton.text")); // NOI18N
171  checkButton.addActionListener(new java.awt.event.ActionListener() {
172  public void actionPerformed(java.awt.event.ActionEvent evt) {
173  checkButtonActionPerformed(evt);
174  }
175  });
176  gridBagConstraints = new java.awt.GridBagConstraints();
177  gridBagConstraints.gridx = 2;
178  gridBagConstraints.gridy = 2;
179  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
180  add(checkButton, gridBagConstraints);
181  gridBagConstraints = new java.awt.GridBagConstraints();
182  gridBagConstraints.gridx = 0;
183  gridBagConstraints.gridy = 1;
184  gridBagConstraints.gridwidth = 3;
185  gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
186  gridBagConstraints.weightx = 1.0;
187  gridBagConstraints.weighty = 1.0;
188  gridBagConstraints.insets = new java.awt.Insets(5, 0, 9, 0);
189  add(scrollPane, gridBagConstraints);
190  }// </editor-fold>//GEN-END:initComponents
191 
192  private void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uncheckButtonActionPerformed
193  setSetAllSelected(false);
194  }//GEN-LAST:event_uncheckButtonActionPerformed
195 
196  private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkButtonActionPerformed
197  setSetAllSelected(true);
198  }//GEN-LAST:event_checkButtonActionPerformed
199 
200 
201  // Variables declaration - do not modify//GEN-BEGIN:variables
202  private javax.swing.JButton checkButton;
203  private javax.swing.JScrollPane scrollPane;
204  private javax.swing.JLabel titleLabel;
205  private javax.swing.JButton uncheckButton;
206  // End of variables declaration//GEN-END:variables
207 
213  final class ObjectCheckBox<T> implements CheckBoxJList.CheckboxListItem {
214 
215  private static final long serialVersionUID = 1L;
216 
217  private final T object;
218  private final String displayName;
219  private boolean checked;
220 
228  ObjectCheckBox(String displayName, boolean initialState, T object) {
229  this.displayName = displayName;
230  this.object = object;
231  this.checked = initialState;
232  }
233 
234  T getObject() {
235  return object;
236  }
237 
238  @Override
239  public boolean isChecked() {
240  return checked;
241  }
242 
243  @Override
244  public void setChecked(boolean checked) {
245  this.checked = checked;
246  }
247 
248  @Override
249  public String getDisplayName() {
250  return displayName;
251  }
252 
253  @Override
254  public boolean equals(Object obj) {
255  if(obj instanceof ObjectCheckBox) {
256  return object.equals(((ObjectCheckBox)obj).object);
257  }
258  return false;
259  }
260 
261  @Override
262  public int hashCode() {
263  int hash = 7;
264  hash = 31 * hash + Objects.hashCode(this.object);
265  return hash;
266  }
267  }
268 
269 }

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