source: src/main/java/weka/gui/ReaderToTextPane.java @ 7

Last change on this file since 7 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.3 KB
RevLine 
[4]1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * ReaderToTextArea.java
19 * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.gui;
23
24import java.awt.Color;
25import java.io.LineNumberReader;
26import java.io.Reader;
27
28import javax.swing.JTextPane;
29import javax.swing.text.Style;
30import javax.swing.text.StyleConstants;
31import javax.swing.text.StyleContext;
32import javax.swing.text.StyledDocument;
33
34/**
35 * A class that sends all lines from a reader to a JTextPane component.
36 *
37 * @author Len Trigg (trigg@cs.waikato.ac.nz)
38 * @author FracPete (fracpete at waikato dot ac dot nz)
39 * @version $Revision: 5142 $
40 */
41public class ReaderToTextPane
42  extends Thread {
43
44  /** The reader being monitored. */
45  protected LineNumberReader m_Input;
46
47  /** The output text component. */
48  protected JTextPane m_Output;
49 
50  /** the color to use. */
51  protected Color m_Color;
52
53  /**
54   * Sets up the thread. Using black as color for displaying the text.
55   *
56   * @param input       the Reader to monitor
57   * @param output      the TextArea to send output to
58   */
59  public ReaderToTextPane(Reader input, JTextPane output) {
60    this(input, output, Color.BLACK);
61  }
62
63  /**
64   * Sets up the thread.
65   *
66   * @param input       the Reader to monitor
67   * @param output      the TextArea to send output to
68   * @param color       the color to use
69   */
70  public ReaderToTextPane(Reader input, JTextPane output, Color color) {
71    StyledDocument      doc;
72    Style               style;
73
74    setDaemon(true);
75   
76    m_Color  = color;
77    m_Input  = new LineNumberReader(input);
78    m_Output = output;
79   
80    doc   = m_Output.getStyledDocument();
81    style = StyleContext.getDefaultStyleContext()
82                        .getStyle(StyleContext.DEFAULT_STYLE);
83    style = doc.addStyle(getStyleName(), style);
84    StyleConstants.setFontFamily(style, "monospaced");
85    StyleConstants.setForeground(style, m_Color);
86  }
87 
88  /**
89   * Returns the color in use.
90   *
91   * @return            the color
92   */
93  public Color getColor() {
94    return m_Color;
95  }
96 
97  /**
98   * Returns the style name.
99   *
100   * @return            the style name
101   */
102  protected String getStyleName() {
103    return "" + m_Color.hashCode();
104  }
105
106  /**
107   * Sit here listening for lines of input and appending them straight
108   * to the text component.
109   */
110  public void run() {
111    while (true) {
112      try {
113        StyledDocument doc = m_Output.getStyledDocument();
114        doc.insertString(
115            doc.getLength(), 
116            m_Input.readLine() + '\n', 
117            doc.getStyle(getStyleName()));
118        m_Output.setCaretPosition(doc.getLength());
119      }
120      catch (Exception ex) {
121        try {
122          sleep(100);
123        }
124        catch (Exception e) {
125          // ignored
126        }
127      }
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.