Add predefined Velocity and CSS templates if wiki builder is assigned to a project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / ResultSetAction.java
1 package com.quantum.view.tableview;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5
6 import com.quantum.sql.SQLResultSetResults;
7 import com.quantum.ui.dialog.ExceptionDisplayDialog;
8 import com.quantum.ui.dialog.SQLExceptionDialog;
9 import com.quantum.util.connection.ConnectionUtil;
10
11 import org.eclipse.jface.viewers.ISelectionProvider;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.swt.widgets.Shell;
14 import org.eclipse.ui.IViewPart;
15 import org.eclipse.ui.actions.SelectionListenerAction;
16
17 /**
18  * @author BC Holmes
19  */
20 public abstract class ResultSetAction extends SelectionListenerAction {
21         private IViewPart view;
22         private ConnectionUtil connectionUtil = new ConnectionUtil();
23         private final ISelectionProvider selectionProvider;
24         
25         /**
26          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
27          */
28         public ResultSetAction(IViewPart view, ISelectionProvider selectionProvider) {
29                 super("");
30                 this.selectionProvider = selectionProvider;
31                 this.view = view;
32                 this.selectionProvider.addSelectionChangedListener(this);
33         }
34
35         public void run() {
36                 try {
37                         IStructuredSelection selection = 
38                                 (IStructuredSelection) this.selectionProvider.getSelection();
39                         if (!selection.isEmpty()) {
40                                 SQLResultSetResults results = (SQLResultSetResults) selection.getFirstElement();
41                                 executeResultSetAction(results);
42                         }
43                 } catch (SQLException e) {
44                         SQLExceptionDialog.openException(getShell(), null, e);
45                 } catch (RuntimeException e) {
46                         ExceptionDisplayDialog.openError(getShell(), null, null, e);
47                 }
48         }
49
50         protected boolean updateSelection(IStructuredSelection selection) {
51                 return selection != null && !selection.isEmpty();
52         }
53         /**
54          * @param results
55          * @throws SQLException
56          */
57         protected abstract void executeResultSetAction(SQLResultSetResults results) throws SQLException;
58
59         /**
60          * @param results
61          * @return
62          */
63         protected Connection getConnection(SQLResultSetResults results) {
64                 return this.connectionUtil.connect(results.getBookmark(), getShell());
65         }
66
67         /**
68          * @return
69          */
70         private Shell getShell() {
71                 return this.view.getSite().getShell();
72         }
73 }