001 /** 002 * planningtool - A Planning Tool with Critiquing Support. 003 * 004 * Copyright (C) 2006 olale 005 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License 008 * as published by the Free Software Foundation; either version 2 009 * of the License, or (at your option) any later version. 010 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 020 * Contact information: 021 * E-mail: olale@ida.liu.se 022 * olale@lysator.liu.se 023 */ 024 package se.liu.ida.critiquer.gui; 025 026 import java.awt.Component; 027 import java.awt.event.MouseAdapter; 028 import java.awt.event.MouseEvent; 029 030 import javax.swing.JCheckBox; 031 import javax.swing.JList; 032 import javax.swing.ListCellRenderer; 033 import javax.swing.ListModel; 034 import javax.swing.ListSelectionModel; 035 import javax.swing.border.Border; 036 import javax.swing.border.EmptyBorder; 037 038 public class CheckBoxList extends JList { 039 /** 040 * 041 */ 042 private static final long serialVersionUID = 1L; 043 protected static Border noFocusBorder = 044 new EmptyBorder(1, 1, 1, 1); 045 046 047 /** 048 * @param dataModel 049 */ 050 public CheckBoxList(ListModel dataModel) { 051 super(dataModel); 052 init(); 053 } 054 055 public CheckBoxList() { 056 init(); 057 } 058 059 private void init() { 060 setCellRenderer(new CellRenderer()); 061 062 addMouseListener(new MouseAdapter() 063 { 064 public void mousePressed(MouseEvent e) 065 { 066 int index = locationToIndex(e.getPoint()); 067 068 if (index != -1) { 069 JCheckBox checkbox = (JCheckBox) 070 getModel().getElementAt(index); 071 checkbox.setSelected( 072 !checkbox.isSelected()); 073 repaint(); 074 } 075 } 076 } 077 ); 078 079 setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 080 } 081 082 protected class CellRenderer implements ListCellRenderer 083 { 084 public Component getListCellRendererComponent( 085 JList list, Object value, int index, 086 boolean isSelected, boolean cellHasFocus) 087 { 088 return (JCheckBox) value; 089 } 090 } 091 } 092