jnwindow.java

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

JAVA
350
字号
package org.jnode.wt.components;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.peer.WindowPeer;

import org.jnode.wt.decorators.BorderDecorator;
import org.jnode.wt.decorators.JNBorderFactory;
import org.jnode.wt.desktop.JNDesktop;
import org.jnode.wt.desktop.JNDesktopManager;
import org.jnode.wt.desktop.JNodeEvent;
import org.jnode.wt.events.JNWindowEvent;
import org.jnode.wt.events.JNodeMouseEvent;

/**
 * @author vali
 *
 * To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JNWindow extends org.jnode.wt.components.JNSimpleContainer implements WindowPeer{
    // Color constants
    public static Color COLOR_ACTVE = Color.decode("0x7B68EE");
    public static Color COLOR_INACTVE = Color.decode("0xB0C4DE");
    public static Color DEFAULT_PANEL_BACKGROUND = Color.decode("0xFAEBD7");

    // status flags    c
    public boolean inRelocateMode = false;
    public boolean inResizeMode = false;

    // size constants
    public static int TITLEBAR_HEIGHT = 20;

    private boolean titleBarFlag = true;

    // panels
    private JNTitleBar titleBar = null;

    private JNPanel defaultPanel = new JNPanel();/* {

        public void recalculate() {
            final JNContainer parent = getParent();

            if (parent instanceof JNSimpleContainer && ((JNSimpleContainer) parent).getBorder() != null) {
                setSize(
                        (parent.getSize().width - ((JNSimpleContainer) parent).getInsets().left) - ((JNSimpleContainer) parent).getInsets().right,
                        parent.getSize().height - ((JNSimpleContainer) parent).getInsets().top - ((JNSimpleContainer) parent).getInsets().bottom);
            } else {
                this.setSize(new Dimension(parent.getSize().width, getSize().height));
            }
        }
    };*/


    public void recalculate() {
        super.recalculate();
        final BorderDecorator border = getBorder();
        int x = 0;
        int y = 0;
        int w = getWidth();
        int h = getHeight();
        if (border != null) {
            x += border.getLeftDepth();
            y += border.getTopDepth();
            w -= (border.getLeftDepth() + border.getRightDepth());
            h -= (border.getTopDepth() + border.getBottomDepth());
        }
        if (titleBar != null) {
            titleBar.setSize(w, TITLEBAR_HEIGHT);
            titleBar.setLocation(x, y);
            titleBar.recalculate();
            y += TITLEBAR_HEIGHT;
            h -= TITLEBAR_HEIGHT;
        }
        defaultPanel.setSize(w, h);
        defaultPanel.setLocation(x, y);
        defaultPanel.recalculate();
    }


    private boolean active = false;


    public JNWindow() {
        this(true);
    }

    public JNWindow(boolean hasTitleBar) {
        super();

        this.titleBarFlag = hasTitleBar;
        this.setBorder(JNBorderFactory.getBorderDecorator(JNBorderFactory.SINGLE_LINE_BORDER));
        setParent(JNDesktopManager.getDesktopManager().getCurrentDesktop());
        if (hasTitleBar) {
            titleBar = new JNTitleBar(this);
            super.add(titleBar, 0);
            defaultPanel.setBackground(DEFAULT_PANEL_BACKGROUND);
            defaultPanel.setLocation(0, 0);
            defaultPanel.recalculate();
            super.add(defaultPanel, 1);
        } else {
            defaultPanel.setBackground(DEFAULT_PANEL_BACKGROUND);
            super.add(defaultPanel, 0);
        }

    }

    public org.jnode.wt.components.JNComponent add(org.jnode.wt.components.JNComponent comp) {
        return getDefaultPanel().add(comp);
    }

    public org.jnode.wt.components.JNComponent add(org.jnode.wt.components.JNComponent comp, int index) {
        return getDefaultPanel().add(comp, index);
    }

