scrollableshellconsole.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 400 行

JAVA
400
字号
/*
 * $Id: ScrollableShellConsole.java,v 1.5 2004/02/24 08:04:30 epr Exp $
 */
package org.jnode.driver.console.x86;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import java.io.PrintStream;

import org.jnode.driver.console.ConsoleException;
import org.jnode.driver.console.ConsoleInputStream;
import org.jnode.driver.console.ConsoleOutputStream;
import org.jnode.driver.input.KeyboardEvent;
import org.jnode.driver.input.PointerEvent;
import org.jnode.system.event.FocusEvent;

/**
 * @author epr
 * @author Bengt B鋠erman (support for 8-bit char representation)
 */
public class ScrollableShellConsole extends AbstractTextConsole{

	private final PrintStream err;
	private final PrintStream out;
	private final InputStream in;
	
	private int height;
	private char[] buffer;
	private int ofsY;
	private boolean visible;

	private final boolean doScroll = true;

	
	public ScrollableShellConsole(org.jnode.driver.console.ConsoleManager mgr, String name) throws ConsoleException {
		super(mgr, name);
		out = new PrintStream(new ConsoleOutputStream(this, 0x07), true);
		err = new PrintStream(new ConsoleOutputStream(this, 0x04), true);
		in = new ConsoleInputStream(this);
		this.height = scrHeight * 10;
		this.buffer = new char[height * scrWidth];
		this.setTabSize(4);
	}


	/**
	 * Get the errorstream to this console.
	 * 
	 * @return PrintStream
	 */
	public PrintStream getErr() {
		return err;
	}

	/**
	 * Get the inputstream of this console.
	 * 
	 * @return InputStream
	 */
	public InputStream getIn() {
		return in;
	}

	/**
	 * Get the outputstream to this console.
	 * 
	 * @return PrintStream
	 */
	public PrintStream getOut() {
		return out;
	}

