1 19 20 package org.netbeans.modules.tasklist.core.checklist; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.MouseAdapter ; 24 import java.awt.event.MouseEvent ; 25 import javax.swing.AbstractAction ; 26 import javax.swing.Action ; 27 import javax.swing.JComponent ; 28 import javax.swing.JList ; 29 import javax.swing.KeyStroke ; 30 31 34 public class CheckList extends JList { 35 36 private static final long serialVersionUID = 1; 37 38 47 public CheckList(CheckListModel dataModel) { 48 super(dataModel); 49 setCellRenderer(new DefaultCheckListCellRenderer()); 50 Action action = new CheckAction(); 51 getActionMap().put("check", action); 52 registerKeyboardAction(action, KeyStroke.getKeyStroke(' '), 53 JComponent.WHEN_FOCUSED); 54 addMouseListener( 55 new MouseAdapter () { 56 public void mousePressed(MouseEvent e) { 57 JList list = (JList ) e.getComponent(); 58 59 int index = list.locationToIndex(e.getPoint()); 60 if (index < 0) 61 return; 62 63 if (e.getX() > 15) 64 return; 65 66 CheckListModel model = (CheckListModel) getModel(); 67 model.setChecked(index, !model.isChecked(index)); 68 69 e.consume(); 70 repaint(); 71 } 72 } 73 ); 74 } 75 76 84 public CheckList(boolean[] state, Object [] listData) { 85 this(new DefaultCheckListModel(state, listData)); 86 } 87 88 91 public CheckList() { 92 this(new AbstractCheckListModel() { 93 public boolean isChecked(int index) { 94 return false; 95 } 96 public void setChecked(int index, boolean c) { 97 } 98 public int getSize() { 99 return 0; 100 } 101 public Object getElementAt(int index) { 102 return null; 103 } 104 }); 105 } 106 107 110 public static class CheckAction extends AbstractAction { 111 112 private static final long serialVersionUID = 1; 113 114 public void actionPerformed(ActionEvent e) { 115 JList list = (JList ) e.getSource(); 116 int index = list.getSelectedIndex(); 117 if (index < 0) 118 return; 119 CheckListModel model = (CheckListModel) list.getModel(); 120 model.setChecked(index, !model.isChecked(index)); 121 } 122 } 123 124 129 public void setModel(CheckListModel m) { 130 super.setModel(m); 131 } 132 } 133 | Popular Tags |