Quantum version 2.4.1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / PreviousPageAction.java
1 package com.quantum.view.tableview;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.sql.SQLException;
6
7 import com.quantum.ImageStore;
8 import com.quantum.Messages;
9 import com.quantum.sql.SQLResultSetResults;
10 import com.quantum.sql.Scrollable;
11
12 import org.eclipse.jface.viewers.ISelectionProvider;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.ui.IViewPart;
15
16
17 /**
18  * @author BC
19  */
20 public class PreviousPageAction extends ResultSetAction implements PropertyChangeListener {
21
22         private SQLResultSetResults resultSet;
23
24         public PreviousPageAction(IViewPart view, ISelectionProvider selectionProvider) {
25                 super(view, selectionProvider);
26                 setText(Messages.getString(getClass(), "text"));
27                 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.PREVIOUS));
28                 setToolTipText(Messages.getString(getClass(), "text"));
29                 setEnabled(hasPreviousPage((IStructuredSelection) selectionProvider.getSelection()));
30         }
31
32         protected void executeResultSetAction(SQLResultSetResults results) throws SQLException {
33                 if (results instanceof Scrollable) {
34                         ((Scrollable) results).previousPage(getConnection(results));
35                 }
36         }
37         protected boolean updateSelection(IStructuredSelection selection) {
38                 setResultSet(getResultSet(selection));
39                 return hasPreviousPage(selection);
40         }
41
42         /**
43          * @param srollable
44          */
45         private void setResultSet(SQLResultSetResults results) {
46                 if (this.resultSet != null) {
47                         this.resultSet.removePropertyChangeListener(this);
48                 }
49                 
50                 this.resultSet = results;
51                 if (results != null) {
52                         this.resultSet.addPropertyChangeListener(this);
53                 }
54         }
55
56         /**
57          * @param selection
58          */
59         private SQLResultSetResults getResultSet(IStructuredSelection selection) {
60                 return (selection != null && !selection.isEmpty() && 
61                                 (selection.getFirstElement() instanceof SQLResultSetResults)) 
62                                 ? (SQLResultSetResults) selection.getFirstElement()
63                                 : null;
64         }
65
66         /**
67          * @param selection
68          * @return
69          */
70         private boolean hasPreviousPage(IStructuredSelection selection) {
71                 return !selection.isEmpty() && 
72                         (selection.getFirstElement() instanceof Scrollable) &&
73                         ((Scrollable) selection.getFirstElement()).hasPreviousPage();
74         }
75
76         public void propertyChange(PropertyChangeEvent event) {
77                 if ("rows".equals(event.getPropertyName())) {
78                         setEnabled(hasPreviousPage(getStructuredSelection()));
79                 }
80         }
81
82 }