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.beans.PropertyChangeEvent;
022: import java.beans.PropertyChangeListener;
023: import java.beans.VetoableChangeListener;
024: import java.beans.PropertyVetoException;
025:
026: import javax.swing.Action;
027: import javax.swing.BorderFactory;
028: import javax.swing.JLabel;
029: import javax.swing.JList;
030: import javax.swing.JToggleButton;
031: import javax.swing.SwingConstants;
032: import javax.swing.event.InternalFrameAdapter;
033: import javax.swing.event.InternalFrameEvent;
034:
035: import net.sourceforge.squirrel_sql.fw.gui.BasePopupMenu;
036: import net.sourceforge.squirrel_sql.fw.gui.ToolBar;
037: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
038: import net.sourceforge.squirrel_sql.fw.util.ICommand;
039: import net.sourceforge.squirrel_sql.fw.util.StringManager;
040: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
041:
042: import net.sourceforge.squirrel_sql.client.IApplication;
043: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
044: import net.sourceforge.squirrel_sql.client.mainframe.action.CopyDriverAction;
045: import net.sourceforge.squirrel_sql.client.mainframe.action.CreateDriverAction;
046: import net.sourceforge.squirrel_sql.client.mainframe.action.DeleteDriverAction;
047: import net.sourceforge.squirrel_sql.client.mainframe.action.InstallDefaultDriversAction;
048: import net.sourceforge.squirrel_sql.client.mainframe.action.ModifyDriverAction;
049: import net.sourceforge.squirrel_sql.client.mainframe.action.ModifyDriverCommand;
050: import net.sourceforge.squirrel_sql.client.mainframe.action.ShowDriverWebsiteAction;
051: import net.sourceforge.squirrel_sql.client.mainframe.action.ShowLoadedDriversOnlyAction;
052: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
053:
054: /**
055: * This windows displays a list of JDBC drivers and allows the user
056: * to maintain their details, add new ones etc.
057: *
058: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
059: */
060: public class DriversListInternalFrame extends BaseListInternalFrame {
061: /** Internationalized strings for this class. */
062: private static final StringManager s_stringMgr = StringManagerFactory
063: .getStringManager(BaseListInternalFrame.class);
064:
065: /** Application API. */
066: private IApplication _app;
067:
068: /** User Interface facory. */
069: private UserInterfaceFactory _uiFactory;
070:
071: /**
072: * Default ctor.
073: */
074: public DriversListInternalFrame(IApplication app, DriversList list) {
075: super (new UserInterfaceFactory(app, list));
076: _app = app;
077: _uiFactory = (UserInterfaceFactory) getUserInterfaceFactory();
078: _uiFactory.setDriversListInternalFrame(this );
079:
080: // Enable/disable actions depending on whether an item is selected in
081: // the list.
082: _uiFactory.enableDisableActions();
083:
084: addVetoableChangeListener(new VetoableChangeListener() {
085: public void vetoableChange(PropertyChangeEvent evt)
086: throws PropertyVetoException {
087: if (IS_CLOSED_PROPERTY.equals(evt.getPropertyName())
088: && Boolean.TRUE.equals(evt.getNewValue())) {
089: // i18n[DriversListInternalFrame.error.ctrlF4key=Probably closed by the ctrl F4 key. See BasicDesktopPaneUi.CloseAction]
090: throw new PropertyVetoException(
091: s_stringMgr
092: .getString("DriversListInternalFrame.error.ctrlF4key"),
093: evt);
094: }
095: }
096: });
097:
098: addInternalFrameListener(new InternalFrameAdapter() {
099: public void internalFrameClosing(InternalFrameEvent e) {
100: nowVisible(false);
101: }
102: });
103:
104: _app.getSquirrelPreferences().addPropertyChangeListener(
105: new PropertyChangeListener() {
106: public void propertyChange(PropertyChangeEvent evt) {
107: final String propName = evt != null ? evt
108: .getPropertyName() : null;
109: _uiFactory.propertiesChanged(propName);
110: }
111: });
112: }
113:
114: public void nowVisible(boolean b) {
115: _app.getMainFrame().setEnabledDriversMenu(b);
116: }
117:
118: /**
119: * Retrieve the index of the currently selected driver.
120: *
121: * @return index of currently selected driver.
122: */
123: public int getSelectedIndex() {
124: return _uiFactory._driversList.getSelectedIndex();
125: }
126:
127: private final static class UserInterfaceFactory implements
128: BaseListInternalFrame.IUserInterfaceFactory {
129: private IApplication _app;
130: private DriversList _driversList;
131: private ToolBar _tb;
132: private BasePopupMenu _pm = new BasePopupMenu();
133: private DriversListInternalFrame _tw;
134:
135: UserInterfaceFactory(IApplication app, DriversList list) {
136: super ();
137: if (app == null) {
138: throw new IllegalArgumentException(
139: "IApplication == null");
140: }
141: if (list == null) {
142: throw new IllegalArgumentException(
143: "DriversList == null");
144: }
145:
146: _app = app;
147: // _driversList = new DriversList(app);
148: _driversList = list;
149:
150: final ActionCollection actions = app.getActionCollection();
151: _pm.add(actions.get(CreateDriverAction.class));
152: _pm.addSeparator();
153: _pm.add(actions.get(ModifyDriverAction.class));
154: _pm.add(actions.get(CopyDriverAction.class));
155: _pm.add(actions.get(ShowDriverWebsiteAction.class));
156: _pm.addSeparator();
157: _pm.add(actions.get(DeleteDriverAction.class));
158: _pm.addSeparator();
159: }
160:
161: public ToolBar getToolBar() {
162: return _tb;
163: }
164:
165: public BasePopupMenu getPopupMenu() {
166: return _pm;
167: }
168:
169: public JList getList() {
170: return _driversList;
171: }
172:
173: public String getWindowTitle() {
174: return s_stringMgr
175: .getString("DriversListInternalFrame.windowtitle");
176: }
177:
178: public ICommand getDoubleClickCommand() {
179: ICommand cmd = null;
180: ISQLDriver driver = _driversList.getSelectedDriver();
181: if (driver != null) {
182: cmd = new ModifyDriverCommand(_app, driver);
183: }
184: return cmd;
185: }
186:
187: /**
188: * Enable/disable actions depending on whether an item is selected
189: * in list.
190: */
191: public void enableDisableActions() {
192: boolean enable = false;
193: try {
194: enable = _driversList.getSelectedDriver() != null;
195: } catch (Exception ignore) {
196: // Getting an error in the JDK.
197: // Exception occurred during event dispatching:
198: // java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
199: // at java.util.Vector.elementAt(Vector.java:417)
200: // at javax.swing.DefaultListModel.getElementAt(DefaultListModel.java:70)
201: // at javax.swing.JList.getSelectedValue(JList.java:1397)
202: // at net.sourceforge.squirrel_sql.client.gui.mainframe.DriversList.getSelectedDriver(DriversList.java:77)
203: }
204:
205: final ActionCollection actions = _app.getActionCollection();
206: actions.get(CopyDriverAction.class).setEnabled(enable);
207: actions.get(DeleteDriverAction.class).setEnabled(enable);
208: actions.get(ModifyDriverAction.class).setEnabled(enable);
209: actions.get(ShowDriverWebsiteAction.class).setEnabled(
210: enable);
211: }
212:
213: void setDriversListInternalFrame(DriversListInternalFrame tw) {
214: _tw = tw;
215: propertiesChanged(null);
216: }
217:
218: public void propertiesChanged(String propName) {
219: if (propName == null
220: || propName
221: .equals(SquirrelPreferences.IPropertyNames.SHOW_DRIVERS_TOOL_BAR)) {
222: boolean show = _app.getSquirrelPreferences()
223: .getShowDriversToolBar();
224: if (show) {
225: createToolBar();
226: } else {
227: _tb = null;
228: }
229: _tw.setToolBar(getToolBar());
230: }
231: }
232:
233: private void createToolBar() {
234: _tb = new ToolBar();
235: _tb.setUseRolloverButtons(true);
236: _tb.setFloatable(false);
237:
238: final JLabel lbl = new JLabel(getWindowTitle(),
239: SwingConstants.CENTER);
240: lbl.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
241: _tb.add(lbl, 0);
242:
243: final ActionCollection actions = _app.getActionCollection();
244: _tb.add(actions.get(CreateDriverAction.class));
245: _tb.add(actions.get(ModifyDriverAction.class));
246: _tb.add(actions.get(CopyDriverAction.class));
247: _tb.add(actions.get(ShowDriverWebsiteAction.class));
248: _tb.add(actions.get(DeleteDriverAction.class));
249: _tb.addSeparator();
250: _tb.add(actions.get(InstallDefaultDriversAction.class));
251: _tb.addSeparator();
252: // _tb.add(actions.get(ShowLoadedDriversOnlyAction.class));
253:
254: final Action act = actions
255: .get(ShowLoadedDriversOnlyAction.class);
256: final JToggleButton btn = new JToggleButton(act);
257: final boolean show = _app.getSquirrelPreferences()
258: .getShowLoadedDriversOnly();
259: btn.setSelected(show);
260: btn.setText(null);
261: _tb.add(btn);
262: }
263: }
264: }
|