📄 windowsupport.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/gui/WindowSupport.java,v $// $RCSfile: WindowSupport.java,v $// $Revision: 1.14.2.9 $// $Date: 2007/03/08 19:22:19 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.gui;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Frame;import java.awt.Point;import java.awt.Toolkit;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.util.Iterator;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JInternalFrame;import javax.swing.JLayeredPane;import com.bbn.openmap.Environment;import com.bbn.openmap.event.ListenerSupport;import com.bbn.openmap.util.Debug;/** * The WindowSupport class provides support for managing JFrames or * JInternalFrames for other components. The frame is disposed of when the * window is closed, and recreated when displayInWindow is called. The * WindowSupport remembers size and location changes for the window when it is * recreated. * <p> * * The WindowSupport class now has inner classes that are used to create * different types of windows for components. Dlg is used for JDialogs, which * remain on top of the main application frame even when they don't have focus. * IntrnlFrm is used for JInternalFrames, for windows that get clipped if the * move off the area of the main application. Frm is used for a standard JFrame * which can be obscured by the main application window. Components are free to * specify which WSDisplay type they always want to be display in, or they can * ask for a standard WindowSupport object which will use the static * defaultWindowSupportDisplayType variable to determine the WSDisplay type (for * a consistent feel across the application). The default setting for this * variable is to use the Frm type. */public class WindowSupport extends ListenerSupport implements ComponentListener, ActionListener { protected Component content; protected String title; protected Point componentLocation; protected Dimension componentSize; public final static String DisplayWindowCmd = "displayWindowCmd"; public final static String KillWindowCmd = "killWindowCmd"; protected transient WSDisplay display; protected static Class defaultWindowSupportDisplayType = Frm.class; /** * Create the window support. * * @param content the content to display in the window. * @param windowTitle the title of the window. */ public WindowSupport(Component content, String windowTitle) { super(content); setContent(content); this.title = windowTitle; } public WindowSupport(Component content, WSDisplay display) { super(content); setContent(content); setDisplay(display); if (display != null) { this.title = display.getTitle(); } } /** * Set the location of the window. */ public void setComponentLocation(Point p) { componentLocation = p; } /** * Get the location of the window. */ public Point getComponentLocation() { return componentLocation; } /** * Set the size of the window. */ public void setComponentSize(Dimension dim) { componentSize = dim; } /** * Get the size of the window. */ public Dimension getComponentSize() { return componentSize; } /** * ComponentListener method, new size is noted. */ public void componentResized(ComponentEvent e) { Component source = (Component) e.getSource(); setComponentSize(source.getSize()); Iterator it = iterator(); while (it.hasNext()) { ((ComponentListener) it.next()).componentResized(e); } } /** * ComponentListener method, new location is noted. */ public void componentMoved(ComponentEvent e) { setComponentLocation(((Component) e.getSource()).getLocation()); Iterator it = iterator(); while (it.hasNext()) { ((ComponentListener) it.next()).componentMoved(e); } } /** * ComponentListener method. */ public void componentShown(ComponentEvent e) { Iterator it = iterator(); while (it.hasNext()) { ((ComponentListener) it.next()).componentShown(e); } } /** * ComponentListener method. WindowSupport kills the window when it is * hidden. */ public void componentHidden(ComponentEvent e) { Iterator it = iterator(); while (it.hasNext()) { ((ComponentListener) it.next()).componentHidden(e); } // We need to do this after componentHidden notifications, // otherwise the component never finds out it's been hidden, // it gets removed as a ComponentListener at cleanup. Component source = (Component) e.getSource(); if (display == source) { cleanUp(); } } public void actionPerformed(ActionEvent ae) { String command = ae.getActionCommand(); if (command == KillWindowCmd) { killWindow(); } else if (command == DisplayWindowCmd) { displayInWindow(); } } protected void finalize() { if (Debug.debugging("gc")) { Debug.output("WindowSupport being gc'd"); } } /** * Sets the title of the JInternalFrame/JDialog. */ public void setTitle(String tle) { title = tle; if (display != null) { display.setTitle(tle); } } public String getTitle() { return title; } /** * Sets the content in the JInternalFrame/JDialog. */ public void setContent(Component comp) { if (content instanceof ComponentListener) { removeComponentListener((ComponentListener) content); } content = comp; if (display != null) { display.setContent(comp); } if (content instanceof ComponentListener) { addComponentListener((ComponentListener) content); } } public Component getContent() { return content; } protected int maxHeight = -1; protected int maxWidth = -1; /** * Sets the maximum pixel size of the window. If you don't care about a * particular dimension, set it to be less than zero and the natural size of * the content will be displayed. */ public void setMaxSize(int width, int height) { maxHeight = height; maxWidth = width; } /** * Called when a component hasn't specified what kind of window they want. * If the Environment.useInternalFrames flag isn't set, then the * getDefaultWindowSupportDisplayType() method is called to find out which * WSDisplay type class should be created for the component. IF that returns * null, a Frm is created. * * @param owner * @return WSDisplay */ protected WSDisplay createDisplay(Frame owner) { WSDisplay wsd; if (persistentDisplayType == null && Environment.getBoolean(Environment.UseInternalFrames)) { wsd = new IntrnlFrm(title); } else { Class wTypeClass = persistentDisplayType == null ? getDefaultWindowSupportDisplayType() : persistentDisplayType; if (wTypeClass == Dlg.class) { wsd = new Dlg(owner, title); } else if (wTypeClass == IntrnlFrm.class) { wsd = new IntrnlFrm(title); } else { wsd = new Frm(title); } } setDisplay(wsd); return wsd; } public static Class getDefaultWindowSupportDisplayType() { return defaultWindowSupportDisplayType; } public static void setDefaultWindowSupportDisplayType( Class defaultWindowSupportDisplayType) { WindowSupport.defaultWindowSupportDisplayType = defaultWindowSupportDisplayType; } public void setDisplay(WSDisplay dis) { if (display != null) { display.removeComponentListener(this); } display = dis; if (display != null) { display.addComponentListener(this); display.setContent(modifyContent(content)); } } /** * Subclass method to allow modifications to content, wrappers, etc. This * version just returns comp. */ public Component modifyContent(Component comp) { return comp; } public WSDisplay getDisplay() { return display; } /** * Display the window, and find out what the natural or revised size and * location are for the window. */ public void displayInWindow() { displayInWindow(null); } /** * Display the window, and find out what the natural or revised size and * location are for the window. * * @param owner Frame for JDialog */ public void displayInWindow(Frame owner) { Dimension dim = getComponentSize(); if (dim != null) { content.setSize(dim); } // -1 is a flag for the positioning code to recenter the // -window on the owner if it's not null, for JDialogs. displayInWindow(owner, -1, -1, -1, -1); } /** * Display the window. * * @param x the horizontal pixel location for the window. * @param y the vertical pixel location for the window.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -