jndesktop.java

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

JAVA
592
字号
/*
 * $Id: JNDesktop.java,v 1.27 2004/02/28 09:20:54 epr Exp $
 */
package org.jnode.wt.desktop;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.jnode.wt.components.JNWindow;
import org.jnode.wt.decorators.JNCursor;
import org.jnode.wt.events.JNDesktopListener;
import org.jnode.wt.events.JNWindowEvent;
import org.jnode.wt.events.JNodeKeyEvent;
import org.jnode.wt.events.JNodeMouseEvent;

/**
 * @author vali
 */
public class JNDesktop extends org.jnode.wt.components.JNLayeredContainer {

    public static final Integer DESKTOP_LAYER = new Integer(1002);

    public static final Integer ACTIVE_LAYER = new Integer(1001);

    public static final Integer DESKTOP_BACKGROUND_LAYER = new Integer(999);

    // this is set to true in case we want to close..
    private boolean inClose = false;

    // the desktop listeners
    private final List desktopListeners = new ArrayList();

    private JNWindow activeWindow = null;

    private JNCursor cursor = null;

    private int lastMouseX = 0, lastMouseY = 0;

    private JNCursor lastCursor = cursor;

    //private final JNDesktopBackground bg = new JNDesktopBackground();

    public JNDesktop() {
        super();
        setBackground(Color.lightGray);
        setOpaque(true);
        startAppManager();
    }

    public void activateWindow(JNWindow window) {
        if (!window.isActive()) {
            if (this.activeWindow != null) {
                this.activeWindow.setActive(false);
                getDesktopManager().postEvent(
                        new JNWindowEvent(activeWindow,
                                JNWindowEvent.WINDOW_DEACTIVATED));
                this.moveToLayer(activeWindow, DEFAULT_LAYER);
            }
            window.setActive(true);
            this.moveToLayer(window, ACTIVE_LAYER);
            this.activeWindow = window;
            getDesktopManager().postEvent(
                    new JNWindowEvent(activeWindow,
                            JNWindowEvent.WINDOW_ACTIVATED));
        }

    }

    public synchronized void addDesktopListener(
            JNDesktopListener desktopListener) {
        desktopListeners.add(desktopListener);
    }

    public void close() {
        // post a window close event to all windows
        for (Iterator iter = this.containers.values().iterator(); iter
                .hasNext();) {
            org.jnode.wt.components.JNSimpleContainer container = (org.jnode.wt.components.JNSimpleContainer) iter
                    .next();
            for (Iterator iterator = container.getComponentList().iterator(); iterator
                    .hasNext();) {
                org.jnode.wt.components.JNComponent window = (org.jnode.wt.components.JNComponent) iterator
                        .next();
                if (window instanceof JNWindow) {
                    // post a close event
                    getDesktopManager().postEvent(
                            new JNWindowEvent((JNWindow) window,
                                    JNWindowEvent.WINDOW_CLOSING));
                }
            }
        }
        inClose = true;
    }

    protected void closed() {
        // close all desktop resources and call the desktoplisteners
        if (desktopListeners != null)
                for (Iterator iter = desktopListeners.iterator(); iter
                        .hasNext();) {
                    JNDesktopListener element = (JNDesktopListener) iter.next();
                    element.desktopClosed(this);
                }
        getDesktopManager().removeDesktop(this);
    }

    /**
     *  
     */
    public void createRandomWindow() {
        System.out.println("createRandomWindow");
        JNWindow window = new JNWindow();
        window.setSize(new Dimension(200, 100));
        window.setLocation(50, 50);
        window.setVisible(true);
    }

    /**
     * @return Returns the cursor.
     */
    public JNCursor getCursor() {
        return this.cursor;
    }

    public Graphics getGraphics() {
        return getDesktopManager().getGraphics();
    }

    protected void internallyPaint(Graphics g) {
        final JNDesktopManager mgr = getDesktopManager();
        if (mgr.testContainer != null) {
            g.clearRect(0, 0, mgr.testContainer.getBounds().width,
                    mgr.testContainer.getBounds().height);
        }
        super.internallyPaint(g);
    }

    public boolean isShowing() {
        /*
         * if(visible) { JNContainer parent = this.parent;
         * 
         * if(parent == null) { return false; }
         * 
         * return parent.isShowing();
         *  } return false;
         *  
         */

        if (inClose)
            return false;
        else
            return true;
    }

    public void processKeyEvent(JNodeKeyEvent e) {
        if (activeWindow != null) {
            activeWindow.processKeyEvent(e);
            e.consume();
        }
    }

