using Runnable to comply to the SWT threading model.
[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.util.connection.ConnectionUtil;
9
10 import org.eclipse.jface.viewers.ISelectionProvider;
11 import org.eclipse.jface.viewers.IStructuredSelection;
12 import org.eclipse.swt.widgets.Shell;
13 import org.eclipse.ui.IViewPart;
14 import org.eclipse.ui.actions.SelectionListenerAction;
15
16 /**
17  * @author BC Holmes
18  */
19 public abstract class ResultSetAction extends SelectionListenerAction {
20         private IViewPart view;
21         private ConnectionUtil connectionUtil = new ConnectionUtil();
22         private final ISelectionProvider selectionProvider;
23         
24         /**
25          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
26          */
27         public ResultSetAction(IViewPart view, ISelectionProvider selectionProvider) {
28                 super("");
29                 this.selectionProvider = selectionProvider;
30                 this.view = view;
31                 this.selectionProvider.addSelectionChangedListener(this);
32         }
33
34         public void run() {
35                 try {
36                         IStructuredSelection selection = 
37                                 (IStructuredSelection) this.selectionProvider.getSelection();
38                         if (!selection.isEmpty()) {
39                                 SQLResultSetResults results = (SQLResultSetResults) selection.getFirstElement();
40                                 executeResultSetAction(results);
41                         }
42                 } catch (SQLException e) {
43                         ExceptionDisplayDialog.openError(getShell(), null, null, e);
44                 } catch (RuntimeException e) {
45                         ExceptionDisplayDialog.openError(getShell(), null, null, e);
46                 }
47         }
48
49         protected boolean updateSelection(IStructuredSelection selection) {
50                 return selection != null && !selection.isEmpty();
51         }
52         /**
53          * @param results
54          * @throws SQLException
55          */
56         protected abstract void executeResultSetAction(SQLResultSetResults results) throws SQLException;
57
58         /**
59          * @param results
60          * @return
61          */
62         protected Connection getConnection(SQLResultSetResults results) {
63                 return this.connectionUtil.connect(results.getBookmark(), getShell());
64         }
65
66         /**
67          * @return
68          */
69         private Shell getShell() {
70                 return this.view.getSite().getShell();
71         }
72 }