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, Icon icon, T obj) {
56 ObjectCheckBox<T> newCheckBox =
new ObjectCheckBox<>(displayName, icon,
true, obj);
58 if(!model.contains(newCheckBox)) {
59 model.addElement(newCheckBox);
67 model.removeAllElements();
71 return model.isEmpty();
75 public void setEnabled(
boolean enabled) {
76 checkboxList.setEnabled(enabled);
77 checkButton.setEnabled(enabled);
78 uncheckButton.setEnabled(enabled);
79 checkboxList.setEnabled(enabled);
87 List<T> getSelectedElements() {
88 List<T> selectedElements =
new ArrayList<>();
89 Enumeration<ObjectCheckBox<T>> elements = model.elements();
91 while (elements.hasMoreElements()) {
92 ObjectCheckBox<T> element = elements.nextElement();
93 if (element.isChecked()) {
94 selectedElements.add(element.getObject());
98 return selectedElements;
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();
122 void setPanelTitle(String title) {
123 titleLabel.setText(title);
131 void setPanelTitleIcon(Icon icon) {
132 titleLabel.setIcon(icon);
140 @SuppressWarnings(
"unchecked")
142 private
void initComponents() {
143 java.awt.GridBagConstraints gridBagConstraints;
145 titleLabel =
new javax.swing.JLabel();
146 uncheckButton =
new javax.swing.JButton();
147 checkButton =
new javax.swing.JButton();
148 scrollPane =
new javax.swing.JScrollPane();
150 setLayout(
new java.awt.GridBagLayout());
152 org.openide.awt.Mnemonics.setLocalizedText(titleLabel,
org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class,
"CheckBoxListPanel.titleLabel.text"));
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);
160 org.openide.awt.Mnemonics.setLocalizedText(uncheckButton,
org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class,
"CheckBoxListPanel.uncheckButton.text"));
161 uncheckButton.addActionListener(
new java.awt.event.ActionListener() {
162 public void actionPerformed(java.awt.event.ActionEvent evt) {
163 uncheckButtonActionPerformed(evt);
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);
174 org.openide.awt.Mnemonics.setLocalizedText(checkButton,
org.openide.util.NbBundle.getMessage(CheckBoxListPanel.class,
"CheckBoxListPanel.checkButton.text"));
175 checkButton.addActionListener(
new java.awt.event.ActionListener() {
176 public void actionPerformed(java.awt.event.ActionEvent evt) {
177 checkButtonActionPerformed(evt);
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);
196 private void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt) {
197 setSetAllSelected(
false);
200 private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {
201 setSetAllSelected(
true);
206 private javax.swing.JButton checkButton;
207 private javax.swing.JScrollPane scrollPane;
208 private javax.swing.JLabel titleLabel;
209 private javax.swing.JButton uncheckButton;
217 final class ObjectCheckBox<T>
implements CheckBoxJList.CheckboxListItem {
219 private static final long serialVersionUID = 1L;
221 private final T object;
222 private final String displayName;
223 private final Icon icon;
224 private boolean checked;
234 ObjectCheckBox(String displayName, Icon icon,
boolean initialState, T
object) {
235 this.displayName = displayName;
237 this.
object = object;
238 this.checked = initialState;
246 public boolean isChecked() {
251 public void setChecked(
boolean checked) {
252 this.checked = checked;
256 public String getDisplayName() {
261 public boolean hasIcon() {
266 public Icon getIcon() {
271 public boolean equals(Object obj) {
272 if(obj instanceof ObjectCheckBox) {
273 return object.equals(((ObjectCheckBox)obj).
object);
279 public int hashCode() {
281 hash = 31 * hash + Objects.hashCode(this.
object);