added file extension php5
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / ViewHelper.java
1 /*
2  * Created on 22-jul-2003
3  *
4  */
5 package com.quantum.view;
6
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.FileOutputStream;
10 import java.sql.Connection;
11 import java.sql.SQLException;
12
13 import com.quantum.Messages;
14 import com.quantum.QuantumPlugin;
15 import com.quantum.sql.MultiSQLServer;
16 import com.quantum.sql.SQLResults;
17
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.FileDialog;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IViewPart;
23
24 /**
25  * @author panic
26  *
27  */
28 public class ViewHelper {
29
30         public static SQLResults tryGetResults(IViewPart view, Connection con, String query) {
31                 return tryGetResults(view.getSite().getShell(), con, query);
32         }
33
34         public static SQLResults tryGetResults(Shell shell, Connection con, String query) {
35                 SQLResults results = null;      
36                 MultiSQLServer server = MultiSQLServer.getInstance();
37                 try {
38                         results = server.execute(con, query);
39                 } catch (SQLException e) {
40                         LogProxy log = LogProxy.getInstance();
41                         log.addText(LogProxy.ERROR, e.getLocalizedMessage(), e); //$NON-NLS-1$ //$NON-NLS-2$
42                         results = new SQLResults();
43                         results.setIsError(true);
44                         MessageDialog.openConfirm(shell, "Database returned error", e.getLocalizedMessage());  //$NON-NLS-1$                                                    
45                 }
46                 return results;
47         }
48         
49         public static FileOutputStream askSaveFile(String key, Shell shell) {
50                 return askSaveFile(key, shell, null, null);
51         }
52         /**
53          * Asks the user for a file to be saved. Uses a key to get the preferences and save the path.
54          * The preferences should be defined in the Messages file.
55          * @param key
56          * @param shell
57          * @param filterExt
58          * @param filterNames
59          * @return - An already opened FileOutputStream, or NULL if nothing selected, dialog canceled or some error.
60          */
61         public static FileOutputStream askSaveFile(String key, Shell shell, String[] filterExt, String[] filterNames) {
62         
63                 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
64                 
65                 if (filterExt == null) {
66                         dialog.setFilterExtensions(new String[]{Messages.getString("filedialog."+key+".filter"),
67                                                                                                         Messages.getString("filedialog.allfiles.filter")}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
68                         dialog.setFilterNames(new String[]{     Messages.getString("filedialog."+key+".name"),
69                                                                                                 Messages.getString("filedialog.allfiles.name")}); //$NON-NLS-1$ //$NON-NLS-2$
70                 } else {
71                         dialog.setFilterExtensions(filterExt);
72                         dialog.setFilterNames(filterNames);
73                 }       
74                 
75                 dialog.setFilterPath(QuantumPlugin.getDefault().getPreferenceStore().getString("quantum.dialogs."+ key + ".path"));
76                 String filename = dialog.open();
77                 if (filename == null) return null;
78                 // We save the used path
79                 QuantumPlugin.getDefault().getPreferenceStore().setValue("quantum.dialogs."+ key + ".path", filename);
80                 
81                 FileOutputStream out = null;
82                 File target = new File(filename);
83                 if (target.exists() && Messages.getString("filedialog.options.ConfirmOverwrite").equals("y")) {
84                         boolean confirmOverwrite = 
85                                 MessageDialog.openConfirm(shell, Messages.getString("filedialog.message.ConfirmOverwriteTitle"), //$NON-NLS-1$
86                                         Messages.getString("filedialog.message.ConfirmOverwriteMessage") + target.getName() + //$NON-NLS-1$ 
87                                         Messages.getString("filedialog.message.ConfirmOverwriteQuestion"));  //$NON-NLS-1$
88                         if (!confirmOverwrite) return null; 
89                 } 
90
91                 try {
92                         out = new FileOutputStream(target);
93                 } catch (FileNotFoundException e) {
94                         MessageDialog.openConfirm(shell, Messages.getString("filedialog.message.CannotOpenFileTitle"), //$NON-NLS-1$
95                                                 Messages.getString("filedialog.message.CannotOpenFileMessage") + filename+ //$NON-NLS-1$ 
96                                                 Messages.getString("filedialog.message.CannotOpenFileExplain"));  //$NON-NLS-1$ 
97
98                 }
99                 return out;             
100         }       
101 }