001: package net.sourceforge.squirrel_sql.client.gui.db;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.util.Iterator;
022:
023: import net.sourceforge.squirrel_sql.fw.gui.SortedListModel;
024: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
025: import net.sourceforge.squirrel_sql.fw.util.IObjectCacheChangeListener;
026: import net.sourceforge.squirrel_sql.fw.util.ObjectCacheChangeEvent;
027:
028: import net.sourceforge.squirrel_sql.client.IApplication;
029:
030: /**
031: * Model for a <CODE>DriverList</CODE>.
032: *
033: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
034: */
035: class DriversListModel extends SortedListModel {
036: private static final long serialVersionUID = 1L;
037:
038: /** Application API. */
039: private IApplication _app;
040:
041: /**
042: * Specify whether only JDBC drivers that can be loaded should be loaded
043: * into this model.
044: */
045: private boolean _showLoadedDriversOnly;
046:
047: /**
048: * Load drivers from the <CODE>DataCache</CODE>.
049: *
050: * @param app Application API.
051: */
052: public DriversListModel(IApplication app)
053: throws IllegalArgumentException {
054: super ();
055: if (app == null) {
056: throw new IllegalArgumentException(
057: "Null IApplication passed");
058: }
059: _app = app;
060: load();
061: _app.getDataCache().addDriversListener(new MyDriversListener());
062: }
063:
064: /**
065: * Specify whether only JDBC drivers that can be loaded
066: * are displayed.
067: *
068: * @param show If <TT>true</TT> only show JDBC drivers
069: * that can be loaded.
070: */
071: public void setShowLoadedDriversOnly(boolean show) {
072: if (show != _showLoadedDriversOnly) {
073: _showLoadedDriversOnly = show;
074: load();
075: }
076: }
077:
078: /**
079: * Load from <CODE>DataCache</CODE>.
080: */
081: private void load() {
082: clear();
083: Iterator<ISQLDriver> it = _app.getDataCache().drivers();
084: while (it.hasNext()) {
085: addDriver(it.next());
086: }
087: }
088:
089: /**
090: * Add an <CODE>ISQLDriver</CODE> to this model.
091: *
092: * @param driver <CODE>ISQLDriver</CODE> to be added.
093: */
094: private void addDriver(ISQLDriver driver) {
095: if (!_showLoadedDriversOnly || driver.isJDBCDriverClassLoaded()) {
096: addElement(driver);
097: }
098: }
099:
100: /**
101: * Remove an <CODE>ISQLDriver</CODE> from this model.
102: *
103: * @param driver <CODE>ISQLDriver</CODE> to be removed.
104: */
105: private void removeDriver(ISQLDriver driver) {
106: removeElement(driver);
107: }
108:
109: /**
110: * Listener to changes in <CODE>ObjectCache</CODE>. As drivers are
111: * added to/removed from <CODE>DataCache</CODE> this model is updated.
112: */
113: private class MyDriversListener implements
114: IObjectCacheChangeListener {
115: /**
116: * A driver has been added to the cache.
117: *
118: * @param evt Describes the event in the cache.
119: */
120: public void objectAdded(ObjectCacheChangeEvent evt) {
121: Object obj = evt.getObject();
122: if (obj instanceof ISQLDriver) {
123: addDriver((ISQLDriver) obj);
124: }
125: }
126:
127: /**
128: * A driver has been removed from the cache.
129: *
130: * @param evt Describes the event in the cache.
131: */
132: public void objectRemoved(ObjectCacheChangeEvent evt) {
133: Object obj = evt.getObject();
134: if (obj instanceof ISQLDriver) {
135: removeDriver((ISQLDriver) obj);
136: }
137: }
138: }
139: }
|