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.HashMap;
022: import java.util.Map;
023:
024: import javax.swing.event.InternalFrameAdapter;
025: import javax.swing.event.InternalFrameEvent;
026:
027: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
028: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
029: import net.sourceforge.squirrel_sql.fw.id.IIdentifierFactory;
030: import net.sourceforge.squirrel_sql.fw.persist.ValidationException;
031: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
032: import net.sourceforge.squirrel_sql.fw.util.StringManager;
033: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
034: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
035: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
036:
037: import net.sourceforge.squirrel_sql.client.IApplication;
038: import net.sourceforge.squirrel_sql.client.util.IdentifierFactory;
039:
040: /**
041: * TODO: Move all code other than for window creation up to AliasWindowManager
042: * Factory to handle creation of maintenance sheets for SQL Driver objects.
043: *
044: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
045: */
046: class DriverWindowFactory implements
047: AliasInternalFrame.IMaintenanceType {
048: /** Logger for this class. */
049: private static ILogger s_log = LoggerController
050: .createLogger(DriverWindowFactory.class);
051:
052: /** Application API. */
053: private IApplication _app;
054:
055: /** Internationalized strings for this class. */
056: private static final StringManager s_stringMgr = StringManagerFactory
057: .getStringManager(DriverWindowFactory.class);
058:
059: /**
060: * Collection of <TT>DriverMaintDialog</TT> that are currently visible
061: * modifying an existing driver. Keyed by
062: * <TT>ISQLDriver.getIdentifier()</TT>.
063: */
064: private final Map<IIdentifier, DriverInternalFrame> _modifySheets = new HashMap<IIdentifier, DriverInternalFrame>();
065:
066: /**
067: * ctor.
068: *
069: * @param app Application API.
070: *
071: * @throws IllegalArgumentException
072: * Thrown if <tt>null</tt> <tt>IApplication</tt> passed.
073: */
074: public DriverWindowFactory(IApplication app) {
075: super ();
076: if (app == null) {
077: throw new IllegalArgumentException("IApplication == null");
078: }
079:
080: _app = app;
081: }
082:
083: /**
084: * Get a maintenance sheet for the passed driver. If one
085: * already exists it will be returned, otherwise one will be created.
086: *
087: * @param driver The driver that user has requested to modify.
088: *
089: * @return The maintenance sheet for the passed driver.
090: *
091: * @throws IllegalArgumentException if a <TT>null</TT> <TT>ISQLDriver</TT> passed.
092: */
093: public synchronized DriverInternalFrame getModifySheet(
094: ISQLDriver driver) {
095: if (driver == null) {
096: throw new IllegalArgumentException("ISQLDriver == null");
097: }
098:
099: DriverInternalFrame sheet = get(driver);
100: if (sheet == null) {
101: sheet = new DriverInternalFrame(_app, driver, MODIFY);
102: _modifySheets.put(driver.getIdentifier(), sheet);
103: _app.getMainFrame().addInternalFrame(sheet, true, null);
104:
105: sheet.addInternalFrameListener(new InternalFrameAdapter() {
106: public void internalFrameClosed(InternalFrameEvent evt) {
107: synchronized (DriverWindowFactory.this ) {
108: DriverInternalFrame frame = (DriverInternalFrame) evt
109: .getInternalFrame();
110: _modifySheets.remove(frame.getSQLDriver()
111: .getIdentifier());
112: }
113: }
114: });
115: GUIUtils.centerWithinDesktop(sheet);
116: }
117:
118: return sheet;
119: }
120:
121: /**
122: * Create and show a new maintenance sheet to allow the user to create a new driver.
123: *
124: * @return The new maintenance sheet.
125: */
126: public DriverInternalFrame getCreateSheet() {
127: final net.sourceforge.squirrel_sql.client.gui.db.DataCache cache = _app
128: .getDataCache();
129: final IIdentifierFactory factory = IdentifierFactory
130: .getInstance();
131: final ISQLDriver driver = cache.createDriver(factory
132: .createIdentifier());
133: final DriverInternalFrame sheet = new DriverInternalFrame(_app,
134: driver, NEW);
135: _app.getMainFrame().addInternalFrame(sheet, true, null);
136: GUIUtils.centerWithinDesktop(sheet);
137: return sheet;
138: }
139:
140: /**
141: * Create and show a new maintenance sheet that will allow the user to create a
142: * new driver that is a copy of the passed one.
143: *
144: * @return The new maintenance sheet.
145: *
146: * @throws IllegalArgumentException if a <TT>null</TT> <TT>ISQLDriver</TT> passed.
147: */
148: public DriverInternalFrame showCopySheet(ISQLDriver driver) {
149: if (driver == null) {
150: throw new IllegalArgumentException("ISQLDriver == null");
151: }
152:
153: final DataCache cache = _app.getDataCache();
154: final IIdentifierFactory factory = IdentifierFactory
155: .getInstance();
156: ISQLDriver newDriver = cache.createDriver(factory
157: .createIdentifier());
158: try {
159: newDriver.assignFrom(driver);
160: } catch (ValidationException ex) {
161: // i18n[DriverWindowFactory.error.copyingdriver=Error occured copying the driver]
162: s_log
163: .error(
164: s_stringMgr
165: .getString("DriverWindowFactory.error.copyingdriver"),
166: ex);
167: }
168: final DriverInternalFrame sheet = new DriverInternalFrame(_app,
169: newDriver, COPY);
170: _app.getMainFrame().addInternalFrame(sheet, true, null);
171: GUIUtils.centerWithinDesktop(sheet);
172:
173: return sheet;
174: }
175:
176: private DriverInternalFrame get(ISQLDriver driver) {
177: return _modifySheets.get(driver.getIdentifier());
178: }
179: }
|