Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
CheckBoxJList.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.awt.BorderLayout;
22 import java.awt.Component;
23 import java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import javax.swing.Icon;
26 import javax.swing.JCheckBox;
27 import javax.swing.JLabel;
28 import javax.swing.JList;
29 import javax.swing.JPanel;
30 import javax.swing.ListCellRenderer;
31 import javax.swing.ListSelectionModel;
32 
38 public final class CheckBoxJList<T extends CheckBoxJList.CheckboxListItem> extends JList<T> {
39 
40  private static final long serialVersionUID = 1L;
41 
47  public interface CheckboxListItem {
48 
54  boolean isChecked();
55 
61  void setChecked(boolean checked);
62 
68  String getDisplayName();
69 
75  boolean hasIcon();
76 
82  Icon getIcon();
83  }
84 
88  public CheckBoxJList() {
89  initalize();
90  }
91 
95  private void initalize() {
96  CellRenderer cellRenderer = new CellRenderer();
97  cellRenderer.init();
98  setCellRenderer(cellRenderer);
99  addMouseListener(new MouseAdapter() {
100  @Override
101  public void mousePressed(MouseEvent e) {
102  int index = locationToIndex(e.getPoint());
103  if (index != -1 && isEnabled()) {
104  CheckBoxJList.CheckboxListItem element = getModel().getElementAt(index);
105  element.setChecked(!element.isChecked());
106  repaint();
107  }
108  }
109  });
110  setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
111  }
112 
116  class CellRenderer extends JPanel implements ListCellRenderer<CheckBoxJList.CheckboxListItem> {
117 
118  private static final long serialVersionUID = 1L;
119 
120  private final JCheckBox checkbox = new JCheckBox();
121  private final JLabel label = new JLabel();
122 
123  public void init() {
124  setLayout(new BorderLayout(2, 0));
125  add(checkbox, BorderLayout.WEST);
126  add(label, BorderLayout.CENTER);
127  }
128 
129  @Override
130  public Component getListCellRendererComponent(
131  JList<? extends CheckBoxJList.CheckboxListItem> list, CheckBoxJList.CheckboxListItem value, int index,
132  boolean isSelected, boolean cellHasFocus) {
133 
134  setBackground(list.getBackground());
135 
136  checkbox.setSelected(value.isChecked());
137  checkbox.setBackground(list.getBackground());
138  checkbox.setEnabled(list.isEnabled());
139  checkbox.setOpaque(list.isOpaque());
140  label.setText(value.getDisplayName());
141  label.setEnabled(list.isEnabled());
142  label.setOpaque(list.isOpaque());
143  label.setBackground(list.getBackground());
144  if (value.hasIcon()) {
145  label.setIcon(value.getIcon());
146  }
147 
148  setOpaque(list.isOpaque());
149  setEnabled(list.isEnabled());
150  return this;
151  }
152  }
153 }

Copyright © 2012-2022 Basis Technology. Generated on: Thu Sep 29 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.