// PathVisio,
// a tool for data visualization and analysis using Biological Pathways
// Copyright 2006-2007 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.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import org.pathvisio.debug.Logger;
 
public class JCheckBoxList extends JList
{
    protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
 
    /**
     * Create a list containing a checkbox for each item. Equivalent to
     * <code>JCheckBoxList(true)</code>
     */
    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());
 
        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();
                	//TODO: Use real checkbox size
                	if(checkOnSelect || p.x <= 20) {
                		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);
    }
 
    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;
        }
    }
}

@@ -924,16 +924,41 @@
 Event;%0A%0A
+import javax.swing.Icon;%0A
 import j
@@ -979,16 +979,16 @@
 eckBox;%0A
-
 import j
@@ -1085,43 +1085,8 @@
 el;%0A
-import javax.swing.SwingUtilities;%0A
 impo
@@ -1187,44 +1187,8 @@
 der;
-%0A%0Aimport org.pathvisio.debug.Logger;
 %0A %0Ap
@@ -1299,24 +1299,50 @@
  1, 1, 1);%0A 
+   private Icon checkIcon;
 %0A    /**%0A   
@@ -1924,24 +1924,89 @@
 derer());%0A %0A
+        checkIcon = UIManager.getIcon(%22CheckBox.icon%22);%0A        %0A
         addM
@@ -2349,56 +2349,8 @@
 ();%0A
-                %09//TODO: Use real checkbox size%0A
     
@@ -2389,18 +2389,40 @@
  p.x %3C= 
-20
+checkIcon.getIconWidth()
 ) %7B%0A    

tweak to jcheckboxlist, now checks for real size of checkbox instead of constant value