synchronized from quantum plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ViewTableDetailsAction.java
1 package com.quantum.actions;
2
3 import java.sql.Connection;
4 import java.util.List;
5
6 import com.quantum.Messages;
7 import com.quantum.QuantumPlugin;
8 import com.quantum.model.Bookmark;
9 import com.quantum.model.Entity;
10 import com.quantum.sql.SQLResults;
11 import com.quantum.util.connection.ConnectionUtil;
12 import com.quantum.view.ViewHelper;
13 import com.quantum.view.bookmark.EntityNode;
14 import com.quantum.view.tableview.TableView;
15
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.viewers.ISelection;
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 public class ViewTableDetailsAction extends SelectionListenerAction  {
24         private IViewPart view;
25     private ConnectionUtil connectionUtil = new ConnectionUtil();
26         
27         public ViewTableDetailsAction(IViewPart view) {
28         super(Messages.getString(ViewTableDetailsAction.class, "text"));
29         setImageDescriptor(QuantumPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
30                 this.view = view;
31         }
32
33         public void run() {
34         Entity entity = getEntity();
35                 StringBuffer query = new StringBuffer();
36                 query.append(Messages.getString("MetaDataKey")+Messages.getString("MetaDataKeySeparator")); //$NON-NLS-1$ //$NON-NLS-2$
37             query.append(entity.getQuotedTableName());
38         Connection connection = connectionUtil.getConnection(getBookmark(), getShell());
39         if (connection != null) {
40             SQLResults results = ViewHelper.tryGetResults(view, connection, query.toString());
41             if (results != null && ! results.isError()) 
42                 TableView.getInstance().loadQuery(entity.getBookmark(), results);
43         }
44         }
45         public void selectionChanged(IAction action, ISelection selection) {
46         }
47     protected Bookmark getBookmark() {
48         return getEntity().getBookmark();
49     }
50     protected Shell getShell() {
51         return this.view.getViewSite().getShell();
52     }
53     protected Entity getEntity() {
54         List list = getSelectedNonResources();
55         return ((EntityNode) list.get(0)).getEntity();
56     }
57
58     /**
59      * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
60      */
61     protected boolean updateSelection(IStructuredSelection selection) {
62         boolean enabled = super.updateSelection(selection);
63         return enabled && selection.size() == 1;
64     }
65
66 }