001: package net.sourceforge.squirrel_sql.client.session;
002:
003: import java.io.BufferedInputStream;
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.util.HashMap;
009:
010: import javax.swing.JFileChooser;
011: import javax.swing.JFrame;
012:
013: import net.sourceforge.squirrel_sql.client.gui.mainframe.MainFrame;
014: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
015: import net.sourceforge.squirrel_sql.fw.gui.ChooserPreviewer;
016: import net.sourceforge.squirrel_sql.fw.gui.Dialogs;
017: import net.sourceforge.squirrel_sql.fw.util.FileExtensionFilter;
018: import net.sourceforge.squirrel_sql.fw.util.StringManager;
019: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
020:
021: public class FileManager {
022: private ISQLPanelAPI _sqlPanelAPI;
023:
024: private File _toSaveTo = null;
025:
026: private static final StringManager s_stringMgr = StringManagerFactory
027: .getStringManager(FileManager.class);
028:
029: private JFileChooser fileChooser = null;
030:
031: private HashMap<FileExtensionFilter, String> fileAppenixes = new HashMap<FileExtensionFilter, String>();
032:
033: FileManager(ISQLPanelAPI sqlPanelAPI) {
034: _sqlPanelAPI = sqlPanelAPI;
035: }
036:
037: public boolean save() {
038: return saveIntern(false);
039: }
040:
041: public boolean saveAs() {
042: return saveIntern(true);
043: }
044:
045: public boolean open(File f) {
046: boolean result = false;
047: _sqlPanelAPI.getSession().selectMainTab(
048: ISession.IMainPanelTabIndexes.SQL_TAB);
049: result = true;
050: _sqlPanelAPI.setEntireSQLScript("");
051: loadScript(f);
052: return result;
053: }
054:
055: public boolean open(boolean appendToExisting) {
056: boolean result = false;
057: JFileChooser chooser = getFileChooser();
058: chooser.setAccessory(new ChooserPreviewer());
059:
060: SquirrelPreferences prefs = _sqlPanelAPI.getSession()
061: .getApplication().getSquirrelPreferences();
062: MainFrame frame = _sqlPanelAPI.getSession().getApplication()
063: .getMainFrame();
064:
065: if (prefs.isFileOpenInPreviousDir()) {
066: String fileName = prefs.getFilePreviousDir();
067: if (fileName != null) {
068: chooser.setCurrentDirectory(new File(fileName));
069: }
070: } else {
071: String dirName = prefs.getFileSpecifiedDir();
072: if (dirName != null) {
073: chooser.setCurrentDirectory(new File(dirName));
074: }
075: }
076: _sqlPanelAPI.getSession().selectMainTab(
077: ISession.IMainPanelTabIndexes.SQL_TAB);
078: if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
079: result = true;
080: File selectedFile = chooser.getSelectedFile();
081: if (!appendToExisting) {
082: _sqlPanelAPI.setEntireSQLScript("");
083: }
084: loadScript(selectedFile);
085:
086: }
087: return result;
088: }
089:
090: private void loadScript(File file) {
091: SquirrelPreferences prefs = _sqlPanelAPI.getSession()
092: .getApplication().getSquirrelPreferences();
093: FileInputStream fis = null;
094: BufferedInputStream bis = null;
095: try {
096: StringBuffer sb = new StringBuffer();
097: fis = new FileInputStream(file);
098: bis = new BufferedInputStream(fis);
099: byte[] bytes = new byte[2048];
100: int iRead = bis.read(bytes);
101: while (iRead != -1) {
102: sb.append(new String(bytes, 0, iRead));
103: iRead = bis.read(bytes);
104: }
105: _sqlPanelAPI.appendSQLScript(sb.toString(), true);
106: setFile(file);
107: prefs.setFilePreviousDir(file.getAbsolutePath());
108: } catch (java.io.IOException io) {
109: _sqlPanelAPI.getSession().showErrorMessage(io);
110: } finally {
111: if (bis != null) {
112: try {
113: bis.close();
114: } catch (IOException ignore) {
115: }
116: }
117: if (fis != null) {
118: try {
119: fis.close();
120: } catch (IOException ignore) {
121: }
122: }
123: }
124: }
125:
126: public boolean saveIntern(boolean toNewFile) {
127: boolean result = false;
128: if (toNewFile) {
129: _toSaveTo = null;
130: }
131:
132: JFileChooser chooser = getFileChooser();
133:
134: SquirrelPreferences prefs = _sqlPanelAPI.getSession()
135: .getApplication().getSquirrelPreferences();
136: MainFrame frame = _sqlPanelAPI.getSession().getApplication()
137: .getMainFrame();
138:
139: for (;;) {
140: if (null == _toSaveTo) {
141: if (prefs.isFileOpenInPreviousDir()) {
142: String dirName = prefs.getFilePreviousDir();
143: if (dirName != null) {
144: chooser.setCurrentDirectory(new File(dirName));
145: }
146: } else {
147: String dirName = prefs.getFileSpecifiedDir();
148: if (dirName != null) {
149: chooser.setCurrentDirectory(new File(dirName));
150: }
151: }
152: }
153:
154: _sqlPanelAPI.getSession().selectMainTab(
155: ISession.IMainPanelTabIndexes.SQL_TAB);
156:
157: if (null != _toSaveTo) {
158: if (saveScript(frame, _toSaveTo, false)) {
159: result = true;
160: }
161: break;
162: }
163:
164: if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
165:
166: _toSaveTo = chooser.getSelectedFile();
167:
168: if (!_toSaveTo.exists()
169: && null != fileAppenixes.get(chooser
170: .getFileFilter())) {
171: if (!_toSaveTo.getAbsolutePath().endsWith(
172: fileAppenixes.get(chooser.getFileFilter())
173: .toString())) {
174: _toSaveTo = new File(_toSaveTo
175: .getAbsolutePath()
176: + fileAppenixes.get(chooser
177: .getFileFilter()));
178: }
179: }
180:
181: if (saveScript(frame, _toSaveTo, true)) {
182: result = true;
183: break;
184: } else {
185: result = false;
186: break;
187: }
188: } else {
189: break;
190: }
191: }
192: return result;
193: }
194:
195: private boolean saveScript(JFrame frame, File file,
196: boolean askReplace) {
197: boolean doSave = false;
198: if (askReplace && file.exists()) {
199: // i18n[FileManager.confirm.filereplace={0} \nalready exists. Do you want to replace it?]
200: String confirmMsg = s_stringMgr.getString(
201: "FileManager.confirm.filereplace", file
202: .getAbsolutePath());
203: doSave = Dialogs.showYesNo(frame, confirmMsg);
204: //i18n
205: if (!doSave) {
206: return false;
207: }
208: if (!file.canWrite()) {
209: // i18n[FileManager.error.cannotwritefile=File {0} \ncannot be written to.]
210: String msg = s_stringMgr.getString(
211: "FileManager.error.cannotwritefile", file
212: .getAbsolutePath());
213: Dialogs.showOk(frame, msg);
214: return false;
215: }
216: file.delete();
217: } else {
218: doSave = true;
219: }
220:
221: SquirrelPreferences prefs = _sqlPanelAPI.getSession()
222: .getApplication().getSquirrelPreferences();
223:
224: if (doSave) {
225: prefs.setFilePreviousDir(file.getParent());
226:
227: FileOutputStream fos = null;
228: try {
229: fos = new FileOutputStream(file);
230:
231: String sScript = _sqlPanelAPI.getEntireSQLScript();
232:
233: fos.write(sScript.getBytes());
234: setFile(file);
235: // i18n[FileManager.savedfile=Saved to {0}]
236: String msg = s_stringMgr
237: .getString("FileManager.savedfile", file
238: .getAbsolutePath());
239: _sqlPanelAPI.getSession().showMessage(msg);
240: } catch (IOException ex) {
241: _sqlPanelAPI.getSession().showErrorMessage(ex);
242: } finally {
243: if (fos != null) {
244: try {
245: fos.close();
246: } catch (IOException ignore) {
247: }
248: }
249: }
250: }
251: return true;
252: }
253:
254: private void setFile(File file) {
255: _toSaveTo = file;
256: _sqlPanelAPI.getSession().getActiveSessionWindow().setSqlFile(
257: file);
258: }
259:
260: public File getFile() {
261: return _toSaveTo;
262: }
263:
264: private JFileChooser getFileChooser() {
265: if (fileChooser == null) {
266: fileChooser = new JFileChooser();
267: FileExtensionFilter filter = new FileExtensionFilter(
268: "Text files", new String[] { ".txt" });
269: fileChooser.addChoosableFileFilter(filter);
270: fileAppenixes.put(filter, ".txt");
271: filter = new FileExtensionFilter("SQL files",
272: new String[] { ".sql" });
273: fileChooser.addChoosableFileFilter(filter);
274: fileAppenixes.put(filter, ".sql");
275: }
276: return fileChooser;
277: }
278:
279: public void clearCurrentFile() {
280: _toSaveTo = null;
281: }
282:
283: }
|