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

org.bidib.wizard.mvc.booster.model.BoosterTableModel Maven / Gradle / Ivy

There is a newer version: 2.0.0-M1
Show newest version
package org.bidib.wizard.mvc.booster.model;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.bidib.jbidibc.core.Node;
import org.bidib.jbidibc.core.enumeration.BoosterState;
import org.bidib.wizard.main.ApplicationContext;
import org.bidib.wizard.main.DefaultApplicationContext;
import org.bidib.wizard.mvc.main.model.NodeLabels;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class BoosterTableModel extends Model {

    private static final long serialVersionUID = 1L;

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

    public static final String PROPERTY_BOOSTERS = "boosters";

    private ArrayListModel boosterList = new ArrayListModel<>();

    private ApplicationContext applicationContext;

    private NodeLabels nodeLabels;

    public BoosterTableModel() {
        applicationContext = DefaultApplicationContext.getInstance();

        nodeLabels = applicationContext.get(DefaultApplicationContext.KEY_NODE_LABELS, NodeLabels.class);
        nodeLabels.addPropertyChangeListener(NodeLabels.PROPERTY_LABELS, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                LOGGER.debug("NodeLabels have been changed.");

                List boosters = new LinkedList<>(getBoosters());
                for (BoosterModel booster : boosters) {
                    String nodeLabel = booster.prepareNodeLabel();
                    booster.setNodeLabel(nodeLabel);
                }

                if (boosterList.size() > 0) {
                    boosterList.fireContentsChanged(0, boosterList.size() - 1);
                }
            }
        });
    }

    public void addBooster(Node node) {
        synchronized (boosterList) {
            BoosterModel booster = new BoosterModel(node, nodeLabels);
            if (!boosterList.contains(booster)) {
                LOGGER.info("Add booster to booster list: {}", node);
                booster.registerNode();

                if (nodeLabels != null) {
                    String nodeLabel = booster.prepareNodeLabel();
                    booster.setNodeLabel(nodeLabel);
                }

                List oldValue = new LinkedList<>(boosterList);
                boosterList.add(booster);

                firePropertyChange(PROPERTY_BOOSTERS, oldValue, boosterList);
            }
            else {
                LOGGER.warn("Node is already in booster list: {}", node);
            }
        }
    }

    public void removeBooster(Node node) {
        synchronized (boosterList) {
            LOGGER.info("Remove booster from booster list: {}", node);

            List oldValue = new LinkedList<>(boosterList);
            int index = boosterList.indexOf(new BoosterModel(node, null));
            if (index > -1) {
                BoosterModel removed = boosterList.remove(index);
                if (removed != null) {
                    removed.freeNode();
                }

                firePropertyChange(PROPERTY_BOOSTERS, oldValue, boosterList);
            }
        }
    }

    public ArrayListModel getBoosterListModel() {
        return boosterList;
    }

    public List getBoosters() {
        return Collections.unmodifiableList(boosterList);
    }

    public void setBoosterState(byte[] address, BoosterState state) {
        synchronized (boosterList) {
            for (BoosterModel booster : boosterList) {
                if (Arrays.equals(booster.getNodeAddress(), address)) {
                    LOGGER.trace("Found booster to update: {}", booster);
                    booster.setState(state);

                    int index = boosterList.indexOf(booster);
                    boosterList.fireContentsChanged(index);
                    break;
                }
            }
        }
    }

    public void setBoosterCurrent(byte[] address, int current) {
        synchronized (boosterList) {
            for (BoosterModel booster : boosterList) {
                if (Arrays.equals(booster.getNodeAddress(), address)) {
                    LOGGER.trace("Found booster to update: {}", booster);
                    booster.setCurrent(current);

                    int index = boosterList.indexOf(booster);
                    boosterList.fireContentsChanged(index);
                    break;
                }
            }
        }
    }

    public void setBoosterMaxCurrent(byte[] address, int maxCurrent) {
        synchronized (boosterList) {
            for (BoosterModel booster : boosterList) {
                if (Arrays.equals(booster.getNodeAddress(), address)) {
                    LOGGER.trace("Found booster to update: {}", booster);
                    booster.setMaxCurrent(maxCurrent);

                    int index = boosterList.indexOf(booster);
                    boosterList.fireContentsChanged(index);
                    break;
                }
            }
        }
    }

    public void setBoosterVoltage(byte[] address, int voltage) {
        synchronized (boosterList) {
            for (BoosterModel booster : boosterList) {
                if (Arrays.equals(booster.getNodeAddress(), address)) {
                    LOGGER.trace("Found booster to update: {}", booster);
                    booster.setVoltage(voltage);

                    int index = boosterList.indexOf(booster);
                    boosterList.fireContentsChanged(index);
                    break;
                }
            }
        }
    }

    public void setBoosterTemperature(byte[] address, int temperature) {
        synchronized (boosterList) {
            for (BoosterModel booster : boosterList) {
                if (Arrays.equals(booster.getNodeAddress(), address)) {
                    LOGGER.trace("Found booster to update: {}", booster);
                    booster.setTemperature(temperature);

                    int index = boosterList.indexOf(booster);
                    boosterList.fireContentsChanged(index);
                    break;
                }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy