[Util] Pagination

Discussion in 'Resources' started by Polaris29, Jul 22, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Polaris29

    Hello, I noticed that there weren't any good utilities for pagination, so I made a somewhat better utility for it.

    Source:
    https://dl.dropboxusercontent.com/u/38824615/Pagination/Pagination.java
    https://dl.dropboxusercontent.com/u/38824615/Pagination/HtmlEntity.java
    The PaginationBook, PaginationPage, and PaginationEntry classes are inner classes within Pagination (which might not be a good idea).

    Usage:
    1. Get a PaginationBook from the Pagination.paginate method.
    Code:java
    1.  
    2. ArrayList<String> lines = new ArrayList<String>();
    3. for (int i = 1; i <= 100; i++) {
    4. lines.add("#"+i);
    5. }
    6.  
    7. PaginationBook book = Pagination.paginate(lines, 10);
    8.  


    2. Display a page with book.displayPage(CommandSender recipient, int pageNumber) or book.get(int pageNumber).display(CommandSender recipient);
    If recipient is null, System.out.println() will be used instead.
    Code:java
    1. book.displayPage(null, 1);


    3. The output page of the above steps would be:
    Show Spoiler

    #1
    #2
    #3
    #4
    #5
    #6
    #7
    #8
    #9
    #10
    Page 1/10


    4. You can also set the format of the pages and list items, there are custom variables that you can use in the formats, such as %n which is a line break.
    Code:java
    1.  
    2. ArrayList<String> lines = new ArrayList<String>();
    3. for (int i = 1; i <= 100; i++) {
    4. lines.add("#"+i);
    5. }
    6.  
    7. PaginationBook book = Pagination.paginate(lines, 10);
    8.  
    9. book.setEntryFormat("Line "+book.getEntryFormat());
    10. book.setPageFormat("=== Stuff ===%n%entriesPage %page out of %pagemax");
    11.  
    12. book.displayPage(null, 1);
    13.  

    The output would be:
    Show Spoiler

    === Stuff ===
    Line #1
    Line #2
    Line #3
    Line #4
    Line #5
    Line #6
    Line #7
    Line #8
    Line #9
    Line #10
    Page 1 out of 10


    5. There's also Html entity decoding.
    You first have toggle it on with book.decodeHtmlEntities(true);
    Then &szlig; would show up as ß, &raquo; as », &#402; as ƒ

    6. This is also mostly a fluent interface. So you can do this:
    Code:java
    1.  
    2. PaginationBook book = (PaginationBook) Pagination.paginate(lines, 10)
    3. .decodeHtmlEntities(true)
    4. .setEntryFormat("Line %entry%n")
    5. .setPageFormat("=== Stuff ===%n%entriesPage %page out of %pagemax");
    6.  


    7. You can change the individual "preferences" for everything within the Pagination book, so "decodeHtmlEntities" can be true for page 1 and false for page 2. But if you update the parent, its children will also take on what you changed, so if you set decodeHtmlEntities to true for the book, all of it's pages will also become true for that field. You can circumvent this by adding a second boolean argument that is false to the methods that change things around. Ex: decodeColorCodes(true, false);
     
    Wruczek, TigerHix, Jake0oo0 and 3 others like this.
Thread Status:
Not open for further replies.

Share This Page