Source Code Cross Referenced for FileManager.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » client » session » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.client.session 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.