X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTComboBox.java;h=b64dbde058ad006d4a5d80fd83e9ddd4303e8ba2;hb=bff0df27562e1cca8e6be47e7ace9bd5bb1adbfa;hp=38224b89e8c3037ac94855324dff2dbc90b6ef0f;hpb=051e29138b18fb4b731a72f8727475b10e4c74e4;p=fanfix.git diff --git a/src/jexer/TComboBox.java b/src/jexer/TComboBox.java index 38224b8..b64dbde 100644 --- a/src/jexer/TComboBox.java +++ b/src/jexer/TComboBox.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2017 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -61,6 +61,16 @@ public class TComboBox extends TWidget { */ private TAction updateAction = null; + /** + * If true, the field cannot be updated to a value not on the list. + */ + private boolean limitToListValue = true; + + /** + * The maximum height of the values drop-down when it is visible. + */ + private int maxValuesHeight = 3; + // ------------------------------------------------------------------------ // Constructors ----------------------------------------------------------- // ------------------------------------------------------------------------ @@ -75,45 +85,57 @@ public class TComboBox extends TWidget { * @param values the possible values for the box, shown in the drop-down * @param valuesIndex the initial index in values, or -1 for no default * value - * @param valuesHeight the height of the values drop-down when it is - * visible + * @param maxValuesHeight the maximum height of the values drop-down when + * it is visible * @param updateAction action to call when a new value is selected from * the list or enter is pressed in the edit field */ public TComboBox(final TWidget parent, final int x, final int y, final int width, final List values, final int valuesIndex, - final int valuesHeight, final TAction updateAction) { + final int maxValuesHeight, final TAction updateAction) { // Set parent and window super(parent, x, y, width, 1); + assert (values != null); + this.updateAction = updateAction; + this.maxValuesHeight = maxValuesHeight; - field = new TField(this, 0, 0, width - 1, false, "", - updateAction, null); - if (valuesIndex >= 0) { + field = addField(0, 0, width - 3, false, "", updateAction, null); + if ((valuesIndex >= 0) && (valuesIndex < values.size())) { field.setText(values.get(valuesIndex)); } - list = new TList(this, values, 0, 1, width, valuesHeight, + list = addList(values, 0, 1, width, + Math.max(3, Math.min(values.size() + 1, maxValuesHeight)), new TAction() { public void DO() { field.setText(list.getSelected()); list.setEnabled(false); list.setVisible(false); - TComboBox.this.setHeight(1); - TComboBox.this.activate(field); + TComboBox.super.setHeight(1); + if (TComboBox.this.limitToListValue == false) { + TComboBox.this.activate(field); + } if (updateAction != null) { - updateAction.DO(); + updateAction.DO(TComboBox.this); } } } ); + if (valuesIndex >= 0) { + list.setSelectedIndex(valuesIndex); + } list.setEnabled(false); list.setVisible(false); - setHeight(1); - activate(field); + super.setHeight(1); + if (limitToListValue) { + field.setEnabled(false); + } else { + activate(field); + } } // ------------------------------------------------------------------------ @@ -128,7 +150,8 @@ public class TComboBox extends TWidget { */ private boolean mouseOnArrow(final TMouseEvent mouse) { if ((mouse.getY() == 0) - && (mouse.getX() == getWidth() - 1) + && (mouse.getX() >= getWidth() - 3) + && (mouse.getX() <= getWidth() - 1) ) { return true; } @@ -145,17 +168,14 @@ public class TComboBox extends TWidget { if ((mouseOnArrow(mouse)) && (mouse.isMouse1())) { // Make the list visible or not. if (list.isActive()) { - list.setEnabled(false); - list.setVisible(false); - setHeight(1); - activate(field); + hideList(); } else { - list.setEnabled(true); - list.setVisible(true); - setHeight(list.getHeight() + 1); - activate(list); + showList(); } } + + // Pass to parent for the things we don't care about. + super.onMouseDown(mouse); } /** @@ -165,11 +185,15 @@ public class TComboBox extends TWidget { */ @Override public void onKeypress(final TKeypressEvent keypress) { + if (keypress.equals(kbEsc)) { + if (list.isActive()) { + hideList(); + return; + } + } + if (keypress.equals(kbAltDown)) { - list.setEnabled(true); - list.setVisible(true); - setHeight(list.getHeight() + 1); - activate(list); + showList(); return; } @@ -178,10 +202,7 @@ public class TComboBox extends TWidget { || (keypress.equals(kbBackTab)) ) { if (list.isActive()) { - list.setEnabled(false); - list.setVisible(false); - setHeight(1); - activate(field); + hideList(); return; } } @@ -194,6 +215,33 @@ public class TComboBox extends TWidget { // TWidget ---------------------------------------------------------------- // ------------------------------------------------------------------------ + /** + * Override TWidget's width: we need to set child widget widths. + * + * @param width new widget width + */ + @Override + public void setWidth(final int width) { + if (field != null) { + field.setWidth(width - 3); + } + if (list != null) { + list.setWidth(width); + } + super.setWidth(width); + } + + /** + * Override TWidget's height: we can only set height at construction + * time. + * + * @param height new widget height (ignored) + */ + @Override + public void setHeight(final int height) { + // Do nothing + } + /** * Draw the combobox down arrow. */ @@ -201,13 +249,24 @@ public class TComboBox extends TWidget { public void draw() { CellAttributes comboBoxColor; + if (!isAbsoluteActive()) { + // We lost focus, turn off the list. + if (list.isActive()) { + hideList(); + } + } + if (isAbsoluteActive()) { comboBoxColor = getTheme().getColor("tcombobox.active"); } else { comboBoxColor = getTheme().getColor("tcombobox.inactive"); } - getScreen().putCharXY(getWidth() - 1, 0, GraphicsChars.DOWNARROW, + putCharXY(getWidth() - 3, 0, GraphicsChars.DOWNARROWLEFT, + comboBoxColor); + putCharXY(getWidth() - 2, 0, GraphicsChars.DOWNARROW, + comboBoxColor); + putCharXY(getWidth() - 1, 0, GraphicsChars.DOWNARROWRIGHT, comboBoxColor); } @@ -215,6 +274,28 @@ public class TComboBox extends TWidget { // TComboBox -------------------------------------------------------------- // ------------------------------------------------------------------------ + /** + * Hide the drop-down list. + */ + public void hideList() { + list.setEnabled(false); + list.setVisible(false); + super.setHeight(1); + if (limitToListValue == false) { + activate(field); + } + } + + /** + * Show the drop-down list. + */ + public void showList() { + list.setEnabled(true); + list.setVisible(true); + super.setHeight(list.getHeight() + 1); + activate(list); + } + /** * Get combobox text value. * @@ -230,14 +311,63 @@ public class TComboBox extends TWidget { * @param text the new text in the edit field */ public void setText(final String text) { + setText(text, true); + } + + /** + * Set combobox text value. + * + * @param text the new text in the edit field + * @param caseSensitive if true, perform a case-sensitive search for the + * list item + */ + public void setText(final String text, final boolean caseSensitive) { field.setText(text); for (int i = 0; i < list.getMaxSelectedIndex(); i++) { - if (list.getSelected().equals(text)) { - list.setSelectedIndex(i); - return; + if (caseSensitive == true) { + if (list.getListItem(i).equals(text)) { + list.setSelectedIndex(i); + return; + } + } else { + if (list.getListItem(i).toLowerCase().equals(text.toLowerCase())) { + list.setSelectedIndex(i); + return; + } } } list.setSelectedIndex(-1); } + /** + * Set combobox text to one of the list values. + * + * @param index the index in the list + */ + public void setIndex(final int index) { + list.setSelectedIndex(index); + field.setText(list.getSelected()); + } + + /** + * Get a copy of the list of strings to display. + * + * @return the list of strings + */ + public final List getList() { + return list.getList(); + } + + /** + * Set the new list of strings to display. + * + * @param list new list of strings + */ + public final void setList(final List list) { + this.list.setList(list); + this.list.setHeight(Math.max(3, Math.min(list.size() + 1, + maxValuesHeight))); + field.setText(""); + } + }