// The MIT License (MIT)
//
// Copyright (c) 2017 Tim Jones
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package io.github.jonestimd.finance.swing.event;

import io.github.jonestimd.finance.domain.UniqueId;
import io.github.jonestimd.finance.domain.event.DomainEvent;
import io.github.jonestimd.swing.component.BeanListComboBox;
import io.github.jonestimd.swing.component.BeanListModel;

public class ComboBoxDomainEventListener<ID, T extends UniqueId<ID> & Comparable<? super T>> implements DomainEventListener<ID, T> {
    private final BeanListModel<T> comboBoxModel;

    public ComboBoxDomainEventListener(BeanListComboBox<T> comboBox) {
        this(comboBox.getModel());
    }

    public ComboBoxDomainEventListener(BeanListModel<T> comboBoxModel) {
        this.comboBoxModel = comboBoxModel;
    }

    public void onDomainEvent(DomainEvent<ID, T> event) {
        if (event.isDelete() || event.isChange() || event.isReplace()) {
            event.getDomainObjects().forEach(this::removeItem);
        }
        if (event.isAdd() || event.isChange()) {
            event.getDomainObjects().forEach(this::addItem);
        }
    }

    protected void addItem(T item) {
        int index = 0;
        while (index < comboBoxModel.getSize() && item.compareTo(comboBoxModel.getElementAt(index)) > 0) {
            index++;
        }
        comboBoxModel.insertElementAt(item, index);
    }

    protected void removeItem(T item) {
        for (int i = 0; i < comboBoxModel.getSize(); i++) {
            T element = comboBoxModel.getElementAt(i);
            if (isSameId(item, element)) {
                comboBoxModel.removeElementAt(i);
                break;
            }
        }
    }

    private boolean isSameId(T item, T element) {
        return element != null && element.getClass().equals(item.getClass()) && element.getId().equals(item.getId());
    }

    public String toString() {
        return "ComboBoxDomainEventListener[" + comboBoxModel + "]";
    }
}
@@ -2792,23 +2792,40 @@
 urn 
-element != null
+UniqueId.isSameId(item, element)
  && 
@@ -2870,48 +2870,8 @@
 s())
- && element.getId().equals(item.getId())
 ;%0A  

Handle mixed account/category items in transaction type combo box