Initial import of the Guide contributed to the project byRobert Kraske. Was originall...
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / BaseSequenceAction.java
1 package com.quantum.actions;
2
3 import java.sql.Connection;
4 import java.util.List;
5
6 import com.quantum.adapters.DatabaseAdapter;
7 import com.quantum.model.Bookmark;
8 import com.quantum.sql.SQLResultSetCollection;
9 import com.quantum.sql.SQLResultSetResults;
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.bookmark.TreeNode;
15
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.ui.IViewPart;
19 import org.eclipse.ui.actions.SelectionListenerAction;
20
21 /**
22  * @author BC
23  */
24 public abstract class BaseSequenceAction extends SelectionListenerAction {
25     
26     protected IViewPart view;
27     private ConnectionUtil connectionUtil = new ConnectionUtil();
28
29     protected BaseSequenceAction(String text, IViewPart view) {
30         super(text);
31         this.view = view;
32     }
33
34     protected Bookmark getBookmark() {
35         TreeNode node = getEntityNode();
36         return node.getBookmark();
37     }
38     
39     protected Connection getConnection() {
40         return connectionUtil.getConnection(getBookmark(), getShell());
41     }
42
43     protected EntityNode getEntityNode() {
44         List list = getSelectedNonResources();
45         return (EntityNode) list.get(0);
46     }
47
48     protected Shell getShell() {
49         return this.view.getViewSite().getShell();
50     }
51     
52     protected boolean updateSelection(IStructuredSelection selection) {
53         boolean enabled = super.updateSelection(selection);
54         return enabled && selection.size() == 1 
55             && (selection.getFirstElement() instanceof EntityNode) 
56             && ((EntityNode) selection.getFirstElement()).isSequence();
57     }
58
59         public void run() {
60                 EntityNode sequence = getEntityNode();
61                 String name = null;
62                 if (sequence.isSequence()) {
63                         Bookmark bookmark = sequence.getBookmark();
64                         DatabaseAdapter adapter = bookmark.getAdapter();
65                         if (adapter == null) return;
66                         name = sequence.getName();
67                         String query = getQuery(sequence, name, adapter);
68                         
69                         SQLResults results = ViewHelper.tryGetResults(view, bookmark, getConnection(), query);
70                         if (results != null && results.isResultSet()) 
71                                 SQLResultSetCollection.getInstance().addSQLResultSet((SQLResultSetResults) results);
72                 }
73         }
74
75         /**
76          * @param sequence
77          * @param name
78          * @param adapter
79          * @return
80          */
81         protected abstract String getQuery(EntityNode sequence, String name, DatabaseAdapter adapter);
82 }