View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.gui.table;
22  
23  import java.awt.Component;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.awt.event.MouseEvent;
27  import java.util.EventObject;
28  
29  import javax.swing.AbstractCellEditor;
30  import javax.swing.JRadioButton;
31  import javax.swing.JTable;
32  import javax.swing.table.TableCellEditor;
33  
34  /**
35   * Adapted from source code of javax.swing.DefaultCellEditor
36   */
37  public class RadioButtonCellEditor extends AbstractCellEditor implements
38  		TableCellEditor {
39  
40  	private static final long serialVersionUID = 1L;
41  
42  	JRadioButton radioButton;
43  
44  	protected int clickCountToStart = 1;
45  
46  	public RadioButtonCellEditor() {
47  		radioButton = new JRadioButton();
48  		radioButton.setHorizontalAlignment(JRadioButton.CENTER);
49  		radioButton.addActionListener(new ActionListener() {
50  			public void actionPerformed(ActionEvent e) {
51  				// do not allow de-selection of radioButton
52  				// this is provided for in the method
53  				// resetNonSelectedValues
54  				// of the CustomTableModel class
55  				if (!radioButton.isSelected())
56  					cancelCellEditing();
57  				stopCellEditing();
58  			}
59  		});
60  	}
61  
62  	public Component getTableCellEditorComponent(JTable table,
63  			Object value, boolean isSelected, int row, int col) {
64  		radioButton.setSelected(((Boolean) value).booleanValue());
65  		return radioButton;
66  	}
67  
68  	public Component getComponent() {
69  		return radioButton;
70  	}
71  
72  	public int getClickCountToStart() {
73  		return clickCountToStart;
74  	}
75  
76  	public Object getCellEditorValue() {
77  		return Boolean.valueOf(radioButton.isSelected());
78  	}
79  
80  	public boolean isCellEditable(EventObject anEvent) {
81  		if (anEvent instanceof MouseEvent)
82  			return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
83  		return true;
84  	}
85  
86  	public boolean shouldSelectCell(EventObject anEvent) {
87  		return true;
88  	}
89  
90  	public boolean stopCellEditing(EventObject anEvent) {
91  		fireEditingStopped();
92  		return true;
93  	}
94  
95  	public void cancelCellEditing() {
96  		fireEditingCanceled();
97  	}
98  }