chatboxtextinput: conditionally close input from ondone callback

This commit is contained in:
Daniel
2020-04-04 15:29:01 -04:00
committed by Max Weber
parent 8eee9d12f6
commit 2706199944
3 changed files with 23 additions and 7 deletions

View File

@@ -106,7 +106,7 @@ public class ChatboxTextInput extends ChatboxInput implements KeyListener, Mouse
private Runnable onClose = null;
@Getter
private Consumer<String> onDone = null;
private Predicate<String> onDone = null;
@Getter
private Consumer<String> onChanged = null;
@@ -235,6 +235,20 @@ public class ChatboxTextInput extends ChatboxInput implements KeyListener, Mouse
}
public ChatboxTextInput onDone(Consumer<String> onDone)
{
this.onDone = (s) ->
{
onDone.accept(s);
return true;
};
return this;
}
/**
* Called when the user attempts to close the input by pressing enter
* Return false to cancel the close
*/
public ChatboxTextInput onDone(Predicate<String> onDone)
{
this.onDone = onDone;
return this;
@@ -753,9 +767,9 @@ public class ChatboxTextInput extends ChatboxInput implements KeyListener, Mouse
break;
case KeyEvent.VK_ENTER:
ev.consume();
if (onDone != null)
if (onDone != null && !onDone.test(getValue()))
{
onDone.accept(getValue());
return;
}
chatboxPanelManager.close();
return;

View File

@@ -37,6 +37,7 @@ import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.inject.Inject;
@@ -407,7 +408,7 @@ public class BankTagsPlugin extends Plugin implements MouseWheelListener, KeyLis
chatboxPanelManager.openTextInput(name + " tags:<br>(append " + VAR_TAG_SUFFIX + " for variation tag)")
.addCharValidator(FILTERED_CHARS)
.value(initialValue)
.onDone((newValue) ->
.onDone((Consumer<String>) (newValue) ->
clientThread.invoke(() ->
{
// Split inputted tags to vartags (ending with *) and regular tags

View File

@@ -45,6 +45,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.IntPredicate;
import java.util.stream.Collectors;
import javax.inject.Inject;
@@ -268,7 +269,7 @@ public class TabInterface
chatboxPanelManager.openTextInput((inventory ? "Inventory " : "Equipment ") + " tags:")
.addCharValidator(FILTERED_CHARS)
.onDone((newTags) ->
.onDone((Consumer<String>) (newTags) ->
clientThread.invoke(() ->
{
final List<String> tags = Text.fromCSV(newTags.toLowerCase());
@@ -290,7 +291,7 @@ public class TabInterface
case NewTab.NEW_TAB:
chatboxPanelManager.openTextInput("Tag name")
.addCharValidator(FILTERED_CHARS)
.onDone((tagName) -> clientThread.invoke(() ->
.onDone((Consumer<String>) (tagName) -> clientThread.invoke(() ->
{
if (!Strings.isNullOrEmpty(tagName))
{
@@ -908,7 +909,7 @@ public class TabInterface
{
chatboxPanelManager.openTextInput("Enter new tag name for tag \"" + oldTag + "\":")
.addCharValidator(FILTERED_CHARS)
.onDone((newTag) -> clientThread.invoke(() ->
.onDone((Consumer<String>) (newTag) -> clientThread.invoke(() ->
{
if (!Strings.isNullOrEmpty(newTag) && !newTag.equalsIgnoreCase(oldTag))
{