package morepaste.util; import java.io.Serializable; import java.awt.*; import java.awt.event.*; import java.util.NoSuchElementException; import javax.swing.*; /** * @version 1.0.1 2002-01-26 * @author Takashi KOBAYASHI */ public class ListChecker extends MouseAdapter { private JList jl; private CheckBoxCellRenderer ccr; public ListChecker( JList l, CheckBoxCellRenderer c ) { jl = l; ccr = c; } public void mouseClicked( MouseEvent e ) { Point p = e.getPoint(); int idx = jl.locationToIndex( p ); CheckBoxListModel clm = (CheckBoxListModel)jl.getModel(); try { if( idx > -1 && idx < jl.getModel().getSize() && clm.getElementAt( idx ) != null && clm.isEnabledAt( idx ) && (e.getModifiers() & InputEvent.BUTTON1_MASK) != 0 && ( ccr.isInCheckBox( toLocationInCell( p ), clm.getElementAt( idx ).toString() ) || e.getClickCount() == 2 ) ) { clm.setSelectedAt( idx, ! clm.isSelectedAt( idx ) ); jl.repaint(); } } catch( NoSuchElementException exp ) {} } private Point toLocationInCell( Point p ) { int idx = jl.locationToIndex( p ); if( idx == 0 ){ return p; } else { Rectangle r = jl.getCellBounds( 0, idx - 1 ); return new Point( p.x, p.y - r.height ); } } }