Guys I think cursors are the best choice for paging ... It is also
safe to send a cursor to the client and this is exactly what i am
doing ... I am saving all cursors on the client in an ArrayList and
using them to page forward and backward ... of coarse u have to start
by page 1 and move forward to be able to move backward ... I have 3
Buttons (First-Page, Previous-Page and Next Page) ... No Last-Page
because I did not implement any global Counters :) Tell me what u
think ... This is sample code:

//
----------------------------------------------------------------------------------
public enum CursorEnum {
        START, PREVIOUS, NEXT, CURRENT;
}
//
----------------------------------------------------------------------------------
public class Counter {
        private int count = 0;
        ...
}
//
----------------------------------------------------------------------------------
public class Page implements IsSerializable {

        private ArrayList<DataObject> objects; // DataObject = Parent of all
Objects ...

        private String cursorNext;

        ... // getter & setters
}
//
----------------------------------------------------------------------------------

private final ArrayList<String> cursors = new ArrayList<String>();
private final Counter cursorPosition = new Counter();

.....

// Select cursor to use ...
String cursorString = null;
switch (cursor) { // read CursorEnum cursor
case START:
        break;
case PREVIOUS:
        if (cursors.size() > 2) {
                cursorString = cursors.get(cursors.size() - 3);
        }
        break;
case NEXT:
        if (!cursors.isEmpty()) {
                cursorString = cursors.get(cursors.size() - 1);
        }
        break;
case CURRENT:
        if (cursors.size() > 1) {
                cursorString = cursors.get(cursors.size() - 2);
        }
        break;
}

//
----------------------------------------------------------------------------------

final String startingAt = cursorString;

// Call AsyncCallback ...

...getPage(className, startingAt, limit, filters, sortFields,
                new AsyncCallback<Page>() {

                        @Override
                        public void onFailure(Throwable caught) {
                                ...
                        }

                        @Override
                        public void onSuccess(Page page) {
                                ...

                                objects = page.getObjects();
                                if (objects.isEmpty()) {
                                        label_Range.setText("( Empty )");
                                        cursors.clear();
                                        cursorPosition.setCount(0);
                                } else {
                                        switch (cursor) {
                                        case START:
                                                cursors.clear();
                                                cursorPosition.setCount(0);
                                                break;
                                        case PREVIOUS:
                                                if (cursors.size() > 2) {
                                                        
cursors.remove(cursors.size() - 1);
                                                        
cursors.remove(cursors.size() - 1);
                                                        
cursorPosition.subtract(limit);
                                                } else {
                                                        cursors.clear();
                                                        
cursorPosition.setCount(0);
                                                }
                                                break;
                                        case NEXT:
                                                if (cursors.size() > 0) {
                                                        
cursorPosition.add(limit);
                                                }
                                                break;
                                        case CURRENT:
                                                if (cursors.size() > 0) {
                                                        
cursors.remove(cursors.size() - 1);
                                                }
                                                break;
                                        }
                                        cursors.add(page.getCursorNext());
                                        label_Range.setText("("
                                                        + 
(cursorPosition.getCount() + 1)
                                                        + " - "
                                                        + 
(cursorPosition.getCount() + objects
                                                                        
.size()) + ")");
                                }

                                ... // render objects
                        }
                });

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to