001: package net.sourceforge.squirrel_sql.client.gui.db.aliasproperties;
002:
003: import net.sourceforge.squirrel_sql.client.IApplication;
004: import net.sourceforge.squirrel_sql.client.gui.db.SQLAlias;
005: import net.sourceforge.squirrel_sql.fw.gui.MultipleLineLabel;
006: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
007: import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
008: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverPropertyCollection;
009: import net.sourceforge.squirrel_sql.fw.util.StringManager;
010: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
011:
012: import java.awt.*;
013: import java.awt.event.ActionListener;
014: import java.awt.event.ActionEvent;
015: import java.sql.Driver;
016: import java.sql.DriverPropertyInfo;
017: import java.sql.SQLException;
018: import java.util.Properties;
019:
020: /**
021: * This dialog allows the user to review and maintain
022: * the properties for a JDBC driver.
023: *
024: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
025: */
026: public class DriverPropertiesController implements
027: IAliasPropertiesPanelController {
028: /**
029: * Internationalized strings for this class.
030: */
031: private static final StringManager s_stringMgr = StringManagerFactory
032: .getStringManager(DriverPropertiesController.class);
033:
034: private DriverPropertiesPanel _propsPnl;
035:
036: /**
037: * The driver properties. This is only available once OK pressed.
038: */
039: private SQLDriverPropertyCollection _driverPropInfo;
040: private ISQLAlias _alias;
041: private IApplication _app;
042: String _errMsg;
043: private Color _origTblColor;
044:
045: public DriverPropertiesController(SQLAlias alias, IApplication app) {
046: _alias = alias;
047: _app = app;
048: String aliasUrl = alias.getUrl();
049:
050: IIdentifier driverIdentifier = alias.getDriverIdentifier();
051: if (driverIdentifier == null) {
052: // I18n[DriverPropertiesController.noDriverSelected=No driver available in this Alias.\nCan not load driver properties tab.]
053: _errMsg = s_stringMgr
054: .getString("DriverPropertiesController.noDriverSelected");
055: _app.getMessageHandler().showErrorMessage(_errMsg);
056: return;
057: }
058: final Driver jdbcDriver = app.getSQLDriverManager()
059: .getJDBCDriver(driverIdentifier);
060: if (jdbcDriver == null) {
061: // I18n[DriverPropertiesController.loadingDriverFailed=Loading JDBC driver "{0}" failed.\nCan not load driver properties tab.]
062: _errMsg = s_stringMgr.getString(
063: "DriverPropertiesController.loadingDriverFailed",
064: app.getDataCache().getDriver(driverIdentifier)
065: .getName());
066: _app.getMessageHandler().showErrorMessage(_errMsg);
067: return;
068: } else {
069: try {
070: if (!jdbcDriver.acceptsURL(aliasUrl)) {
071: String driverName = app.getDataCache().getDriver(
072: driverIdentifier).getName();
073: //I18n[DriverPropertiesController.invalidUrl=According to
074: //the driver "{0}", the url "{1}" is invalid.]
075: _errMsg = s_stringMgr.getString(
076: "DriverPropertiesController.invalidUrl",
077: new String[] { driverName, aliasUrl });
078: _app.getMessageHandler().showErrorMessage(_errMsg);
079: return;
080: }
081: } catch (Exception e) {
082: // I18n[DriverPropertiesController.loadingDriverFailed=Loading JDBC driver "{0}" failed.\nCan not load driver properties tab.]
083: _errMsg = s_stringMgr
084: .getString(
085: "DriverPropertiesController.loadingDriverFailed",
086: app.getDataCache().getDriver(
087: driverIdentifier).getName());
088: _app.getMessageHandler().showErrorMessage(_errMsg);
089: return;
090: }
091: }
092:
093: DriverPropertyInfo[] infoAr;
094: try {
095: infoAr = jdbcDriver.getPropertyInfo(alias.getUrl(),
096: new Properties());
097: } catch (SQLException e) {
098: // I18n[DriverPropertiesController.gettingDriverPropetiesFailed=Loading the properties from the JDBC driver failed.\nCan not load driver properties tab.]
099: _errMsg = s_stringMgr
100: .getString("DriverPropertiesController.loadingDriverFailed");
101: _app.getMessageHandler().showErrorMessage(_errMsg);
102: return;
103: }
104:
105: SQLDriverPropertyCollection driverPropertiesClone = alias
106: .getDriverPropertiesClone();
107: driverPropertiesClone.applyDriverPropertynfo(infoAr);
108: _propsPnl = new DriverPropertiesPanel(driverPropertiesClone);
109:
110: _propsPnl.chkUseDriverProperties.setSelected(alias
111: .getUseDriverProperties());
112: updateTableEnabled();
113:
114: _propsPnl.chkUseDriverProperties
115: .addActionListener(new ActionListener() {
116: public void actionPerformed(ActionEvent e) {
117: updateTableEnabled();
118: }
119: });
120:
121: }
122:
123: private void updateTableEnabled() {
124: if (null == _origTblColor) {
125: _origTblColor = _propsPnl.tbl.getForeground();
126: }
127:
128: _propsPnl.tbl.setEnabled(_propsPnl.chkUseDriverProperties
129: .isSelected());
130:
131: if (_propsPnl.chkUseDriverProperties.isSelected()) {
132: _propsPnl.tbl.setForeground(_origTblColor);
133: } else {
134: _propsPnl.tbl.setForeground(Color.lightGray);
135: }
136: }
137:
138: /**
139: * Retrieve the database driver properties. This is only valid if the
140: * OK button was pressed.
141: *
142: * @return the database driver properties.
143: */
144: public SQLDriverPropertyCollection getSQLDriverPropertyCollection() {
145: return _driverPropInfo;
146: }
147:
148: public Component getPanelComponent() {
149: if (null == _propsPnl) {
150: return new MultipleLineLabel(_errMsg);
151: } else {
152: return _propsPnl;
153: }
154: }
155:
156: public void applyChanges() {
157: if (null != _propsPnl) {
158: _alias.setDriverProperties(_propsPnl
159: .getSQLDriverProperties());
160: _alias
161: .setUseDriverProperties(_propsPnl.chkUseDriverProperties
162: .isSelected());
163: }
164: }
165:
166: public String getTitle() {
167: //i18n[DriverPropertiesController.title=DriverProperties]
168: return s_stringMgr
169: .getString("DriverPropertiesController.title");
170: }
171:
172: public String getHint() {
173: //i18n[DriverPropertiesController.Hint=DriverProperties]
174: return s_stringMgr
175: .getString("DriverPropertiesController.title");
176: }
177:
178: }
|