001    /*
002     * UnrealMessageTextArea.java
003     *
004     * Created on February 19, 2002, 12:44 AM
005     */
006    
007    package edu.tamu.gamebots.ui;
008    
009    import edu.isi.gamebots.client.*;
010    import javax.swing.*;
011    import javax.swing.text.*;
012    import java.awt.*;
013    
014    /**
015     * <p>
016     * Prints messages from the Gamebots server for the bot to the TextPanel. Time
017     * messages are printed in red to make it easy to visually parse message blocks.
018     * Messages from the server come very rapidly (10 full updates/second) so this has
019     * the ability to be paused so as not to overwhelm system resources.
020     * </p>
021     * @author Ryan Rozich (rozich@tamu.edu)
022     */
023    public class UnrealMessageTextArea extends javax.swing.JTextPane {
024      
025      private StyledDocument doc;
026      private boolean paused = false;
027      
028      /**
029       * <p>
030       * Creates a new instance of UnrealMessageTextArea
031       * </p>
032       */
033      public UnrealMessageTextArea() {
034        super();
035        doc = getStyledDocument();
036      }
037      
038      /**
039       * <p>
040       * Add a message to this TextPane
041       * </p>
042       * @param msg The message to print
043       */  
044      public void addMessage(Message msg){
045        if(!paused){
046          SimpleAttributeSet attributes = new SimpleAttributeSet();
047          
048          // make beg and end statements red
049          if(msg.getType().equalsIgnoreCase(GamebotsConstants.BEG) || msg.getType().equalsIgnoreCase(GamebotsConstants.END)){
050            StyleConstants.setBold(attributes,true);
051            attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.red);
052          }
053          
054          //insert message
055          try{
056            doc.insertString(0,msg + "\n", attributes);
057          }
058          catch(BadLocationException e){
059            e.printStackTrace();
060            System.exit(1);
061          }
062        }
063      }
064      
065      /**
066       * <p>
067       * Pause the updates of this TextPane
068       * </p>
069       */  
070      public void pause(){ paused = true; }
071      /** 
072       * <p>
073       * If this is currently paused, it will start to print messages again
074       * </p>
075       */  
076      public void unpause(){ paused = false; }
077      /**
078       * <p>
079       * Toggles between paused and unpaused state. In other words, if currently
080       * not-paused, it will pause and visa-versa.
081       * </p>
082       */  
083      public void togglePause(){ paused = !paused; }
084      
085    }