View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.table;
21  
22  import javax.swing.event.TableModelEvent;
23  import javax.swing.table.AbstractTableModel;
24  
25  
26  /**
27   * <code>TableModelRows</code> is implementation of <code>TableModel</code>,
28   * which accepts data in form of <code>TableRowModel</code>, which cvan be set
29   * only as construction parameter.
30   *
31   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
32   * @version $Id: TableModelRows.java,v 1.6 2008-04-22 12:28:40 jbobnar Exp $
33   *
34   * @since May 3, 2004.
35   */
36  public class TableModelRows extends AbstractTableModel
37  	implements TableRowModelListener
38  {
39  	private static final long serialVersionUID = 1L;
40  	private TableRowModel rowModel;
41  
42  	/**
43  	 * Creates a new TableModelRows object.
44  	 *
45  	 * @param model the row mode, which is used as data source
46  	 */
47  	public TableModelRows(TableRowModel model)
48  	{
49  		this();
50  		rowModel = model;
51  		model.addTableRowModelListener(this);
52  	}
53  
54  	protected TableModelRows()
55  	{
56  		super();
57  	}
58  
59  	/*
60  	 * @see javax.swing.table.TableModel#getColumnCount()
61  	 */
62  	public int getColumnCount()
63  	{
64  		return rowModel.getColumnCount();
65  	}
66  
67  	/* (non-Javadoc)
68  	 * @see javax.swing.table.TableModel#getRowCount()
69  	 */
70  	public int getRowCount()
71  	{
72  		return rowModel.getRowCount();
73  	}
74  
75  	/* (non-Javadoc)
76  	 * @see javax.swing.table.TableModel#getValueAt(int, int)
77  	 */
78  	public Object getValueAt(int rowIndex, int columnIndex)
79  	{
80  		TableRow r = rowModel.getRowAt(rowIndex);
81  
82  		if (r != null) {
83  			return r.getValueAt(columnIndex);
84  		}
85  
86  		return null;
87  	}
88  
89  	/**
90  	 * Returns row model wrapped inside thia table model.
91  	 *
92  	 * @return the row model
93  	 */
94  	public TableRowModel getRowModel()
95  	{
96  		return rowModel;
97  	}
98  
99  	/* (non-Javadoc)
100 	 * @see com.cosylab.gui.components.table.TableRowModelListener#tableChanged(com.cosylab.gui.components.table.TableRowModelEvent)
101 	 */
102 	public void tableChanged(TableRowModelEvent event)
103 	{
104 		int first = event.getRowIndex();
105 		int last = first;
106 		
107 		if (first==TableRowModelEvent.HEADER_ROW) {
108 			fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
109 			return;
110 		}
111 
112 		for (int i = 1; i < event.getRows().length; i++) {
113 			int j = rowModel.getRowIndex(event.getRows()[i]);
114 
115 			if (j < first) {
116 				first = j;
117 			} else if (j > last) {
118 				last = j;
119 			}
120 		}
121 
122 		if (first == -1) {
123 			first = 0;
124 			last = getRowCount();
125 		}
126 
127 		fireTableChanged(new TableModelEvent(this, first, last,
128 		        event.getValue(), event.getType()));
129 	}
130 
131 	/* (non-Javadoc)
132 	 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
133 	 */
134 	public String getColumnName(int column)
135 	{
136 		return rowModel.getColumnName(column);
137 	}
138 
139 	/* (non-Javadoc)
140 	 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
141 	 */
142 	public boolean isCellEditable(int rowIndex, int columnIndex)
143 	{
144 		TableRow r = rowModel.getRowAt(rowIndex);
145 
146 		if (r == null) {
147 			return false;
148 		}
149 
150 		return r.isValueSettable(columnIndex);
151 	}
152 }
153 
154 /* __oOo__ */