Skip to content

Commit

Permalink
chatboxtextinput: conditionally close input from ondone callback
Browse files Browse the repository at this point in the history
  • Loading branch information
DSerp authored and abextm committed Apr 27, 2020
1 parent 8eee9d1 commit 2706199
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static class Line
private Runnable onClose = null;

@Getter
private Consumer<String> onDone = null;
private Predicate<String> onDone = null;

@Getter
private Consumer<String> onChanged = null;
Expand Down Expand Up @@ -235,6 +235,20 @@ public ChatboxTextInput onClose(Runnable onClose)
}

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;
Expand Down Expand Up @@ -753,9 +767,9 @@ public void keyPressed(KeyEvent ev)
break;
case KeyEvent.VK_ENTER:
ev.consume();
if (onDone != null)
if (onDone != null && !onDone.test(getValue()))
{
onDone.accept(getValue());
return;
}
chatboxPanelManager.close();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand Down Expand Up @@ -407,7 +408,7 @@ public void onMenuOptionClicked(MenuOptionClicked event)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
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;
Expand Down Expand Up @@ -268,7 +269,7 @@ private void handleDeposit(MenuOptionClicked event, Boolean inventory)

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());
Expand All @@ -290,7 +291,7 @@ private void handleNewTab(ScriptEvent event)
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))
{
Expand Down Expand Up @@ -908,7 +909,7 @@ private void renameTab(String oldTag)
{
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))
{
Expand Down

0 comments on commit 2706199

Please sign in to comment.