Skip to content

Commit

Permalink
Add search field to the PagedSelectors.
Browse files Browse the repository at this point in the history
Add missing tooltips.
  • Loading branch information
BONNe committed Sep 20, 2021
1 parent b138e50 commit a75c243
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -35,6 +36,7 @@ private ChallengeSelector(User user, Material border, Map<Challenge, List<String

this.elements = challengesDescriptionMap.keySet().stream().toList();
this.selectedElements = new HashSet<>(this.elements.size());
this.filterElements = this.elements;
}


Expand All @@ -61,7 +63,7 @@ protected void build()

GuiUtils.fillBorder(panelBuilder, this.border);

this.populateElements(panelBuilder, this.elements);
this.populateElements(panelBuilder, this.filterElements);

panelBuilder.item(3, this.createButton(Button.ACCEPT_SELECTED));
panelBuilder.item(5, this.createButton(Button.CANCEL));
Expand All @@ -70,6 +72,32 @@ protected void build()
}


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


/**
* This method creates PanelItem button of requested type.
* @param button Button which must be created.
Expand Down Expand Up @@ -218,4 +246,9 @@ private enum Button
* Border Material.
*/
private final Material border;

/**
* Current value.
*/
private List<Challenge> filterElements;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ private MultiBlockSelector(User user, Mode mode, Set<Material> excluded, BiConsu
}
}
}).
// Sort by name
sorted(Comparator.comparing(Material::name)).
collect(Collectors.toList());
// Init without filters applied.
this.filterElements = this.elements;
}


Expand Down Expand Up @@ -102,7 +106,7 @@ protected void build()

GuiUtils.fillBorder(panelBuilder, Material.BLUE_STAINED_GLASS_PANE);

this.populateElements(panelBuilder, this.elements);
this.populateElements(panelBuilder, this.filterElements);

panelBuilder.item(3, this.createButton(Button.ACCEPT_SELECTED));
panelBuilder.item(5, this.createButton(Button.CANCEL));
Expand All @@ -111,6 +115,29 @@ protected void build()
}


/**
* This method is called when filter value is updated.
*/
@Override
protected void updateFilters()
{
if (this.searchString == null || this.searchString.isBlank())
{
this.filterElements = this.elements;
}
else
{
this.filterElements = this.elements.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());
}).
distinct().
collect(Collectors.toList());
}
}


/**
* This method creates PanelItem button of requested type.
* @param button Button which must be created.
Expand Down Expand Up @@ -260,4 +287,9 @@ public enum Mode
* This variable stores consumer.
*/
private final BiConsumer<Boolean, Collection<Material>> consumer;

/**
* Stores filtered items.
*/
private List<Material> filterElements;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ private MultiEntitySelector(User user, boolean asEgg, Mode mode, Set<EntityType>
return true;
}
}).
// Sort by name
sorted(Comparator.comparing(EntityType::name)).
collect(Collectors.toList());
// Init without filters applied.
this.filterElements = this.elements;
}


Expand Down Expand Up @@ -86,7 +90,7 @@ protected void build()

GuiUtils.fillBorder(panelBuilder, Material.BLUE_STAINED_GLASS_PANE);

this.populateElements(panelBuilder, this.elements);
this.populateElements(panelBuilder, this.filterElements);

panelBuilder.item(3, this.createButton(Button.ACCEPT_SELECTED));
panelBuilder.item(5, this.createButton(Button.CANCEL));
Expand All @@ -95,6 +99,29 @@ protected void build()
}


/**
* This method is called when filter value is updated.
*/
@Override
protected void updateFilters()
{
if (this.searchString == null || this.searchString.isBlank())
{
this.filterElements = this.elements;
}
else
{
this.filterElements = this.elements.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());
}).
distinct().
collect(Collectors.toList());
}
}


/**
* This method creates PanelItem button of requested type.
* @param button Button which must be created.
Expand Down Expand Up @@ -248,4 +275,9 @@ public enum Mode
* Indicates that entity must be displayed as egg.
*/
private final boolean asEgg;

/**
* Stores filtered items.
*/
private List<EntityType> filterElements;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.panel.ConversationUtils;
import world.bentobox.challenges.utils.Constants;


Expand All @@ -32,6 +34,7 @@ public abstract class PagedSelector<T>
protected PagedSelector(User user)
{
this.user = user;
this.searchString = "";
}


Expand All @@ -50,6 +53,12 @@ protected PagedSelector(User user)
protected abstract PanelItem createElementButton(T object);


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


/**
* Populate elements.
*
Expand Down Expand Up @@ -87,16 +96,26 @@ else if (this.pageIndex > (size / MAX_ELEMENTS))
index++;
}

// Add next page button if there are more than MAX_ELEMENTS objects and pageIndex + 1 is
// larger or equal to the max page count.
if (size > MAX_ELEMENTS && !(1.0 * size / MAX_ELEMENTS <= this.pageIndex + 1))
{
panelBuilder.item(26, this.getButton(CommonButtons.NEXT));

}

// Add previous page button if pageIndex is not 0.
if (this.pageIndex > 0)
{
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 All @@ -120,6 +139,9 @@ protected PanelItem getButton(CommonButtons button)
description.add(this.user.getTranslation(reference + "description",
Constants.PARAMETER_NUMBER, String.valueOf(this.pageIndex + 2)));

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

icon = new ItemStack(Material.OAK_SIGN, this.pageIndex + 2);
clickHandler = (panel, user, clickType, slot) ->
{
Expand All @@ -133,6 +155,9 @@ else if (button == CommonButtons.PREVIOUS)
description.add(this.user.getTranslation(reference + "description",
Constants.PARAMETER_NUMBER, String.valueOf(this.pageIndex)));

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

icon = new ItemStack(Material.OAK_SIGN, Math.max(1, this.pageIndex));
clickHandler = (panel, user, clickType, slot) ->
{
Expand All @@ -141,6 +166,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 @@ -162,7 +240,8 @@ else if (button == CommonButtons.PREVIOUS)
private enum CommonButtons
{
NEXT,
PREVIOUS
PREVIOUS,
SEARCH
}


Expand All @@ -175,4 +254,9 @@ private enum CommonButtons
* User who opens gui.
*/
protected final User user;

/**
* Text that contains filter string.
*/
protected String searchString;
}

0 comments on commit a75c243

Please sign in to comment.