ComboBox adds same value multiple times

For some reason, if I enter a non-existent value into my combo, it creates the entity multiple times. I press enter, it gets added, I click the arrow, it gets added again. The code is getting called from Select.changeVariables. Not quite sure if this is some sort of a bug, or if I’m doing something wrong…


triggerSelector = new ComboBox("", getService().getTriggerContainer()) {
  
  {
    setItemCaptionPropertyId("name");
    setNewItemsAllowed(true);
    setImmediate(true);
    setNewItemHandler(
      new NewItemHandler() {          
        @Override
        public void addNewItem(String newItemCaption) {
          Object itemId = getContainerDataSource().addEntity(new Trigger(newItemCaption, "")); 
          fireItemSetChange();
          triggerSelector.setValue(itemId);                  
        }
      }
    );
  }
  
  @Override
  public TriggerContainer getContainerDataSource() {
    return (TriggerContainer) super.getContainerDataSource();
  }
  
};      

com.vaadin.ui.Select


String newFilter;
if ((newFilter = (String) variables.get("filter")) != null) {
  // this is a filter request
  currentPage = ((Integer) variables.get("page")).intValue();
  filterstring = newFilter;
  if (filterstring != null) {
    filterstring = filterstring.toLowerCase();
  }
  optionRepaint();
} else if (isNewItemsAllowed()) {
  // New option entered (and it is allowed)
  final String newitem = (String) variables.get("newitem");
  if (newitem != null && newitem.length() > 0) {
    getNewItemHandler().addNewItem(newitem); <- Handler call
    // rebuild list
    filterstring = null;
    prevfilterstring = null;
  }
}

Only thing I could think of was to add a function to TriggerContainer to accept the caption and return a true/false value indicating whether or not it was found.


new NewItemHandler() {          
  @Override
  public void addNewItem(String newItemCaption) {
    if (!getContainerDataSource().hasTrigger(newItemCaption)) { <- Add this function
      log.debug("Adding Trigger: " + newItemCaption);
      Object triggerId = getContainerDataSource().addEntity(new Trigger(newItemCaption, ""));
      setValue(triggerId);
    }
  }
}