A Pager System

This commit is contained in:
Olof Larsson 2013-12-07 13:44:47 +01:00
parent bd98a9f87a
commit 579919f75f
5 changed files with 198 additions and 10 deletions

View File

@ -0,0 +1,32 @@
package com.massivecraft.mcore.pager;
import java.util.Collection;
import java.util.List;
public interface Pager<T>
{
// -------------------------------------------- //
// DATA SUPPLY
// -------------------------------------------- //
public Collection<? extends T> getItems();
public int getItemsPerPage();
// -------------------------------------------- //
// CORE
// -------------------------------------------- //
public int size();
public boolean isValid(int number);
public boolean isEmpty();
public List<T> get(int number);
// -------------------------------------------- //
// TXT
// -------------------------------------------- //
public String getMessageEmpty();
public String getMessageInvalid();
public List<String> getPageTxt(int number, String title, Stringifier<T> stringifier);
}

View File

@ -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<T> implements Pager<T>
{
// -------------------------------------------- //
// 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<T> get(int number)
{
// Return null if the page number is invalid
if (!this.isValid(number)) return null;
// Create return value
List<T> ret = new ArrayList<T>();
// Forge list from collection
List<T> items = null;
if (this.getItems() instanceof List)
{
items = (List<T>)this.getItems();
}
else
{
items = new ArrayList<T>(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("<i>Sorry, no pages available.");
}
public String getMessageInvalid()
{
return Txt.parse("<b>Invalid, page must be between 1 and %d.", this.size());
}
@Override
public List<String> getPageTxt(int number, String title, Stringifier<T> stringifier)
{
List<String> ret = new ArrayList<String>();
ret.add(Txt.titleize(title + Txt.parse("<a>") + " " + number + "/" + this.size()));
if (this.isEmpty())
{
ret.add(this.getMessageEmpty());
return ret;
}
List<T> 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;
}
}

View File

@ -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<T> extends PagerAbstract<T>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private Collection<? extends T> items;
@Override public Collection<? extends T> getItems() { return items; }
public void setItems(Collection<? extends T> 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<? extends T> items, int itemsPerPage)
{
this.items = items;
this.itemsPerPage = itemsPerPage;
}
public PagerSimple(Collection<? extends T> items, CommandSender sender)
{
this(items, (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE);
}
}

View File

@ -0,0 +1,6 @@
package com.massivecraft.mcore.pager;
public interface Stringifier<T>
{
public String toString(T item);
}

View File

@ -417,16 +417,7 @@ public class Txt
public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title, CommandSender sender) public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title, CommandSender sender)
{ {
int pageheight; return getPage(lines, pageHumanBased, title, (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE);
if (sender instanceof Player)
{
pageheight = PAGEHEIGHT_PLAYER;
}
else
{
pageheight = PAGEHEIGHT_CONSOLE;
}
return getPage(lines, pageHumanBased, title, pageheight);
} }
public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title, int pageheight) public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title, int pageheight)