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