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

📄 toolkit.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
字号:
/*
 * $Id: Toolkit.java,v 1.13 2004/02/24 08:13:52 epr Exp $
 */
package charva.awt;

import java.util.LinkedList;
import java.lang.InterruptedException;

import javax.naming.NameNotFoundException;

import org.jnode.driver.console.ConsoleException;
import org.jnode.driver.console.x86.AbstractTextConsole;
import org.jnode.driver.console.x86.ConsoleManager;
import org.jnode.driver.console.x86.RawTextConsole;
import org.jnode.driver.input.KeyboardEvent;
import org.jnode.driver.input.KeyboardListener;
import org.jnode.naming.InitialNaming;

import charva.awt.event.KeyEvent;

/**
 * @author vali
 * @author Levente S醤tha
 */
public class Toolkit extends AbstractToolkit implements KeyboardListener {
    private static Toolkit instance = null;
    private int[] colorpairs = new int[256];
    private LinkedList keyQueue = new LinkedList();
    private AbstractTextConsole console = null;
    //private final int iniCurX;
    //private final int iniCurY;
    private int curX;
    private int curY;
    private int leftClip;
    private int rightClip;
    private int topClip;
    private int bottomClip;

    /**
     * The constructor can only be called by the getDefaultToolkit() method,
     * making this an example of the Singleton pattern.
     */
    private Toolkit() {
        _evtQueue = EventQueue.getInstance();

        /* If the terminal is capable of handling colors, initialize the
         * color capability and the first color-pair (the default
         * foreground and background colors are white-on-black, but
         * can be modified by setting the static variables _defaultForeground
         * and _defaultBackground.
         */
        if (this.hasColors() && Toolkit.isColorEnabled) {
            startColors();
            _colorPairs.add(
                    new ColorPair(_defaultForeground, _defaultBackground));
        }
    }

    /**
     * This static method instantiates a Toolkit object if it does not
     * already exist; and returns a reference to the instantiated Toolkit.
     */
    public static Toolkit getDefaultToolkit() {
        if (instance == null) {
            instance = new Toolkit();
            try {
                // set the console
                final ConsoleManager conMgr = (ConsoleManager) InitialNaming.lookup(ConsoleManager.NAME);
        		RawTextConsole console;
				try {
					console = new RawTextConsole(conMgr,"testconsole");
					conMgr.registerConsole(console);
					conMgr.focus(console);
					console.clearScreen();
	                instance.init(console);
				} catch (ConsoleException e1) {
					e1.printStackTrace();
				}
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return instance;
    }

    private void init(AbstractTextConsole cons) {
        console = cons;
        console.addKeyboardListener(this);
        curX = /*iniCurX =*/ console.getCursorX();
        curY = /*iniCurY =*/ console.getCursorY();
        resetClipRect();
    }

    public synchronized void waitTillFinished() {
        while (isKeyboardReaderRunning())
            try {
                instance.wait();
            } catch (InterruptedException ie) {
                //ignore
            }
    }


    public void close() {
        clear();
        SyncQueue.getInstance().stop();
        setKeyboardReaderRunning(false);
        keyQueue.notifyAll();
        console = null;
        instance = null;
    }

    public void clear() {
        console.clearScreen();
    }

    public void setCursor(int x_, int y_) {
        curX = x_;
        curY = y_;
        console.setCursor(x_, y_);
    }

    public void addChar(int chr, int attrib, int colorpair_) {
        putCharWithClip(chr, attrib, colorpair_);
    }

    public void addVerticalLine(int length, int attrib, int colorpair) {
        int x = curX;
        for(int ln = curY + length, i = curY; i < ln; i++){
            setCursor(x, i);
            putCharWithClip(ACS_VLINE, attrib, colorpair);
        }
    }

    public void addString(String str_, int attrib_, int colorpair_) {
        if (str_ == null)
            return;

        for (int i = 0, ln = str_.length(); i < ln; i++)
            putCharWithClip(str_.charAt(i), attrib_, colorpair_);
    }

/*    public void drawBoxNative(int left_, int top_, int right_, int bottom_,
                              int colorpair_) {
        if ( top_ >= topClip && top_ <= bottomClip) {
            console.setChar(left_, ofsY + top_, (char) ACS_ULCORNER, colorpairs[colorpair_]);
            for (int i = left_ + 1; i < right_; i++) {
                console.setChar(i, ofsY + top_, (char) ACS_HLINE, colorpairs[colorpair_]);
            }
        }

        if (bottom_ >= topClip && bottom_ <= bottomClip) {
            console.setChar(left_, ofsY + bottom_, (char) ACS_LLCORNER, colorpairs[colorpair_]);
            for (int i = left_ + 1; i < right_; i++) {
                console.setChar(i, ofsY + bottom_, (char) ACS_HLINE, colorpairs[colorpair_]);
            }
        }

        if (left_ >= leftClip && left_ <= rightClip) {
            console.setChar(right_, ofsY + top_, (char) ACS_URCORNER, colorpairs[colorpair_]);
            for (int j = top_ + 1; j < bottom_; j++) {
                console.setChar(left_, ofsY + j, (char) ACS_VLINE, colorpairs[colorpair_]);

            }
        }
        if (right_ >= leftClip && right_ <= rightClip) {
            console.setChar(right_, ofsY + bottom_, (char) ACS_LRCORNER, colorpairs[colorpair_]);
            for (int j = top_ + 1; j < bottom_; j++) {
                console.setChar(right_, ofsY + j, (char) ACS_VLINE, colorpairs[colorpair_]);
            }
        }
    }*/

    public void drawBoxNative(int left_, int top_, int right_, int bottom_,
                              int colorpair_) {
            setCharWithClip(left_, top_, ACS_ULCORNER, colorpair_);
            for (int i = left_ + 1; i < right_; i++) {
                setCharWithClip(i, top_, ACS_HLINE, colorpair_);
            }

            setCharWithClip(left_, bottom_, ACS_LLCORNER, colorpair_);
            for (int i = left_ + 1; i < right_; i++) {
                setCharWithClip(i, bottom_, ACS_HLINE, colorpair_);
            }

            setCharWithClip(right_, top_, ACS_URCORNER, colorpair_);
            for (int j = top_ + 1; j < bottom_; j++) {
                setCharWithClip(left_, j, ACS_VLINE, colorpair_);

            }
            setCharWithClip(right_, bottom_, ACS_LRCORNER, colorpair_);
            for (int j = top_ + 1; j < bottom_; j++) {
                setCharWithClip(right_, j, ACS_VLINE, colorpair_);
            }
    }


    public void blankBoxNative(int left_, int top_, int right_, int bottom_, int colorpair_) {
        for (int i = left_; i < right_; i++)
            for (int j = top_; j < bottom_; j++)
                setCharWithClip(i, j, ' ', colorpair_);
    }

    public void setClipRectNative(int left_, int top_, int right_, int bottom_) {
        leftClip = left_;
        rightClip = right_;
        topClip = top_;
        bottomClip = bottom_;
    }

    public void resetClipRect() {
        leftClip = 0;
        rightClip = console.getWidth();
        topClip = 0;
        bottomClip = console.getHeight();
    }

    public void beep() {
//        SpeakerUtils.beep();
    }

    public int getScreenRows() {
        return console.getHeight();
    }

    public int getScreenColumns() {
        return console.getWidth();
    }

    public boolean hasColors() {
        return true;
    }

    public int getMaxColorPairs() {
        return 256;
    }

    public void startColors() {
    }

    public void sync() {
    }


    public void initColorPair(int index, int fgnd_, int bgnd_)
            throws TerminfoCapabilityException {
        colorpairs[index] = ((0xF & bgnd_) << 4) | (0xF & fgnd_);
    }

    public String getTtyName() {
        return console.getConsoleName();
    }

    public MouseEventInfo getMouseEventInfo() {
        return null;
    }

    protected int readKey() {
        synchronized (keyQueue) {
            while (keyQueue.isEmpty() && isKeyboardReaderRunning()) {
                try {
                    keyQueue.wait();
                } catch (InterruptedException ie) {

                }
            }
            if(isKeyboardReaderRunning()){
                int ch = ((Integer) keyQueue.removeFirst()).intValue();
                return ch;
            }else{
                return -1;
            }
        }
    }

    protected int getx() {
        return console.getCursorX();
    }

    protected int gety() {
        return console.getCursorY();
    }


    public void keyPressed(KeyboardEvent event) {
        synchronized (keyQueue) {
            int key_code = event.getKeyCode();
            int key_char = event.getKeyChar();
            int key;
            switch (key_code) {
                case java.awt.event.KeyEvent.VK_LEFT:
                    key = KeyEvent.VK_LEFT;
                    break;
                case java.awt.event.KeyEvent.VK_RIGHT:
                    key = KeyEvent.VK_RIGHT;
                    break;
                case java.awt.event.KeyEvent.VK_UP:
                    key = KeyEvent.VK_UP;
                    break;
                case java.awt.event.KeyEvent.VK_DOWN:
                    key = KeyEvent.VK_DOWN;
                    break;
                case java.awt.event.KeyEvent.VK_PAGE_DOWN:
                    key = KeyEvent.VK_PAGE_DOWN;
                    break;
                case java.awt.event.KeyEvent.VK_PAGE_UP:
                    key = KeyEvent.VK_PAGE_UP;
                    break;

                case java.awt.event.KeyEvent.VK_HOME:
                    key = KeyEvent.VK_HOME;
                    break;

                case java.awt.event.KeyEvent.VK_END:
                    key = KeyEvent.VK_END;
                    break;

                case java.awt.event.KeyEvent.VK_BACK_SPACE:
                    key = KeyEvent.VK_BACK_SPACE;
                    break;

                case java.awt.event.KeyEvent.VK_INSERT:
                    key = KeyEvent.VK_INSERT;
                    break;

                case java.awt.event.KeyEvent.VK_DELETE:
                    key = KeyEvent.VK_DELETE;
                    break;

                case java.awt.event.KeyEvent.VK_ENTER:
                    key = KeyEvent.VK_ENTER;
                    break;

                case java.awt.event.KeyEvent.VK_ESCAPE:
                    key = KeyEvent.VK_ESCAPE;
                    break;

                case java.awt.event.KeyEvent.VK_F1:
                case java.awt.event.KeyEvent.VK_F2:
                case java.awt.event.KeyEvent.VK_F3:
                case java.awt.event.KeyEvent.VK_F4:
                case java.awt.event.KeyEvent.VK_F5:
                case java.awt.event.KeyEvent.VK_F6:
                case java.awt.event.KeyEvent.VK_F7:
                case java.awt.event.KeyEvent.VK_F8:
                case java.awt.event.KeyEvent.VK_F9:
                case java.awt.event.KeyEvent.VK_F10:
                case java.awt.event.KeyEvent.VK_F11:
                case java.awt.event.KeyEvent.VK_F12:
                case java.awt.event.KeyEvent.VK_F13:
                case java.awt.event.KeyEvent.VK_F14:
                case java.awt.event.KeyEvent.VK_F15:
                case java.awt.event.KeyEvent.VK_F16:
                case java.awt.event.KeyEvent.VK_F17:
                case java.awt.event.KeyEvent.VK_F18:
                case java.awt.event.KeyEvent.VK_F19:
                case java.awt.event.KeyEvent.VK_F20:
                    key = KeyEvent.VK_F1 + key_code - java.awt.event.KeyEvent.VK_F1;
                    break;
                default:
                    key = key_char;
            }

            keyQueue.add(new Integer(key));
            keyQueue.notifyAll();
        }
    }

    public void keyReleased(KeyboardEvent event) {

    }

    private void setCharWithClip(int x, int y, int ch, int cp){
         if(x >= leftClip && x <= rightClip &&
                y >= topClip && y <= bottomClip)
            console.setChar(x,  y, (char) ch, colorpairs[cp]);
    }


    private void putCharWithClip(int ch, int att, int cp) {
        if (curY >= topClip && curY <= bottomClip &&
                curX >= leftClip && curX <= rightClip) {
            console.putChar((char) ch, computeColor(att, cp));
            curX++;
        }
    }

    private int computeColor(int att, int cp){
        int c = colorpairs[cp];

        if( (att & A_REVERSE) != 0)
            c = ((c << 4) & 0xF0) | ((c >> 4) & 0xF);

        if( (att & A_UNDERLINE) != 0)
            c = c | 8;

        if ( (att & A_BOLD) != 0)
            c = c | 8;

        int bu = A_BOLD | A_UNDERLINE;
        if ( (att & bu) == bu)
            c = c ^ 0xFF;

        return c;
    }


//NOT USED
//	public void redrawWin() {
//	}
//	public String getStringCapability(String capname_)
//			throws TerminfoCapabilityException {
//		return null;
//	}
//	public int getNumericCapability(String capname_)
//			throws TerminfoCapabilityException {
//		return 0;
//	}
//NOT USED
//	public boolean getBooleanCapability(String capname_)
//			throws TerminfoCapabilityException {
//		return false;
//	}
//NOT USED
//	public void putp(String str_) {
//	}
//NOT USED
//	public void print(String str_) throws TerminfoCapabilityException {
//	}
//NOT USED
//	public void addHorizontalLine(int length_, int attrib_, int colorpair) {
//	}
}

⌨️ 快捷键说明

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