📄 messageviewer.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * web: http://yale.cs.uni-dortmund.de/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package edu.udo.cs.yale.gui;import edu.udo.cs.yale.Yale;import edu.udo.cs.yale.tools.LogService;import java.io.OutputStream;import javax.swing.JTextArea;import javax.swing.JScrollPane;import javax.swing.JViewport;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.JPopupMenu;import java.awt.event.ActionEvent;import javax.swing.KeyStroke;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.KeyEvent;import java.awt.event.InputEvent;import java.io.File;import java.io.FileWriter;import java.io.PrintWriter;import java.io.IOException;/** A text area displaying the log output. All kinds of streams can be redirected to this * viewer by using an instance of a special inner stream subclass. */public class MessageViewer extends JScrollPane implements MouseListener { private class ClearMessageAction extends AbstractAction { private ClearMessageAction() { super("Clear message viewer"); putValue(SHORT_DESCRIPTION, "Clear the message viewer"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_C)); } public void actionPerformed(ActionEvent e) { textArea.setText(""); } } private class SaveLogFileAction extends AbstractAction { private SaveLogFileAction() { super("Save log file..."); putValue(SHORT_DESCRIPTION, "Save the logging output to a file"); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S)); } public void actionPerformed(ActionEvent e) { File file = new File("./"); String logFile = Yale.getExperiment().getRootOperator().getParameterAsString("logfile"); if (logFile != null) { file = Yale.getExperiment().resolveFileName(logFile); } else { file = Yale.getExperiment().getExperimentFile(); if (file != null) file = file.getParentFile(); } file = SwingTools.chooseFile(MessageViewer.this, file, false); if (file != null) { try { PrintWriter out = new PrintWriter(new FileWriter(file)); out.println(textArea.getText()); out.close(); } catch (IOException ex) { SwingTools.showErrorMessage("Cannot write log file.", ex); } } } } private class FindAction extends AbstractAction { private FindAction() { super("Find..."); putValue(SHORT_DESCRIPTION, "Find text in the logging output."); putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_F)); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK)); } public void actionPerformed(ActionEvent e) { new SearchDialog(MessageViewer.this, textArea).show(); } } /** A special output stream that appends all its incoming characters to the text field as soon * as it reads a newline. */ private class MessageViewerOutputStream extends OutputStream { private StringBuffer currentString; private MessageViewerOutputStream() { this.currentString = new StringBuffer(); } public void write(int b) { char c = (char)b; switch (b) { case 0x000a: case 0x000d: if (currentString.length() > 0) { textArea.append(currentString.toString()+"\n"); //getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum()); textArea.setCaretPosition(textArea.getDocument().getLength()); currentString = new StringBuffer(); } break; case '\t': currentString.append(" "); break; default: currentString.append(c); break; } } } /** A stream that can be used to print to this text area. */ public final OutputStream outputStream = new MessageViewerOutputStream(); public final Action CLEAR_MESSAGE_VIEWER_ACTION = new ClearMessageAction(); public final Action SAVE_LOGFILE_ACTION = new SaveLogFileAction(); public final Action FIND_ACTION = new FindAction(); private JTextArea textArea; private String searchPattern = null; public MessageViewer() { this(new JTextArea(8,40)); } private MessageViewer(JTextArea textArea) { super(textArea); this.textArea = textArea; textArea.setEditable(false); textArea.addMouseListener(this); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { if (e.getModifiers() == MouseEvent.BUTTON3_MASK) { createPopupMenu().show(textArea, e.getX(), e.getY()); } } public JPopupMenu createPopupMenu() { JPopupMenu menu = new JPopupMenu(); menu.add(CLEAR_MESSAGE_VIEWER_ACTION); menu.add(SAVE_LOGFILE_ACTION); menu.add(FIND_ACTION); return menu; } public String getLogMessage() { return textArea.getText(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -