⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gconsole.java

📁 用Swing实现的CHM制作工具
💻 JAVA
字号:
package g2w.app.gchm.lib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/**
 * GConsole is a console emulator with some simple functions.
 * <ul>
 * <li><b>Alert:</b>append a text message in red.</li>
 * <li><b>Hey: </b>append a text message in green.</li>
 * <li><b>say: </b>append a text message in black.</li>
 * <li><b>exec: </b>excute a command and capture the output if 
 * the argument <i>capture</i> is true..</li>
 * 
 * @author YangLe
 * @version 008 2009-3-21 19:37:08
 */
public class GConsole extends JPanel {
	/** SimpleAttributeSet constants. */
	/** Warning style. */
	final public static SimpleAttributeSet WARNING = new SimpleAttributeSet();
	/** Highlight style. */
	final public static SimpleAttributeSet HIGHLIGHT = new SimpleAttributeSet(); 
	/** Plain style. */
	final public static SimpleAttributeSet PLAIN = new SimpleAttributeSet(); 
	
	/** Configure constants. */
	static {
		StyleConstants.setForeground(WARNING, Color.RED);
		StyleConstants.setForeground(HIGHLIGHT, new Color(58, 186, 48));
		StyleConstants.setForeground(PLAIN, Color.BLACK);
	}
	
    private JTextPane console;
    private JScrollPane scroll;
    
	/**
	 * Construct a console with the default setup.
	 */
	public GConsole() {
		initComponents();
	}
	
	/**
	 * Initialize all components.
	 */
	private void initComponents() {
		setLayout(new BorderLayout());
		console = new JTextPane();
		console.setEditable(false);
		scroll = new JScrollPane(console);
		add(scroll, BorderLayout.CENTER);
		
		validate();
		repaint();
	}

	public void append(String text, AttributeSet style) {
		// Get the document.
		Document doc = console.getDocument();
		try {
			doc.insertString(doc.getLength(), text + "\n", style);
			console.setCaretPosition(doc.getLength());
		} catch (BadLocationException e) {}
	}
	
	public void alert(String text) {
		append(text, WARNING);
	}
	
	public void hey(String text) {
		append(text, HIGHLIGHT);	
	}
	
	public void say(String text) {
		append(text, PLAIN);	
	}
	
	public void exec(String command, boolean capture) throws IOException {
		// Initialize a new process.
		Process process = Runtime.getRuntime().exec(command);
		if (!capture) return;
		// Capture the input stream.
		InputStream inputStream = process.getInputStream();
		captureStream(inputStream, false);
		InputStream errorStream = process.getErrorStream();
		captureStream(errorStream, true);
	}
	
	public void captureStream(InputStream is, final boolean isErrorStream) {
		final BufferedReader br = new BufferedReader(new InputStreamReader(is));
		new Thread(new Runnable() {
			public void run() {
				String text = null;
				int emptyLineCount = -1;
				try {
					while ((text = br.readLine()) != null) {
						if (text.trim().isEmpty()) {
							emptyLineCount++;
							continue;
						}
						while (--emptyLineCount > 0)
							say("");
						emptyLineCount = -1;
						if (isErrorStream)
							alert(text);
						else
							say(text);
					}
				} catch (IOException e) {
					alert(e.getMessage());
				}
			}
		}).start();
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -