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

org.bidib.wizard.integration.client.statusbar.IntegrationStatusBarItem Maven / Gradle / Ivy

package org.bidib.wizard.integration.client.statusbar;

import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import org.bidib.wizard.api.locale.Resources;
import org.bidib.wizard.client.common.nodetree.JideNodeTree;
import org.bidib.wizard.common.utils.ImageUtils;
import org.bidib.wizard.integration.core.z21lan.StatusModel;
import org.bidib.wizard.integration.core.z21lan.StatusModel.ReceiverStatus;
import org.bidib.wizard.integration.core.z21lan.Z21ReceiverStatusModel;

import com.jidesoft.status.StatusBarItem;

public class IntegrationStatusBarItem extends StatusBarItem {

    private static final long serialVersionUID = 1L;

    private JLabel receiverLabel;

    private Icon greenBallIcon;

    private Icon blueBallIcon;

    private Icon grayBallIcon;

    private final Z21ReceiverStatusModel statusModel;

    public IntegrationStatusBarItem(final Z21ReceiverStatusModel statusModel) {
        this.statusModel = statusModel;
    }

    public void initialize() {
        this.greenBallIcon = ImageUtils.createImageIcon(JideNodeTree.class, "/icons/green-ball.png", 16, 16);
        this.blueBallIcon = ImageUtils.createImageIcon(JideNodeTree.class, "/icons/blue-ball.png", 16, 16);
        this.grayBallIcon = ImageUtils.createImageIcon(JideNodeTree.class, "/icons/gray-ball.png", 16, 16);

        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        add(Box.createRigidArea(new Dimension(5, 0)));
        this.receiverLabel = new JLabel("Z21Receiver");
        add(receiverLabel);
        add(Box.createRigidArea(new Dimension(5, 0)));

        this.statusModel.addPropertyChangeListener(StatusModel.PROPERTY_RECEIVER_STATUS, evt -> {

            if (SwingUtilities.isEventDispatchThread()) {
                updateStatus();
            }
            else {
                SwingUtilities.invokeLater(() -> {
                    updateStatus();
                });
            }
        });
        this.statusModel.addPropertyChangeListener(StatusModel.PROPERTY_CONNECTED_CLIENTS_COUNT, evt -> {

            if (SwingUtilities.isEventDispatchThread()) {
                updateStatus();
            }
            else {
                SwingUtilities.invokeLater(() -> {
                    updateStatus();
                });
            }
        });

        updateStatus();
    }

    private void updateStatus() {
        switch (this.statusModel.getReceiverStatus()) {
            case disconnected:
                this.receiverLabel.setIcon(blueBallIcon);
                break;
            case connected:
                this.receiverLabel.setIcon(greenBallIcon);
                break;
            default:
                this.receiverLabel.setIcon(grayBallIcon);
                break;
        }

        int connectedClientsCount = this.statusModel.getConnectedClientsCount();
        this.receiverLabel
            .setToolTipText(this.statusModel.getReceiverStatus() == ReceiverStatus.disabled
                ? Resources.getString(IntegrationStatusBarItem.class, "z21ReceiverDisabled.tooltip") : Resources
                    .getString(IntegrationStatusBarItem.class, "z21ReceiverEnabled.tooltip", connectedClientsCount));
    }

    @Override
    public String getItemName() {
        return IntegrationStatusBarItem.class.getSimpleName();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy