abstractconsolemanager.java

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

JAVA
305
字号
/*
 * $Id: AbstractConsoleManager.java,v 1.13 2004/02/28 09:21:02 epr Exp $
 */
package org.jnode.driver.console;

import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import javax.naming.NameNotFoundException;
import javax.naming.NamingException;

import org.jnode.driver.ApiNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.DeviceListener;
import org.jnode.driver.DeviceManager;
import org.jnode.driver.console.x86.ScrollableShellConsole;
import org.jnode.driver.input.KeyboardAPI;
import org.jnode.driver.input.KeyboardEvent;
import org.jnode.driver.input.PointerAPI;
import org.jnode.driver.input.PointerEvent;
import org.jnode.naming.InitialNaming;
import org.jnode.plugin.Plugin;
import org.jnode.plugin.PluginDescriptor;
import org.jnode.plugin.PluginException;
import org.jnode.system.BootLog;
import org.jnode.system.event.FocusEvent;
import org.jnode.vm.VmSystem;

/**
 * @author epr
 */
public abstract class AbstractConsoleManager extends Plugin implements
        ConsoleManager {

    private final Map consoles = new HashMap();

    private Console lastFocused;

    private Console current;

    private int shellConsoleCount = 0;

    Device kb;

    KeyboardAPI kbApi;

    /** The pointer devices that I'm listener on */
    private final LinkedList pointerDevs = new LinkedList();

    private final DeviceManager devMan;

    /**
     * Initialize a new instance
     * 
     * @param descriptor
     */
    public AbstractConsoleManager(PluginDescriptor descriptor)
            throws ConsoleException {
        super(descriptor);
        try {
            devMan = (DeviceManager) InitialNaming.lookup(DeviceManager.NAME);
            openInput(devMan);
        } catch (NameNotFoundException ex) {
            throw new ConsoleException("DeviceManager not found", ex);
        }
        current = null;
    }

    final void initializeKeyboard(Device kbDev) {
        try {
            this.kbApi = (KeyboardAPI) kbDev.getAPI(KeyboardAPI.class);
            this.kbApi.addKeyboardListener(this);
            this.kb = kbDev;
        } catch (ApiNotFoundException ex) {
            BootLog.error("KeyboardAPI not found", ex);
        }
    }

    /**
     * Add a pointer device
     * 
     * @param pDev
     */
    final void addPointerDevice(Device pDev) {
        try {
            final PointerAPI pApi = (PointerAPI) pDev.getAPI(PointerAPI.class);
            pointerDevs.add(pDev);
            pApi.addPointerListener(this);
        } catch (ApiNotFoundException ex) {
            BootLog.error("PointerAPI not found", ex);
        }
    }

    private final void openInput(DeviceManager devMan) {
        final Collection kbs = devMan.getDevicesByAPI(KeyboardAPI.class);
        if (!kbs.isEmpty()) {
            initializeKeyboard((Device) kbs.iterator().next());
        }
        final Collection pointers = devMan.getDevicesByAPI(PointerAPI.class);
        for (Iterator i = pointers.iterator(); i.hasNext();) {
            addPointerDevice((Device) i.next());
        }
        devMan.addListener(new DevManListener());
    }

    /**
     * Remove a pointer device
     * 
     * @param pDev
     */
    final void removePointer(Device pDev) {
        if (pointerDevs.remove(pDev)) {
            try {
                final PointerAPI pApi = (PointerAPI) pDev
                        .getAPI(PointerAPI.class);
                pApi.removePointerListener(this);
            } catch (ApiNotFoundException ex) {
                BootLog.error("PointerAPI not found", ex);
            }
        }
    }

    /**
     * Gets the console with the given index
     * 
     * @param name
     * @return The console
     */
    public Console getConsole(String name) {
        return (Console) consoles.get(name);
    }

    /**
     * Gets the currently focused console.
     * 
     * @return Console
     */
    public Console getFocus() {
        return current;
    }

    /**
     * Focus the given console
     * 
     * @param console
     */
    public synchronized void focus(Console console) {
        if (console == null) return;

        if (current != null) {
            current.focusLost(new FocusEvent(FocusEvent.FOCUS_LOST));
        }
        lastFocused = current;
        current = console;

        current.focusGained(new FocusEvent(FocusEvent.FOCUS_GAINED));
    }

    public Console getConsoleWithAccelerator(int keyCode) {
        for (Iterator iter = consoles.values().iterator(); iter.hasNext();) {
            Console console = (Console) iter.next();
            if (console.getAcceleratorKeyCode() == keyCode) return console;
        }
        return null;
    }

    public Console getShellConsole(int index) {
        return this.getConsole(ConsoleManager.ShellConsoleName + index);
    }

    public ScrollableShellConsole createShellConsole() throws ConsoleException {
        if (shellConsoleCount == ConsoleManager.MAX_SHELL_CONSOLE_NR)
                throw new ConsoleException(
                        "Maximum number of shell consoles already registered!");

        ScrollableShellConsole console = new ScrollableShellConsole(this,
                ConsoleManager.ShellConsoleName + (++shellConsoleCount));
        this.registerConsole(console);
        console.setAcceleratorKeyCode(KeyEvent.VK_F1 + (shellConsoleCount - 1));
        return console;
    }

    public void unregisterConsole(Console console) {
        consoles.remove(console.getConsoleName());
        current = null;
        this.focus(this.lastFocused);
    }

    public void registerConsole(Console console) {
        consoles.put(console.getConsoleName(), console);
        if (current == null) {
            current = console;
            current.focusGained(new FocusEvent(FocusEvent.FOCUS_GAINED));
        }
    }

    /**
     * Start this plugin
     * 
     * @throws PluginException
     */
    protected void startPlugin() throws PluginException {
        //BootLog.debug("Creating consoles");
        try {
            InitialNaming.bind(NAME, this);
            final Console c = createShellConsole();
            this.focus(c);
            if (c instanceof ScrollableShellConsole)
                    ((ScrollableShellConsole) c).getOut().print(
                            VmSystem.getBootLog());
        } catch (NameNotFoundException ex) {
            throw new PluginException("Cannot find DeviceManager", ex);
        } catch (NamingException ex) {
            throw new PluginException(ex);
        } catch (ConsoleException ex) {
            throw new PluginException("Cannot create consoles", ex);
        }
    }

    /**
     * Stop this plugin
     * 
     * @throws PluginException
     */
    protected void stopPlugin() throws PluginException {
        InitialNaming.unbind(NAME);
        for (Iterator i = consoles.values().iterator(); i.hasNext();) {
            Console console = (Console) i.next();
            console.close();
        }
    }

    /**
     * @see org.jnode.driver.input.KeyboardListener#keyPressed(org.jnode.driver.input.KeyboardEvent)
     */
    public void keyPressed(KeyboardEvent event) {
        if (current != null) {
            current.keyPressed(event);
        }
    }

    /**
     * @see org.jnode.driver.input.KeyboardListener#keyReleased(org.jnode.driver.input.KeyboardEvent)
     */
    public void keyReleased(KeyboardEvent event) {
        if (current != null) {
            current.keyReleased(event);
        }
    }

    /**
     * @see org.jnode.driver.input.PointerListener#pointerStateChanged(org.jnode.driver.input.PointerEvent)
     */
    public void pointerStateChanged(PointerEvent event) {
        if (current != null) {
            current.pointerStateChanged(event);
        }
    }

    public String[] listConsoleNames() {
        String[] returnArray = new String[ consoles.keySet().size()];
        int i = 0;
        for (Iterator iter = consoles.keySet().iterator(); iter.hasNext(); i++) {
            String element = (String) iter.next();
            returnArray[ i] = element;
        }
        return returnArray;
    }

    /**
     * This listener looks for registration of a keyboard device.
     * 
     * @author epr
     */
    class DevManListener implements DeviceListener {

        /**
         * @param device
         * @see org.jnode.driver.DeviceListener#deviceStarted(org.jnode.driver.Device)
         */
        public void deviceStarted(Device device) {
            if (device.implementsAPI(KeyboardAPI.class)) {
                initializeKeyboard(device);
            } else if (device.implementsAPI(PointerAPI.class)) {
                addPointerDevice(device);
            }
        }

        /**
         * @param device
         * @see org.jnode.driver.DeviceListener#deviceStop(org.jnode.driver.Device)
         */
        public void deviceStop(Device device) {
            if (device.implementsAPI(PointerAPI.class)) {
                removePointer(device);
            }
        }
    }

}

⌨️ 快捷键说明

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