Package kameleon.gui.view

Source Code of kameleon.gui.view.ViewMessages

/*
* 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.ComponentOrientation;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import kameleon.exception.KameleonException;
import kameleon.gui.language.SwitchLanguage;
import kameleon.gui.model.GenerationMessage;
import kameleon.gui.model.InformationMessage;
import kameleon.gui.model.Message;
import kameleon.gui.model.Model;
import kameleon.gui.model.Observer;
import kameleon.gui.model.PlugInMessage;
import kameleon.gui.util.FileConstants;
import kameleon.gui.util.LanguageConstants;

/**
* View showing messages about the recent activities.
*
* @author    Fromentin Xavier, Schnell Michaël
* @version    1.0
*/
public class ViewMessages extends JScrollPane
implements Observer, FileConstants, LanguageConstants {

  /**
   * Needed to serialize this class.
   *
   * @see    java.io.Serializable
   */
  private static final long serialVersionUID = -7537764891787710973L ;
 
  /**
   * Constant used to initialize graphical components.
   */
  private static final String EMPTY_TEXT = "" ;//$NON-NLS-1$
 
  /**
   * Component containing the content of this view.
   */
  private JPanel content ;

  /**
   * Model of this view.
   */
  private Model model ;

  /**
   * List of the view displaying the messages.
   */
  private List<ViewMessage> messages ;
 
  /**
   * Layout manager for this view.
   */
  private GridBagLayout gridbag ;

  /**
   * Sole constructor.
   *
   * @param  model
   *       model of this view
   *
   * @throws  KameleonException
   *       if an error occurred while building this instance
   */
  public ViewMessages(Model model) throws KameleonException {
    super() ;
    this.content = new JPanel() ;
    this.setViewportView(this.content) ;
    this.setBorder(BorderFactory.createTitledBorder(EMPTY_TEXT)) ;
   
    this.content.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT) ;
    this.gridbag = new GridBagLayout() ;
    this.content.setLayout(this.gridbag) ;

    this.model = model ;
    this.model.addObserver(this) ;
    this.messages = new ArrayList<ViewMessage>() ;
   
    this.reloadLanguage() ;
  }// ViewMessages(Model)

  /**
   * Updates the list of displayed messages.
   */
  @Override
  public void update() throws KameleonException {
    for(ViewMessage message : this.messages) {
      message.update() ;
    }// for
   
    if (this.model.messagesUpdated()) {
      for(Message message : this.model.getUpdatedMessages()) {
        if (this.contains(message)) {// Update message
          this.messages.get(message.getIndex()).updateMessage(message) ;
        } else {// New message
          ViewMessage vm = null ;
          if (message instanceof PlugInMessage) {
            vm = new ViewPlugInMessage(this.model, (PlugInMessage) message) ;
          } else if (message instanceof GenerationMessage) {
            vm = new ViewGenerationMessage(this.model, (GenerationMessage) message) ;
          } else if (message instanceof InformationMessage) {
            vm = new ViewInformationMessage(this.model, (InformationMessage) message) ;
          }// if
          if (this.messages.isEmpty()) {
            this.messages.add(vm) ;
          } else {
            this.messages.add(message.getIndex(), vm) ;
          }// if
         
          GridBagConstraints constraints = new GridBagConstraints() ;
          constraints.gridwidth = GridBagConstraints.REMAINDER ;
          constraints.gridheight = 1 ;
          constraints.fill = GridBagConstraints.HORIZONTAL ;
          constraints.weightx = 1.0 ;
          constraints.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING ;
         
          this.content.removeAll() ;
          for(ViewMessage vmsg : this.messages) {
            this.content.add(vmsg) ;
            this.gridbag.setConstraints(vmsg, constraints) ;
          }// for
        }// if
      }// for
    }// if
  }// update()

  /**
   * Updates the text to match the currently selected language.
   *
   * @throws   KameleonException
   *       if an error occurred while updating the text of this view
   */
  @Override
  public void reloadLanguage() throws KameleonException {
    SwitchLanguage sl = SwitchLanguage.getInstance() ;
   
    TitledBorder border = (TitledBorder) this.getBorder() ;
    border.setTitle(sl.getText(MESSAGES_TITLE_BORDER)) ;
   
    for(ViewMessage message : this.messages) {
      message.reloadLanguage() ;
    }// for
  }// reloadLanguage()
 
  /**
   * Indicates whether the given messages is already being displayed.
   *
   * @param   message
   *       message whose presence is tested
   *
   * @return  {@code true} is the given message is already being displayed,
   *       {@code false} otherwise
   */
  private boolean contains(Message message) {
    boolean contains = false ;
    Iterator<ViewMessage> iter = this.messages.iterator() ;
    while (iter.hasNext() && !contains) {
      contains = iter.next().equals(message) ;
    }// while
    return contains ;
  }// contains(Message)

}// class ViewMessages
TOP

Related Classes of kameleon.gui.view.ViewMessages

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.