19 package org.sleuthkit.autopsy.geolocation;
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;
31 final class CheckBoxListPanel<T>
extends javax.swing.JPanel {
33 private static final long serialVersionUID = 1L;
35 private final DefaultListModel<ObjectCheckBox<T>> model =
new DefaultListModel<>();
36 private final CheckBoxJList<ObjectCheckBox<T>> checkboxList;
44 checkboxList =
new CheckBoxJList<>();
45 checkboxList.setModel(model);
46 scrollPane.setViewportView(checkboxList);
55 void addElement(String displayName, T obj) {
56 ObjectCheckBox<T> newCheckBox =
new ObjectCheckBox<>(displayName,
true, obj);
58 if(!model.contains(newCheckBox)) {
59 model.addElement(newCheckBox);
67 model.removeAllElements();
71 public void setEnabled(
boolean enabled) {
72 checkboxList.setEnabled(enabled);
73 checkButton.setEnabled(enabled);
74 uncheckButton.setEnabled(enabled);
75 checkboxList.setEnabled(enabled);
83 List<T> getSelectedElements() {
84 List<T> selectedElements =
new ArrayList<>();
85 Enumeration<ObjectCheckBox<T>> elements = model.elements();
87 while (elements.hasMoreElements()) {
88 ObjectCheckBox<T> element = elements.nextElement();
89 if (element.isChecked()) {
90 selectedElements.add(element.getObject());
94 return selectedElements;
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();
118 void setPanelTitle(String title) {
119 titleLabel.setText(title);
127 void setPanelTitleIcon(Icon icon) {
128 titleLabel.setIcon(icon);
136 @SuppressWarnings(
"unchecked")
138 private
void initComponents() {
139 java.awt.GridBagConstraints gridBagConstraints;
141 titleLabel =
new javax.swing.JLabel();
142 uncheckButton =
new javax.swing.JButton();
143 checkButton =
new javax.swing.JButton();
144 scrollPane =
new javax.swing.JScrollPane();
146 setLayout(
new java.awt.GridBagLayout());
148 org.openide.awt.Mnemonics.setLocalizedText(titleLabel,
org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class,
"CheckBoxListPanel.titleLabel.text"));
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);
156 org.openide.awt.Mnemonics.setLocalizedText(uncheckButton,
org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class,
"CheckBoxListPanel.uncheckButton.text"));
157 uncheckButton.addActionListener(
new java.awt.event.ActionListener() {
158 public void actionPerformed(java.awt.event.ActionEvent evt) {
159 uncheckButtonActionPerformed(evt);
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);
170 org.openide.awt.Mnemonics.setLocalizedText(checkButton,
org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class,
"CheckBoxListPanel.checkButton.text"));
171 checkButton.addActionListener(
new java.awt.event.ActionListener() {
172 public void actionPerformed(java.awt.event.ActionEvent evt) {
173 checkButtonActionPerformed(evt);
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);
192 private void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt) {
193 setSetAllSelected(
false);
196 private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {
197 setSetAllSelected(
true);
202 private javax.swing.JButton checkButton;
203 private javax.swing.JScrollPane scrollPane;
204 private javax.swing.JLabel titleLabel;
205 private javax.swing.JButton uncheckButton;
213 final class ObjectCheckBox<T>
implements CheckBoxJList.CheckboxListItem {
215 private static final long serialVersionUID = 1L;
217 private final T object;
218 private final String displayName;
219 private boolean checked;
228 ObjectCheckBox(String displayName,
boolean initialState, T
object) {
229 this.displayName = displayName;
230 this.
object = object;
231 this.checked = initialState;
239 public boolean isChecked() {
244 public void setChecked(
boolean checked) {
245 this.checked = checked;
249 public String getDisplayName() {
254 public boolean equals(Object obj) {
255 if(obj instanceof ObjectCheckBox) {
256 return object.equals(((ObjectCheckBox)obj).
object);
262 public int hashCode() {
264 hash = 31 * hash + Objects.hashCode(this.
object);