001: package net.sourceforge.squirrel_sql.client.gui.db.aliasproperties;
002:
003: import net.sourceforge.squirrel_sql.client.gui.db.SQLAliasSchemaDetailProperties;
004:
005: import javax.swing.table.DefaultTableModel;
006: import java.util.ArrayList;
007: import java.util.Arrays;
008:
009: public class SchemaTableModel extends DefaultTableModel {
010: private static final long serialVersionUID = 1L;
011:
012: static final int IX_SCHEMA_NAME = 0;
013: static final int IX_TABLE = 1;
014: static final int IX_VIEW = 2;
015: static final int IX_PROCEDURE = 3;
016: private SQLAliasSchemaDetailProperties[] _schemaDetails;
017:
018: public SchemaTableModel(
019: SQLAliasSchemaDetailProperties[] schemaDetails) {
020: _schemaDetails = schemaDetails;
021: }
022:
023: public Object getValueAt(int row, int column) {
024: SQLAliasSchemaDetailProperties buf = _schemaDetails[row];
025: switch (column) {
026: case IX_SCHEMA_NAME:
027: return buf.getSchemaName();
028: case IX_TABLE:
029: return SchemaTableCboItem.getItemForID(buf.getTable());
030: case IX_VIEW:
031: return SchemaTableCboItem.getItemForID(buf.getView());
032: case IX_PROCEDURE:
033: return SchemaTableCboItem.getItemForID(buf.getProcedure());
034: default:
035: throw new IllegalArgumentException("Unkown column index "
036: + column);
037:
038: }
039: }
040:
041: public void setValueAt(Object aValue, int row, int column) {
042: SQLAliasSchemaDetailProperties buf = _schemaDetails[row];
043:
044: switch (column) {
045: case IX_TABLE:
046: buf.setTable(((SchemaTableCboItem) aValue).getID());
047: break;
048: case IX_VIEW:
049: buf.setView(((SchemaTableCboItem) aValue).getID());
050: break;
051: case IX_PROCEDURE:
052: buf.setProcedure(((SchemaTableCboItem) aValue).getID());
053: break;
054: default:
055: throw new IllegalArgumentException("Unkown column index "
056: + column);
057:
058: }
059:
060: fireTableCellUpdated(row, column);
061: }
062:
063: public boolean isCellEditable(int row, int column) {
064: return IX_SCHEMA_NAME != column;
065: }
066:
067: public int getRowCount() {
068: if (null == _schemaDetails) {
069: return 0;
070: } else {
071: return _schemaDetails.length;
072: }
073: }
074:
075: public SQLAliasSchemaDetailProperties[] getData() {
076: return _schemaDetails;
077: }
078:
079: public void updateSchemas(String[] schemaNames) {
080: ArrayList<SQLAliasSchemaDetailProperties> newDetails = new ArrayList<SQLAliasSchemaDetailProperties>();
081:
082: for (int i = 0; i < schemaNames.length; i++) {
083:
084: boolean found = false;
085: for (int j = 0; j < _schemaDetails.length; j++) {
086: if (_schemaDetails[j].getSchemaName().equalsIgnoreCase(
087: schemaNames[i])) {
088: newDetails.add(_schemaDetails[j]);
089: found = true;
090: break;
091: }
092: }
093:
094: if (false == found) {
095: SQLAliasSchemaDetailProperties buf = new SQLAliasSchemaDetailProperties();
096: buf.setSchemaName(schemaNames[i]);
097: newDetails.add(buf);
098: }
099:
100: }
101:
102: _schemaDetails = newDetails
103: .toArray(new SQLAliasSchemaDetailProperties[newDetails
104: .size()]);
105: Arrays.sort(_schemaDetails);
106:
107: fireTableDataChanged();
108: }
109:
110: public void setColumnTo(int modelIndex, SchemaTableCboItem toItem) {
111: for (int i = 0; i < _schemaDetails.length; i++) {
112: if (IX_TABLE == modelIndex) {
113: _schemaDetails[i].setTable(toItem.getID());
114: } else if (IX_VIEW == modelIndex) {
115: _schemaDetails[i].setView(toItem.getID());
116: } else if (IX_PROCEDURE == modelIndex) {
117: _schemaDetails[i].setProcedure(toItem.getID());
118: }
119: }
120:
121: fireTableDataChanged();
122: }
123: }
|