/*
* 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.view;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import kameleon.exception.FileReadingException;
import kameleon.gui.exception.UnknownKeyException;
import kameleon.gui.model.FileModel;
import kameleon.gui.model.InformationMessage;
import kameleon.gui.model.InformationMessage.State;
import kameleon.gui.model.Message;
import kameleon.gui.model.Model;
import kameleon.gui.util.FileConstants;
import kameleon.gui.util.ImageUtility;
/**
* View displaying a simple information message. The message is displayed with an
* icon to its left matching the type of message : error, success or information.
*
* @author Schnell Michaël
* @version 1.0
*/
public class ViewInformationMessage extends ViewMessage
implements FileConstants {
/**
* Needed to serialize this class.
*
* @see java.io.Serializable
*/
private static final long serialVersionUID = 2981269781301634713L ;
/**
* Icon displayed left of the text.
*/
protected JLabel icon ;
/**
* Graphical component displaying the text of the message.
*/
protected JTextPane messageArea ;
/**
* Layout manager for this view.
*/
private GridBagLayout gridbag ;
/**
* Sole constructor.
*
* @param model
* model used by this view
*
* @param message
* model backing the information about the displayed message
*
* @throws FileReadingException
* If an icon could not be loaded
*
* @throws UnknownKeyException
* if an error occurred while updating the text of this view
*/
public ViewInformationMessage(Model model, InformationMessage message)
throws FileReadingException, UnknownKeyException {
super(model, message) ;
this.gridbag = new GridBagLayout() ;
this.setLayout(this.gridbag) ;
this.setBorder(BorderFactory.createTitledBorder(
FileModel.DATE_FORMATTER.format(new Date()))) ;
this.build(message) ;
this.reloadLanguage() ;
}// ViewInformationMessage(Model, InformationMessage)
/**
* Builds the content of this instance.
*
* @param iMessage
* model backing the information about the displayed information
*
* @throws FileReadingException
* If an icon could not be loaded
*/
private void build(InformationMessage iMessage) throws FileReadingException {
this.makeMessageRow(iMessage) ;
}// build(InformationMessage)
/**
* Builds the components need to display the message with the appropriate icon.
*
* @param iMessage
* model backing the information about the displayed information
*
* @throws FileReadingException
* If an icon could not be loaded
*/
private void makeMessageRow(InformationMessage iMessage)
throws FileReadingException {
this.makeIcon(iMessage.getState()) ;
this.makeArea(iMessage) ;
}// makeMessageRow(InformationMessage)
/**
* Builds the component used to display the text of the message.
*
* @param iMessage
* model backing the information about the displayed information
*/
private void makeArea(InformationMessage iMessage) {
GridBagConstraints constraints ;
this.messageArea = new JTextPane() ;
this.messageArea.setEditable(false) ;
this.messageArea.setOpaque(false) ;
this.messageArea.setBorder(BorderFactory.createEmptyBorder()) ;
this.messageArea.setBackground(new Color(0, 0, 0, 0)) ;
constraints = new GridBagConstraints() ;
constraints.gridwidth = GridBagConstraints.REMAINDER ;
constraints.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING ;
constraints.fill = GridBagConstraints.HORIZONTAL ;
constraints.weightx = 1.0 ;
constraints.weighty = 0.0 ;
this.add(this.messageArea) ;
this.gridbag.setConstraints(this.messageArea, constraints) ;
}// makeArea(InformationMessage)
/**
* Builds the appropriate icon for the given message.
*
* @param type
* type of the displayed information
*
* @throws FileReadingException
* If an icon could not be loaded
*/
private void makeIcon(State type) throws FileReadingException {
JLabel label = new JLabel() ;
this.initIcon(label, type) ;
GridBagConstraints constraints = new GridBagConstraints() ;
constraints.gridwidth = 1 ;
constraints.anchor = GridBagConstraints.ABOVE_BASELINE_TRAILING ;
constraints.fill = GridBagConstraints.HORIZONTAL ;
constraints.insets = new Insets(2, 2, 2, 2) ;
constraints.weightx = 0.0 ;
constraints.weighty = 0.0 ;
this.add(label) ;
this.gridbag.setConstraints(label, constraints) ;
this.icon = label ;
}// makeIcon(State)
/**
* Initializes the icon of the given label using the
* given state.
*
* @param iconLabel
* label that will contain the icon
*
* @param type
* type used to determine the matching icon
*
* @throws FileReadingException
* If an icon could not be loaded
*/
private void initIcon(JLabel iconLabel, State type)
throws FileReadingException {
String iconPath = null ;
switch(type) {
case INFORMATION:
iconPath = INFORMATION_FILE ;
break ;
case SUCCESS:
iconPath = CONVERTED_FILE ;
break ;
case ERROR:
iconPath = ERROR_FILE ;
}// switch
InputStream src = new BufferedInputStream(ViewInformationMessage.class
.getResourceAsStream(iconPath)) ;
iconLabel.setIcon(new ImageIcon(ImageUtility.getImageBytes(src))) ;
try {
src.close() ;
} catch(IOException ioe) {
this.model.displayDebugInformation(
new FileReadingException(ioe.getMessage())) ;
}// try
}// initIcon(JLabel, State)
/**
* Updates the displayed text.
*
* @param newMessage
* new message informations
*
* @throws UnknownKeyException
* if an error occurred while updating the text of this view
*/
@Override
public void updateMessage(Message newMessage) throws UnknownKeyException {
this.message = newMessage ;
// Update message
this.reloadLanguage() ;
}// updateMessage(Message)
/** {@inheritDoc} */
@Override
public void reloadLanguage() throws UnknownKeyException {
InformationMessage iMessage = (InformationMessage) this.message ;
this.messageArea.setText(iMessage.getMessage()) ;
}// reloadLanguage()
/** {@inheritDoc} */
@Override
public void update() {
/* Nothing to be done.*/
}// update()
}// class InformationMessage