📄 dockwrapper.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/dock/DockWrapper.java,v $// $RCSfile: DockWrapper.java,v $// $Revision: 1.5.2.2 $// $Date: 2005/08/09 17:59:30 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.gui.dock;import java.awt.Cursor;import java.awt.Insets;import java.awt.Color;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.event.*;import javax.swing.plaf.ButtonUI;import java.util.ArrayList;import java.util.List;import java.util.Iterator;import com.bbn.openmap.util.Debug;/** * A panel that contains controls that will either be docked, * internal-framed or external framed... * * @author Ben Lubin * @version $Revision: 1.5.2.2 $ on $Date: 2005/08/09 17:59:30 $ * @since 12/5/02 */public class DockWrapper extends JPanel { /* package */static final int UNDEF = -1; /* package */static final int EXTERNAL = 1; /* package */static final int INTERNAL = 2; /* package */static final int DOCK_NORTH = 3; /* package */static final int DOCK_SOUTH = 4; /* package */static final int DOCK_WEST = 5; /* package */static final int DOCK_EAST = 6; /** Is this wrapper currently resizable? */ private boolean resizable = false; /** Which docking state we are in */ private int state = UNDEF; /** are we transparent? */ private boolean transparent = false; /** Holds the tabbed pane if we are currently holding tabs. */ private JTabbedPane tabPane = null; BasicDockPanel dockPanel; /** Contents of this wrapper, size >1 if tabbed, 1 if not tabbed. */ private List children = new ArrayList(1); /** Other Wrappers that have been docked onto this one. */ private List dockedWrappers = new ArrayList(0); private MouseHandler mouseHandler = new MouseHandler(); public DockWrapper(BasicDockPanel dp) { dockPanel = dp; setLayout(new BorderLayout()); setOpaque(false); } /** Special constructor for use in the cardinal DockWrappers */ /* package */DockWrapper(BasicDockPanel dp, int state) { this(dp); this.state = state; } //Accessor Methods: /////////////////// public void addChild(JComponent child) { children.add(child); if (isTabbed()) { String tabName = dockPanel.getConstraint(child).getTabName(); if (tabName == null) { tabName = child.getName(); } tabPane.insertTab(tabName, null, child, null, 0); } else { add(child, BorderLayout.CENTER); } } public void removeChild(JComponent child) { if (children.size() < 1) { Debug.error("DockWrapper: Unexpected children list"); } if (isTabbed()) { tabPane.remove(child); } else { remove(child); } children.remove(child); } /** * Get all of the children that we are holding. The returned list * will have one element iff the DockWrapper is not tabbed. * * @return a list of JComponents that are the children. */ public List getChildren() { return children; } /** * Get the one and only child if we are not tabbed. If we are * tabbed, there may be more than one child... */ public JComponent getChild() { if (children.size() == 0) return null; return (JComponent) children.get(0); } //Tabbing Methods: ////////////////// /* package */void setTabName(JComponent child, String name) { if (isTabbed()) { tabPane.setTitleAt(tabPane.indexOfComponent(child), name); } } /** Returns true iff this dockable contains more than one component */ public boolean isTabbed() { return tabPane != null; } /** * Get the index of the dockwrapper that we should use for * tabbing-up, or -1 if there is none. */ public int getDockedWrapperIndexForTabbing() { int idx = 0; for (Iterator iter = getDockedWrappers().iterator(); iter.hasNext();) { DockWrapper dw = (DockWrapper) iter.next(); if (dw.canTab()) { return idx; } idx++; } return -1; } private void tab(DockWrapper w) { //Get rid of the existing wrapper: dockPanel.removeWrapper(w); if (children.size() == 1) { //Create the tab pane: JComponent child = getChild(); removeChild(child); tabPane = new JTabbedPane(JTabbedPane.BOTTOM); add(tabPane, BorderLayout.CENTER); setOpaque(true); addChild(child); } for (Iterator iter = w.getChildren().iterator(); iter.hasNext();) { JComponent child = (JComponent) iter.next(); dockPanel.setWrapper(child, this); addChild(child); } }// private void untab(JComponent child) {// if (child == null) {// throw new RuntimeException("Can't untab null");// }// removeChild(child);// if (children.size() == 1) {// JComponent curChild = getChild();// removeChild(curChild);// remove(tabPane);// tabPane = null;// addChild(curChild);// setOpaque(false);// }// DockWrapper dw = dockPanel.createDockWrapper(child);// } //Transparency Methods: /////////////////////// public void doLayout() { updateTransparency(); super.doLayout(); } /** * Set the indicated JComponent to transparent or not transparent. * * @return true iff this call has changed the state */ protected static boolean setTransparent(JComponent child, boolean t) { boolean ret = false; if (child instanceof JPanel) { child.setOpaque(!t); ret |= child.isOpaque() == t; } if (child instanceof JToolBar) { child.setOpaque(!t); ret |= child.isOpaque() == t; } if (child instanceof AbstractButton) { AbstractButton b = (AbstractButton) child; if (t) { if (!(b.getUI() instanceof TransparentButtonUI)) { b.setContentAreaFilled(false); b.setUI((ButtonUI) TransparentButtonUI.createUI(b)); ret = true; } } else { if (b.getUI() instanceof TransparentButtonUI) { b.setContentAreaFilled(true); b.setUI((ButtonUI) UIManager.getUI(b)); ret = true; } } } for (int i = 0; i < child.getComponentCount(); i++) { Object o = child.getComponent(i); if (o instanceof JComponent) { JComponent c = (JComponent) o; ret |= setTransparent(c, t); } } return ret; } //Resizing Methods: /////////////////// protected void makeNotResizable() { setBorder(null); removeMouseListener(mouseHandler); removeMouseMotionListener(mouseHandler); } protected void makeResizable() { setBorder(makeResizeBorder()); addMouseListener(mouseHandler); addMouseMotionListener(mouseHandler); } /** get the border that mouse is in */ protected int inBorder(int x, int y) { Border b = getBorder(); if (b == null) { return UNDEF; } Insets i = b.getBorderInsets(this); if (x <= i.left) { return DOCK_WEST; } if (x >= getWidth() - i.right) { return DOCK_EAST; } if (y <= i.top) { return DOCK_NORTH; } if (y >= getHeight() - i.bottom) { return DOCK_SOUTH; } return UNDEF; } protected Border makeResizeBorder() { Color highlightOuter = UIManager.getColor("controlLtHighlight"); Color highlightInner = UIManager.getColor("controlHighlight"); Color shadowOuter = UIManager.getColor("controlDkShadow"); Color shadowInner = UIManager.getColor("controlShadow"); if (transparent) { highlightOuter = new Color(0, 0, 0, 50); highlightInner = new Color(0, 0, 0, 75); shadowOuter = new Color(0, 0, 0, 175); shadowInner = new Color(0, 0, 0, 150); } Border border = BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, highlightOuter, highlightInner, shadowOuter, shadowInner), BorderFactory.createBevelBorder(BevelBorder.LOWERED, highlightOuter, highlightInner, shadowOuter, shadowInner)); return border; } //Constraint methods: ///////////////////// /** * Set the transparency of this DockWrapper to whatever the * Constraint says it should be. * * @return true iff the state has changed. */ public boolean updateTransparency() { boolean ret = false; if (children.size() == 1) { boolean t = dockPanel.getConstraint(getChild()).canTransparent(); transparent = t; ret |= setTransparent(getChild(), t); } else { transparent = false; for (Iterator iter = children.iterator(); iter.hasNext();) { JComponent c = (JComponent) iter.next(); ret |= setTransparent(c, transparent); } } return ret; } /** Make the dock wrapper's border reflect its resizability */ public void updateResizable() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -