KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > editor > SQLEditorProviderImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.sql.editor;
21
22 import java.io.BufferedWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import org.netbeans.api.db.explorer.DatabaseConnection;
29 import org.netbeans.modules.db.api.sql.execute.SQLExecuteCookie;
30 import org.netbeans.modules.db.spi.sql.editor.SQLEditorProvider;
31 import org.netbeans.modules.db.sql.editor.ui.actions.ConnectionAction;
32 import org.openide.ErrorManager;
33 import org.openide.cookies.OpenCookie;
34 import org.openide.filesystems.FileLock;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.Repository;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.DataObjectNotFoundException;
40 import org.openide.util.NbBundle;
41
42 /**
43  *
44  * @author Andrei Badea
45  */

46 public class SQLEditorProviderImpl implements SQLEditorProvider {
47     
48     // TODO: should ensure that the number of the generated temporary file
49
// is greater than any of the numbers of the existing files
50

51     private static final String JavaDoc CMD_FOLDER = "Databases/SQLCommands"; // NOI18N
52

53     public void openSQLEditor(DatabaseConnection dbconn, String JavaDoc sql, boolean execute) {
54         FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
55         FileObject tmpFo = root.getFileObject(CMD_FOLDER);
56         if (tmpFo == null) {
57             try {
58                 tmpFo = FileUtil.createFolder(root, CMD_FOLDER );
59             } catch (IOException JavaDoc e) {
60                 ErrorManager.getDefault().notify(e);
61             }
62         }
63         
64         FileObject sqlFo = null;
65         
66         int i = 1;
67         for (;;) {
68             String JavaDoc nameFmt = NbBundle.getMessage(SQLEditorProviderImpl.class, "LBL_SQLCommandFileName");
69             String JavaDoc name = MessageFormat.format(nameFmt, new Object JavaDoc[] { new Integer JavaDoc(i) });
70             try {
71                 sqlFo = tmpFo.createData(name);
72             } catch (IOException JavaDoc e) {
73                 i++;
74                 continue;
75             }
76             break;
77         }
78         
79         try {
80             FileLock lock = sqlFo.lock();
81             try {
82                 OutputStream JavaDoc stream = sqlFo.getOutputStream(lock);
83                 try {
84                     // write an utf-8 byte order mark
85
stream.write(0xef);
86                     stream.write(0xbb);
87                     stream.write(0xbf);
88                     BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(stream, "utf-8")); // NOI18N
89
writer.write(sql);
90                     writer.close();
91                 } finally {
92                     stream.close();
93                 }
94             } finally {
95                 lock.releaseLock();
96             }
97         } catch (IOException JavaDoc e) {
98             ErrorManager.getDefault().notify(e);
99         }
100         
101         DataObject sqlDo;
102         try {
103             sqlDo = DataObject.find(sqlFo);
104         } catch (DataObjectNotFoundException e) {
105             ErrorManager.getDefault().notify(e);
106             return;
107         }
108         
109         OpenCookie openCookie = (OpenCookie)sqlDo.getCookie(OpenCookie.class);
110         openCookie.open();
111         
112         SQLExecuteCookie sqlCookie = (SQLExecuteCookie)sqlDo.getCookie(SQLExecuteCookie.class);
113         sqlCookie.setDatabaseConnection(dbconn);
114         if (execute) {
115             sqlCookie.execute();
116         }
117     }
118 }
119
Popular Tags