Autopsy  4.14.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, Icon icon, T obj) {
56  ObjectCheckBox<T> newCheckBox = new ObjectCheckBox<>(displayName, icon, true, obj);
57 
58  if(!model.contains(newCheckBox)) {
59  model.addElement(newCheckBox);
60  }
61  }
62 
66  void clearList() {
67  model.removeAllElements();
68  }
69 
70  boolean isEmpty() {
71  return model.isEmpty();
72  }
73 
74  @Override
75  public void setEnabled(boolean enabled) {
76  checkboxList.setEnabled(enabled);
77  checkButton.setEnabled(enabled);
78  uncheckButton.setEnabled(enabled);
79  checkboxList.setEnabled(enabled);
80  }
81 
87  List<T> getSelectedElements() {
88  List<T> selectedElements = new ArrayList<>();
89  Enumeration<ObjectCheckBox<T>> elements = model.elements();
90 
91  while (elements.hasMoreElements()) {
92  ObjectCheckBox<T> element = elements.nextElement();
93  if (element.isChecked()) {
94  selectedElements.add(element.getObject());
95  }
96  }
97 
98  return selectedElements;
99  }
100 
106  void setSetAllSelected(boolean selected) {
107  Enumeration<ObjectCheckBox<T>> enumeration = model.elements();
108  while (enumeration.hasMoreElements()) {
109  ObjectCheckBox<T> element = enumeration.nextElement();
110  element.setChecked(selected);
111  checkboxList.repaint();
112  checkboxList.revalidate();
113 
114  }
115  }
116 
122  void setPanelTitle(String title) {
123  titleLabel.setText(title);
124  }
125 
131  void setPanelTitleIcon(Icon icon) {
132  titleLabel.setIcon(icon);
133  }
134 
140  @SuppressWarnings("unchecked")
141  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
142  private void initComponents() {
143  java.awt.GridBagConstraints gridBagConstraints;
144 
145  titleLabel = new javax.swing.JLabel();
146  uncheckButton = new javax.swing.JButton();
147  checkButton = new javax.swing.JButton();
148  scrollPane = new javax.swing.JScrollPane();
149 
150  setLayout(new java.awt.GridBagLayout());
151 
152  org.openide.awt.Mnemonics.setLocalizedText(titleLabel, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.titleLabel.text")); // NOI18N
153  gridBagConstraints = new java.awt.GridBagConstraints();
154  gridBagConstraints.gridx = 0;
155  gridBagConstraints.gridy = 0;
156  gridBagConstraints.gridwidth = 3;
157  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
158  add(titleLabel, gridBagConstraints);
159 
160  org.openide.awt.Mnemonics.setLocalizedText(uncheckButton, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.uncheckButton.text")); // NOI18N
161  uncheckButton.addActionListener(new java.awt.event.ActionListener() {
162  public void actionPerformed(java.awt.event.ActionEvent evt) {
163  uncheckButtonActionPerformed(evt);
164  }
165  });
166  gridBagConstraints = new java.awt.GridBagConstraints();
167  gridBagConstraints.gridx = 1;
168  gridBagConstraints.gridy = 2;
169  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
170  gridBagConstraints.weightx = 1.0;
171  gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 9);
172  add(uncheckButton, gridBagConstraints);
173 
174  org.openide.awt.Mnemonics.setLocalizedText(checkButton, org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class, "CheckBoxListPanel.checkButton.text")); // NOI18N
175  checkButton.addActionListener(new java.awt.event.ActionListener() {
176  public void actionPerformed(java.awt.event.ActionEvent evt) {
177  checkButtonActionPerformed(evt);
178  }
179  });
180  gridBagConstraints = new java.awt.GridBagConstraints();
181  gridBagConstraints.gridx = 2;
182  gridBagConstraints.gridy = 2;
183  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
184  add(checkButton, gridBagConstraints);
185  gridBagConstraints = new java.awt.GridBagConstraints();
186  gridBagConstraints.gridx = 0;
187  gridBagConstraints.gridy = 1;
188  gridBagConstraints.gridwidth = 3;
189  gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
190  gridBagConstraints.weightx = 1.0;
191  gridBagConstraints.weighty = 1.0;
192  gridBagConstraints.insets = new java.awt.Insets(5, 0, 9, 0);
193  add(scrollPane, gridBagConstraints);
194  }// </editor-fold>//GEN-END:initComponents
195 
196  private void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uncheckButtonActionPerformed
197  setSetAllSelected(false);
198  }//GEN-LAST:event_uncheckButtonActionPerformed
199 
200  private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkButtonActionPerformed
201  setSetAllSelected(true);
202  }//GEN-LAST:event_checkButtonActionPerformed
203 
204 
205  // Variables declaration - do not modify//GEN-BEGIN:variables
206  private javax.swing.JButton checkButton;
207  private javax.swing.JScrollPane scrollPane;
208  private javax.swing.JLabel titleLabel;
209  private javax.swing.JButton uncheckButton;
210  // End of variables declaration//GEN-END:variables
211 
217  final class ObjectCheckBox<T> implements CheckBoxJList.CheckboxListItem {
218 
219  private static final long serialVersionUID = 1L;
220 
221  private final T object;
222  private final String displayName;
223  private final Icon icon;
224  private boolean checked;
225 
234  ObjectCheckBox(String displayName, Icon icon, boolean initialState, T object) {
235  this.displayName = displayName;
236  this.icon = icon;
237  this.object = object;
238  this.checked = initialState;
239  }
240 
241  T getObject() {
242  return object;
243  }
244 
245  @Override
246  public boolean isChecked() {
247  return checked;
248  }
249 
250  @Override
251  public void setChecked(boolean checked) {
252  this.checked = checked;
253  }
254 
255  @Override
256  public String getDisplayName() {
257  return displayName;
258  }
259 
260  @Override
261  public boolean hasIcon() {
262  return icon != null;
263  }
264 
265  @Override
266  public Icon getIcon() {
267  return icon;
268  }
269 
270  @Override
271  public boolean equals(Object obj) {
272  if(obj instanceof ObjectCheckBox) {
273  return object.equals(((ObjectCheckBox)obj).object);
274  }
275  return false;
276  }
277 
278  @Override
279  public int hashCode() {
280  int hash = 7;
281  hash = 31 * hash + Objects.hashCode(this.object);
282  return hash;
283  }
284  }
285 
286 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.