+ /**
+ * 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<String> getList() {
+ return list.getList();
+ }
+
+ /**
+ * Set the new list of strings to display.
+ *
+ * @param list new list of strings
+ */
+ public final void setList(final List<String> list) {
+ this.list.setList(list);
+ this.list.setHeight(Math.max(3, Math.min(list.size() + 1,
+ maxValuesHeight)));
+ field.setText("");
+ }
+
+ /**
+ * Make sure the widget displays all its elements correctly according to
+ * the current size and content.
+ */
+ public void reflowData() {
+ // TODO: why setW/setH/reflow not enough for the scrollbars?
+ TList list = this.list;
+ if (list != null) {
+ int valuesHeight = this.valuesHeight;
+ if (valuesHeight < 0) {
+ valuesHeight = values == null ? 0 : values.size() + 1;
+ }
+
+ list.onResize(new TResizeEvent(Type.WIDGET, getWidth(),
+ valuesHeight));
+ setHeight(valuesHeight + 1);
+ }
+
+ field.onResize(new TResizeEvent(Type.WIDGET, getWidth(),
+ field.getHeight()));
+ }
+
+ @Override
+ public void onResize(TResizeEvent resize) {
+ super.onResize(resize);
+ reflowData();
+ }
+
+ /**
+ * Display the drop-down menu represented by {@link TComboBox#list}.
+ */
+ private void displayDropdown() {
+ if (this.list != null) {
+ hideDropdown();
+ }
+
+ int valuesHeight = this.valuesHeight;
+ if (valuesHeight < 0) {
+ valuesHeight = values == null ? 0 : values.size() + 1;
+ }
+
+ TList list = new TList(this, values, 0, 1, getWidth(), valuesHeight,
+ new TAction() {
+ @Override
+ public void DO() {
+ TList list = TComboBox.this.list;
+ if (list == null) {
+ return;
+ }
+
+ field.setText(list.getSelected());
+ hideDropdown();
+
+ if (updateAction != null) {
+ updateAction.DO();
+ }
+ }
+ }
+ );
+
+ int i = -1;
+ if (values != null) {
+ String current = field.getText();
+ for (i = 0 ; i < values.size() ; i++) {
+ String value = values.get(i);
+ if ((caseSensitive && current.equals(value))
+ || (!caseSensitive && current.equalsIgnoreCase(value))) {
+ break;
+ }
+ }
+
+ if (i >= values.size()) {
+ i = -1;
+ }
+ }
+ list.setSelectedIndex(i);
+
+ list.setEnabled(true);
+ list.setVisible(true);
+
+ this.list = list;
+
+ reflowData();
+ activate(list);
+ }
+
+ /**
+ * Hide the drop-down menu represented by {@link TComboBox#list}.
+ */
+ private void hideDropdown() {
+ TList list = this.list;
+
+ if (list != null) {
+ list.setEnabled(false);
+ list.setVisible(false);
+ removeChild(list);
+
+ setHeight(1);
+ if (limitToListValue == false) {
+ activate(field);
+ }
+
+ this.list = null;
+ }
+ }