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;
38 private final DefaultListModel<ObjectCheckBox<T>>
model =
new DefaultListModel<>();
48 checkboxList.setModel(model);
59 public void addElement(String displayName, Icon icon, T obj) {
60 ObjectCheckBox<T> newCheckBox =
new ObjectCheckBox<>(displayName, icon,
true, obj);
62 if (!model.contains(newCheckBox)) {
63 model.addElement(newCheckBox);
71 model.removeAllElements();
75 return model.isEmpty();
80 checkboxList.setEnabled(enabled);
83 checkboxList.setEnabled(enabled);
92 List<T> selectedElements =
new ArrayList<>();
93 Enumeration<ObjectCheckBox<T>> elements = model.elements();
95 while (elements.hasMoreElements()) {
96 ObjectCheckBox<T> element = elements.nextElement();
97 if (element.isChecked()) {
98 selectedElements.add(element.getObject());
102 return selectedElements;
113 Set<T> toSelect = selected == null ? Collections.emptySet() :
new HashSet<>(selected);
114 for (
int i = 0; i < model.size(); i++) {
115 ObjectCheckBox<T> item = model.get(i);
116 boolean shouldBeSelected = toSelect.contains(item.getObject());
117 if (item.isChecked() != shouldBeSelected) {
118 item.setChecked(shouldBeSelected);
123 checkboxList.repaint();
124 checkboxList.revalidate();
133 Enumeration<ObjectCheckBox<T>> enumeration = model.elements();
134 while (enumeration.hasMoreElements()) {
135 ObjectCheckBox<T> element = enumeration.nextElement();
136 element.setChecked(selected);
137 checkboxList.repaint();
138 checkboxList.revalidate();
166 @SuppressWarnings(
"unchecked")
169 java.awt.GridBagConstraints gridBagConstraints;
176 setLayout(
new java.awt.GridBagLayout());
179 gridBagConstraints =
new java.awt.GridBagConstraints();
180 gridBagConstraints.gridx = 0;
181 gridBagConstraints.gridy = 0;
182 gridBagConstraints.gridwidth = 3;
183 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
187 uncheckButton.addActionListener(
new java.awt.event.ActionListener() {
188 public void actionPerformed(java.awt.event.ActionEvent evt) {
192 gridBagConstraints =
new java.awt.GridBagConstraints();
193 gridBagConstraints.gridx = 1;
194 gridBagConstraints.gridy = 2;
195 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
196 gridBagConstraints.weightx = 1.0;
197 gridBagConstraints.insets =
new java.awt.Insets(0, 0, 0, 9);
201 checkButton.addActionListener(
new java.awt.event.ActionListener() {
202 public void actionPerformed(java.awt.event.ActionEvent evt) {
206 gridBagConstraints =
new java.awt.GridBagConstraints();
207 gridBagConstraints.gridx = 2;
208 gridBagConstraints.gridy = 2;
209 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
211 gridBagConstraints =
new java.awt.GridBagConstraints();
212 gridBagConstraints.gridx = 0;
213 gridBagConstraints.gridy = 1;
214 gridBagConstraints.gridwidth = 3;
215 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
216 gridBagConstraints.weightx = 1.0;
217 gridBagConstraints.weighty = 1.0;
218 gridBagConstraints.insets =
new java.awt.Insets(5, 0, 9, 0);
243 final class ObjectCheckBox<T>
implements CheckBoxJList.CheckboxListItem {
245 private static final long serialVersionUID = 1L;
247 private final T object;
248 private final String displayName;
249 private final Icon icon;
250 private boolean checked;
260 ObjectCheckBox(String displayName, Icon icon,
boolean initialState, T
object) {
261 this.displayName = displayName;
263 this.
object = object;
264 this.checked = initialState;
272 public boolean isChecked() {
277 public void setChecked(
boolean checked) {
278 this.checked = checked;
282 public String getDisplayName() {
287 public boolean hasIcon() {
292 public Icon getIcon() {
297 public boolean equals(Object obj) {
298 if (obj instanceof ObjectCheckBox) {
299 return object.equals(((ObjectCheckBox) obj).
object);
305 public int hashCode() {
307 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 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)