KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > fonts > CheckBoxList


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * CheckBoxList.java
28  *
29  * Created on October 5, 2006, 9:53 AM
30  *
31  */

32
33 package it.businesslogic.ireport.gui.fonts;
34
35 /**
36  *
37  * @author gtoffoli
38  */

39 import javax.swing.*;
40
41 import java.awt.*;
42 import java.awt.event.*;
43
44 public class CheckBoxList extends JList
45 {
46     
47    public CheckBoxList()
48    {
49       super();
50       
51       setModel( new DefaultListModel());
52       setCellRenderer(new CheckboxCellRenderer());
53       
54       addMouseListener(new MouseAdapter()
55         {
56             public void mousePressed(MouseEvent e)
57             {
58                int index = locationToIndex(e.getPoint());
59
60                if (index != -1) {
61                   Object JavaDoc obj = getModel().getElementAt(index);
62                   if (obj instanceof JCheckBox)
63                   {
64                     JCheckBox checkbox = (JCheckBox)obj;
65                               
66                     checkbox.setSelected(
67                                      !checkbox.isSelected());
68                     repaint();
69                   }
70                }
71             }
72          }
73       );
74
75       setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
76    }
77    
78    
79    public int[] getCheckedIdexes()
80    {
81        java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
82        DefaultListModel dlm = (DefaultListModel)getModel();
83        for (int i=0; i<dlm.size(); ++i)
84        {
85             Object JavaDoc obj = getModel().getElementAt(i);
86             if (obj instanceof JCheckBox)
87             {
88                 JCheckBox checkbox = (JCheckBox)obj;
89                 if (checkbox.isSelected())
90                 {
91                     list.add(new Integer JavaDoc(i));
92                 }
93             }
94        }
95        
96        int[] indexes = new int[list.size()];
97        
98        for (int i=0; i<list.size(); ++i)
99        {
100             indexes[i] = ((Integer JavaDoc)list.get(i)).intValue();
101        }
102        
103        return indexes;
104    }
105    
106    
107    public java.util.List JavaDoc getCheckedItems()
108    {
109        java.util.List JavaDoc list = new java.util.ArrayList JavaDoc();
110        DefaultListModel dlm = (DefaultListModel)getModel();
111        for (int i=0; i<dlm.size(); ++i)
112        {
113             Object JavaDoc obj = getModel().getElementAt(i);
114             if (obj instanceof JCheckBox)
115             {
116                 JCheckBox checkbox = (JCheckBox)obj;
117                 if (checkbox.isSelected())
118                 {
119                     list.add(checkbox);
120                 }
121             }
122        }
123        return list;
124    }
125 }
126
127
Popular Tags