bug 1469012, exception in CompletionProposal
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ViewTableAction.java
1 package com.quantum.actions;
2
3 import java.sql.SQLException;
4
5 import com.quantum.ImageStore;
6 import com.quantum.Messages;
7 import com.quantum.adapters.DatabaseAdapter;
8 import com.quantum.model.Bookmark;
9 import com.quantum.model.Entity;
10 import com.quantum.sql.MultiSQLServer;
11 import com.quantum.sql.SQLResultSetCollection;
12 import com.quantum.sql.SQLResultSetResults;
13 import com.quantum.sql.SQLResults;
14 import com.quantum.ui.dialog.SQLExceptionDialog;
15 import com.quantum.util.connection.ConnectionUtil;
16 import com.quantum.view.bookmark.EntityNode;
17
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IViewPart;
21 import org.eclipse.ui.actions.SelectionListenerAction;
22
23 /**
24  * @author root
25  * Implements action for "View Table"
26 */
27 public class ViewTableAction extends SelectionListenerAction {
28     private Entity entity;
29     private ConnectionUtil connectionUtil = new ConnectionUtil();
30         private final IViewPart view;
31     
32         public ViewTableAction(IViewPart view) {
33         super(Messages.getString(ViewTableAction.class, "text"));
34                 this.view = view;
35         setImageDescriptor(
36                         ImageStore.getImageDescriptor(ImageStore.OPEN_TABLE));
37         }
38
39         public void run() {
40                 
41                 Bookmark bookmark = this.entity.getBookmark();
42                 DatabaseAdapter adapter = bookmark.getAdapter();
43                 String query = adapter.getTableQuery((entity).getQualifiedName());
44                 
45                 try {
46                         SQLResults results = MultiSQLServer.getInstance().execute(
47                                         bookmark, 
48                                         this.connectionUtil.connect(bookmark, getShell()),
49                                         this.entity,
50                                         query);
51                         
52                         if (results != null && results.isResultSet()) {
53                                 SQLResultSetCollection.getInstance().addSQLResultSet((SQLResultSetResults) results);
54                         }
55                 } catch (SQLException e) {
56                         SQLExceptionDialog.openException(getShell(), bookmark, e);
57                 }
58         }
59
60         /**
61          * @return
62          */
63         private Shell getShell() {
64                 return this.view.getSite().getShell();
65         }
66
67         public boolean updateSelection(IStructuredSelection selection) {
68         boolean enabled = super.updateSelection(selection);
69         enabled &= (selection.size() == 1);
70         
71         if (enabled) {
72             Object object = selection.getFirstElement();
73             if (object != null && object instanceof EntityNode) {
74                 EntityNode entityNode = (EntityNode) object;
75                 this.entity = entityNode.getEntity();
76                 enabled &= (entityNode.isTable() || entityNode.isView());
77             }
78         }
79         return enabled;
80         }
81 }