KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > checklist > CheckList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.checklist;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.MouseAdapter JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.KeyStroke JavaDoc;
30
31 /**
32  * List with checkboxes.
33  */

34 public class CheckList extends JList JavaDoc {
35
36     private static final long serialVersionUID = 1;
37
38     /**
39      * Constructs a <code>CheckList</code> that displays the elements in the
40      * specified, non-<code>null</code> model.
41      * All <code>CheckList</code> constructors delegate to this one.
42      *
43      * @param dataModel the data model for this list
44      * @exception IllegalArgumentException if <code>dataModel</code>
45      * is <code>null</code>
46      */

47     public CheckList(CheckListModel dataModel) {
48         super(dataModel);
49         setCellRenderer(new DefaultCheckListCellRenderer());
50         Action JavaDoc action = new CheckAction();
51         getActionMap().put("check", action);
52         registerKeyboardAction(action, KeyStroke.getKeyStroke(' '),
53             JComponent.WHEN_FOCUSED);
54         addMouseListener(
55             new MouseAdapter JavaDoc() {
56                 public void mousePressed(MouseEvent JavaDoc e) {
57                     JList JavaDoc list = (JList JavaDoc) 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     /**
77      * Constructs a <code>JList</code> that displays the elements in
78      * the specified array. This constructor just delegates to the
79      * <code>ListModel</code> constructor.
80      *
81      * @param state state of the checkboxes
82      * @param listData the array of Objects to be loaded into the data model
83      */

84     public CheckList(boolean[] state, Object JavaDoc[] listData) {
85         this(new DefaultCheckListModel(state, listData));
86     }
87
88     /**
89      * Constructs a <code>CheckList</code> with an empty model.
90      */

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 JavaDoc getElementAt(int index) {
102                 return null;
103             }
104         });
105     }
106     
107     /**
108      * Check/uncheck currently selected item
109      */

110     public static class CheckAction extends AbstractAction JavaDoc {
111
112         private static final long serialVersionUID = 1;
113
114         public void actionPerformed(ActionEvent JavaDoc e) {
115         JList JavaDoc list = (JList JavaDoc) 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     /**
125      * Sets new model
126      *
127      * @param m new model != null
128      */

129     public void setModel(CheckListModel m) {
130         super.setModel(m);
131     }
132 }
133
Popular Tags