001: package org.dbbrowser.ui.helper;
002:
003: import infrastructure.propertymanager.PropertyManager;
004: import java.util.ArrayList;
005: import java.util.List;
006: import java.util.Collections;
007: import javax.swing.table.AbstractTableModel;
008: import org.dbbrowser.db.engine.model.ColumnInfo;
009: import org.dbbrowser.db.engine.model.DBRow;
010: import org.dbbrowser.db.engine.model.DBTable;
011: import org.dbbrowser.db.engine.model.DBTableCell;
012: import org.dbbrowser.ui.UIControllerForQueries;
013:
014: /**
015: * Private class representing the model for the table
016: * @author amangat
017: */
018: public class DBTableDataTableModel extends AbstractTableModel {
019: private static final long serialVersionUID = UIControllerForQueries.version;
020:
021: private List listOfColumnInfosIncludingRowHeaderColumn = new ArrayList();
022: private DBTable dbTable = null;
023:
024: public DBTableDataTableModel(DBTable newDBTable) {
025: dbTable = newDBTable;
026: listOfColumnInfosIncludingRowHeaderColumn.addAll(dbTable
027: .getListOfColumnInfos());
028: ColumnInfo dummyColumnInfoForRowHeadercolumn = new ColumnInfo(
029: " ", ColumnInfo.COLUMN_TYPE_VARCHAR, null, null,
030: null, null, Boolean.FALSE, Boolean.FALSE, null);
031:
032: //if sorting enabled, sort the data in table and sort the columns
033: String sortColumnsFlag = PropertyManager.getInstance()
034: .getProperty("dbbrowser-ui-sort-columns-in-table");
035: if ("true".equals(sortColumnsFlag)) {
036: Collections.sort(listOfColumnInfosIncludingRowHeaderColumn,
037: new ColumnInfoComparator());
038: }
039:
040: listOfColumnInfosIncludingRowHeaderColumn.add(0,
041: dummyColumnInfoForRowHeadercolumn);
042: }
043:
044: /**
045: * Returns the column name
046: * @return - String
047: */
048: public String getColumnName(int col) {
049: ColumnInfo columnInfo = (ColumnInfo) listOfColumnInfosIncludingRowHeaderColumn
050: .get(col);
051: String columnName = columnInfo.getColumnName();
052:
053: //If it is a primary key column, add the word (PK) at the end
054: if (columnInfo.isPrimaryKeyColumn().booleanValue()) {
055: columnName = columnName + "(PK)";
056: }
057: return columnName;
058: }
059:
060: /**
061: * Returns the number for rows
062: * @return - int
063: */
064: public int getRowCount() {
065: return dbTable.getListOfRows().size();
066: }
067:
068: /**
069: * Returns the number of columns
070: * @return - int
071: */
072: public int getColumnCount() {
073: return listOfColumnInfosIncludingRowHeaderColumn.size();
074: }
075:
076: /**
077: * Returns the value for a row and column
078: * @return Object
079: */
080: public Object getValueAt(int row, int col) {
081: String cellValue = null;
082:
083: if (col == 0) {
084: return "" + (row + 1);
085: } else {
086: DBRow dbRow = (DBRow) dbTable.getListOfRows().get(row);
087: List listOfTableCells = dbRow.getListOFDBTableCells();
088:
089: //if sorting enabled, sort the data in table and sort the columns
090: String sortColumnsFlag = PropertyManager.getInstance()
091: .getProperty("dbbrowser-ui-sort-columns-in-table");
092: if ("true".equals(sortColumnsFlag)) {
093: Collections.sort(listOfTableCells,
094: new DBTableCellComparator());
095: }
096:
097: DBTableCell dbTableCell = (DBTableCell) listOfTableCells
098: .get(col - 1);
099: cellValue = dbTableCell.getFormattedValue();
100: }
101:
102: return cellValue;
103: }
104:
105: /**
106: * Returns the object value for a row and column
107: * @return Object - unformatted object
108: */
109: public Object getObjectValueAt(int row, int col) {
110: Object cellValue = null;
111:
112: if (col == 0) {
113: return "" + (row + 1);
114: } else {
115: DBRow dbRow = (DBRow) dbTable.getListOfRows().get(row);
116: List listOfTableCells = dbRow.getListOFDBTableCells();
117:
118: //if sorting enabled, sort the data in table and sort the columns
119: String sortColumnsFlag = PropertyManager.getInstance()
120: .getProperty("dbbrowser-ui-sort-columns-in-table");
121: if ("true".equals(sortColumnsFlag)) {
122: Collections.sort(listOfTableCells,
123: new DBTableCellComparator());
124: }
125:
126: DBTableCell dbTableCell = (DBTableCell) listOfTableCells
127: .get(col - 1);
128: cellValue = dbTableCell.getValue();
129: }
130:
131: return cellValue;
132: }
133:
134: /**
135: * Returns the db table
136: * @return
137: */
138: public DBTable getDBTable() {
139: return dbTable;
140: }
141:
142: /**
143: * Returns the class for the column data
144: * @return Class
145: */
146: public Class getColumnClass(int columnIndex) {
147: Class classToReturn = null;
148:
149: ColumnInfo columnInfo = (ColumnInfo) listOfColumnInfosIncludingRowHeaderColumn
150: .get(columnIndex);
151:
152: //if the type is a number (Double or Integer), return a Number.class
153: if ((columnInfo.getColumnTypeName() != null)
154: && (columnInfo.getColumnTypeName()
155: .equals(ColumnInfo.COLUMN_TYPE_NUMBER))) {
156: classToReturn = Number.class;
157: } else {
158: classToReturn = String.class;
159: }
160:
161: return classToReturn;
162: }
163: }
|