001: package net.sourceforge.squirrel_sql.plugins.exportconfig.action;
002:
003: /*
004: * Copyright (C) 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.awt.Frame;
022: import java.io.File;
023: import java.io.IOException;
024:
025: import javax.swing.JFileChooser;
026:
027: import net.sourceforge.squirrel_sql.fw.gui.ChooserPreviewer;
028: import net.sourceforge.squirrel_sql.fw.gui.Dialogs;
029: import net.sourceforge.squirrel_sql.fw.util.*;
030: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
031: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
032: import net.sourceforge.squirrel_sql.fw.xml.XMLException;
033:
034: import net.sourceforge.squirrel_sql.client.IApplication;
035:
036: import net.sourceforge.squirrel_sql.plugins.exportconfig.ExportConfigPlugin;
037:
038: /**
039: * Base class for all the "save" commands.
040: *
041: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
042: */
043: abstract class AbstractSaveCommand implements ICommand {
044: private static final StringManager s_stringMgr = StringManagerFactory
045: .getStringManager(AbstractSaveCommand.class);
046:
047: /** Logger for this class. */
048: private final static ILogger s_log = LoggerController
049: .createLogger(AbstractSaveCommand.class);
050:
051: /** The last directory saved to. */
052: private static File s_lastDir;
053:
054: /** Parent frame. */
055: private final Frame _frame;
056:
057: /** Current plugin. */
058: private ExportConfigPlugin _plugin;
059:
060: /**
061: * Ctor.
062: *
063: * @param frame Parent frame for dialog.
064: * @param plugin The current plugin.
065: *
066: * @throws IllegalArgumentException
067: * Thrown if a�<TT>null</TT> <TT>ISession</TT>,
068: * <TT>Resources</TT> or <TT>MysqlPlugin</TT> passed.
069: */
070: AbstractSaveCommand(Frame frame, ExportConfigPlugin plugin) {
071: super ();
072: if (frame == null) {
073: throw new IllegalArgumentException("Frame == null");
074: }
075: if (plugin == null) {
076: throw new IllegalArgumentException(
077: "ExportConfigPlugin == null");
078: }
079: _frame = frame;
080: _plugin = plugin;
081: }
082:
083: public void execute() throws BaseException {
084: final JFileChooser chooser = new JFileChooser();
085: chooser.addChoosableFileFilter(new FileExtensionFilter(
086: "XML files", new String[] { ".xml" }));
087: File file = null;
088: if (s_lastDir != null) {
089: file = new File(s_lastDir, getDefaultFilename());
090: } else {
091: file = new File((File) null, getDefaultFilename());
092: }
093: chooser.setSelectedFile(file);
094: chooser.setAccessory(new ChooserPreviewer());
095: chooser.setDialogTitle("Save " + getSaveDescription());
096:
097: for (;;) {
098: if (chooser.showSaveDialog(_frame) == JFileChooser.CANCEL_OPTION) {
099: break;
100: }
101: if (saveFile(chooser.getSelectedFile())) {
102: break;
103: }
104: }
105: }
106:
107: private boolean saveFile(File file) {
108: if (file.exists()) {
109: // i18n[exportconfig.fileExistsReplace={0}\nalready exists. Do you want to replace it?]
110: String msg = s_stringMgr.getString(
111: "exportconfig.fileExistsReplace", file
112: .getAbsolutePath());
113:
114: if (!Dialogs.showYesNo(_frame, msg)) {
115: return false;
116: }
117: if (!file.canWrite()) {
118: // i18n[exportconfig.fileExistsButReadOnly={0}\nexists and you cannot write to it.\nPlease use a different name.]
119: msg = s_stringMgr.getString(
120: "exportconfig.fileExistsButReadOnly", file
121: .getAbsolutePath());
122:
123: Dialogs.showOk(_frame, msg);
124: return false;
125: }
126: file.delete();
127: }
128:
129: s_lastDir = file.getParentFile();
130:
131: final IApplication app = _plugin.getApplication();
132: try {
133: writeToFile(file);
134:
135: String[] params = new String[] { getSaveDescription(),
136: file.getAbsolutePath() };
137:
138: // i18n[exportconfig.fileSavedTo={0} saved to {1}]
139: String msg = s_stringMgr.getString(
140: "exportconfig.fileSavedTo", params);
141:
142: Dialogs.showOk(_frame, msg);
143: } catch (IOException ex) {
144: // i18n[exportconfig.ioErrorWritingTo=IO Error writing to\n{0}]
145: String msg = s_stringMgr.getString(
146: "exportconfig.ioErrorWritingTo", file
147: .getAbsolutePath());
148: _plugin.getApplication().showErrorDialog(msg, ex);
149: } catch (XMLException ex) {
150: // i18n[exportconfig.xmlErrorWritingTo=XML Error writing to\n{0}]
151: String msg = s_stringMgr.getString(
152: "exportconfig.xmlErrorWritingTo", file
153: .getAbsolutePath());
154: _plugin.getApplication().showErrorDialog(msg, ex);
155: }
156: return true;
157: }
158:
159: /**
160: * Override this to supply the description of the objects being saved.
161: *
162: * @return description of the objects being saved.
163: */
164: protected abstract String getSaveDescription();
165:
166: /**
167: * Override this to supply the default file name to be used.
168: *
169: * @return The default file name.
170: */
171: protected abstract String getDefaultFilename();
172:
173: /**
174: * Override this m,ethod to do the actual writing out to the file system.
175: *
176: * @param file The <TT>File</TT> to be written to.
177: *
178: * @throws IOException Thrown if an IO error occurs.
179: * @throws XMLException Thrown if an XML error occurs.
180: */
181: protected abstract void writeToFile(File file) throws IOException,
182: XMLException;
183: }
|