📄 gplogconsole.java
字号:
/*
* @(#)GPLogConsole.java 1.0 29.01.2003
*
* Copyright (C) 2003 luzar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package org.jgraph.pad;
import java.awt.AWTEvent;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URLEncoder;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.SwingUtilities;
import org.jgraph.pad.resources.*;
import org.jgraph.utils.BrowserLauncher;
/** Shows the System.in and System.out in a nice JFrame.
*
* The Frame looks like this:<br>
* <img src="doc-files/GPLogConsole.jpg">
*
* @author Thomas Genssler (FZI)
* @author Sven Luzar*/
public class GPLogConsole extends JFrame {
/** The PrintStream for the System.out
*
*/
private PrintStream stdout = null;
/** The PrintStream for the System.err
*
*/
private PrintStream stderr = null;
/** Title of the Frame
*
*/
private String frameTitle = "";
/** Card Layout for the Window
*
*/
CardLayout cardLayout = new CardLayout();
/** Text area for the System.err output
*/
JTextArea stderrText = new JTextArea();
/** ScrollPane for the System.out text area
*/
JScrollPane stdoutScrollPane = new JScrollPane();
/** Text area for the System.out output
*/
JTextArea stdoutText = new JTextArea();
/** ScrollPane for the System.err text area
*/
JScrollPane stderrScrollPane = new JScrollPane();
/** Tabbed pane for the System.out and System.err text area
*/
JTabbedPane jTabbedPane1 = new JTabbedPane();
/** Icon for the Window
*/
Image myIcon = null;
/** If <code>true</code>, the console will become visible when any system output occurs.
*/
boolean makeVisibleOnError = false;
/** PopUpMenu for save and clear the output textareas*/
InternalPopupMenu popup = new InternalPopupMenu();
/** creates an instance
*
*/
public GPLogConsole(
String title,
Image icon,
boolean makeVisible)
{
super();
frameTitle = title;
myIcon = icon;
this.makeVisibleOnError = makeVisible;
if ((frameTitle == null) || (frameTitle.equals(""))) {
frameTitle = "Test drive";
}
this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
/**Overriden, in order to be able to deal with window events*/
protected void processWindowEvent(WindowEvent e) {
//
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
// only close the window when we are not in embedded mode
// release resources and exit if we are not running embedded, buttonImage.buttonEdge., as
// part of another application
//super.processWindowEvent(buttonEdge);
this.dispose() ;
}
}
/** Initialises the Swing components
*
*/
private void jbInit() throws Exception {
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
});
this.setTitle(frameTitle);
this.getContentPane().setLayout(cardLayout);
if (myIcon != null)
this.setIconImage(myIcon);
//re-direct stderr and stdout
redirect();
stderrText.setForeground(Color.red);
stderrText.setBackground(Color.lightGray);
stderrText.setEditable(false);
stderrText.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
stderrText_mouseClicked(e);
}
});
stdoutText.setForeground(Color.black);
stdoutText.setEditable(false);
stdoutText.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
stdoutText_mouseClicked(e);
}
});
jTabbedPane1.setTabPlacement(JTabbedPane.BOTTOM);
jTabbedPane1.setMinimumSize(new Dimension(400, 400));
jTabbedPane1.setPreferredSize(new Dimension(400, 400));
this.getContentPane().add(jTabbedPane1, "jTabbedPane1"/*#Frozen*/);
jTabbedPane1.add(stdoutScrollPane, Translator.getString("StandardOut"/*#Finished:Original="Standard out"*/));
jTabbedPane1.add(stderrScrollPane, Translator.getString("StandardError"/*#Finished:Original="Standard error"*/));
stderrScrollPane.getViewport().add(stderrText, null);
stdoutScrollPane.getViewport().add(stdoutText, null);
// make sure the last updated log is always in front
stdoutText.getDocument().addDocumentListener(
new MyDocumentListener(this, jTabbedPane1, stdoutScrollPane));
stderrText.getDocument().addDocumentListener(
new MyDocumentListener(this, jTabbedPane1, stderrScrollPane));
this.pack();
}
/* Sets the new OutputStream for System.out and System.err
*
*/
private void redirect() {
stdout = System.out;
stderr = System.err;
try {
System.setOut(new JTextAreaOutputStream(stdoutText, stdout, true));
// System.out.println("Standard out has been re-directed");
System.setErr(new JTextAreaOutputStream(stderrText, stderr, true));
// System.err.println("Standard error has been re-directed");
} catch (Exception ex){
System.err .println("Error while re-directing the output. Ignoring...");
}
}
/** disposes this window
*/
void this_windowDispose(WindowEvent e) {
this.dispose();
}
/** closes this window
*/
void this_windowClosing(WindowEvent e) {
this_windowDispose(e);
System.exit(0);
}
/** Shows the popup menu for the System.out textarea
*/
void stdoutText_mouseClicked(MouseEvent e) {
if (e.getModifiers() == InputEvent.META_MASK) {
popup.setTextArea(stdoutText);
popup.show(this.stdoutText, e.getX(), e.getY());
}
}
/** Shows the popup menu for the System.err textarea
*/
void stderrText_mouseClicked(MouseEvent e) {
if (e.getModifiers() == InputEvent.META_MASK) {
popup.setTextArea(stderrText);
popup.show(this.stderrText, e.getX(), e.getY());
}
}
}
/** Document listener to detect changes at the
* text areas and switches the right one text area
* to front.*/
class MyDocumentListener implements DocumentListener {
/** The Tabbed pane to switch the right one text area to front
*/
private JTabbedPane paneToSwitch = null;
/** The component which is in front
*/
private Component componentInFront = null;
/** The parent container
*/
private GPLogConsole lc = null;
/** creats an instance of this listener
*
*/
public MyDocumentListener(GPLogConsole l, JTabbedPane paneToSwitch,
Component inFront) {
this.paneToSwitch = paneToSwitch;
this.componentInFront = inFront;
this.lc = l;
}
/** Calls getInFront()
* @see #getInFront
*
*/
public void changedUpdate(DocumentEvent e) {
getInFront();
}
/** Calls getInFront()
* @see #getInFront
*
*/
public void insertUpdate(DocumentEvent e) {
getInFront();
if (lc.makeVisibleOnError){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
lc.setVisible(true);
lc.toFront();
}
});
}
}
/** Calls getInFront()
* @see #getInFront
*
*/
public void removeUpdate(DocumentEvent e) {
getInFront();
}
/** Switches the rights one text area to front
*/
void getInFront() {
// bring the attached component in front
paneToSwitch.setSelectedComponent(this.componentInFront);
}
}
/** Internal Popup Menu with a clear and a save button to
* clear or save the text areas.*/
class InternalPopupMenu extends JPopupMenu {
/** Menu item for clearing the text area
*/
JMenuItem jMenuItemClearWindow = new JMenuItem(Translator.getString("ClearOutput"/*#Finished:Original="Clear output"*/));
/** Menu item for saving the text area
*/
JMenuItem jMenuItemSaveToFile = new JMenuItem(Translator.getString("SaveToFile"/*#Finished:Original="Save to file..."*/));
/** Menu item for emailing the text area contents to tech support
*/
JMenuItem jMenuItemSendEmail = new JMenuItem(Translator.getString("Error.EmailTechSupport"));
/** The current textarea
*/
private JTextArea currentWindow = null;
/** creates an instance
*/
public InternalPopupMenu() {
super();
this.add(jMenuItemClearWindow);
this.addSeparator();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -