Introduce JCR query part
[gpl/argeo-jcr.git] / swt / org.argeo.cms.jcr.ui / src / org / argeo / cms / jcr / swt / JcrQueryTabularPart.java
diff --git a/swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/JcrQueryTabularPart.java b/swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/JcrQueryTabularPart.java
new file mode 100644 (file)
index 0000000..2ee35a2
--- /dev/null
@@ -0,0 +1,63 @@
+package org.argeo.cms.jcr.swt;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.query.Query;
+
+import org.argeo.cms.ux.widgets.AbstractTabularPart;
+import org.argeo.jcr.JcrException;
+
+public class JcrQueryTabularPart extends AbstractTabularPart<Query, Node> {
+       private int cursor = 0;
+       private NodeIterator nit = null;
+
+       @Override
+       public int getItemCount() {
+               return (int) nit.getSize();
+       }
+
+       @Override
+       public Node getData(int row) {
+               // System.out.println("Row " + row);
+               Node res;
+               if (row == cursor) {
+                       res = nit.nextNode();
+                       cursor++;
+               } else if (row > cursor) {
+                       nit.skip(row - cursor);
+                       cursor = row;
+                       res = nit.nextNode();
+                       cursor++;
+               } else if (row < cursor) {
+                       try {
+                               nit = getInput().execute().getNodes();
+                       } catch (RepositoryException e) {
+                               throw new JcrException("Cannot refresh query", e);
+                       }
+                       notifyItemCountChange();
+                       nit.skip(row);
+                       cursor = row;
+                       res = nit.nextNode();
+                       cursor++;
+               } else {
+                       throw new IllegalStateException("Cursor is " + cursor + " and row is " + row);
+               }
+               return res;
+       }
+
+       @Override
+       public void refresh() {
+               cursor = 0;
+               try {
+                       nit = getInput().execute().getNodes();
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot refresh query", e);
+               }
+               super.refresh();
+       }
+
+       public long getQuerySize() {
+               return nit.getSize();
+       }
+}