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())); } } }