/*
 * MessageLine.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class implements a swing component which can display a persistent message
 * which can be temporarily replaced with a transient message
 */
 
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.util.logging.*;
public class MessageLine extends JPanel
{
  private String persistentText = "";
  private static final long TRANSIENT_INTERVAL = 5000;
  static Logger log = Logger.getLogger ("LdapVoipDialler");
  
  public MessageLine (String initialText)
  {
    super (new GridBagLayout());
    
    // Initialise with initial text
    displayText (initialText);
  }

  private void displayText (String text)
  {
    removeAll ();
    JLabel label = new JLabel (text);
    add (label,
         new GridBagConstraints (0, 0, 1, 1, 1, 1, GridBagConstraints.WEST, 
                                 GridBagConstraints.HORIZONTAL, new Insets (0, 0, 0, 0), 0, 0));
    validate ();
    repaint ();
  }
    
  // This function sets the text that is to be displayed persistently  
  public void setPersistentText (String text)
  {
    this.persistentText = text;
    displayText (text);
  }
  
  // This function clears the persistent text
  public void clearPersistentText ()
  {
    setPersistentText ("");
  }
  
  // This function sets text to be displayed momentarily
  public void setTransientText (String text) 
  {
    displayText (text);
    
    // Set timer to re-display the persistent message
    java.util.Timer timer = new java.util.Timer ();
    TimerTask task = new TimerTask ()
    {
      public void run ()
      {
        displayText (persistentText);
      }
    };
    timer.schedule (task, TRANSIENT_INTERVAL);
  }

  // This function sets text to be displayed until replaced with another message  
  public void setTemporaryText (String text) 
  {
    displayText (text);
  }
  
  // This function clears any temporary text and re-displays the persistent text
  public void clearTemporaryText () 
  {
    displayText (persistentText);
  }
}
    
    
  