⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dockablewindow.java

📁 一款即时通讯软件
💻 JAVA
字号:
package edu.ou.kmi.buddyspace.utils;

/*
 * DockableWindow.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 23 October 2002, 13:20
 */

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * <code>DockableWindow</code> is window which can be docked or stand-alone.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public abstract class DockableWindow extends JComponent {

    protected JFrame frame = null;
    protected Container contentPane = null;
    protected boolean docked = false;
    protected Image icon = null;
    protected String title = "Docking Win";
    protected String ID = "id";
    protected static Dimension DEFAULT_SIZE = new Dimension(300, 400);
    protected Dimension viewSize;
    protected WinManager winMan = null;
    protected boolean select = true;
    protected boolean moveToBack = false;


    public DockableWindow(String ID, String title, boolean docked,
                                                        WinManager winMan) {
        this(ID, title, null, DEFAULT_SIZE, docked, winMan);
    }


    public DockableWindow(String ID, String title, Image icon, boolean docked,
                                                        WinManager winMan) {
        this(ID, title, icon, DEFAULT_SIZE, docked, winMan);
    }


    /**
     * Main constructor...
     */
    public DockableWindow(String ID, String title, Image icon, Dimension viewSize,
                                        boolean docked, WinManager winMan) {

        this.ID = ID;
        this.title = title;
        this.icon = icon;
        this.viewSize = viewSize;
        this.winMan = winMan;

        frame = new JFrame();

        contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(this, BorderLayout.CENTER);

        frame.setTitle(title);
        frame.setSize(viewSize);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                close();
            }
            public void windowActivated(WindowEvent e) {
                DockableWindow.this.doLayout();
                DockableWindow.this.revalidate();
                if (!DockableWindow.this.docked) {
                    frame.doLayout();
                    frame.invalidate();
                }
            }
            public void windowOpened(WindowEvent e) {
                DockableWindow.this.doLayout();
                DockableWindow.this.revalidate();
                if (!DockableWindow.this.docked) {
                    frame.doLayout();
                    frame.invalidate();
                }
            }
        });
        frame.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                DockableWindow.this.viewSize = frame.getSize();
            }
        });

        setIcon(icon);
        this.docked = docked;
        //setDocked(docked, false);
    }


    public void setIcon(Image icon) {
        this.icon = icon;
        if (icon != null)
            frame.setIconImage(icon);
    }


    public void setTitle(String title) {
        this.title = title;
        if (title != null)
            frame.setTitle(title);
    }


    public String getTitle() {
        return title;
    }


    public JFrame getFrame() {
        return frame;
    }


    public String getID() {
        return ID;
    }


    /** Sets if the window will automatically popup */
    public void setAutoSelect(boolean select) {
        this.select = select;
    }


    public boolean isVisible() {
        return super.isVisible() && (docked || frame.isVisible());
    }


    public boolean isDocked() {
        return docked;
    }


    public void setDocked(boolean docked) {
        setDocked(docked, select);
    }

    public void setDocked(boolean docked, boolean select) {
        this.docked = docked;

    /**    if (!docked)
        {
            JFrame f = new JFrame();
            f.getContentPane().setLayout(new BorderLayout());
            f.getContentPane().add(new JButton("JHJH"), BorderLayout.NORTH);
            f.getContentPane().add(this, BorderLayout.CENTER);
            f.setSize(800, 600);
            f.setVisible(true);
            return;
        }*/

        setPreferredSize(new Dimension(contentPane.getWidth(),
                                       contentPane.getHeight()));

        if (!docked) {
            if (!contentPane.isAncestorOf(this)) {
                contentPane.add(this, BorderLayout.CENTER);
            }
        }

        //frame.setSize(viewSize);
        if (frame.isVisible() == docked) {
            if (!select && !docked) {
                //moveToBack = true;
                frame.setState(Frame.ICONIFIED);
            }
            frame.setVisible(!docked);
            /*if (!select && !docked)
                frame.toBack();*/
        }

    }


    public void dockAsTab(JTabbedPane tabbedPane) {
        setDocked(true);

        if (tabbedPane == null || -1 != tabbedPane.indexOfComponent(this))
            return;

        if (icon != null)
            tabbedPane.addTab(title, new ImageIcon(icon), this);
        else
            tabbedPane.addTab(title, this);
    }


    public void showWindow(boolean show) {
        showWindow(show, select);
    }


    public void showWindow(boolean show, boolean select) {
        if (!docked) {
            if (!select && show && !frame.isVisible()) {
                //moveToBack = true;
                frame.setState(Frame.ICONIFIED);
            }
            frame.setVisible(show);
            /*if (!select && show && moveToBack)
                frame.toBack();*/
        }
        //setVisible(show);
    }


    public void toFront() {
        if (!docked) {
            frame.setState(JFrame.NORMAL);
            frame.toFront();
        }
        else
            requestFocus();
    }


    public void close() {
        if (winMan != null)
            winMan.closeWindow(this);
        else {
            prepareOnClose();
            showWindow(false);
        }
    }


    public void prepareOnClose() {
        frame.setVisible(false);
        cancelListening();
    }


    /** Removes itself from listening */
    abstract public void cancelListening();


    /** Updates LookAndFeel */
    public void updateLAF() {
        if (docked)
            SwingUtilities.updateComponentTreeUI(this);
        else
            SwingUtilities.updateComponentTreeUI(frame);
    }


    public int getFrameX() {
        return (frame != null)? frame.getX() : 0;
    }


    public int getFrameY() {
        return (frame != null)? frame.getY() : 0;
    }


    public int getFrameWidth() {
        return viewSize.width;
    }


    public int getFrameHeight() {
        return viewSize.height;
    }


    public void setFrameSize(int width, int height) {
        viewSize = new Dimension(width, height);
        if (frame == null) return;
        frame.setSize(viewSize);
        doLayout();
        revalidate();
        frame.doLayout();
        frame.invalidate();
    }


    public void setFrameLocation(int x, int y) {
        if (frame == null) return;
        frame.setLocation(x, y);
        doLayout();
        revalidate();
        frame.doLayout();
        frame.invalidate();
    }

}

⌨️ 快捷键说明

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