// PathVisio, // a tool for data visualization and analysis using Biological Pathways // Copyright 2006-2011 BiGCaT Bioinformatics // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // package org.pathvisio.visualization.plugins; import java.awt.Component; import java.awt.Point; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.Icon; import javax.swing.JCheckBox; import javax.swing.JList; import javax.swing.ListCellRenderer; import javax.swing.ListSelectionModel; import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; /** * A list where each item has a checkbox in front of it. * Useful for selecting a subset from a list such as a list of available Samples. */ public class JCheckBoxList extends JList { protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); private Icon checkIcon; /** * Create a list containing a checkbox for each item. Equivalent to * JCheckBoxList(true) */ public JCheckBoxList() { this(true); } /** * Create a list containing a checkbox for each item * @param checkOnSelect If true, the checkbox is checked/unchecked when the * user clicks on any point within a row. If false, the checkbox is only * checked/unchecked when the user clicks excactly on the checkbox. */ public JCheckBoxList(final boolean checkOnSelect) { setCellRenderer(new CheckBoxCellRenderer()); checkIcon = UIManager.getIcon("CheckBox.icon"); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { int index = locationToIndex(e.getPoint()); if (index != -1) { JCheckBox checkbox = (JCheckBox) getModel().getElementAt(index); Point p = e.getPoint(); if(checkOnSelect || p.x <= checkIcon.getIconWidth()) { checkbox.doClick(); repaint(); } } } }); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { int index = getSelectedIndex(); if (index != -1) { JCheckBox checkbox = (JCheckBox) getModel().getElementAt(index); checkbox.doClick(); repaint(); } } } }); setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } /** list cell renderer that draws item + checkbox in front of it */ protected class CheckBoxCellRenderer implements ListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { JCheckBox checkbox = (JCheckBox) value; checkbox.setBackground(isSelected ? getSelectionBackground() : getBackground()); checkbox.setForeground(isSelected ? getSelectionForeground() : getForeground()); checkbox.setEnabled(isEnabled()); checkbox.setFont(getFont()); checkbox.setFocusPainted(false); checkbox.setBorderPainted(true); checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder); return checkbox; } } }