window.java

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

JAVA
444
字号
/* class Window
 *
 * Copyright (C) 2001-2003  R M Pitman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package charva.awt;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Enumeration;
import java.util.Vector;

import charva.awt.event.AWTEvent;
import charva.awt.event.AdjustmentEvent;
import charva.awt.event.GarbageCollectionEvent;
import charva.awt.event.InvocationEvent;
import charva.awt.event.PaintEvent;
import charva.awt.event.ScrollEvent;
import charva.awt.event.SyncEvent;
import charva.awt.event.WindowEvent;
import charva.awt.event.WindowListener;

/**
 * The Window class represents a "toplevel" window with no decorative frame.
 * The window is initially invisible; you must use the show() method to make it
 * visible.
 */
public class Window extends Container implements Runnable {

    public Window(Window owner_) {
        _owner = owner_;
        init();
    }

    public Window(Frame owner_) {
        _owner = owner_;
        init();
    }

    private void init() {
        _term = Toolkit.getDefaultToolkit();
        super._layoutMgr = new BorderLayout();
        _visible = false;

        // The window inherits the colors of its parent if there is one,
        // otherwise use the default colors as set in charva.awt.Toolkit.
        if (_owner != null) {
            super.setForeground(_owner.getForeground());
            super.setBackground(_owner.getBackground());
        } else {
            super.setForeground(Toolkit.getDefaultForeground());
            super.setBackground(Toolkit.getDefaultBackground());
        }
    }

    /**
     * Return the Window that is the "owner" of this Window.
     */
    public Window getOwner() {
        return _owner;
    }

    /**
     * Register a WindowListener object for this window.
     */
    public void addWindowListener(WindowListener listener_) {
        if (_windowListeners == null) _windowListeners = new Vector();

        _windowListeners.add(listener_);
    }

    /**
     * Process window events occurring on this window by dispatching them to
     * any registered WindowListener objects.
     */
    protected void processWindowEvent(WindowEvent evt_) {
        if (_windowListeners == null) return;

        Enumeration e = _windowListeners.elements();
        while (e.hasMoreElements()) {
            WindowListener wl = (WindowListener) e.nextElement();
            switch (evt_.getID()) {

            case AWTEvent.WINDOW_CLOSING:
                wl.windowClosing(evt_);
                break;

            case AWTEvent.WINDOW_OPENED:
                wl.windowOpened(evt_);
                break;
            }
        }
    }

    /**
     * Returns true if this Window is currently displayed.
     */
    public boolean isDisplayed() {
        return _term.isWindowDisplayed(this);
    }

    /**
     * Causes this Window to be sized to fit the preferred sizes and layouts of
     * its contained components.
     */
    public void pack() {
        setSize(minimumSize());
        super.doLayout(); // call method in Container superclass
    }

    /**
     * Lay out the contained components, draw the window and its contained
     * components, and then read input events off the EventQueue and send them
     * to the component that has the input focus.
     */
    public void show() {

        if (_visible) return; // This window is already visible.

        _visible = true;
        _term.addWindow(this);

        /*
         * Start the keyboard-reader thread _after_ the first window has been
         * displayed, otherwise we get a NoSuchElement exception if the user
         * starts typing before the first window is displayed, because the
         * _windowList is empty.
         */
        _term.startKeyboardReader();

        /*
         * call doLayout in Container superclass. This will lay out all of the
         * contained components (i.e. children) and their descendants, and in
         * the process will set the valid flag of all descendants.
         */
        super.doLayout(); // call method in Container superclass

        this.adjustLocation(); // ensure it fits inside the screen

        this.draw();

        /*
         * Rather than call Toolkit.sync() directly here, we put a SyncEvent
         * onto the SyncQueue. The SyncThread will read it off the SyncQueue
         * and then sleep for 50msec before putting the SyncEvent onto the
         * EventQueue, from which it will be picked up by the active Window
         * (i.e. an instance of this class). The active Window will then call
         * Toolkit.sync() directly. This slight delay speeds up the case where
         * a window opens and then immediately opens a new window (and
         * another...).
         */
        SyncQueue.getInstance().postEvent(new SyncEvent(this));

        if (_dispatchThreadRunning)
            run();
        else {
            _dispatchThreadRunning = true;
            Thread dispatchThread = new Thread(this);
            dispatchThread.setName("event dispatcher");
            dispatchThread.start();

            /*
             * If "charva.script.playback" is defined, we start up a thread for
             * playing back the script. Keys from both the script and the
             * keyboard will cause "fireKeystroke()" to be invoked. The
             * playback thread is started _after_ "addWindow()" is called for
             * the first time, to make sure that _windowList is non-empty when
             * the playback thread calls "fireKeystroke()".
             */
            startPlayback();

        }
    }

    public void run() {
        /*
         * Loop and process input events until the window closes.
         */
        try {
            EventQueue evtQueue = _term.getSystemEventQueue();
            for (_windowClosed = false; _windowClosed != true;) {

                java.util.EventObject evt = evtQueue.getNextEvent();

                /*
                 * The event object should always be an AWTEvent. If not, we
                 * will get a ClassCastException.
                 */
                processEvent((AWTEvent) evt);
            } // end FOR loop
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    /**
     * Process an event off the event queue. This method can be extended by
     * subclasses of Window to deal with application-specific events.
     */
    protected void processEvent(AWTEvent evt_) {
        Object source = evt_.getSource();

        if (evt_ instanceof AdjustmentEvent)
            ((Adjustable) source)
                    .processAdjustmentEvent((AdjustmentEvent) evt_);

⌨️ 快捷键说明

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