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