001: /*
002: ** $Id: SchemaListModel.java,v 1.4 2000/10/26 08:34:16 mrw Exp $
003: **
004: ** Mike Wilson, July 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.Enumeration;
031: import java.util.Vector;
032: import javax.swing.AbstractListModel;
033: import javax.swing.ListModel;
034: import uk.co.whisperingwind.framework.ExceptionDialog;
035: import uk.co.whisperingwind.framework.Model;
036: import uk.co.whisperingwind.framework.SwingThread;
037:
038: /**
039: ** Models a list of schemas available in the database. Can be used as
040: ** the data model for a JList.
041: */
042:
043: class SchemaListModel extends Model {
044: private SchemaVectorModel listModel = new SchemaVectorModel();
045: private Connection connection = null;
046:
047: /*
048: ** Construct a SchemaListModel and load the available schema names
049: ** from the database connection.
050: */
051:
052: public SchemaListModel(Connection c) {
053: connection = c;
054: }
055:
056: /*
057: ** Find all the schema names for the connection.
058: */
059:
060: public void load() {
061: ListLoader listLoader = new ListLoader();
062: listLoader.start();
063: }
064:
065: public ListModel getListModel() {
066: return listModel;
067: }
068:
069: private class SchemaVectorModel extends AbstractListModel {
070: private Vector schemaList = new Vector();
071:
072: public void add(String item) {
073: schemaList.add(item);
074: }
075:
076: public Enumeration elements() {
077: return schemaList.elements();
078: }
079:
080: public Object getElementAt(int i) {
081: return schemaList.elementAt(i);
082: }
083:
084: public int getSize() {
085: return schemaList.size();
086: }
087:
088: public void fire() {
089: fireIntervalAdded(this , 0, getSize() - 1);
090: }
091: }
092:
093: private class ListLoader extends SwingThread {
094: public void construct() {
095: fireEvent("schema", "begin");
096:
097: try {
098: DatabaseMetaData metaData = connection.getMetaData();
099: ResultSet tables = metaData.getSchemas();
100:
101: while (tables.next())
102: listModel.add(tables.getString("TABLE_SCHEM"));
103: } catch (SQLException ex) {
104: new ExceptionDialog(ex);
105: }
106: }
107:
108: public void finished() {
109: listModel.fire();
110: fireEvent("schema", "end");
111: }
112: }
113: }
|