|
| 1 | +package nl.bascoder.keymanager; |
| 2 | + |
| 3 | +import java.sql.ResultSetMetaData; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.logging.Level; |
| 8 | +import java.util.logging.Logger; |
| 9 | + |
| 10 | +import javax.swing.SwingWorker; |
| 11 | +import javax.swing.table.AbstractTableModel; |
| 12 | + |
| 13 | +/** |
| 14 | + * TableModel bound to database. |
| 15 | + * |
| 16 | + * |
| 17 | + * |
| 18 | + * @author Bas van Marwijk |
| 19 | + * @since 9-11-14 |
| 20 | + * @version 1.0 - creation |
| 21 | + */ |
| 22 | +public class DatabaseTableModel extends AbstractTableModel { |
| 23 | + |
| 24 | + private DatabaseManager dbManager; |
| 25 | + private ResultSetMetaData resultSetMetaData; |
| 26 | + private final List<Row> rows; |
| 27 | + |
| 28 | + public DatabaseTableModel() { |
| 29 | + this.dbManager = DatabaseManager.getInstance(); |
| 30 | + rows = new ArrayList<>(); |
| 31 | + |
| 32 | + initResultSet(); |
| 33 | + |
| 34 | + new SwingWorker<Void, Row>() { |
| 35 | + public Void doInBackground() { |
| 36 | + // TODO: Process ResultSet and create Rows. Call publish() for every N rows created. |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + protected void process(Row... chunks) { |
| 41 | + // TODO: Add to ResultSetTableModel List and fire TableEvent. |
| 42 | + } |
| 43 | + }.execute(); |
| 44 | + } |
| 45 | + |
| 46 | + private void initResultSet() { |
| 47 | + dbManager.getConnection(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns the number of rows in the model. A |
| 52 | + * <code>JTable</code> uses this method to determine how many rows it |
| 53 | + * should display. This method should be quick, as it |
| 54 | + * is called frequently during rendering. |
| 55 | + * |
| 56 | + * @return the number of rows in the model |
| 57 | + * @see #getColumnCount |
| 58 | + */ |
| 59 | + @Override |
| 60 | + public int getRowCount() { |
| 61 | + return rows.size(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the number of columns in the model. A |
| 66 | + * <code>JTable</code> uses this method to determine how many columns it |
| 67 | + * should create and display by default. |
| 68 | + * |
| 69 | + * @return the number of columns in the model |
| 70 | + * @see #getRowCount |
| 71 | + */ |
| 72 | + @Override |
| 73 | + public int getColumnCount() { |
| 74 | + try { |
| 75 | + return resultSetMetaData.getColumnCount(); |
| 76 | + } catch (SQLException e) { |
| 77 | + Logger.getGlobal().log(Level.SEVERE, e.getMessage() + " " + e.getErrorCode(), e); |
| 78 | + return 0; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the name of the column at <code>columnIndex</code>. This is used |
| 84 | + * to initialize the table's column header name. Note: this name does |
| 85 | + * not need to be unique; two columns in a table can have the same name. |
| 86 | + * |
| 87 | + * @param columnIndex the index of the column |
| 88 | + * @return the name of the column |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public String getColumnName(int columnIndex) { |
| 92 | + try { |
| 93 | + return resultSetMetaData.getColumnName(columnIndex - 1); |
| 94 | + } catch (SQLException e) { |
| 95 | + Logger.getGlobal().log(Level.SEVERE, e.getMessage() + " " + e.getErrorCode(), e); |
| 96 | + return null; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns the most specific superclass for all the cell values |
| 102 | + * in the column. This is used by the <code>JTable</code> to set up a |
| 103 | + * default renderer and editor for the column. |
| 104 | + * |
| 105 | + * @param columnIndex the index of the column |
| 106 | + * @return the common ancestor class of the object values in the model. |
| 107 | + */ |
| 108 | + @Override |
| 109 | + public Class<?> getColumnClass(int columnIndex) { |
| 110 | + // TODO return correct class |
| 111 | + return Object.class; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns true if the cell at <code>rowIndex</code> and |
| 116 | + * <code>columnIndex</code> |
| 117 | + * is editable. Otherwise, <code>setValueAt</code> on the cell will not |
| 118 | + * change the value of that cell. |
| 119 | + * |
| 120 | + * @param rowIndex the row whose value to be queried |
| 121 | + * @param columnIndex the column whose value to be queried |
| 122 | + * @return true if the cell is editable |
| 123 | + * @see #setValueAt |
| 124 | + */ |
| 125 | + @Override |
| 126 | + public boolean isCellEditable(int rowIndex, int columnIndex) { |
| 127 | + // only id is not editable |
| 128 | + return columnIndex != 0; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns the value for the cell at <code>columnIndex</code> and |
| 133 | + * <code>rowIndex</code>. |
| 134 | + * |
| 135 | + * @param rowIndex the row whose value is to be queried |
| 136 | + * @param columnIndex the column whose value is to be queried |
| 137 | + * @return the value Object at the specified cell |
| 138 | + */ |
| 139 | + @Override |
| 140 | + public Object getValueAt(int rowIndex, int columnIndex) { |
| 141 | + return rows.get(rowIndex) |
| 142 | + .getValue(columnIndex); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Internal used row |
| 147 | + */ |
| 148 | + private class Row { |
| 149 | + private final Object[] values; |
| 150 | + |
| 151 | + public Row(Object[] values) { |
| 152 | + this.values = values; |
| 153 | + } |
| 154 | + |
| 155 | + public int getSize() { |
| 156 | + return values.length; |
| 157 | + } |
| 158 | + |
| 159 | + public Object getValue(int i) { |
| 160 | + return values[i]; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
0 commit comments