/** * Gets selected item for view model. * * @see #setModelField(String) * @return the value or null if no item is selected */ public Object getModelValue() { if (!forceListItem) { return this.editorField.getText(); } else { Object item = super.getSelectedItem(); if (item == null || item == emptyItem || !(item instanceof YListItem)) { return null; } else { YListItem comboItem = (YListItem) super.getSelectedItem(); if (modelField == null) { return comboItem.getItemModel(); } else { return YCoreToolkit.getBeanValue(comboItem.getItemModel(), modelField); } } } }
/** * Sets model value from view model. Finds matching item from the combo box and sets it selected. * * @param obj the view model value * @see #setModelField(String) */ public void setModelValue(Object obj) throws YException { this.settingModelValue = true; try { if (obj == null) { this.setSelectedIndex(-1); } else { if (!forceListItem) { this.editorField.setText(obj.toString()); } else { // searching corresponding item in list: if (items == null) { throw new YException("Combo model for YComboBox is not set."); } else { for (int i = 0; i < this.items.length; i++) { YListItem item = (YListItem) items[i]; if (!(item == emptyItem)) { if (this.modelField == null) { if (obj.equals(item.getItemModel())) { this.setSelectedItem(item); } } else { Object value = YCoreToolkit.getBeanValue(item.getItemModel(), modelField); if (obj.equals(value)) { this.setSelectedItem(item); } } } } } } } if (forceListItem && getSelectedItem() != null) { // this fixes caret position in the editor... this.editorField.setText(getSelectedItem().toString()); this.editorField.setCaretPosition(0); } } finally { settingModelValue = false; } }