001: /*
002: ** $Id: SchemaForeignModel.java,v 1.6 2000/10/26 08:34:16 mrw Exp $
003: **
004: ** Mike Wilson, September 2000, mrw@whisperingwind.co.uk
005: **
006: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
007: **
008: ** This program is free software; you can redistribute it and/or modify
009: ** it under the terms of the GNU General Public License as published by
010: ** the Free Software Foundation; either version 2 of the License, or
011: ** (at your option) any later version.
012: **
013: ** This program is distributed in the hope that it will be useful,
014: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
015: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: ** GNU General Public License for more details.
017: **
018: ** You should have received a copy of the GNU Library General
019: ** Public License along with this library; if not, write to the
020: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
021: ** Boston, MA 02111-1307 USA.
022: */
023:
024: package uk.co.whisperingwind.vienna;
025:
026: import java.sql.Connection;
027: import java.sql.DatabaseMetaData;
028: import java.sql.ResultSet;
029: import java.sql.SQLException;
030: import java.util.HashMap;
031: import java.util.Vector;
032: import javax.swing.table.TableModel;
033: import uk.co.whisperingwind.framework.ExceptionDialog;
034: import uk.co.whisperingwind.framework.Model;
035: import uk.co.whisperingwind.framework.SwingThread;
036: import uk.co.whisperingwind.framework.VectorTableModel;
037:
038: /**
039: ** Foreign key Model for the Schema View. Maintains a list of columns
040: ** comprising the table's foreign key.
041: */
042:
043: class SchemaForeignModel extends Model {
044: private Connection connection = null;
045: private String schemaName = null;
046: private String loadedTable = null;
047: private HashMap typeMap = null;
048: private ForeignLoader foreignLoader = null;
049: private VectorTableModel foreignModel = new VectorTableModel();
050:
051: public SchemaForeignModel(Connection theConnection,
052: String theSchemaName) {
053: connection = theConnection;
054: schemaName = theSchemaName;
055:
056: foreignModel.addName("Column", 32);
057: foreignModel.addName("References", 32);
058: foreignModel.addName("In", 32);
059: foreignModel.addName("Sequence", 32);
060: }
061:
062: public TableModel getTableModel() {
063: return foreignModel;
064: }
065:
066: public void clear() {
067: foreignModel.clear();
068: }
069:
070: /*
071: ** Load the column details for the specified table. Runs in a
072: ** separate thread so the GUI remains alive.
073: **
074: ** If a "Load columns" thread is already running, stop it and wait
075: ** for it to complete before starting another.
076: */
077:
078: public boolean load(String tableName) {
079: boolean started = false;
080:
081: if (tableName != null && !tableName.equals(loadedTable)) {
082: if (foreignLoader != null)
083: foreignLoader.interrupt();
084:
085: loadedTable = new String(tableName);
086: foreignLoader = new ForeignLoader();
087: foreignLoader.start();
088: started = true;
089: }
090:
091: return started;
092: }
093:
094: /*
095: */
096:
097: private class ForeignLoader extends SwingThread {
098: private VectorTableModel newForeignModel = new VectorTableModel();
099:
100: public void construct() {
101: newForeignModel.addName("Column", 32);
102: newForeignModel.addName("References", 32);
103: newForeignModel.addName("In", 32);
104: newForeignModel.addName("Sequence", 32);
105:
106: try {
107: DatabaseMetaData metaData = connection.getMetaData();
108:
109: ResultSet columnSet = metaData.getImportedKeys(null,
110: schemaName, loadedTable);
111:
112: while (columnSet.next() && !stopped) {
113: String column = columnSet
114: .getString("FKCOLUMN_NAME");
115: String references = columnSet
116: .getString("PKCOLUMN_NAME");
117: String table = columnSet.getString("PKTABLE_NAME");
118: String sequence = columnSet.getString("KEY_SEQ");
119:
120: Vector row = newForeignModel.addRow();
121: row.add(column);
122: row.add(references);
123: row.add(table);
124: row.add(sequence);
125: }
126: } catch (SQLException ex) {
127: new ExceptionDialog(ex);
128: }
129: }
130:
131: public void finished() {
132: if (!stopped) {
133: foreignModel = newForeignModel;
134: fireEvent(SchemaForeignModel.this , "foreign", "updated");
135: }
136:
137: foreignLoader = null;
138: }
139: }
140: }
|