19 package org.sleuthkit.autopsy.guiutils;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Enumeration;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Objects;
28 import javax.swing.DefaultListModel;
29 import javax.swing.Icon;
30 import javax.swing.event.ListSelectionEvent;
31 import javax.swing.event.ListSelectionListener;
40 private final DefaultListModel<ObjectCheckBox<T>>
model =
new DefaultListModel<>();
50 checkboxList.setModel(model);
61 public void addElement(String displayName, Icon icon, T obj) {
62 ObjectCheckBox<T> newCheckBox =
new ObjectCheckBox<>(displayName, icon,
true, obj);
64 if (!model.contains(newCheckBox)) {
65 model.addElement(newCheckBox);
73 model.removeAllElements();
77 return model.isEmpty();
82 checkboxList.setEnabled(enabled);
85 checkboxList.setEnabled(enabled);
94 List<T> selectedElements =
new ArrayList<>();
95 Enumeration<ObjectCheckBox<T>> elements = model.elements();
97 while (elements.hasMoreElements()) {
98 ObjectCheckBox<T> element = elements.nextElement();
99 if (element.isChecked()) {
100 selectedElements.add(element.getObject());
104 return selectedElements;
115 Set<T> toSelect = selected == null ? Collections.emptySet() :
new HashSet<>(selected);
116 for (
int i = 0; i < model.size(); i++) {
117 ObjectCheckBox<T> item = model.get(i);
118 boolean shouldBeSelected = toSelect.contains(item.getObject());
119 if (item.isChecked() != shouldBeSelected) {
120 item.setChecked(shouldBeSelected);
124 checkboxList.repaint();
125 checkboxList.revalidate();
134 Enumeration<ObjectCheckBox<T>> enumeration = model.elements();
135 while (enumeration.hasMoreElements()) {
136 ObjectCheckBox<T> element = enumeration.nextElement();
137 element.setChecked(selected);
138 checkboxList.repaint();
139 checkboxList.revalidate();
168 checkboxList.addListSelectionListener(listener);
176 @SuppressWarnings(
"unchecked")
179 java.awt.GridBagConstraints gridBagConstraints;
186 setLayout(
new java.awt.GridBagLayout());
189 gridBagConstraints =
new java.awt.GridBagConstraints();
190 gridBagConstraints.gridx = 0;
191 gridBagConstraints.gridy = 0;
192 gridBagConstraints.gridwidth = 3;
193 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
197 uncheckButton.addActionListener(
new java.awt.event.ActionListener() {
198 public void actionPerformed(java.awt.event.ActionEvent evt) {
202 gridBagConstraints =
new java.awt.GridBagConstraints();
203 gridBagConstraints.gridx = 1;
204 gridBagConstraints.gridy = 2;
205 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
206 gridBagConstraints.weightx = 1.0;
207 gridBagConstraints.insets =
new java.awt.Insets(0, 0, 0, 9);
211 checkButton.addActionListener(
new java.awt.event.ActionListener() {
212 public void actionPerformed(java.awt.event.ActionEvent evt) {
216 gridBagConstraints =
new java.awt.GridBagConstraints();
217 gridBagConstraints.gridx = 2;
218 gridBagConstraints.gridy = 2;
219 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
221 gridBagConstraints =
new java.awt.GridBagConstraints();
222 gridBagConstraints.gridx = 0;
223 gridBagConstraints.gridy = 1;
224 gridBagConstraints.gridwidth = 3;
225 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
226 gridBagConstraints.weightx = 1.0;
227 gridBagConstraints.weighty = 1.0;
228 gridBagConstraints.insets =
new java.awt.Insets(5, 0, 9, 0);
253 final class ObjectCheckBox<T>
implements CheckBoxJList.CheckboxListItem {
255 private static final long serialVersionUID = 1L;
257 private final T object;
258 private final String displayName;
259 private final Icon icon;
260 private boolean checked;
270 ObjectCheckBox(String displayName, Icon icon,
boolean initialState, T
object) {
271 this.displayName = displayName;
273 this.
object = object;
274 this.checked = initialState;
282 public boolean isChecked() {
287 public void setChecked(
boolean checked) {
288 if (this.checked != checked) {
289 this.checked = checked;
292 for (ListSelectionListener listener : checkboxList.getListSelectionListeners()) {
293 listener.valueChanged(
new ListSelectionEvent(checkboxList, 0, model.getSize() - 1,
false));
300 public String getDisplayName() {
305 public boolean hasIcon() {
310 public Icon getIcon() {
315 public boolean equals(Object obj) {
316 if (obj instanceof ObjectCheckBox) {
317 return object.equals(((ObjectCheckBox) obj).
object);
323 public int hashCode() {
325 hash = 31 * hash + Objects.hashCode(this.
object);
javax.swing.JButton checkButton
javax.swing.JScrollPane scrollPane
final CheckBoxJList< ObjectCheckBox< T > > checkboxList
void checkButtonActionPerformed(java.awt.event.ActionEvent evt)
javax.swing.JLabel titleLabel
void addListSelectionListener(ListSelectionListener listener)
void addElement(String displayName, Icon icon, T obj)
javax.swing.JButton uncheckButton
void uncheckButtonActionPerformed(java.awt.event.ActionEvent evt)
void setEnabled(boolean enabled)
void setSetAllSelected(boolean selected)
void setPanelTitleIcon(Icon icon)
List< T > getSelectedElements()
final DefaultListModel< ObjectCheckBox< T > > model
void setSelectedElements(List< T > selected)
static final long serialVersionUID
void setPanelTitle(String title)