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  /* (swing1.1.1) */
24  import java.awt.Component;
25  import java.awt.event.MouseEvent;
26  import java.util.EventObject;
27  import java.util.Hashtable;
28  
29  import javax.swing.DefaultCellEditor;
30  import javax.swing.JTable;
31  import javax.swing.JTextField;
32  import javax.swing.event.CellEditorListener;
33  import javax.swing.table.TableCellEditor;
34  
35  
36  /**
37   * Each row TableCellEditor
38   * 
39   */
40  
41  public class EachRowEditor implements TableCellEditor {
42  	protected Hashtable editors;
43  
44  	protected TableCellEditor editor, defaultEditor;
45  
46  	JTable table;
47  
48  	/**
49  	 * Constructs a EachRowEditor. create default editor
50  	 * 
51  	 * @see TableCellEditor
52  	 * @see DefaultCellEditor
53  	 */
54  	public EachRowEditor(JTable table) {
55  		this.table = table;
56  		editors = new Hashtable();
57  		defaultEditor = new DefaultCellEditor(new JTextField());
58  	}
59  
60  	/**
61  	 * @param row
62  	 *            table row
63  	 * @param editor
64  	 *            table cell editor
65  	 */
66  	public void setEditorAt(int row, TableCellEditor editor) {
67  		editors.put(new Integer(row), editor);
68  	}
69  
70  	public Component getTableCellEditorComponent(JTable table, Object value,
71  			boolean isSelected, int row, int column) {
72  		// editor = (TableCellEditor)editors.get(new Integer(row));
73  		// if (editor == null) {
74  		// editor = defaultEditor;
75  		// }
76  		return editor.getTableCellEditorComponent(table, value, isSelected,
77  				row, column);
78  	}
79  
80  	public Object getCellEditorValue() {
81  		return editor.getCellEditorValue();
82  	}
83  
84  	public boolean stopCellEditing() {
85  		return editor.stopCellEditing();
86  	}
87  
88  	public void cancelCellEditing() {
89  		editor.cancelCellEditing();
90  	}
91  
92  	public boolean isCellEditable(EventObject anEvent) {
93  		selectEditor((MouseEvent) anEvent);
94  		return editor.isCellEditable(anEvent);
95  	}
96  
97  	public void addCellEditorListener(CellEditorListener l) {
98  		editor.addCellEditorListener(l);
99  	}
100 
101 	public void removeCellEditorListener(CellEditorListener l) {
102 		editor.removeCellEditorListener(l);
103 	}
104 
105 	public boolean shouldSelectCell(EventObject anEvent) {
106 		selectEditor((MouseEvent) anEvent);
107 		return editor.shouldSelectCell(anEvent);
108 	}
109 
110 	protected void selectEditor(MouseEvent e) {
111 		int row;
112 		if (e == null) {
113 			row = table.getSelectionModel().getAnchorSelectionIndex();
114 		} else {
115 			row = table.rowAtPoint(e.getPoint());
116 		}
117 		editor = (TableCellEditor) editors.get(new Integer(row));
118 		if (editor == null) {
119 			editor = defaultEditor;
120 		}
121 	}
122 	
123 }