/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

package ipipan.poliqarp.gui;

import javax.swing.*;

/** 
 * A multilingual <code>JLabel</code> used to display a status line at the
 * bottom of the main window.
 *
 * A <code>StatusLine</code> can display different kinds of texts according 
 * to the state it is in. In some states, the status line also displays some
 * variable, language-independent information that can be passed using the
 * content parameters.
 */
public class StatusLine extends JLabel implements MultilingualEntity,
   ResultEventListener
{
   /**
    * States this StatusLine can be in.
    */
   public enum State {
      /**
       * "Loading <em>param1</em>, please wait..."
       */
      LOADING,
      
      /**
       * "Current corpus: <em>param1</em>"
       */
      CURRENT,

      /**
       * "Executing query (displaying results: <em>param1</em> of
       * <em>param2</em>)"
       */
      EXECUTING,
          
      /**
       * "Displaying results <em>param1</em> (of <em>param2</em>)"
       */
      RESULTS,
      
      /**
       * "Sorting query results, please wait..."
       */
      SORTING,
      
      /**
       * "No corpus loaded." This is the initial content of a status line.
       */
      NOCURRENT,

      /**
       * "Searching corpus"
       */
      MATCHSEARCHING,

      /**
       * "Creating statistics"
       */
      MATCHCOUNTING,

      /**
       * "Saving results"
       */
      WRITERESULTS
   }

   private State state;
   private String param1, param2;

   /**
    * Creates a default status line. It is in state <code>NOCURRENT</code>
    * and has no content parameters.
    */
   public StatusLine()
   {
      state = State.NOCURRENT;
      param1 = param2 = null;
   }

   /**
    * Sets a state for this status line.
    * @param state the new state
    * @param param1 first content parameter
    * @param param2 second content parameter
    */
   public void setState(State state, String param1, String param2)
   {
      this.state = state;
      if(param1 != null) 
         this.param1 = param1;
      if(param2 != null) 
         this.param2 = param2;
   }

   /**
    * Sets a state for this status line.
    * @param state the new state
    * @param param the only content parameter
    */
   public void setState(State state, String param)
   {
      this.state = state;
      this.param1 = param;
   }

   /**
    * Sets a state for this status line.
    * @param state the new state
    */
   public void setState(State state)
   {
      this.state = state;
   }

   /**
    * Updates the contents of this <code>StatusLine</code> based on language
    * setting of the given configuration, the current state and content
    * parameters.
    */
   public void updateLanguage(Configuration c)
   {
      String newText = null;
      switch (state) {
         case LOADING:
            newText = c.getComponentName("TEXT_LOADING1") + param1 + 
               c.getComponentName("TEXT_LOADING2");
            break;
         case CURRENT:
            newText = c.getComponentName("TEXT_CURRENT") + param1;
            break;
         case NOCURRENT:
            newText = c.getComponentName("TEXT_NOLOAD");
            break;
         case SORTING:
            newText = c.getComponentName("TEXT_SORTING");
            break;
         case EXECUTING:
            newText = c.getComponentName("TEXT_EXECUTING1") + " " + param1 +
            " " + c.getComponentName("TEXT_EXECUTING2") + " " + param2 +
            " " + c.getComponentName("TEXT_EXECUTING3");
            break;
         case RESULTS:
            newText = c.getComponentName("TEXT_DISPLAYRES1") + " " + param1 +
               " (" + c.getComponentName("TEXT_DISPLAYRES2") + " " + param2 +
               ")";
            break;
         case MATCHSEARCHING:
            newText = c.getComponentName("TEXT_MATCHSEARCHING");
            break;
         case MATCHCOUNTING:
            newText = c.getComponentName("TEXT_MATCHCOUNTING");
            break;
         case WRITERESULTS:
            newText = c.getComponentName("TEXT_WRITERESULTS");
            break;
      }
      setText(newText);
   }

   /**
    * Notifies this status line that the results shown by the result table
    * have changed. When the state of this line is one of <code>RESULTS</code>
    * or <code>EXECUTING</code> (and it should be upon reception of such 
    * message), this involves updating the content parameters.
    */
   public void resultChanged(ResultEvent e)
   {
      ResultModel res = e.getSource();
      if (state == State.EXECUTING || state == State.RESULTS) {         
         int from = res.getStartPosition(), to = res.getEndPosition(), 
            total = res.getTotal();
         if (to > 0)
            from++;
         setState(state, "" + from + " - " + to, "" + total);
      }
      updateLanguage(e.getConfig());
   }
}
