/*
* Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of its contributors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package kameleon.gui.model;
import java.util.ArrayList;
import java.util.List;
import kameleon.exception.InvalidIndexException;
import kameleon.exception.KameleonException;
/**
* Model for the graphical interface handling the message system.
*
* @author Schnell Michaël
* @version 1.0
*/
public class MessageModel extends Observable {
/**
* List of displayed messages.
*/
private List<Message> messageList ;
/**
* Flag used to indicate if a message has been updated.
* (a new message counts as a message update)
*/
private boolean messageUpdate ;
/**
* List of the updated messages.
*/
private List<Message> updatedMessages ;
/**
* Builds an instance with the given options.
*
* @param debugMode
* flag indicating if the debug mode should be activated
* ({@code true} means activated)
*/
public MessageModel(boolean debugMode) {
super(debugMode) ;
this.messageList = new ArrayList<Message>() ;
this.updatedMessages = new ArrayList<Message>() ;
this.messageUpdate = false ;
}// MessageModel(boolean)
/**
* Builds an instance with default options.
*/
public MessageModel() {
this(DEFAULT_DEBUG_MODE) ;
}// MessageModel() ;
/**
* Returns the number of currently displayed messages.
*
* @return Number of currently displayed messages
*/
public int getNMessages() {
return this.messageList.size() ;
}// getNMessages()
/**
* Returns the instance of {@code Message} at the given index in the
* list of messages.
*
* @param index
* index of the requested message
*
* @return Instance of {@code Message} at the given index
*
* @throws InvalidIndexException
* if {@code ((index < 0) || (index >= this.getNMessages())}
*/
public Message getMessage(int index) throws InvalidIndexException {
if ((index < 0) || (index >= this.messageList.size())) {
throw new InvalidIndexException(index) ;
}// if
return this.messageList.get(index) ;
}// getMessage(int)
/**
* Indicates if a message has just been updated (or added).
*
* @return {@code true} if a message has just been updated,
* {@code false} otherwise
*/
public boolean messagesUpdated() {
return this.messageUpdate ;
}// messagesUpdated()
/**
* Returns the list of the updated messages.
*
* @return instance of {@code List<Message>} containing
* the updated messages
*/
public List<Message> getUpdatedMessages() {
return this.updatedMessages;
}// getUpdatedMessages()
/**
* Adds a new message at the beginning of the list.
*
* @param message
* added message
*/
public void addMessage(Message message) {
// Update the message indexes
for(Message msg : this.messageList) {
msg.setIndex(msg.getIndex()+1) ;
}// for
// Add the message
if (this.messageList.isEmpty()) {
this.messageList.add(message) ;
} else {
this.messageList.add(0, message) ;
}// if
message.setIndex(0) ;
// Notify observers
this.notifyMessageUpdate(message) ;
}// addMessage(Message)
/**
* Replaces the message with the same index in the list
* with the given message.
*
* @param newMessage
* new message for the index {@code newMessage.getIndex()}
*/
public void updateMessage(Message newMessage) {
// Update message
this.messageList.set(newMessage.getIndex(), newMessage) ;
// Notify observers
this.notifyMessageUpdate(newMessage) ;
}// updateMessage()
/**
* Sets the flags and notifies the observers.
*
* @param messages
* updated messages
*/
private void notifyMessageUpdate(Message... messages) {
this.messageUpdate = true ;
for(Message message : messages) {
if (!this.updatedMessages.contains(message)) {
this.updatedMessages.add(message) ;
}// if
}// for
try {
this.notifyObservers() ;
} catch (KameleonException ke) {
this.displayDebugInformation(ke) ;
}// try
this.updatedMessages.clear() ;
this.messageUpdate = false ;
}// notifyMessageUpdate(Message...)
/**
* Displays debug information using an instance of
* {@code DebugMessage} if the debug mode is activated.
*/
@Override
public void displayDebugInformation(KameleonException ke) {
if (this.debugMode) {
this.addMessage(new DebugMessage(ke)) ;
}// if
}// displayDebugInformation(KameleonException)
}// class MessageModel