Quantum version 2.4.1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / PreviousPageAction.java
diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/view/tableview/PreviousPageAction.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/view/tableview/PreviousPageAction.java
new file mode 100644 (file)
index 0000000..25ebe0a
--- /dev/null
@@ -0,0 +1,82 @@
+package com.quantum.view.tableview;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.sql.SQLException;
+
+import com.quantum.ImageStore;
+import com.quantum.Messages;
+import com.quantum.sql.SQLResultSetResults;
+import com.quantum.sql.Scrollable;
+
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IViewPart;
+
+
+/**
+ * @author BC
+ */
+public class PreviousPageAction extends ResultSetAction implements PropertyChangeListener {
+
+       private SQLResultSetResults resultSet;
+
+       public PreviousPageAction(IViewPart view, ISelectionProvider selectionProvider) {
+               super(view, selectionProvider);
+               setText(Messages.getString(getClass(), "text"));
+               setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.PREVIOUS));
+               setToolTipText(Messages.getString(getClass(), "text"));
+               setEnabled(hasPreviousPage((IStructuredSelection) selectionProvider.getSelection()));
+       }
+
+       protected void executeResultSetAction(SQLResultSetResults results) throws SQLException {
+               if (results instanceof Scrollable) {
+                       ((Scrollable) results).previousPage(getConnection(results));
+               }
+       }
+       protected boolean updateSelection(IStructuredSelection selection) {
+               setResultSet(getResultSet(selection));
+               return hasPreviousPage(selection);
+       }
+
+       /**
+        * @param srollable
+        */
+       private void setResultSet(SQLResultSetResults results) {
+               if (this.resultSet != null) {
+                       this.resultSet.removePropertyChangeListener(this);
+               }
+               
+               this.resultSet = results;
+               if (results != null) {
+                       this.resultSet.addPropertyChangeListener(this);
+               }
+       }
+
+       /**
+        * @param selection
+        */
+       private SQLResultSetResults getResultSet(IStructuredSelection selection) {
+               return (selection != null && !selection.isEmpty() && 
+                               (selection.getFirstElement() instanceof SQLResultSetResults)) 
+                               ? (SQLResultSetResults) selection.getFirstElement()
+                               : null;
+       }
+
+       /**
+        * @param selection
+        * @return
+        */
+       private boolean hasPreviousPage(IStructuredSelection selection) {
+               return !selection.isEmpty() && 
+                       (selection.getFirstElement() instanceof Scrollable) &&
+                       ((Scrollable) selection.getFirstElement()).hasPreviousPage();
+       }
+
+       public void propertyChange(PropertyChangeEvent event) {
+               if ("rows".equals(event.getPropertyName())) {
+                       setEnabled(hasPreviousPage(getStructuredSelection()));
+               }
+       }
+
+}