Skip to content

Commit

Permalink
Add search button to the CommonPagedPanel.
Browse files Browse the repository at this point in the history
Search button will allow to search elements if there are more than displayed elements.
  • Loading branch information
BONNe committed Sep 20, 2021
1 parent 8c9ddb1 commit 09d5bfc
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Consumer;

import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
Expand All @@ -27,7 +27,7 @@
/**
* This panel implements common things for Paged pages.
*/
public abstract class CommonPagedPanel extends CommonPanel
public abstract class CommonPagedPanel<T> extends CommonPanel
{
/**
* Instantiates a new Common paged panel.
Expand Down Expand Up @@ -57,16 +57,28 @@ protected CommonPagedPanel(@NonNull CommonPanel parentPanel)
}


/**
* This method is called when filter value is updated.
*/
protected abstract void updateFilters();


/**
* Create element button panel item.
*
* @param object the object
* @return the panel item
*/
protected abstract PanelItem createElementButton(T object);


/**
* Populate elements.
*
* @param panelBuilder the panel builder
* @param objectList the object list
* @param buttonBuilder the button builder
*/
protected void populateElements(PanelBuilder panelBuilder,
List<?> objectList,
Function<Object, PanelItem> buttonBuilder)
protected void populateElements(PanelBuilder panelBuilder, List<T> objectList)
{
final int MAX_ELEMENTS = 21;
final int size = objectList.size();
Expand All @@ -91,7 +103,7 @@ else if (this.pageIndex > (size / MAX_ELEMENTS))
{
if (!panelBuilder.slotOccupied(index))
{
panelBuilder.item(index, buttonBuilder.apply(objectList.get(objectIndex++)));
panelBuilder.item(index, this.createElementButton(objectList.get(objectIndex++)));
}

index++;
Expand All @@ -107,6 +119,13 @@ else if (this.pageIndex > (size / MAX_ELEMENTS))
{
panelBuilder.item(18, this.getButton(CommonButtons.PREVIOUS));
}

// Add search button only if there is more than MAX_ELEMENTS objects or searchString
// is not blank.
if (!this.searchString.isBlank() || objectList.size() > MAX_ELEMENTS)
{
panelBuilder.item(40, this.getButton(CommonButtons.SEARCH));
}
}


Expand Down Expand Up @@ -157,6 +176,59 @@ else if (button == CommonButtons.PREVIOUS)
return true;
};
}
else if (button == CommonButtons.SEARCH)
{
description.add(this.user.getTranslation(reference + "description"));

if (this.searchString != null && !this.searchString.isEmpty())
{
description.add(this.user.getTranslation(reference + "search",
Constants.PARAMETER_VALUE, this.searchString));
}

description.add("");
description.add(this.user.getTranslation(Constants.TIPS + "left-click-to-edit"));

if (!this.searchString.isEmpty())
{
description.add(this.user.getTranslation(Constants.TIPS + "right-click-to-clear"));
}

icon = new ItemStack(Material.ANVIL);

clickHandler = (panel, user, clickType, slot) -> {
if (clickType.isRightClick())
{
// Clear string.
this.searchString = "";
this.updateFilters();
// Rebuild gui.
this.build();
}
else
{
// Create consumer that process description change
Consumer<String> consumer = value ->
{
if (value != null)
{
this.searchString = value;
this.updateFilters();
}

this.build();
};

// start conversation
ConversationUtils.createStringInput(consumer,
user,
user.getTranslation(Constants.CONVERSATIONS + "write-search"),
user.getTranslation(Constants.CONVERSATIONS + "search-updated"));
}

return true;
};
}
else
{
icon = new ItemStack(Material.PAPER);
Expand All @@ -178,12 +250,18 @@ else if (button == CommonButtons.PREVIOUS)
private enum CommonButtons
{
NEXT,
PREVIOUS
PREVIOUS,
SEARCH
}


/**
* Current page index.
*/
private int pageIndex;

/**
* Text that contains filter string.
*/
protected String searchString = "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/**
* This class contains all necessary elements to create Levels Edit GUI.
*/
public class EditLevelPanel extends CommonPagedPanel
public class EditLevelPanel extends CommonPagedPanel<Challenge>
{
// ---------------------------------------------------------------------
// Section: Constructors
Expand Down Expand Up @@ -111,6 +111,16 @@ public static void open(CommonPanel panel, ChallengeLevel level)
// ---------------------------------------------------------------------


/**
* This method is called when filter value is updated.
*/
@Override
protected void updateFilters()
{
// Do nothing here.
}


/**
* This method builds all necessary elements in GUI panel.
*/
Expand Down Expand Up @@ -190,11 +200,15 @@ private void buildRewardsPanel(PanelBuilder panelBuilder)
*/
private void buildChallengesPanel(PanelBuilder panelBuilder)
{
List<Challenge> challengeList = this.addon.getChallengesManager().getLevelChallenges(this.challengeLevel);
List<Challenge> challengeList = this.addon.getChallengesManager().
getLevelChallenges(this.challengeLevel).stream().
filter(challenge -> this.searchString.isBlank() ||
challenge.getFriendlyName().toLowerCase().contains(this.searchString.toLowerCase()) ||
challenge.getUniqueId().toLowerCase().contains(this.searchString.toLowerCase()) ||
challenge.getChallengeType().name().toLowerCase().contains(this.searchString)).
collect(Collectors.toList());

this.populateElements(panelBuilder,
challengeList,
o -> this.createChallengeIcon((Challenge) o));
this.populateElements(panelBuilder, challengeList);

panelBuilder.item(39, this.createButton(Button.ADD_CHALLENGES));
panelBuilder.item(41, this.createButton(Button.REMOVE_CHALLENGES));
Expand Down Expand Up @@ -279,7 +293,8 @@ private PanelItem createMenuButton(MenuType menuType)
* @param challenge Challenge which icon must be created.
* @return PanelItem that represents given challenge.
*/
private PanelItem createChallengeIcon(Challenge challenge)
@Override
protected PanelItem createElementButton(Challenge challenge)
{
return new PanelItemBuilder().
name(Util.translateColorCodes(challenge.getFriendlyName())).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* This class contains all necessary elements to create GUI that lists all challenges.
* It allows to edit them or remove, depending on given input mode.
*/
public class LibraryPanel extends CommonPagedPanel
public class LibraryPanel extends CommonPagedPanel<LibraryEntry>
{
// ---------------------------------------------------------------------
// Section: Constructor
Expand All @@ -54,6 +54,8 @@ private LibraryPanel(CommonPanel parentGUI, Library mode)
case DATABASE -> this.generateDatabaseEntries();
case TEMPLATE -> this.generateTemplateEntries();
};

this.filterElements = this.libraryEntries;
}


Expand Down Expand Up @@ -130,6 +132,32 @@ private List<LibraryEntry> generateTemplateEntries()
// ---------------------------------------------------------------------


/**
* This method is called when filter value is updated.
*/
@Override
protected void updateFilters()
{
if (this.searchString == null || this.searchString.isBlank())
{
this.filterElements = this.libraryEntries;
}
else
{
this.filterElements = this.libraryEntries.stream().
filter(element -> {
// If element name is set and name contains search field, then do not filter out.
return element.name().toLowerCase().contains(this.searchString.toLowerCase()) ||
element.author().toLowerCase().contains(this.searchString.toLowerCase()) ||
element.gameMode().toLowerCase().contains(this.searchString.toLowerCase()) ||
element.language().toLowerCase().contains(this.searchString.toLowerCase());
}).
distinct().
collect(Collectors.toList());
}
}


/**
* {@inheritDoc}
*/
Expand All @@ -156,9 +184,7 @@ protected void build()

GuiUtils.fillBorder(panelBuilder);

this.populateElements(panelBuilder,
this.libraryEntries,
o -> this.createEntryIcon((LibraryEntry) o));
this.populateElements(panelBuilder, this.filterElements);

if (this.mode == Library.WEB)
{
Expand Down Expand Up @@ -232,7 +258,8 @@ private PanelItem createDownloadNow()
* @param libraryEntry LibraryEntry which button must be created.
* @return Entry button.
*/
private PanelItem createEntryIcon(LibraryEntry libraryEntry)
@Override
protected PanelItem createElementButton(LibraryEntry libraryEntry)
{
PanelItemBuilder itemBuilder = new PanelItemBuilder().
name(ChatColor.translateAlternateColorCodes('&', libraryEntry.name())).
Expand Down Expand Up @@ -436,4 +463,9 @@ public enum Library
* List of library elements.
*/
private final List<LibraryEntry> libraryEntries;

/**
* Stores filtered items.
*/
private List<LibraryEntry> filterElements;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package world.bentobox.challenges.panel.admin;


import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.bukkit.Material;
import org.bukkit.World;
Expand All @@ -25,7 +27,7 @@
* This class contains all necessary elements to create GUI that lists all challenges.
* It allows to edit them or remove, depending on given input mode.
*/
public class ListChallengesPanel extends CommonPagedPanel
public class ListChallengesPanel extends CommonPagedPanel<Challenge>
{
// ---------------------------------------------------------------------
// Section: Constructor
Expand Down Expand Up @@ -96,6 +98,16 @@ public static void open(CommonPanel parentGUI, Mode mode)
// ---------------------------------------------------------------------


/**
* This method is called when filter value is updated.
*/
@Override
protected void updateFilters()
{
// Do nothing here.
}


/**
* {@inheritDoc}
*/
Expand All @@ -114,9 +126,15 @@ protected void build()
GuiUtils.fillBorder(panelBuilder);
}

this.populateElements(panelBuilder,
this.addon.getChallengesManager().getAllChallenges(this.world),
o -> this.createChallengeIcon((Challenge) o));
List<Challenge> challengeList = this.addon.getChallengesManager().getAllChallenges(this.world).
stream().
filter(challenge -> this.searchString.isBlank() ||
challenge.getFriendlyName().toLowerCase().contains(this.searchString.toLowerCase()) ||
challenge.getUniqueId().toLowerCase().contains(this.searchString.toLowerCase()) ||
challenge.getChallengeType().name().toLowerCase().contains(this.searchString)).
collect(Collectors.toList());

this.populateElements(panelBuilder, challengeList);

panelBuilder.item(44, this.returnButton);

Expand All @@ -129,7 +147,8 @@ protected void build()
* @param challenge Challenge which button must be created.
* @return Challenge button.
*/
private PanelItem createChallengeIcon(Challenge challenge)
@Override
protected PanelItem createElementButton(Challenge challenge)
{
PanelItemBuilder itemBuilder = new PanelItemBuilder().
name(Util.translateColorCodes(challenge.getFriendlyName())).
Expand Down

0 comments on commit 09d5bfc

Please sign in to comment.