	public void keyPressed(KeyboardEvent event) {

		final int modifiers = event.getModifiers();
		if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
			switch (event.getKeyCode()) {
				case KeyEvent.VK_PAGE_UP :
					scrollUp(10);
					event.consume();
					break;
				case KeyEvent.VK_PAGE_DOWN :
					scrollDown(10);
					event.consume();
					break;
				case KeyEvent.VK_UP :
					scrollUp(1);
					event.consume();
					break;
				case KeyEvent.VK_DOWN :
					scrollDown(1);
					event.consume();
					break;
			}
		}
		super.keyPressed(event);
	}

	/**
	 * @param lines
	 */
	protected void scrollUp(int lines) {
		if (ofsY > 0) {
			ofsY = ofsY - Math.min(ofsY, lines);
			if (visible) {
				updateToScreen();
			}
		}
	}

	/**
	 * @param lines
	 */
	protected void scrollDown(int lines) {
		if (ofsY + scrHeight < height) {
			ofsY = ofsY + Math.min(lines, height - (ofsY + scrHeight));
			if (visible) {
				updateToScreen();
			}
		}
	}


	public void pointerStateChanged(PointerEvent event) {
		if (isFocused() && doScroll) {
			final int z = event.getZ();
			if (z != 0) {
				if (z > 0) {
					scrollUp(z);
				} else {
					scrollDown(-z);
				}
			}
		}
		super.pointerStateChanged(event);
	}

	/**
	 * @param x
	 * @param y
	 * @param ch
	 * @param color
	 * @see org.jnode.driver.console.Console#setChar(int, int, char, int)
	 */
	public void setChar(int x, int y, char ch, int color) {

		// Translate some 8-bit chars to their console representation
		if (ch > 127) {
			switch (ch) {
				case 199 :
					ch = 128;
					break;
				case 252 :
					ch = 129;
					break;
				case 233 :
					ch = 130;
					break;
				case 226 :
					ch = 131;
					break;
				case 228 :
					ch = 132;
					break;
				case 224 :
					ch = 133;
					break;
				case 229 :
					ch = 134;
					break;
				case 231 :
					ch = 135;
					break;
				case 234 :
					ch = 136;
					break;
				case 235 :
					ch = 137;
					break;
				case 232 :
					ch = 138;
					break;
				case 239 :
					ch = 139;
					break;
				case 206 :
					ch = 140;
					break;
				case 204 :
					ch = 141;
					break;
				case 196 :
					ch = 142;
					break;
				case 197 :
					ch = 143;
					break;
				case 201 :
					ch = 144;
					break;
				case 230 :
					ch = 145;
					break;
				case 198 :
					ch = 146;
					break;
				case 244 :
					ch = 147;
					break;
				case 246 :
					ch = 148;
					break;
				case 242 :
					ch = 149;
					break;
				case 251 :
					ch = 150;
					break;
				case 249 :
					ch = 151;
					break;
				case 255 :
					ch = 152;
					break;
				case 214 :
					ch = 153;
					break;
				case 220 :
					ch = 154;
					break;
				case 163 :
					ch = 156;
					break;
				case 165 :
					ch = 157;
					break;
				case 225 :
					ch = 160;
					break;
				case 237 :
					ch = 161;
					break;
				case 243 :
					ch = 162;
					break;
				case 250 :
					ch = 163;
					break;
				case 241 :
					ch = 164;
					break;
				case 209 :
					ch = 165;
					break;
				case 191 :
					ch = 168;
					break;
			}
		}

		int v = (ch & 0xFF) | ((color << 8) & 0xFF00);
		buffer[x + (y * scrWidth)] = (char) v;
		if (visible) {
			if ((y >= ofsY) && (y < ofsY + scrHeight)) {
				screen.set(x, y - ofsY, ch, color);
			}
		}
	}


	/**
	 * @param y
	 */
	public void clearLine(int y) {
		for (int x = 0; x < curX; x++) {
			// TODO: get rid of magic number
			setChar(x, curY, ' ', 0x07);
		}
		setCursor(0, curY);
	}

	/**
	 * @param x
	 * @param y
	 * @see org.jnode.driver.console.Console#setCursor(int, int)
	 */
	public void setCursor(int x, int y) {
		curX = x;
		curY = y;
		if (visible) {
			if ((curY >= ofsY) && (curY < ofsY + scrHeight)) {
				screen.setCursor(curX, curY - ofsY);
			}
		}
	}

	public void putChar(char v, int color) {
		if (v == '\n') {
			// Goto next line
			// Clear till eol
			for (int i = curX; i < scrWidth; i++) {
				setChar(i, curY, ' ', color);
			}
			curX = 0;
			curY++;
		} else if (v == '\b') {
			if (curX > 0) {
				curX--;
			} else if (curY > 0) {
				curX = scrWidth - 1;
				curY--;
			}
			setChar(curX, curY, ' ', color);
		} else if (v == '\t') {
			putChar(' ', color);
			while ((curX % this.getTabSize()) != 0) {
				putChar(' ', color);
			}
		} else {
			setChar(curX, curY, v, color);
			curX++;
		}

		if (curX >= scrWidth) {
			curX = 0;
			curY++;
		}
		if (curY >= height) {
			curY = height - 1;
			System.arraycopy(buffer, scrWidth, buffer, 0, scrWidth * (height - 1));
			// Clear last row
			for (int i = curX; i < scrWidth; i++) {
				setChar(i, curY, ' ', color);
			}
		}
		if (curY >= ofsY + scrHeight - 1) {
			ofsY = (curY - scrHeight) + 1;
			if (visible) {
				updateToScreen();
			}
		}
		setCursor(curX, curY);
	}

	protected void updateToScreen() {
		screen.restore(buffer, ofsY * scrWidth);
		if ((curY >= ofsY) && (curY < ofsY + scrHeight)) {
			screen.setCursor(curX, curY - ofsY);
		}
	}
	/**
	 * @param event
	 * @see org.jnode.system.event.FocusListener#focusGained(org.jnode.system.event.FocusEvent)
	 */
	public void focusGained(FocusEvent event) {
		super.focusGained(event);
		setVisible(true);
		System.setOut(out);
		System.setErr(err);
		System.setIn(in);
	}

	/**
	 * @param event
	 * @see org.jnode.system.event.FocusListener#focusLost(org.jnode.system.event.FocusEvent)
	 */
	public void focusLost(FocusEvent event) {
		super.focusLost(event);
		setVisible(false);
	}

	/**
	 * Sets the visible.
	 * 
	 * @param visible
	 *            The visible to set
	 */
	public void setVisible(boolean visible) {
		this.visible = visible;
		if (visible) {
			updateToScreen();
		}
	}

	/* (non-Javadoc)
	 * @see org.jnode.driver.console.Console#getChar(int, int)
	 */
	public char getChar(int x, int y) {
		return screen.get(x,y);
	}

	/* (non-Javadoc)
	 * @see org.jnode.driver.console.Console#getBgColor(int, int)
	 */
	public char getBgColor(int x, int y) {
		return screen.getColor(x,y);
	}
}

⌨️ 快捷键说明

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