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