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

org.valkyriercp.component.DefaultMessageAreaModel Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package org.valkyriercp.component;

import org.springframework.util.ObjectUtils;
import org.valkyriercp.core.DefaultMessage;
import org.valkyriercp.core.Messagable;
import org.valkyriercp.core.Message;
import org.valkyriercp.core.Severity;
import org.valkyriercp.util.EventListenerListHelper;

import javax.swing.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * A concrete implementation of the Messagable interface. Primarily
 * intended to be used as a delegate for the messagable functionality of
 * more complex classes.
 *
 * @author Oliver Hutchison
 * @see DefaultMessageAreaPane
 */
public class DefaultMessageAreaModel implements Messagable {

    private Messagable delegate;

    private Message message = DefaultMessage.EMPTY_MESSAGE;

    private EventListenerListHelper listenerList = new EventListenerListHelper(PropertyChangeListener.class);

    public DefaultMessageAreaModel() {
        this.delegate = this;
    }

    public DefaultMessageAreaModel(Messagable delegate) {
        this.delegate = delegate;
    }

    /**
     * @return Returns the delegateFor.
     */
    protected Messagable getDelegateFor() {
        return delegate;
    }

    public Message getMessage() {
        return message;
    }

    public boolean hasInfoMessage() {
        return message.getSeverity() == Severity.INFO;
    }

    public boolean hasErrorMessage() {
        return message.getSeverity() == Severity.ERROR;
    }

    public boolean hasWarningMessage() {
        return message.getSeverity() == Severity.WARNING;
    }

    public void setMessage(Message message) {
        if (message == null) {
            message = DefaultMessage.EMPTY_MESSAGE;
        }
        if (ObjectUtils.nullSafeEquals(this.message, message)) {
            return;
        }
        Message oldMsg = this.message;
        this.message = message;
        fireMessageUpdated(oldMsg, this.message);
    }

    public void renderMessage(JComponent component) {
        message.renderMessage(component);
    }

    protected void fireMessageUpdated(Message oldMsg, Message newMsg) {
        listenerList.fire("propertyChange", new PropertyChangeEvent(delegate, MESSAGE_PROPERTY, oldMsg, newMsg));
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        listenerList.add(listener);
    }

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        if (MESSAGE_PROPERTY.equals(propertyName)) {
            listenerList.add(listener);
        }
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        listenerList.remove(listener);
    }

    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        if (MESSAGE_PROPERTY.equals(propertyName)) {
            listenerList.remove(listener);
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy