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

📄 console.java

📁 eq跨平台查询工具源码 eq跨平台查询工具源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Console.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * 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 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 org.executequery.gui.console;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.net.InetAddress;import java.net.UnknownHostException;import javax.swing.JScrollPane;import javax.swing.ScrollPaneConstants;import javax.swing.text.BadLocationException;import javax.swing.text.Document;import javax.swing.text.SimpleAttributeSet;import javax.swing.text.StyleConstants;import org.underworldlabs.util.SystemProperties;import org.executequery.gui.console.commands.ChangeDirCommand;import org.executequery.gui.console.commands.ClearCommand;import org.executequery.gui.console.commands.Command;import org.executequery.gui.console.commands.ExitCommand;import org.executequery.gui.console.commands.HelpCommand;import org.executequery.gui.console.commands.HomeCommand;import org.executequery.gui.console.commands.PwdCommand;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the  *           release of version 3.0.0beta1 has meant a  *           resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * An internal console which provide different kinds of * prompts and which allows to execute both internal and * external (OS specific) commands. The console is embedded * in a <code>JScrollPane</code> and handles it by itself. * <p>Modified from Romain Guy's version for his text editor * JExt http://jext.org. * * @author   Romain Guy * @author   Takis Diakoumis * @version  $Revision: 1.4 $ * @date     $Date: 2006/05/14 06:56:52 $ */public class Console extends JScrollPane {        public static final int WINDOWS_OS = 0;    public static final int UNIX_OS = 1;    public static final int MAC_OS = 2;        private int osType;        /** DOS prompt: /export/home/guy > */    public static final int DOS_PROMPT = 0;    /** Jext prompt: Gfx@/export/home/guy > -- renamed */    public static final int DEFAULT_PROMPT = 1;    /** Linux prompt: guy@csdlyon$ */    public static final int LINUX_PROMPT = 2;    /** SunOS prompt: csdlyon% */    public static final int SUNOS_PROMPT = 3;        /** Default prompt types: DOS, default, Linux and SunOS **/    public static final String[] DEFAULT_PROMPTS = {"$p >", "$u@$p >", "$u@$h$$ ", "$h% "};        // current separators used in command lines    private static final String COMPLETION_SEPARATORS = " \t;:/\\\"\'";        private static final String[] WINDOWS_EXEC = {"cmd.exe", "/c", ""};        // commands    private Command currentCmd, firstCmd;        // processes specific    private Process process;    private String processName;    private StdoutThread stdout;    private StderrThread stderr;        // private fields    private String current;    private Document outputDocument;    private ConsoleTextPane textArea;    private HistoryModel historyModel = new HistoryModel(25);    private int userLimit = 0, typingLocation = 0, index = -1;        // colors    public Color errorColor = Color.red;    public Color promptColor = Color.blue;    public Color outputColor = Color.black;    public Color infoColor = new Color(0, 165, 0);        // prompt    private boolean displayPath;    private String currentPath, hostName, prompt = new String();    private String promptPattern = DEFAULT_PROMPTS[DEFAULT_PROMPT];        /** Instanciates a new console without displaying prompt. */    public Console() {        this(true);    }        /** Creates a new console, embedding it in a <code>JScrollPane</code>.     *  By default console help is displayed.     *  @param display If set on true, prompt is displayed     */    public Console(boolean display) {                super(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);                // check we have the console properties loaded        if (SystemProperties.getProperties("console") == null) {            SystemProperties.loadPropertiesResource("console",                    SystemProperties.getProperty("system", "console.defaults"));        }                currentPath = System.getProperty("user.home");                textArea = new ConsoleTextPane(this);        textArea.setFont(new Font("Monospaced", Font.PLAIN, 11));        outputDocument = textArea.getDocument();        append(SystemProperties.getProperty("console", "console.welcome"), infoColor, false, true);                displayPath = true;                if (display) {            displayPrompt();        }                getViewport().setView(textArea);        FontMetrics fm = getFontMetrics(textArea.getFont());        setPreferredSize(new Dimension(40 * fm.charWidth('m'), 6 * fm.getHeight()));        setMinimumSize(getPreferredSize());        setMaximumSize(getPreferredSize());                initCommands();                // start at the user's home dir        //    builtInCommand("cd " + currentPath);    }        /** Return the <code>Document</code> in which output is performed. */    public Document getOutputDocument() {        return outputDocument;    }        /** Adds a command to the linked list of commands. */    public void addCommand(Command command) {                if (command == null)            return;                currentCmd.next = command;        currentCmd = command;    }        /**     * Return true if command is built-in. If command is built-in,     * it is also executed.     * @param command Command to check and execute     */    private boolean builtInCommand(String command) {        boolean ret = false;        Command _currentCmd = firstCmd;                while (_currentCmd != null) {                        if (_currentCmd.handleCommand(this, command)) {                ret = true;                break;            }                        _currentCmd = _currentCmd.next;                    }                return ret;    }        // inits commands list        private void initCommands() {        firstCmd = currentCmd = new ClearCommand();        addCommand(new ChangeDirCommand());        addCommand(new ExitCommand());        addCommand(new HomeCommand());        addCommand(new HelpCommand());        //    addCommand(new ListCommand());        addCommand(new PwdCommand());    }        /** Set console background color.     *     *  @param color <code>Color</code> to be used     */    public void setBgColor(Color color) {        textArea.setBackground(color);    }        /** Set console error color.     *     *  @param color <code>Color</code> to be used     */    public void setErrorColor(Color color) {        errorColor = color;    }        /** Set console prompt color.     *     *  @param color <code>Color</code> to be used     */    public void setPromptColor(Color color) {        promptColor = color;    }        /** Set console output color.     *     *  @param color <code>Color</code> to be used     */    public void setOutputColor(Color color) {        outputColor = color;        textArea.setForeground(color);        textArea.setCaretColor(color);    }        /** Set console info color.     *     *  @param color <code>Color</code> to be used     */    public void setInfoColor(Color color) {        infoColor = color;    }        /** Set console selection color.     *     *  @param color <code>Color</code> to be used     */    public void setSelectionColor(Color color) {        textArea.setSelectionColor(color);    }        /** Save the history. */    public void save() {        for (int i = 0; i < historyModel.getSize(); i++) {            SystemProperties.setProperty("console",                     "console.history." + i, historyModel.getItem(i));        }            }        /**      * Set the prompt pattern.     *     * @param type The prompt pattern     */    public void setPromptPattern(String prompt) {        if (prompt == null) {            return;        }                promptPattern = prompt;        displayPath = false;        buildPrompt();    }        /** Get prompt pattern. */    public String getPromptPattern() {        return promptPattern;    }        /** Displays the prompt according to the current selected prompt type. */    public void displayPrompt() {                if (prompt == null || displayPath)            buildPrompt();                if (outputDocument == null)            return;                append('\n' + prompt, promptColor);        typingLocation = userLimit = outputDocument.getLength();    }        // builds the prompt according to the prompt pattern    private void buildPrompt() {                int promptType = -1;                if(System.getProperty("mrj.version") != null) {            promptType = DEFAULT_PROMPT;;            osType = MAC_OS;        }        else {            String osName = System.getProperty("os.name");                        if(osName.indexOf("Windows") != -1) {                promptType = DOS_PROMPT;                osType = WINDOWS_OS;            }            else if(osName.indexOf("OS/2") != -1) {                promptType = DEFAULT_PROMPT;                osType = UNIX_OS;            }            else {                promptType = DEFAULT_PROMPT;                osType = UNIX_OS;            }                    }                promptPattern = DEFAULT_PROMPTS[promptType];        displayPath = false;        StringBuffer buf = new StringBuffer();                if (hostName == null) {                        try {                hostName = InetAddress.getLocalHost().getHostName();            } catch (UnknownHostException uhe) {}                    }                for (int i = 0; i < promptPattern.length(); i++) {                        char c = promptPattern.charAt(i);                        switch(c) {                                case '$':                                        if (i == promptPattern.length() - 1)                        buf.append(c);                                        else {                                                switch (promptPattern.charAt(++i)) {                            case 'p':                    // current path                                buf.append(currentPath);                                displayPath = true;                                break;                            case 'u':                    // user name                                buf.append(System.getProperty("user.name"));                                break;                            case 'h':                    // host name                                buf.append(hostName);                                break;                            case '$':                                buf.append('$');                                break;                        }                                            }                                        break;                                    default:                    buf.append(c);                                }                    }                prompt = buf.toString();    }        /** This method appends text in the text area.     *     *  @param text The text to append     *  @param color The color of the text     *  @param italic Set to true will append italic text     *  @param bold Set to true will append bold text     */    private void append(String text, Color color, boolean italic, boolean bold) {                if (outputDocument == null)            return;                SimpleAttributeSet style = new SimpleAttributeSet();                if (color != null)            style.addAttribute(StyleConstants.Foreground, color);                StyleConstants.setBold(style, bold);        StyleConstants.setItalic(style, italic);                try {            outputDocument.insertString(outputDocument.getLength(), text, style);        } catch(BadLocationException bl) {}                textArea.setCaretPosition(outputDocument.getLength());    }        /** This method appends text in the text area.     *     *  @param text The text to append in the text area     *  @apram color The color of the text to append     */    public void append(String text, Color color) {        append(text, color, false, false);    }        /** Adds a command to the history.     *     *  @param command Command to add in the history     */    public void addHistory(String command) {        historyModel.addItem(command);        index = -1;    }        /** Remove a char from current command line. Stands for BACKSPACE action. */    public void removeChar() {                try {            //if (typingLocation != userLimit)            //  outputDocument.remove(--typingLocation, 1);            int pos = textArea.getCaretPosition();                        if (pos <= typingLocation && pos > userLimit) {                outputDocument.remove(pos - 1, 1);                typingLocation--;            }                    } catch (BadLocationException ble) {}            }        /** Delete a char from command line. Stands for DELETE action. */    public void deleteChar() {                try {                        int pos = textArea.getCaretPosition();                        if (pos == outputDocument.getLength())                return;                        if (pos < typingLocation && pos >= userLimit) {                outputDocument.remove(pos, 1);                typingLocation--;            }                    }                catch (BadLocationException ble) {}            }        /** Adds a <code>String</code> to the current command line.     *     *  @param add <code>String</code> to be added     */    public void add(String add) {                try {                        int pos = textArea.getCaretPosition();                        if (pos <= typingLocation && pos >= userLimit)                outputDocument.insertString(pos, add, null);                        typingLocation += add.length();                    }                catch (BadLocationException ble) {}            }        /** Returns the position in characters at which     *  user is allowed to type his commands.     *     *  @return Beginning of user typing space     */    public int getUserLimit() {        return userLimit;    }        /** Returns the position of the end of the console prompt. */

⌨️ 快捷键说明

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