jemrug
jemrug

Reputation: 89

PageableController, ContentProvider & pagination

I am really really stuck, so I have an implementation of View using table,tableviewer and "mycontentprovider"(which requests all from the dao all records in 1 go , so could be 10 to 300). All work nicely. my issue I want change it to a pagination. I want to change it so when link to page "3" is clicked we request page 3 from the data base.

I looked at nebula, the examples all show load all from dao(10, 300 records).
https://angelozerr.wordpress.com/2012/01/06/nebula_pagination/
I know i need to manipulate my doa to get a total and then say get page 3 of 300, only when the 3 link is clicked.
Can I use nebula framework to achieve this?
if so could someone help me and give me an overview of what classes I need to extend, implement to do this? or how can I change the following code to do so:-

public void createPartControl(Composite parent) {
    final List items = createList();
    parent.setLayoutData(new GridData(GridData.FILL_BOTH));
    parent.setLayout(new GridLayout());

    // Left panel
    Table table = new Table(parent, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL
            | SWT.V_SCROLL);
    table.setLayoutData(new GridData(GridData.FILL_BOTH));

    // 2) Initialize the table viewer + SWT Table
    viewer = new TableViewer(table);
    viewer.setContentProvider(ArrayContentProvider.getInstance());
    viewer.setLabelProvider(new LabelProvider());

    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    // 3) Create Table columns with sort of paginated list.
    int pageSize = 10;
    final PageableController controller = new PageableController(pageSize);
    final IPageLoader<PageResult> pageLoader = new PageResultLoaderList(
            items);
    controller.addPageChangedListener(PageLoaderStrategyHelper
            .createLoadPageAndReplaceItemsListener(controller, viewer,
                    pageLoader, PageResultContentProvider.getInstance(),
                    null));

    // Create navigation page links
    ResultAndNavigationPageLinksRenderer resultAndPageLinksDecorator = new ResultAndNavigationPageLinksRenderer(
            parent, SWT.NONE, controller);
    resultAndPageLinksDecorator.setLayoutData(new GridData(
            GridData.FILL_HORIZONTAL));

    createColumns(viewer, controller);
    // 3) Set current page to 0 to refresh the table

    controller.setCurrentPage(0);

    parent.setSize(350, 250);
}

private static void createColumns(final TableViewer viewer,
        PageableController controller) {

    // First column is for the first name
    TableViewerColumn col = createTableViewerColumn(viewer, "Name", 150);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            String p = (String) element;
            return p;
        }
    });
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            Person p = (Person) element;
            return p.getName();
        }
    });
    col.getColumn().addSelectionListener(
            new SortTableColumnSelectionListener("name", controller));

    // Second column is for the adress
    col = createTableViewerColumn(viewer, "Adress", 150);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            Person p = (Person) element;
            Address address = p.getAddress();
            if (address == null) {
                return "";
            }
            return address.getName();
        }
    });
    col.getColumn()
            .addSelectionListener(
                    new SortTableColumnSelectionListener("address.name",
                            controller));
}

private static List createList() {
    List names = new ArrayList();
    for (int i = 1; i < 100; i++) {
        names.add(new Person("Name " + i, i < 25 ? "Adress "
                + Math.random() : null));
    }
    return names;
}

private static TableViewerColumn createTableViewerColumn(
        TableViewer viewer, String title, int bound) {
    final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
            SWT.NONE);
    final TableColumn column = viewerColumn.getColumn();
    column.setText(title);
    column.setWidth(bound);
    column.setResizable(true);
    column.setMoveable(true);
    return viewerColumn;
}

how can I change the above so that I only load 10 items at a time on a click? thanks in advance,
Jemrug

Upvotes: 0

Views: 32

Answers (0)

Related Questions