Skip to content

Commit

Permalink
Issue mabe02#325: Adding a setSelectedItem(..) method to ComboBox
Browse files Browse the repository at this point in the history
  • Loading branch information
mabe02 committed Aug 27, 2017
1 parent 8c3f37c commit a07f861
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
41 changes: 34 additions & 7 deletions src/main/java/com/googlecode/lanterna/gui2/ComboBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ public void setDropDownNumberOfRows(int dropDownNumberOfRows) {

/**
* Programmatically selects one item in the combo box, which causes the displayed text to change to match the label
* of the selected index
* @param selectedIndex Index of the item to select
* of the selected index.
* @param selectedIndex Index of the item to select, or -1 if the selection should be cleared
* @throws IndexOutOfBoundsException if the index is out of range
*/
public synchronized void setSelectedIndex(final int selectedIndex) {
Expand All @@ -327,13 +327,10 @@ public synchronized void setSelectedIndex(final int selectedIndex) {
final int oldSelection = this.selectedIndex;
this.selectedIndex = selectedIndex;
if(selectedIndex == -1) {
text = "";
updateText("");
}
else {
text = items.get(selectedIndex).toString();
}
if(textInputPosition > text.length()) {
textInputPosition = text.length();
updateText(items.get(selectedIndex).toString());
}
runOnGUIThreadIfExistsOtherwiseRunDirect(new Runnable() {
@Override
Expand All @@ -346,6 +343,36 @@ public void run() {
invalidate();
}

/**
* Programmatically selects one item in the combo box by passing in the value the should be selected. If the value
* isn't in the combo box model, nothing happens for read-only combo boxes and for editable ones the text content
* is changed to match the result from calling the {@code toString()} method of {@code item}.
* <p>
* If called with {@code null}, the selection is cleared.
* @param item Item in the combo box to select, or null if the selection should be cleared
*/
public synchronized void setSelectedItem(final V item) {
if(item == null) {
setSelectedIndex(-1);
}
else {
int indexOf = items.indexOf(item);
if (indexOf != -1) {
setSelectedIndex(indexOf);
}
else if (!readOnly) {
updateText(item.toString());
}
}
}

private void updateText(String newText) {
text = newText;
if(textInputPosition > text.length()) {
textInputPosition = text.length();
}
}

/**
* Returns the index of the currently selected item
* @return Index of the currently selected item
Expand Down
41 changes: 38 additions & 3 deletions src/test/java/com/googlecode/lanterna/gui2/ComboBoxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@
package com.googlecode.lanterna.gui2;

import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton;

import java.io.IOException;
import java.util.Arrays;
import java.util.TimeZone;
import java.util.regex.Pattern;

public class ComboBoxTest extends TestBase {
public static void main(String[] args) throws IOException, InterruptedException {
new ComboBoxTest().run(args);
}

@Override
public void init(WindowBasedTextGUI textGUI) {
public void init(final WindowBasedTextGUI textGUI) {
final BasicWindow window = new BasicWindow("ComboBoxTest");
Panel mainPanel = new Panel();

Expand Down Expand Up @@ -58,7 +61,7 @@ public void init(WindowBasedTextGUI textGUI) {
comboBoxCJK.withBorder(Borders.singleLine("CJK"))));
mainPanel.addComponent(new EmptySpace(TerminalSize.ONE));

final TextBox textBoxNewItem = new TextBox();
final TextBox textBoxNewItem = new TextBox(new TerminalSize(20, 1));
Button buttonAddItem = new Button("Add", new Runnable() {
@Override
public void run() {
Expand All @@ -68,7 +71,39 @@ public void run() {
window.setFocusedInteractable(textBoxNewItem);
}
});
mainPanel.addComponent(Panels.horizontal(textBoxNewItem, buttonAddItem));
final TextBox textBoxSetSelectedIndex = new TextBox(new TerminalSize(20, 1), "0");
textBoxSetSelectedIndex.setValidationPattern(Pattern.compile("-?[0-9]+"));
Button buttonSetSelectedIndex = new Button("Set Selected Index", new Runnable() {
@Override
public void run() {
try {
comboBoxEditable.setSelectedIndex(Integer.parseInt(textBoxSetSelectedIndex.getText()));
comboBoxReadOnly.setSelectedIndex(Integer.parseInt(textBoxSetSelectedIndex.getText()));
}
catch (Exception e) {
MessageDialog.showMessageDialog(textGUI, e.getClass().getName(), e.getMessage(), MessageDialogButton.OK);
}
}
});
final TextBox textBoxSetSelectedItem = new TextBox(new TerminalSize(20, 1));
Button buttonSetSelectedItem = new Button("Set Selected Item", new Runnable() {
@Override
public void run() {
try {
comboBoxEditable.setSelectedItem(textBoxSetSelectedItem.getText());
comboBoxReadOnly.setSelectedItem(textBoxSetSelectedItem.getText());
}
catch (Exception e) {
MessageDialog.showMessageDialog(textGUI, e.getClass().getName(), e.getMessage(), MessageDialogButton.OK);
}
}
});
mainPanel.addComponent(
Panels.vertical(
Panels.horizontal(textBoxNewItem, buttonAddItem),
Panels.horizontal(textBoxSetSelectedIndex, buttonSetSelectedIndex),
Panels.horizontal(textBoxSetSelectedItem, buttonSetSelectedItem))
.withBorder(Borders.singleLineBevel("Modify Content")));

mainPanel.addComponent(new EmptySpace(TerminalSize.ONE));
mainPanel.addComponent(comboBoxTimeZones.withBorder(Borders.singleLine("Large ComboBox")));
Expand Down

0 comments on commit a07f861

Please sign in to comment.