001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.dialog;
017    
018    import java.beans.PropertyChangeEvent;
019    import java.beans.PropertyChangeListener;
020    
021    import javax.swing.JComponent;
022    
023    import org.springframework.richclient.core.DefaultMessage;
024    import org.springframework.richclient.core.Message;
025    import org.springframework.richclient.core.Severity;
026    import org.springframework.richclient.util.EventListenerListHelper;
027    import org.springframework.util.ObjectUtils;
028    
029    /**
030     * A concrete implementation of the <code>Messagable</code> interface. Primarily
031     * intended to be used as a delegate for the messagable functionality of
032     * more complex classes.
033     * 
034     * @author Oliver Hutchison
035     * @see DefaultMessagePane
036     */
037    public class DefaultMessageAreaModel implements Messagable {
038    
039        private Messagable delegate;
040    
041        private Message message = DefaultMessage.EMPTY_MESSAGE;
042    
043        private EventListenerListHelper listenerList = new EventListenerListHelper(PropertyChangeListener.class);
044    
045        public DefaultMessageAreaModel() {
046            this.delegate = this;
047        }
048    
049        public DefaultMessageAreaModel(Messagable delegate) {
050            this.delegate = delegate;
051        }
052    
053        /**
054         * @return Returns the delegateFor.
055         */
056        protected Messagable getDelegateFor() {
057            return delegate;
058        }
059    
060        public Message getMessage() {
061            return message;
062        }
063    
064        public boolean hasInfoMessage() {
065            return message.getSeverity() == Severity.INFO;
066        }
067    
068        public boolean hasErrorMessage() {
069            return message.getSeverity() == Severity.ERROR;
070        }
071    
072        public boolean hasWarningMessage() {
073            return message.getSeverity() == Severity.WARNING;
074        }
075    
076        public void setMessage(Message message) {
077            if (message == null) {
078                message = DefaultMessage.EMPTY_MESSAGE;
079            }
080            if (ObjectUtils.nullSafeEquals(this.message, message)) {
081                return;
082            }
083            Message oldMsg = this.message;
084            this.message = message;
085            fireMessageUpdated(oldMsg, this.message);
086        }
087    
088        public void renderMessage(JComponent component) {
089            message.renderMessage(component);
090        }
091        
092        protected void fireMessageUpdated(Message oldMsg, Message newMsg) {
093            listenerList.fire("propertyChange", new PropertyChangeEvent(delegate, MESSAGE_PROPERTY, oldMsg, newMsg));
094        }
095    
096        public void addPropertyChangeListener(PropertyChangeListener listener) {
097            listenerList.add(listener);
098        }
099    
100        public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
101            if (MESSAGE_PROPERTY.equals(propertyName)) {
102                listenerList.add(listener);
103            }
104        }
105    
106        public void removePropertyChangeListener(PropertyChangeListener listener) {
107            listenerList.remove(listener);
108        }
109    
110        public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
111            if (MESSAGE_PROPERTY.equals(propertyName)) {
112                listenerList.remove(listener);
113            }
114        }
115    }