MOA 12.03
Real Time Analytics for Data Streams
TextViewerPanel.java
Go to the documentation of this file.
00001 /*
00002  *    TextViewerPanel.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.gui;
00021 
00022 import java.awt.BorderLayout;
00023 import java.awt.Font;
00024 import java.awt.GridLayout;
00025 import java.awt.Point;
00026 import java.awt.event.ActionEvent;
00027 import java.awt.event.ActionListener;
00028 import java.io.BufferedWriter;
00029 import java.io.File;
00030 import java.io.FileWriter;
00031 import java.io.IOException;
00032 import java.io.PrintWriter;
00033 
00034 import javax.swing.JButton;
00035 import javax.swing.JFileChooser;
00036 import javax.swing.JPanel;
00037 import javax.swing.JScrollPane;
00038 import javax.swing.JTextArea;
00039 
00046 public class TextViewerPanel extends JPanel {
00047 
00048     private static final long serialVersionUID = 1L;
00049 
00050     public static String exportFileExtension = "txt";
00051 
00052     protected JTextArea textArea;
00053 
00054     protected JScrollPane scrollPane;
00055 
00056     protected JButton exportButton;
00057 
00058     public TextViewerPanel() {
00059         this.textArea = new JTextArea();
00060         this.textArea.setEditable(false);
00061         this.textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
00062         this.exportButton = new JButton("Export as .txt file...");
00063         this.exportButton.setEnabled(false);
00064         JPanel buttonPanel = new JPanel();
00065         buttonPanel.setLayout(new GridLayout(1, 2));
00066         buttonPanel.add(this.exportButton);
00067         setLayout(new BorderLayout());
00068         this.scrollPane = new JScrollPane(this.textArea);
00069         add(this.scrollPane, BorderLayout.CENTER);
00070         add(buttonPanel, BorderLayout.SOUTH);
00071         this.exportButton.addActionListener(new ActionListener() {
00072 
00073             public void actionPerformed(ActionEvent e) {
00074                 JFileChooser fileChooser = new JFileChooser();
00075                 fileChooser.setAcceptAllFileFilterUsed(true);
00076                 fileChooser.addChoosableFileFilter(new FileExtensionFilter(
00077                         exportFileExtension));
00078                 if (fileChooser.showSaveDialog(TextViewerPanel.this) == JFileChooser.APPROVE_OPTION) {
00079                     File chosenFile = fileChooser.getSelectedFile();
00080                     String fileName = chosenFile.getPath();
00081                     if (!chosenFile.exists()
00082                             && !fileName.endsWith(exportFileExtension)) {
00083                         fileName = fileName + "." + exportFileExtension;
00084                     }
00085                     try {
00086                         PrintWriter out = new PrintWriter(new BufferedWriter(
00087                                 new FileWriter(fileName)));
00088                         out.write(TextViewerPanel.this.textArea.getText());
00089                         out.close();
00090                     } catch (IOException ioe) {
00091                         GUIUtils.showExceptionDialog(
00092                                 TextViewerPanel.this.exportButton,
00093                                 "Problem saving file " + fileName, ioe);
00094                     }
00095                 }
00096             }
00097         });
00098     }
00099 
00100     public void setText(String newText) {
00101         Point p = this.scrollPane.getViewport().getViewPosition();
00102         this.textArea.setText(newText);
00103         this.scrollPane.getViewport().setViewPosition(p);
00104         this.exportButton.setEnabled(newText != null);
00105     }
00106 
00107     public void addText(String newText) {
00108         String text = textArea.getText();
00109         text += (!text.isEmpty()) ? "\n" : "";
00110         text += newText;
00111         setText(text);
00112     }
00113 }
 All Classes Namespaces Files Functions Variables Enumerations