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.ISQLAlias;
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 Alias objects.
043: *
044: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
045: */
046: class AliasWindowFactory implements AliasInternalFrame.IMaintenanceType {
047: /** Logger for this class. */
048: private static final ILogger s_log = LoggerController
049: .createLogger(AliasWindowFactory.class);
050:
051: /** Application API. */
052: private final IApplication _app;
053:
054: /** Internationalized strings for this class. */
055: private static final StringManager s_stringMgr = StringManagerFactory
056: .getStringManager(AliasWindowFactory.class);
057:
058: /**
059: * Collection of <TT>AliasMaintDialog</TT> that are currently visible modifying
060: * an existing aliss. Keyed by <TT>ISQLAlias.getIdentifier()</TT>.
061: */
062: private Map<IIdentifier, AliasInternalFrame> _modifySheets = new HashMap<IIdentifier, AliasInternalFrame>();
063:
064: /**
065: * ctor.
066: *
067: * @param app Application API.
068: *
069: * @throws IllegalArgumentException
070: * Thrown if <tt>null</tt> <tt>IApplication</tt> passed.
071: */
072: public AliasWindowFactory(IApplication app) {
073: super ();
074: if (app == null) {
075: throw new IllegalArgumentException("IApplication == null");
076: }
077:
078: _app = app;
079: }
080:
081: /**
082: * Get a maintenance sheet for the passed alias. If a maintenance sheet already
083: * exists it will be brought to the front. If one doesn't exist it will be
084: * created.
085: *
086: * @param alias The alias that user has requested to modify.
087: *
088: * @return The maintenance sheet for the passed alias.
089: *
090: * @throws IllegalArgumentException
091: * Thrown if a <TT>null</TT> <TT>ISQLAlias</TT> passed.
092: */
093: public synchronized AliasInternalFrame getModifySheet(
094: ISQLAlias alias) {
095: if (alias == null) {
096: throw new IllegalArgumentException("ISQLALias == null");
097: }
098:
099: AliasInternalFrame sheet = get(alias);
100: if (sheet == null) {
101: sheet = new AliasInternalFrame(_app, alias, MODIFY);
102: _modifySheets.put(alias.getIdentifier(), sheet);
103: _app.getMainFrame().addInternalFrame(sheet, true, null);
104:
105: sheet.addInternalFrameListener(new InternalFrameAdapter() {
106: public void internalFrameClosed(InternalFrameEvent evt) {
107: synchronized (AliasWindowFactory.this ) {
108: AliasInternalFrame frame = (AliasInternalFrame) evt
109: .getInternalFrame();
110: _modifySheets.remove(frame.getSQLAlias()
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
123: * alias.
124: *
125: * @return The new maintenance sheet.
126: */
127: public AliasInternalFrame getCreateSheet() {
128: final net.sourceforge.squirrel_sql.client.gui.db.DataCache cache = _app
129: .getDataCache();
130: final IIdentifierFactory factory = IdentifierFactory
131: .getInstance();
132: final ISQLAlias alias = cache.createAlias(factory
133: .createIdentifier());
134: final AliasInternalFrame sheet = new AliasInternalFrame(_app,
135: alias, NEW);
136: _app.getMainFrame().addInternalFrame(sheet, true, null);
137: GUIUtils.centerWithinDesktop(sheet);
138: return sheet;
139: }
140:
141: /**
142: * Create and show a new maintenance sheet that will allow the user to create a
143: * new alias that is a copy of the passed one.
144: *
145: * @return The new maintenance sheet.
146: *
147: * @throws IllegalArgumentException
148: * Thrown if a <TT>null</TT> <TT>ISQLAlias</TT> passed.
149: */
150: public AliasInternalFrame getCopySheet(SQLAlias alias) {
151: if (alias == null) {
152: throw new IllegalArgumentException("ISQLALias == null");
153: }
154:
155: final DataCache cache = _app.getDataCache();
156: final IIdentifierFactory factory = IdentifierFactory
157: .getInstance();
158: SQLAlias newAlias = cache.createAlias(factory
159: .createIdentifier());
160: try {
161: newAlias.assignFrom(alias, false);
162:
163: if (SQLAliasSchemaProperties.GLOBAL_STATE_SPECIFY_SCHEMAS == newAlias
164: .getSchemaProperties().getGlobalState()) {
165: // i18n[AliasWindowFactory.schemaPropsCopiedWarning=Warning: Your target Alias contains database specific Schema properties copied from the source Alias.\n
166: // Schema loading of the target Alias may be errorneous. Please check your target Alias's Schema properties.]
167: _app
168: .getMessageHandler()
169: .showWarningMessage(
170: s_stringMgr
171: .getString("AliasWindowFactory.schemaPropsCopiedWarning"));
172: }
173:
174: _app.getPluginManager().aliasCopied(alias, newAlias);
175:
176: } catch (ValidationException ex) {
177: // i18n[AliasWindowFactory.error.copyAlias=Error occured copying the alias]
178: s_log.error(s_stringMgr
179: .getString("AliasWindowFactory.error.copyAlias"),
180: ex);
181: }
182: final AliasInternalFrame sheet = new AliasInternalFrame(_app,
183: newAlias, COPY);
184: _app.getMainFrame().addInternalFrame(sheet, true, null);
185: GUIUtils.centerWithinDesktop(sheet);
186: return sheet;
187: }
188:
189: private AliasInternalFrame get(ISQLAlias alias) {
190: return _modifySheets.get(alias.getIdentifier());
191: }
192: }
|