    protected void processMouseEvent(JNodeMouseEvent event) {

        org.jnode.wt.components.JNSimpleContainer sc2 = ((org.jnode.wt.components.JNSimpleContainer) containers
                .get(org.jnode.wt.components.JNLayeredContainer.MENU_COMBO_LAYER));

        if (sc2 != null) sc2.processMouseEvent(event);

        // --------------
        JNWindow window = null;

        if (activeWindow != null) {
            if (activeWindow.getLocation().x <= event.getX()
                    && activeWindow.getLocation().y <= event.getY()
                    && (activeWindow.getLocation().x + activeWindow.getSize().width) >= event
                            .getX()
                    && (activeWindow.getLocation().y + activeWindow.getSize().height) >= event
                            .getY()) {
                window = activeWindow;
            }
        }

        if (window == null)
                for (Iterator it = containers.keySet().iterator(); it.hasNext();) {
                    org.jnode.wt.components.JNSimpleContainer c = (org.jnode.wt.components.JNSimpleContainer) containers
                            .get(it.next());
                    for (ListIterator iter = c.getComponentList()
                            .listIterator(); iter.hasNext();) {
                        org.jnode.wt.components.JNComponent component = (org.jnode.wt.components.JNComponent) iter
                                .next();
                        if (!(component instanceof JNWindow)) continue;
                        JNWindow tempWindow = (JNWindow) component;
                        if (tempWindow.getLocation().x <= event.getX()
                                && tempWindow.getLocation().y <= event.getY()
                                && (tempWindow.getLocation().x + tempWindow
                                        .getSize().width) >= event.getX()
                                && (tempWindow.getLocation().y + tempWindow
                                        .getSize().height) >= event.getY()) {
                            window = tempWindow;
                        }
                    }
                }

        if (window != null) {
            if (!window.isActive()) {
                activateWindow(window);
                // move active window at the end of the list
            }
            // kk
            event.translatePoint(window.getX(), window.getY());

            window.processMouseEvent(event);
            event.consume();

            if (window.getCursor() != null) {
                lastCursor = cursor;
                cursor = window.getCursor();
            }
        } else {

            org.jnode.wt.components.JNSimpleContainer sc = ((org.jnode.wt.components.JNSimpleContainer) containers
                    .get(DESKTOP_LAYER));
            if (sc != null) {
                sc.processMouseEvent(event);
            } else {
                //				System.out.println("JNDesktop.processmouseEvent NULL");
            }

        }
    }

    /*
     * protected void processMouseEvent(JNodeMouseEvent event) { JNWindow
     * window = null; if (activeWindow != null) { if
     * (activeWindow.getLocation().x <= event.getX() &&
     * activeWindow.getLocation().y <= event.getY() &&
     * (activeWindow.getLocation().x + activeWindow.getSize().width) >=
     * event.getX() && (activeWindow.getLocation().y +
     * activeWindow.getSize().height) >= event.getY()) { window = activeWindow; } }
     * 
     * if (window == null)
     * 
     * for (Iterator it = getLayers().iterator(); it.hasNext();) {
     * JNSimpleContainer c = (JNSimpleContainer) it.next(); for (Iterator iter =
     * c.getComponents().iterator(); iter.hasNext();) { final JNComponent
     * component = (JNComponent) iter.next(); if (component instanceof
     * JNWindow) { final JNWindow tempWindow = (JNWindow) component; final
     * Point wLoc = tempWindow.getLocation(); final Dimension wSize =
     * tempWindow.getSize(); if (wLoc.x <= event.getX() && wLoc.y <=
     * event.getY() && (wLoc.x + wSize.width) >= event.getX() && (wLoc.y +
     * wSize.height) >= event.getY()) {
     * 
     * window = tempWindow; } } } } if (window != null) { if
     * (!window.isActive()) { activateWindow(window); // move active window at
     * the end of the list } window.processMouseEvent(event); event.consume();
     * if (window.getCursor() != null) { //lastCursor = cursor; cursor =
     * window.getCursor(); } } else {
     * getLayer(DESKTOP_LAYER).processMouseEvent(event); }
     */

    protected void processMouseMotionEvent(JNodeMouseEvent event) {
        org.jnode.wt.components.JNSimpleContainer sc2 = ((org.jnode.wt.components.JNSimpleContainer) containers
                .get(org.jnode.wt.components.JNLayeredContainer.MENU_COMBO_LAYER));

        if (sc2 != null) sc2.processMouseMotionEvent(event);

        JNWindow window = null;

        if (activeWindow != null) {
            if (activeWindow.getLocation().x <= event.getX()
                    && activeWindow.getLocation().y <= event.getY()
                    && (activeWindow.getLocation().x + activeWindow.getSize().width) >= event
                            .getX()
                    && (activeWindow.getLocation().y + activeWindow.getSize().height) >= event
                            .getY()) {
                window = activeWindow;
            }
        }

        if (window == null)
                for (Iterator it = containers.keySet().iterator(); it.hasNext();) {
                    org.jnode.wt.components.JNSimpleContainer c = (org.jnode.wt.components.JNSimpleContainer) containers
                            .get(it.next());
                    for (ListIterator iter = c.getComponentList()
                            .listIterator(); iter.hasNext();) {
                        org.jnode.wt.components.JNComponent component = (org.jnode.wt.components.JNComponent) iter
                                .next();
                        if (!(component instanceof JNWindow)) continue;
                        JNWindow tempWindow = (JNWindow) component;
                        if (tempWindow.getLocation().x <= event.getX()

⌨️ 快捷键说明

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