View Javadoc

1   package at.ac.tuwien.ifs.bpse.basic.gui;
2   
3   import java.util.List;
4   
5   import javax.swing.table.AbstractTableModel;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.springframework.beans.factory.xml.XmlBeanFactory;
10  import org.springframework.core.io.ClassPathResource;
11  
12  import at.ac.tuwien.ifs.bpse.basic.dao.IStudentDAO;
13  import at.ac.tuwien.ifs.bpse.basic.dao.IStudentDAO.SortOrder;
14  import at.ac.tuwien.ifs.bpse.basic.domain.Student;
15  import at.ac.tuwien.ifs.bpse.basic.helper.Constants;
16  
17  /**
18   * 
19   * The Table model encapsulates the complete data needed for the table in the
20   * GUI form. This includes reading the data from the database using the data
21   * access object (DAO).
22   * 
23   * @author The SE-Team
24   * @version 1.2
25   * 
26   */
27  public class StudentenTableModel extends AbstractTableModel {
28  
29  	/**
30  	 * serialVersionUID, generated by eclipse.
31  	 */
32  	private static final long serialVersionUID = 8737097029189851737L;
33  
34  	/**
35  	 * Retrieves the logger for this class.
36  	 */
37  	private static Log log = LogFactory.getLog(StudentenTableModel.class);
38  
39  	/**
40  	 * The Data Access Object for Students.
41  	 */
42  	private IStudentDAO studentDAO = null;
43  
44  	/**
45  	 * Spring Framework XML Bean Factory.
46  	 */
47  	private XmlBeanFactory xbf;
48  
49  	/**
50  	 * Defines the Names of the Columns of the TableModel.
51  	 */
52  	private String[] columnName = { "Matrikelnummer", "Name" };
53  
54  	/**
55  	 * The list of Students will be filled by StudentDAO.
56  	 */
57  	private List<Student> studenten = null;
58  
59  	/**
60  	 * Order of the TableModel
61  	 * 
62  	 * @see IStudentDAO
63  	 */
64  	private SortOrder sortOrder = SortOrder.StudentId;
65  
66  	/**
67  	 * Creates a new StudentenTableModel with default ordering.
68  	 * 
69  	 * @param order the default Order
70  	 */
71  	public StudentenTableModel(SortOrder order) {
72  		super();
73  		ClassPathResource res = new ClassPathResource(Constants.SPRINGBEANS);
74  		xbf = new XmlBeanFactory(res);
75  		studentDAO = (IStudentDAO) xbf.getBean("StudentDAO");
76  		this.sortOrder = order;
77  		readData();
78  	}
79  
80  	/**
81  	 * Get a List of Students represented in this TableModel.
82  	 * 
83  	 * @return the Students represented in the TableModel
84  	 * @see Student
85  	 * @see List
86  	 */
87  	public List<Student> getStudenten() {
88  		return studenten;
89  	}
90  
91  	/**
92  	 * Returns a single Student out of this TableModel.
93  	 * 
94  	 * @param i
95  	 *            The index of the desired Student.
96  	 * @return The Student at position <c>i</c>.
97  	 * @see #getStudenten()
98  	 */
99  	public Student getStudentAt(int i) {
100 		return studenten.get(i);
101 	}
102 
103 	/**
104 	 * Read the Students from the Database with given order.
105 	 * 
106 	 * @see StudentDAO#getStudents(String)
107 	 */
108 	private void readData() {
109 		studenten = studentDAO.getStudents(sortOrder);
110 	}
111 
112 	/**
113 	 * Reload the Data.
114 	 */
115 	public void reload() {
116 		readData();
117 		fireTableDataChanged();
118 	}
119 
120 	/**
121 	 * Change the Order and reload the Data.
122 	 * 
123 	 * @since 1.1
124 	 * @param order
125 	 *            the new Order
126 	 * @see #sortOrder
127 	 * @see SortOrder
128 	 */
129 	public void setSortOrder(SortOrder order) {
130 		log.info("Sort Order Changed to " + order.toString()
131 				+ ": reading data...");
132 		this.sortOrder = order;
133 		reload();
134 	}
135 	
136 	/**
137 	 * Get the number of rows in this TableModel.
138 	 * 
139 	 * @return number of rows
140 	 */
141 	public int getRowCount() {
142 		//log.debug("rowcount = " + studenten.size());
143 		return studenten.size();
144 	}
145 
146 	/**
147 	 * Get the number of columns in this TableModel.
148 	 * 
149 	 * @return number of colums
150 	 */
151 	public int getColumnCount() {
152 		//log.debug("colCount = " + columnName.length);
153 		return columnName.length;
154 	}
155 
156 	/**
157 	 * Get the title of a column.
158 	 * 
159 	 * @param c
160 	 *            the column-index
161 	 * @return the title
162 	 */
163 	public String getColumnName(int c) {
164 		log.debug("colName = " + columnName[c]);
165 		return columnName[c];
166 	}
167 
168 	/**
169 	 * Get the Class of the data saved in a colum.
170 	 * 
171 	 * @param c
172 	 *            the column-index
173 	 * @return String.class
174 	 */
175 	public Class<?> getColumnClass(int c) {
176 		return String.class;
177 	}
178 
179 	/**
180 	 * Checks whether a cell is editable or not.
181 	 * 
182 	 * @param row
183 	 *            the Row
184 	 * @param col
185 	 *            the Column
186 	 * @return false in any case.
187 	 */
188 	public boolean isCellEditable(int row, int col) {
189 		return false;
190 	}
191 
192 	/**
193 	 * Get the Value from a given Cell, denoted by row and col.
194 	 * 
195 	 * @param row
196 	 *            the Row
197 	 * @param col
198 	 *            the Column
199 	 * @return String containing the value of the cell.
200 	 */
201 	public Object getValueAt(int row, int col) {
202 		//log.debug("getValue row = " + row + " col = " + col);
203 		Student s = studenten.get(row);
204 		switch (col) {
205 		case 0:
206 			return s.getMatnr();
207 		case 1:
208 			return s.getFullname();
209 		default:
210 			return null;
211 		}
212 	}
213 
214 }