Advertisement
Guest User

MessageConsole.java

a guest
Jul 13th, 2014
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.35 KB | None | 0 0
  1. import java.io.*;
  2. import java.awt.*;
  3. import javax.swing.event.*;
  4. import javax.swing.text.*;
  5.  
  6. /*
  7.  *  Create a simple console to display text messages.
  8.  *
  9.  *  Messages can be directed here from different sources. Each source can
  10.  *  have its messages displayed in a different color.
  11.  *
  12.  *  Messages can either be appended to the console or inserted as the first
  13.  *  line of the console
  14.  *
  15.  *  You can limit the number of lines to hold in the Document.
  16.  */
  17. public class MessageConsole
  18. {
  19.     private JTextComponent textComponent;
  20.     private Document document;
  21.     private boolean isAppend;
  22.     private DocumentListener limitLinesListener;
  23.  
  24.     public MessageConsole(JTextComponent textComponent)
  25.     {
  26.         this(textComponent, true);
  27.     }
  28.  
  29.     /*
  30.      *  Use the text component specified as a simply console to display
  31.      *  text messages.
  32.      *
  33.      *  The messages can either be appended to the end of the console or
  34.      *  inserted as the first line of the console.
  35.      */
  36.     public MessageConsole(JTextComponent textComponent, boolean isAppend)
  37.     {
  38.         this.textComponent = textComponent;
  39.         this.document = textComponent.getDocument();
  40.         this.isAppend = isAppend;
  41.         textComponent.setEditable( false );
  42.     }
  43.  
  44.     /*
  45.      *  Redirect the output from the standard output to the console
  46.      *  using the default text color and null PrintStream
  47.      */
  48.     public void redirectOut()
  49.     {
  50.         redirectOut(null, null);
  51.     }
  52.  
  53.     /*
  54.      *  Redirect the output from the standard output to the console
  55.      *  using the specified color and PrintStream. When a PrintStream
  56.      *  is specified the message will be added to the Document before
  57.      *  it is also written to the PrintStream.
  58.      */
  59.     public void redirectOut(Color textColor, PrintStream printStream)
  60.     {
  61.         ConsoleOutputStream cos = new ConsoleOutputStream(textColor, printStream);
  62.         System.setOut( new PrintStream(cos, true) );
  63.     }
  64.  
  65.     /*
  66.      *  Redirect the output from the standard error to the console
  67.      *  using the default text color and null PrintStream
  68.      */
  69.     public void redirectErr()
  70.     {
  71.         redirectErr(null, null);
  72.     }
  73.  
  74.     /*
  75.      *  Redirect the output from the standard error to the console
  76.      *  using the specified color and PrintStream. When a PrintStream
  77.      *  is specified the message will be added to the Document before
  78.      *  it is also written to the PrintStream.
  79.      */
  80.     public void redirectErr(Color textColor, PrintStream printStream)
  81.     {
  82.         ConsoleOutputStream cos = new ConsoleOutputStream(textColor, printStream);
  83.         System.setErr( new PrintStream(cos, true) );
  84.     }
  85.  
  86.     /*
  87.      *  To prevent memory from being used up you can control the number of
  88.      *  lines to display in the console
  89.      *
  90.      *  This number can be dynamically changed, but the console will only
  91.      *  be updated the next time the Document is updated.
  92.      */
  93.     public void setMessageLines(int lines)
  94.     {
  95.         if (limitLinesListener != null)
  96.             document.removeDocumentListener( limitLinesListener );
  97.  
  98.         limitLinesListener = new LimitLinesDocumentListener(lines, isAppend);
  99.         document.addDocumentListener( limitLinesListener );
  100.     }
  101.  
  102.     /*
  103.      *  Class to intercept output from a PrintStream and add it to a Document.
  104.      *  The output can optionally be redirected to a different PrintStream.
  105.      *  The text displayed in the Document can be color coded to indicate
  106.      *  the output source.
  107.      */
  108.     class ConsoleOutputStream extends ByteArrayOutputStream
  109.     {
  110.         private final String EOL = System.getProperty("line.separator");
  111.         private SimpleAttributeSet attributes;
  112.         private PrintStream printStream;
  113.         private StringBuffer buffer = new StringBuffer(80);
  114.         private boolean isFirstLine;
  115.  
  116.         /*
  117.          *  Specify the option text color and PrintStream
  118.          */
  119.         public ConsoleOutputStream(Color textColor, PrintStream printStream)
  120.         {
  121.             if (textColor != null)
  122.             {
  123.                 attributes = new SimpleAttributeSet();
  124.                 StyleConstants.setForeground(attributes, textColor);
  125.             }
  126.  
  127.             this.printStream = printStream;
  128.  
  129.             if (isAppend)
  130.                 isFirstLine = true;
  131.         }
  132.  
  133.         /*
  134.          *  Override this method to intercept the output text. Each line of text
  135.          *  output will actually involve invoking this method twice:
  136.          *
  137.          *  a) for the actual text message
  138.          *  b) for the newLine string
  139.          *
  140.          *  The message will be treated differently depending on whether the line
  141.          *  will be appended or inserted into the Document
  142.          */
  143.         public void flush()
  144.         {
  145.             String message = toString();
  146.  
  147.             if (message.length() == 0) return;
  148.  
  149.             if (isAppend)
  150.                 handleAppend(message);
  151.             else
  152.                 handleInsert(message);
  153.  
  154.             reset();
  155.         }
  156.  
  157.         /*
  158.          *  We don't want to have blank lines in the Document. The first line
  159.          *  added will simply be the message. For additional lines it will be:
  160.          *
  161.          *  newLine + message
  162.          */
  163.         private void handleAppend(String message)
  164.         {
  165.             //  This check is needed in case the text in the Document has been
  166.             //  cleared. The buffer may contain the EOL string from the previous
  167.             //  message.
  168.  
  169.             if (document.getLength() == 0)
  170.                 buffer.setLength(0);
  171.  
  172.             if (EOL.equals(message))
  173.             {
  174.                 buffer.append(message);
  175.             }
  176.             else
  177.             {
  178.                 buffer.append(message);
  179.                 clearBuffer();
  180.             }
  181.  
  182.         }
  183.         /*
  184.          *  We don't want to merge the new message with the existing message
  185.          *  so the line will be inserted as:
  186.          *
  187.          *  message + newLine
  188.          */
  189.         private void handleInsert(String message)
  190.         {
  191.             buffer.append(message);
  192.  
  193.             if (EOL.equals(message))
  194.             {
  195.                 clearBuffer();
  196.             }
  197.         }
  198.  
  199.         /*
  200.          *  The message and the newLine have been added to the buffer in the
  201.          *  appropriate order so we can now update the Document and send the
  202.          *  text to the optional PrintStream.
  203.          */
  204.         private void clearBuffer()
  205.         {
  206.             //  In case both the standard out and standard err are being redirected
  207.             //  we need to insert a newline character for the first line only
  208.  
  209.             if (isFirstLine && document.getLength() != 0)
  210.             {
  211.                 buffer.insert(0, "\n");
  212.             }
  213.  
  214.             isFirstLine = false;
  215.             String line = buffer.toString();
  216.  
  217.             try
  218.             {
  219.                 if (isAppend)
  220.                 {
  221.                     int offset = document.getLength();
  222.                     document.insertString(offset, line, attributes);
  223.                     textComponent.setCaretPosition( document.getLength() );
  224.                 }
  225.                 else
  226.                 {
  227.                     document.insertString(0, line, attributes);
  228.                     textComponent.setCaretPosition( 0 );
  229.                 }
  230.             }
  231.             catch (BadLocationException ble) {}
  232.  
  233.             if (printStream != null)
  234.             {
  235.                 printStream.print(line);
  236.             }
  237.  
  238.             buffer.setLength(0);
  239.         }
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement