001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2002-2003 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.sql.SQLException;
022: import java.util.Map;
023: import java.util.TreeMap;
024:
025: import javax.swing.JComboBox;
026:
027: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
028: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
029: import net.sourceforge.squirrel_sql.fw.util.StringManager;
030: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
031:
032: /**
033: * This <TT>JComboBox</TT> will display all the catalogs
034: * in an SQL connection.
035: *
036: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
037: */
038: public class SQLCatalogsComboBox extends JComboBox {
039: private static final long serialVersionUID = 1L;
040:
041: /** Internationalized strings for this class. */
042: private static final StringManager s_stringMgr = StringManagerFactory
043: .getStringManager(SQLCatalogsComboBox.class);
044:
045: private interface i18n {
046: // i18n[SQLCatalogsComboBox.noneLabel=None]
047: String NONE_LABEL = s_stringMgr
048: .getString("SQLCatalogsComboBox.noneLabel");
049: }
050:
051: /**
052: * Default ctor. Builds an empty combo box.
053: */
054: public SQLCatalogsComboBox() {
055: super ();
056: }
057:
058: /**
059: * Sets the catalogs that should appear in the catalog drop-down menu. Clear control and places all the
060: * catalog names from the list in it in alphabetic sequence. Selects the specified catalog; If the
061: * selectedCatalog is null, then selects the first catalog.
062: *
063: * @param catalogs
064: * an array of catalogs names that should in the catalogs drop-down menu.
065: * @param selectedCatalog
066: * the catalog for the current connection that should be selected.
067: */
068: public void setCatalogs(String[] catalogs, String selectedCatalog) {
069: super .removeAllItems();
070: if (catalogs != null) {
071: final Map<String, String> map = new TreeMap<String, String>();
072: for (String catalog : catalogs) {
073: if (!isEmptyCatalog(catalog)) {
074: map.put(catalog, catalog);
075: }
076: }
077: if (isEmptyCatalog(selectedCatalog)) {
078: addItem(new NoCatalogPlaceHolder());
079: }
080: for (String catalog : map.values()) {
081: addItem(catalog);
082: }
083: if (!isEmptyCatalog(selectedCatalog)) {
084: setSelectedCatalog(selectedCatalog);
085: }
086: }
087: setMaximumSize(getPreferredSize());
088: }
089:
090: private boolean isEmptyCatalog(String catalog) {
091: return catalog == null || "".equals(catalog);
092: }
093:
094: /**
095: * Set the <TT>SQLConnection</TT> for this control. Clear control and place all the catalog names from
096: * the connection in it in alphabetic sequence. Select the first catalog.
097: *
098: * @param conn
099: * <TT>SQLConnection</TT> to retrieve catalog names from.
100: * @throws IllegalArgumentException
101: * Thrown if a <TT>null</TT> <TT>SQLConnection</TT> passed.
102: * @throws SQLException
103: * Thrown if an SQL exception occurs.
104: * @deprecated This method has been deprecated because the view should not have direct access to the model.
105: * Use the setCatalogs method instead.
106: */
107: public void setConnection(ISQLConnection conn) throws SQLException {
108: if (conn == null) {
109: throw new IllegalArgumentException("SQLConnection == null");
110: }
111: final SQLDatabaseMetaData md = conn.getSQLMetaData();
112: if (md.supportsCatalogs()) {
113: final String[] catalogs = md.getCatalogs();
114: if (catalogs != null) {
115: setCatalogs(catalogs, conn.getCatalog());
116: }
117: }
118: }
119:
120: public String getSelectedCatalog() {
121: return getSelectedItem().toString();
122: }
123:
124: public void setSelectedCatalog(String selectedCatalog) {
125: if (selectedCatalog != null) {
126: getModel().setSelectedItem(selectedCatalog);
127: }
128: }
129:
130: /**
131: * @see javax.swing.JComboBox#setSelectedItem(java.lang.Object)
132: */
133: @Override
134: public void setSelectedItem(Object o) {
135: super .setSelectedItem(o);
136: // If the "None" place-holder is in the list in the first position, remove it. It is not possible to
137: // select the "None" place-holder upon startup, because it is already selected in the list if it is
138: // present.
139: if (super .getItemAt(0) instanceof NoCatalogPlaceHolder) {
140: super .removeItemAt(0);
141: validate();
142: }
143: }
144:
145: /**
146: * A place holder for the label "None" which is only intended to appear at startup if:
147: *
148: * 1) the database uses catalogs and
149: * 2) it supports connections where the catalog is not specified.
150: *
151: * We want to allow the user to switch to any other catalog when in this state. The selected catalog by
152: * default is the first item in the list. However, it is confusing to have that be an actual catalog, when
153: * in fact the user did not specify one in the connection URL. So, when we connect, if the driver says that
154: * the catalog is null, this place-holder is created to take the first position in the combobox to allow
155: * the user to choose any other "real" catalog, and when they do, this place-holder gets removed, since it
156: * is no longer needed at that point.
157: *
158: * Note: This placeholder allows us to do instanceof instead of a string comparison, which would prevent
159: * the user from ever having and using a catalog called "None" (or whatever the equivalent i18n message
160: * label is for their internationalized strings)
161: *
162: * @author manningr
163: */
164: private class NoCatalogPlaceHolder {
165: public String toString() {
166: return i18n.NONE_LABEL;
167: }
168: }
169: }
|