/*    private JNPanel createTitleBar() {
        JNPanel bar = new JNPanel() {
            public void processMouseEvent(JNodeMouseEvent event) {
                // treat the titlebar mouse events

                if (event.getID() == JNodeMouseEvent.MOUSE_PRESSED && this.getAbsBounds().contains(event.getX(), event.getY())) {
                    ((JNWindow) this.getParent()).inRelocateMode = true;
                } else if (event.getID() == JNodeMouseEvent.MOUSE_RELEASED && this.getAbsBounds().contains(event.getX(), event.getY())) {
                    ((JNWindow) this.getParent()).inRelocateMode = false;
                }
                super.processMouseEvent(event);
            }

            public Point getAbsLocation() {
                org.jnode.wt.components.JNComponent component = this;
                int x = 0, y = 0;
                do {
                    if (component instanceof org.jnode.wt.components.JNSimpleContainer) {
                        x += component.getLocation().x;
                        y += component.getLocation().y;
                    } else {
                        x += component.getLocation().x;
                        y += component.getLocation().y;
                    }
                    component = component.getParent();
                } while (component != null);
                final org.jnode.wt.components.JNContainer parent = getParent();
                if (parent instanceof org.jnode.wt.components.JNSimpleContainer) {
                    if (((org.jnode.wt.components.JNSimpleContainer) parent).getBorder() != null) {
                        x += ((org.jnode.wt.components.JNSimpleContainer) parent).getBorder().getLeftDepth();
                        y += ((org.jnode.wt.components.JNSimpleContainer) parent).getBorder().getTopDepth();
                    }
                }
                return new Point(x, y);
            }

            public void recalculate() {

                final org.jnode.wt.components.JNContainer parent = getParent();
                int width = getSize().width;
                if (parent != null && parent instanceof org.jnode.wt.components.JNSimpleContainer) {
                    width =
                            (getParent().getSize().width - ((org.jnode.wt.components.JNSimpleContainer) getParent()).getBorder().getLeftDepth())
                            - ((org.jnode.wt.components.JNSimpleContainer) getParent()).getBorder().getRightDepth();
                }
                setSize(width, TITLEBAR_HEIGHT);
            }

        };
        bar.setLocation(0, 0);
        bar.setBorder(JNBorderFactory.getBorderDecorator(JNBorderFactory.SINGLE_LINE_BORDER));
        bar.getBorder().setBaseCollor(Color.decode("0xB0C4DE"));
        bar.setBackground(JNWindow.COLOR_ACTVE);
        JNButton closeButton = new JNButton("") {
            protected void processMouseEvent(JNodeMouseEvent event) {
                if (event.getID() == JNodeMouseEvent.MOUSE_CLICKED) {
                    super.processMouseEvent(event);
                    getDesktopManager().postEvent(new JNWindowEvent(JNWindow.this, JNWindowEvent.WINDOW_CLOSING));
                } else
                    super.processMouseEvent(event);
            }

            public void recalculate() {
                super.recalculate();
                if (getParent() != null)
                    this.setLocation((getParent().getSize().width - 10) - 3, 3);
            }
        };
        closeButton.setSize(new Dimension(10, 10));
        bar.add(closeButton);

        return bar;
    }
*/
    /**
     * @return Returns the defaultPanel.
     */
    public JNPanel getDefaultPanel() {
        return defaultPanel;
    }

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

    public Insets getInsets() {
        if (getBorder() != null) {
            int topDepth = getBorder().getTopDepth() + titleBar.getSize().height;

            if (titleBar != null) {
                topDepth += (titleBar.getInsets().top + titleBar.getInsets().bottom);
            }

            return new Insets(topDepth, getBorder().getLeftDepth(), getBorder().getBottomDepth(), getBorder().getRightDepth());
        } else {
            return new Insets(0, 0, 0, 0);
        }
    }

    public JNDesktop getParentDesktop() {
        org.jnode.wt.components.JNComponent p = getParent();
        while (!(p instanceof JNDesktop)) {
            p = p.getParent();
        }
        return (JNDesktop) p;
    }

    /**
     * @return Returns the active.
     */
    public boolean isActive() {
        return this.active;
    }

    /**
     * @return Returns the hasTitleBar.
     */
    public boolean isTitleBarOn() {
        return this.titleBarFlag;
    }

    public void setLocation(int x, int y) {
        Rectangle oldBounds = getBounds();
        super.setLocation(x, y);
        //overriden to notify subscribers
        getParentDesktop().windowMoved(this, oldBounds);
    }


    protected void processEvent(JNodeEvent e) {
        if (e instanceof JNWindowEvent)
            processWindowEvent((JNWindowEvent) e);
        else
            super.processEvent(e);
    }

    public void processMouseEvent(JNodeMouseEvent event) {
//	System.out.println("\n--- JNWindow event at= "+event.getX()+","+ event.getY());

        super.processMouseEvent(event);

//	System.out.println("+++ JNWindow event at= "+event.getX()+","+ event.getY());
    }


    public void processWindowEvent(JNWindowEvent event) {
        if (titleBarFlag && event.getID() == JNWindowEvent.WINDOW_ACTIVATED) {
            titleBar.setBackground(JNWindow.COLOR_ACTVE);
            this.update(getGraphics());
        } else if (titleBarFlag && event.getID() == JNWindowEvent.WINDOW_DEACTIVATED) {
            titleBar.setBackground(JNWindow.COLOR_INACTVE);
            this.update(getGraphics());
        } else if (event.getID() == JNWindowEvent.WINDOW_CLOSING) {
            // remove the window
            getDesktopManager().postEvent(new JNWindowEvent(this, JNWindowEvent.WINDOW_CLOSED));
        } else if (event.getID() == JNWindowEvent.WINDOW_CLOSED) {
            // call the desktop t handle this
            this.getParentDesktop().processWindowEvent(event);
        }
    }

    public void remove(int i) {
        getDefaultPanel().remove(i);
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public void setBackground(Color c) {
        super.setBackground(c);

        defaultPanel.setBackground(c);
    }

    /**
     * @param defaultPanel
     *            The defaultPanel to set.
     */
    public void setDefaultPanel(JNPanel defaultPanel) {
        this.defaultPanel = defaultPanel;
    }

    public void setForeground(Color c) {
        super.setForeground(c);

        defaultPanel.setForeground(c);
    }

    /**
     * @param hasTitleBar
     *            The hasTitleBar to set.
     */
    public void setTitleBarFlag(boolean hasTitleBar) {
        this.titleBarFlag = hasTitleBar;
    }

    public void setVisible(boolean visible) {
        if (getDesktopManager().getCurrentDesktop() == null) {
            getDesktopManager().addDesktop();//a hack
        }

        getDesktopManager().getCurrentDesktop().registerWindow(this);
        repaint();
    }

    /**
     * @return Returns the title.
     */
    public String getTitle() {
        if (titleBar != null)
            return titleBar.getTitle();
        else
            return null;
    }

    /**
     * @param title
     *            The title to set.
     */
    public void setTitle(String title) {
        if (titleBar != null)
            titleBar.setTitle(title);
    }

    public void toBack() {
        //TODO: implement it
    }

    public void toFront() {
        //TODO: implement it
    }
}

⌨️ 快捷键说明

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