Autopsy  4.14.0
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 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.geolocation;
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 
36 final class CheckBoxJList<T extends CheckBoxJList.CheckboxListItem> extends JList<T> {
37 
38  private static final long serialVersionUID = 1L;
39 
45  interface CheckboxListItem {
46 
52  boolean isChecked();
53 
59  void setChecked(boolean checked);
60 
66  String getDisplayName();
67 
73  boolean hasIcon();
74 
80  Icon getIcon();
81  }
82 
86  CheckBoxJList() {
87  initalize();
88  }
89 
93  private void initalize() {
94  CellRenderer cellRenderer = new CellRenderer();
95  cellRenderer.init();
96  setCellRenderer(cellRenderer);
97  addMouseListener(new MouseAdapter() {
98  @Override
99  public void mousePressed(MouseEvent e) {
100  int index = locationToIndex(e.getPoint());
101  if (index != -1) {
102  CheckBoxJList.CheckboxListItem element = getModel().getElementAt(index);
103  element.setChecked(!element.isChecked());
104  repaint();
105  }
106  }
107  });
108  setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
109  }
110 
114  class CellRenderer extends JPanel implements ListCellRenderer<CheckBoxJList.CheckboxListItem> {
115 
116  private static final long serialVersionUID = 1L;
117 
118  private final JCheckBox checkbox = new JCheckBox();
119  private final JLabel label = new JLabel();
120 
121  public void init() {
122  setLayout(new BorderLayout(2, 0));
123  add(checkbox, BorderLayout.WEST);
124  add(label, BorderLayout.CENTER);
125  }
126 
127  @Override
128  public Component getListCellRendererComponent(
129  JList<? extends CheckBoxJList.CheckboxListItem> list, CheckBoxJList.CheckboxListItem value, int index,
130  boolean isSelected, boolean cellHasFocus) {
131 
132  setBackground(list.getBackground());
133 
134  checkbox.setSelected(value.isChecked());
135  checkbox.setBackground(list.getBackground());
136  checkbox.setEnabled(list.isEnabled());
137  label.setText(value.getDisplayName());
138  label.setEnabled(list.isEnabled());
139  if (value.hasIcon()) {
140  label.setIcon(value.getIcon());
141  }
142 
143  setEnabled(list.isEnabled());
144  return this;
145  }
146  }
147 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.