From 579919f75f7f99894978f1b82b1ef775782d0639 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 7 Dec 2013 13:44:47 +0100 Subject: [PATCH] A Pager System --- src/com/massivecraft/mcore/pager/Pager.java | 32 +++++ .../mcore/pager/PagerAbstract.java | 120 ++++++++++++++++++ .../massivecraft/mcore/pager/PagerSimple.java | 39 ++++++ .../massivecraft/mcore/pager/Stringifier.java | 6 + src/com/massivecraft/mcore/util/Txt.java | 11 +- 5 files changed, 198 insertions(+), 10 deletions(-) create mode 100644 src/com/massivecraft/mcore/pager/Pager.java create mode 100644 src/com/massivecraft/mcore/pager/PagerAbstract.java create mode 100644 src/com/massivecraft/mcore/pager/PagerSimple.java create mode 100644 src/com/massivecraft/mcore/pager/Stringifier.java diff --git a/src/com/massivecraft/mcore/pager/Pager.java b/src/com/massivecraft/mcore/pager/Pager.java new file mode 100644 index 00000000..01d0ec5e --- /dev/null +++ b/src/com/massivecraft/mcore/pager/Pager.java @@ -0,0 +1,32 @@ +package com.massivecraft.mcore.pager; + +import java.util.Collection; +import java.util.List; + +public interface Pager +{ + // -------------------------------------------- // + // DATA SUPPLY + // -------------------------------------------- // + + public Collection getItems(); + public int getItemsPerPage(); + + // -------------------------------------------- // + // CORE + // -------------------------------------------- // + + public int size(); + public boolean isValid(int number); + public boolean isEmpty(); + public List get(int number); + + // -------------------------------------------- // + // TXT + // -------------------------------------------- // + + public String getMessageEmpty(); + public String getMessageInvalid(); + public List getPageTxt(int number, String title, Stringifier stringifier); + +} diff --git a/src/com/massivecraft/mcore/pager/PagerAbstract.java b/src/com/massivecraft/mcore/pager/PagerAbstract.java new file mode 100644 index 00000000..f0c8ee7e --- /dev/null +++ b/src/com/massivecraft/mcore/pager/PagerAbstract.java @@ -0,0 +1,120 @@ +package com.massivecraft.mcore.pager; + +import java.util.ArrayList; +import java.util.List; + +import com.massivecraft.mcore.util.Txt; + +public abstract class PagerAbstract implements Pager +{ + // -------------------------------------------- // + // CORE + // -------------------------------------------- // + + public int size() + { + return (int) Math.ceil((double) this.getItems().size() / this.getItemsPerPage()); + } + + public boolean isValid(int number) + { + if (this.isEmpty()) return false; + if (number < 1) return false; + if (number > this.size()) return false; + return true; + } + + public boolean isEmpty() + { + return this.getItems().isEmpty(); + } + + @SuppressWarnings("unchecked") + public List get(int number) + { + // Return null if the page number is invalid + if (!this.isValid(number)) return null; + + // Create return value + List ret = new ArrayList(); + + // Forge list from collection + List items = null; + if (this.getItems() instanceof List) + { + items = (List)this.getItems(); + } + else + { + items = new ArrayList(this.getItems()); + } + + int index = number - 1; + + // Calculate from and to + int from = index * this.getItemsPerPage(); + int to = from + this.getItemsPerPage(); + if (to > items.size()) + { + to = items.size(); + } + + // Pick them + ret.addAll(items.subList(from, to)); + + // Return return value + return ret; + } + + // -------------------------------------------- // + // TXT + // -------------------------------------------- // + + public String getMessageEmpty() + { + return Txt.parse("Sorry, no pages available."); + } + + public String getMessageInvalid() + { + return Txt.parse("Invalid, page must be between 1 and %d.", this.size()); + } + + @Override + public List getPageTxt(int number, String title, Stringifier stringifier) + { + List ret = new ArrayList(); + + ret.add(Txt.titleize(title + Txt.parse("") + " " + number + "/" + this.size())); + + if (this.isEmpty()) + { + ret.add(this.getMessageEmpty()); + return ret; + } + + List pageItems = this.get(number); + + if (pageItems == null) + { + ret.add(this.getMessageInvalid()); + return ret; + } + + for (T pageItem : pageItems) + { + if (stringifier != null) + { + ret.add(stringifier.toString(pageItem)); + } + else + { + ret.add(pageItem.toString()); + } + } + + return ret; + } + + +} diff --git a/src/com/massivecraft/mcore/pager/PagerSimple.java b/src/com/massivecraft/mcore/pager/PagerSimple.java new file mode 100644 index 00000000..82156e2f --- /dev/null +++ b/src/com/massivecraft/mcore/pager/PagerSimple.java @@ -0,0 +1,39 @@ +package com.massivecraft.mcore.pager; + +import java.util.Collection; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.massivecraft.mcore.util.Txt; + +public class PagerSimple extends PagerAbstract +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private Collection items; + @Override public Collection getItems() { return items; } + public void setItems(Collection items) { this.items = items; } + + private int itemsPerPage; + @Override public int getItemsPerPage() { return this.itemsPerPage; } + public void setItemsPerPage(int itemsPerPage) { this.itemsPerPage = itemsPerPage; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public PagerSimple(Collection items, int itemsPerPage) + { + this.items = items; + this.itemsPerPage = itemsPerPage; + } + + public PagerSimple(Collection items, CommandSender sender) + { + this(items, (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE); + } + +} diff --git a/src/com/massivecraft/mcore/pager/Stringifier.java b/src/com/massivecraft/mcore/pager/Stringifier.java new file mode 100644 index 00000000..fbf2553f --- /dev/null +++ b/src/com/massivecraft/mcore/pager/Stringifier.java @@ -0,0 +1,6 @@ +package com.massivecraft.mcore.pager; + +public interface Stringifier +{ + public String toString(T item); +} diff --git a/src/com/massivecraft/mcore/util/Txt.java b/src/com/massivecraft/mcore/util/Txt.java index 50e6303b..5f110520 100644 --- a/src/com/massivecraft/mcore/util/Txt.java +++ b/src/com/massivecraft/mcore/util/Txt.java @@ -417,16 +417,7 @@ public class Txt public static ArrayList getPage(List lines, int pageHumanBased, String title, CommandSender sender) { - int pageheight; - if (sender instanceof Player) - { - pageheight = PAGEHEIGHT_PLAYER; - } - else - { - pageheight = PAGEHEIGHT_CONSOLE; - } - return getPage(lines, pageHumanBased, title, pageheight); + return getPage(lines, pageHumanBased, title, (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE); } public static ArrayList getPage(List lines, int pageHumanBased, String title, int pageheight)