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.awt.BorderLayout;
022: import java.awt.event.MouseEvent;
023: import java.beans.PropertyChangeEvent;
024: import java.beans.PropertyChangeListener;
025:
026: import javax.swing.JList;
027: import javax.swing.ListSelectionModel;
028: import javax.swing.SwingUtilities;
029: import javax.swing.ToolTipManager;
030:
031: import javax.swing.event.ListDataEvent;
032: import javax.swing.event.ListDataListener;
033: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
034: import net.sourceforge.squirrel_sql.fw.util.StringManager;
035: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
036:
037: import net.sourceforge.squirrel_sql.client.IApplication;
038: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
039: import net.sourceforge.squirrel_sql.client.resources.SquirrelResources;
040:
041: /**
042: * This is a <CODE>JList</CODE> that dispays all the <CODE>ISQLDriver</CODE>
043: * objects.
044: *
045: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
046: */
047: public class DriversList extends JList implements IDriversList {
048: /** Application API. */
049: private IApplication _app;
050:
051: /** Model for this component. */
052: private DriversListModel _model;
053:
054: /** Internationalized strings for this class. */
055: private static final StringManager s_stringMgr = StringManagerFactory
056: .getStringManager(DriversList.class);
057:
058: /**
059: * Ctor specifying Application API object.
060: *
061: * @param app Application API.
062: *
063: * @throws IllegalArgumentException
064: * Thrown if <TT>null</TT> <TT>IApplication</TT> passed.
065: */
066: public DriversList(IApplication app)
067: throws IllegalArgumentException {
068: super ();
069: if (app == null) {
070: throw new IllegalArgumentException(
071: "Null IApplication passed");
072: }
073: _app = app;
074: _model = new DriversListModel(_app);
075: setModel(_model);
076: setLayout(new BorderLayout());
077: getSelectionModel().setSelectionMode(
078: ListSelectionModel.SINGLE_SELECTION);
079:
080: SquirrelResources res = _app.getResources();
081: setCellRenderer(new DriverListCellRenderer(res
082: .getIcon("list.driver.found"), res
083: .getIcon("list.driver.notfound")));
084:
085: propertiesChanged(null);
086:
087: final int selDriverIdx = app.getSquirrelPreferences()
088: .getDriversSelectedIndex();
089: final int size = getModel().getSize();
090: if (selDriverIdx > -1 && selDriverIdx < size) {
091: setSelectedIndex(selDriverIdx);
092: } else {
093: setSelectedIndex(0);
094: }
095:
096: _app.getSquirrelPreferences().addPropertyChangeListener(
097: new PropertyChangeListener() {
098: public void propertyChange(PropertyChangeEvent evt) {
099: final String propName = evt != null ? evt
100: .getPropertyName() : null;
101: propertiesChanged(propName);
102: }
103: });
104:
105: _model.addListDataListener(new ListDataListener() {
106: public void contentsChanged(ListDataEvent evt) {
107: // Unused.
108: }
109:
110: public void intervalAdded(ListDataEvent evt) {
111: final int idx = evt.getIndex0();
112: SwingUtilities.invokeLater(new Runnable() {
113: public void run() {
114: clearSelection();
115: setSelectedIndex(idx);
116: }
117: });
118: }
119:
120: public void intervalRemoved(ListDataEvent evt) {
121: final int idx = evt.getIndex0();
122: SwingUtilities.invokeLater(new Runnable() {
123: public void run() {
124: clearSelection();
125: int modelSize = getModel().getSize();
126: if (idx < modelSize) {
127: setSelectedIndex(idx);
128: } else if (modelSize > 0) {
129: setSelectedIndex(modelSize - 1);
130: }
131: }
132: });
133: }
134: });
135: }
136:
137: /**
138: * Component has been added to its parent.
139: */
140: public void addNotify() {
141: super .addNotify();
142: // Register so that we can display different tooltips depending
143: // which entry in list mouse is over.
144: ToolTipManager.sharedInstance().registerComponent(this );
145: }
146:
147: /**
148: * Component has been removed from its parent.
149: */
150: public void removeNotify() {
151: super .removeNotify();
152: // Don't need tooltips any more.
153: ToolTipManager.sharedInstance().unregisterComponent(this );
154: }
155:
156: /**
157: * Return the <CODE>DriversListModel</CODE> that controls this list.
158: */
159: public DriversListModel getTypedModel() {
160: return _model;
161: }
162:
163: /**
164: * Return the <CODE>ISQLDriver</CODE> that is currently selected.
165: */
166: public ISQLDriver getSelectedDriver() {
167: return (ISQLDriver) getSelectedValue();
168: }
169:
170: /**
171: * Return the description for the driver that the mouse is currently
172: * over as the tooltip text.
173: *
174: * @param event Used to determine the current mouse position.
175: */
176: public String getToolTipText(MouseEvent evt) {
177: String tip = null;
178: final int idx = locationToIndex(evt.getPoint());
179: if (idx != -1) {
180: tip = ((ISQLDriver) getModel().getElementAt(idx)).getName();
181: } else {
182: tip = getToolTipText();
183: }
184: return tip;
185: }
186:
187: /**
188: * Return the tooltip used for this component if the mouse isn't over
189: * an entry in the list.
190: */
191: public String getToolTipText() {
192: // i18n[DriversList.tooltiptext=List of database drivers that can be used to configure an alias]
193: return s_stringMgr.getString("DriversList.tooltiptext");
194: }
195:
196: /**
197: * Application properties have changed so update this object.
198: *
199: * @param propName Name of property that has changed or <TT>null</TT>
200: * if multiple properties have changed.
201: */
202: private void propertiesChanged(String propName) {
203: if (propName == null
204: || propName
205: .equals(SquirrelPreferences.IPropertyNames.SHOW_LOADED_DRIVERS_ONLY)) {
206: boolean show = _app.getSquirrelPreferences()
207: .getShowLoadedDriversOnly();
208: _model.setShowLoadedDriversOnly(show);
209: }
210: }
211: }
|