/*
** Luxor - XML User Interface Language (XUL) Toolkit
** Copyright (c) 2001, 2002 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package luxor.swing.widget;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import luxor.spi.*;
import luxor.status.*;
import luxor.*;

public class HistoryComboBoxHelper
{
   static Logger T = Logger.getLogger( HistoryComboBoxHelper.class );
   private HistoryComboBox _combo;

   private HistoryModel _history;
   private Object _selectedItemBackup;

   public HistoryComboBoxHelper( HistoryComboBox combo, HistoryModel history )
   {
      _combo = combo;
      _history = history;

      JTextField editor = ( JTextField ) _combo.getEditor().getEditorComponent();

      editor.addFocusListener(
         new FocusListener()
         {
            public void focusGained( FocusEvent e )
            {
               onFocusGained();
            }

            public void focusLost( FocusEvent e )
            {
               onFocusLost();
            }
         } );
   }

   public void setSelectedItemBackup( Object obj )
   {
      _selectedItemBackup = obj;
   }

   public Object getSelectedItemBackup()
   {
      return _selectedItemBackup;
   }

   public void add( String text )
   {
      _history.add( text );
   }


   public void onFocusGained()
   {
      T.debug( "enter onFocusGained()" );

      populate();

      // restore selection
      _combo.setSelectedItem( getSelectedItemBackup() );

      T.debug( "leave onFocusGained()" );
   }

   public void onFocusLost()
   {
      try
      {
         T.debug( "enter onFocusLost()" );

         // save selection so we can restore it once we regain focus
         setSelectedItemBackup( _combo.getSelectedItem() );

         // check if we should add the current text to the history box
         // method: check if top history entry is a substring of current text
         //   if it is, replace it with current text
         //   otherwise add it to history box on top as a new entry
         // note, that I don't replace substrings originating from the user profile

         String text = _combo.getText();
         text = text.trim();

         if( text.equals( "" ) )
            return;

         if( _history.size() > 0 )
         {
            HistoryNode top = _history.getHistoryNode( 0 );

            if( text.equals( top.getText() ) )
               return;

            if( text.startsWith( top.getText() ) && top.getSource() == HistoryNode.MEMORY )
            {
               T.debug( "removing history entry " + top.getText() + " for upgrade" );
               _history.removeHistoryNode( top );
            }
         }

         T.debug( "adding history entry " + text );

         _history.prepend( text );

         // save selection to new text
         setSelectedItemBackup( text );

      }
      finally
      {
         T.debug( "leave onFocusLost()" );
      }
   }


   public void populate()
   {
      _combo.removeAllItems();
      for( int i = 0; i < _history.size(); i++ )
      {
         HistoryNode item = _history.getHistoryNode( i );
         _combo.addItem( item.getText() );
      }
   }
}