001: /*
002: ** $Id: SchemaTableModel.java,v 1.12 2000/10/26 08:34:16 mrw Exp $
003: **
004: ** Column Table for the Schema View. Maintains a list of table names
005: ** and types.
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.VectorTableModel;
038: import uk.co.whisperingwind.framework.Model;
039: import uk.co.whisperingwind.framework.SwingThread;
040:
041: class SchemaTableModel extends Model {
042: private Connection connection = null;
043: private String schemaName = null;
044: private TableLoader tableLoader = null;
045: private VectorTableModel tableModel = new VectorTableModel();
046:
047: public SchemaTableModel(Connection theConnection,
048: String theSchemaName) {
049: connection = theConnection;
050: schemaName = theSchemaName;
051:
052: tableModel.addName("Name", 32);
053: tableModel.addName("Type", 32);
054:
055: load(true, true, true, true);
056: }
057:
058: public TableModel getTableModel() {
059: return tableModel;
060: }
061:
062: /*
063: ** Load the table details for the specified schema. Runs in a
064: ** separate thread so the GUI remains alive.
065: */
066:
067: public void load(boolean showTables, boolean showViews,
068: boolean showSynonyms, boolean showSequences) {
069: if (tableLoader != null)
070: tableLoader.interrupt();
071:
072: tableLoader = new TableLoader(showTables, showViews,
073: showSynonyms, showSequences);
074: tableLoader.start();
075: }
076:
077: private class TableLoader extends SwingThread {
078: private boolean showTables;
079: private boolean showViews;
080: private boolean showSynonyms;
081: private boolean showSequences;
082: private VectorTableModel newTableModel = new VectorTableModel();
083:
084: public TableLoader(boolean showTables, boolean showViews,
085: boolean showSynonyms, boolean showSequences) {
086: this .showTables = showTables;
087: this .showViews = showViews;
088: this .showSynonyms = showSynonyms;
089: this .showSequences = showSequences;
090:
091: newTableModel.addName("Name", 32);
092: newTableModel.addName("Type", 32);
093: }
094:
095: public void construct() {
096: try {
097: DatabaseMetaData metaData = connection.getMetaData();
098: ResultSet resultSet = metaData.getTables(null,
099: schemaName, "%", null);
100:
101: while (resultSet.next() && !stopped) {
102: String tableName = resultSet
103: .getString("TABLE_NAME");
104: String tableType = resultSet
105: .getString("TABLE_TYPE");
106: boolean wanted = true;
107:
108: if (tableType.equalsIgnoreCase("TABLE")) {
109: if (!showTables)
110: wanted = false;
111: } else if (tableType.equalsIgnoreCase("VIEW")) {
112: if (!showViews)
113: wanted = false;
114: } else if (tableType.equalsIgnoreCase("SYNONYM")) {
115: if (!showSynonyms)
116: wanted = false;
117: } else if (tableType.equalsIgnoreCase("SEQUENCE")) {
118: if (!showSequences)
119: wanted = false;
120: }
121:
122: if (wanted) {
123: Vector row = newTableModel.addRow();
124: row.add(tableName);
125: row.add(tableType);
126: }
127: }
128: } catch (SQLException ex) {
129: new ExceptionDialog(ex);
130: }
131: }
132:
133: public void finished() {
134: if (!stopped) {
135: tableModel = newTableModel;
136: fireEvent(SchemaTableModel.this , "table", "updated");
137: }
138:
139: tableLoader = null;
140: }
141: }
142: }
|