All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.localhost.client.model.SubscribedClientTableModel Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.localhost.client.model;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import javax.swing.SwingUtilities;

import org.apache.commons.collections4.IterableUtils;
import org.bidib.jbidibc.messages.utils.NodeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jgoodies.binding.beans.Model;
import com.jgoodies.common.collect.ArrayListModel;

public class SubscribedClientTableModel extends Model {

    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = LoggerFactory.getLogger(SubscribedClientTableModel.class);

    public static final String PROPERTY_SUBSCRIBED_CLIENTS = "subscribedClients";

    private final ArrayListModel subscriberList = new ArrayListModel<>();

    public SubscribedClientTableModel() {

    }

    public void addSubscribedClient(final byte[] nodeAddress, Long uniqueId, Integer messageClasses) {

        SwingUtilities.invokeLater(() -> {

            synchronized (subscriberList) {
                final SubscribedClientModel subscriber = new SubscribedClientModel(nodeAddress, uniqueId);
                SubscribedClientModel existingSubscriber =
                    IterableUtils.find(subscriberList, sub -> subscriber.equals(sub));
                // if (!subscriberList.contains(subscriber)) {
                if (existingSubscriber == null) {
                    LOGGER.info("Add node address to subscriber list: {}", NodeUtils.formatAddressLong(nodeAddress));

                    subscriber.setMessageClasses(messageClasses);

                    List oldValue = new LinkedList<>(this.subscriberList);
                    this.subscriberList.add(subscriber);

                    firePropertyChange(PROPERTY_SUBSCRIBED_CLIENTS, oldValue, this.subscriberList);
                }
                else {
                    LOGGER
                        .warn("Node address is already in subscriber list: {}",
                            NodeUtils.formatAddressLong(nodeAddress));

                    // TODO Update the message class?
                    existingSubscriber.setMessageClasses(messageClasses);

                    int index = this.subscriberList.indexOf(existingSubscriber);

                    this.subscriberList.fireContentsChanged(index);

                    // List oldValue = new LinkedList<>(this.subscriberList);
                    // this.subscriberList.add(subscriber);

                    // fireMultiplePropertiesChanged();

                    firePropertyChange(PROPERTY_SUBSCRIBED_CLIENTS, Collections.emptyList(), this.subscriberList);
                }
            }
        });
    }

    public void removeSubscribedClient(final byte[] nodeAddress, Long uniqueId, Integer messageClasses) {

        SwingUtilities.invokeLater(() -> {
            synchronized (subscriberList) {

                LOGGER.info("Remove subscribed client, nodeAddress: {}", NodeUtils.formatAddressLong(nodeAddress));

                List oldValue = new LinkedList<>(subscriberList);
                int index = subscriberList.indexOf(new SubscribedClientModel(nodeAddress, null));
                if (index > -1) {
                    SubscribedClientModel removed = subscriberList.remove(index);
                    if (removed != null) {
                        firePropertyChange(PROPERTY_SUBSCRIBED_CLIENTS, oldValue, subscriberList);
                    }
                }
                else {
                    LOGGER.info("The subscriber is not in the list.");
                }
            }
        });
    }

    public ArrayListModel getSubscriberListModel() {
        return this.subscriberList;
    }

    public List getSubscribers() {
        return Collections.unmodifiableList(this.subscriberList);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy