/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Intersect Software Corporation This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.ui.swing.components; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Insets; import java.awt.Rectangle; import javax.swing.JPanel; import javax.swing.JRootPane; /** * A custom layout for a JRootPane that handles the the layout of a JRootPane's * layeredPane, glassPane, and menuBar, and in addition handles four decorative * components arranged in a border layout. Add the decorative components to the * JRootPane using the directional constants; CENTER is reserved for the content * pane and menu bar. * * @author michael@mpowers.net * @version $Revision: 904 $ */ public class BetterRootLayout extends BorderLayout { /** * Returns the amount of space the layout would like to have. * * @param the Container for which this layout manager is being used * @return a Dimension object containing the layout's preferred size * @throws ClassCastException if parent is not a JRootPane */ public Dimension preferredLayoutSize(Container parent) { JRootPane rootPane = (JRootPane) parent; JPanel proxyPanel = new JPanel(); proxyPanel.setLayout(new BorderLayout()); JPanel contentProxy = null; if (rootPane.getContentPane() != null) { contentProxy = new JPanel(); contentProxy.setMinimumSize(rootPane.getContentPane().getMinimumSize()); contentProxy.setMaximumSize(rootPane.getContentPane().getMaximumSize()); contentProxy.setPreferredSize(rootPane.getContentPane().getPreferredSize()); proxyPanel.add(contentProxy, CENTER); } JPanel menuProxy = null; if (rootPane.getJMenuBar() != null) { menuProxy = new JPanel(); menuProxy.setMinimumSize(rootPane.getJMenuBar().getMinimumSize()); menuProxy.setMaximumSize(rootPane.getJMenuBar().getMaximumSize()); menuProxy.setPreferredSize(rootPane.getJMenuBar().getPreferredSize()); proxyPanel.add(menuProxy, NORTH); } this.addLayoutComponent(proxyPanel, CENTER); Dimension result = super.preferredLayoutSize(parent); this.removeLayoutComponent(proxyPanel); proxyPanel.removeAll(); return result; } /** * Returns the minimum amount of space the layout needs. * * @param the Container for which this layout manager is being used * @return a Dimension object containing the layout's minimum size * @throws ClassCastException if parent is not a JRootPane */ public Dimension minimumLayoutSize(Container parent) { JRootPane rootPane = (JRootPane) parent; JPanel proxyPanel = new JPanel(); proxyPanel.setLayout(new BorderLayout()); JPanel contentProxy = null; if (rootPane.getContentPane() != null) { contentProxy = new JPanel(); contentProxy.setMinimumSize(rootPane.getContentPane().getMinimumSize()); contentProxy.setMaximumSize(rootPane.getContentPane().getMaximumSize()); contentProxy.setPreferredSize(rootPane.getContentPane().getPreferredSize()); proxyPanel.add(contentProxy, CENTER); } JPanel menuProxy = null; if (rootPane.getJMenuBar() != null) { menuProxy = new JPanel(); menuProxy.setMinimumSize(rootPane.getJMenuBar().getMinimumSize()); menuProxy.setMaximumSize(rootPane.getJMenuBar().getMaximumSize()); menuProxy.setPreferredSize(rootPane.getJMenuBar().getPreferredSize()); proxyPanel.add(menuProxy, NORTH); } this.addLayoutComponent(proxyPanel, CENTER); Dimension result = super.minimumLayoutSize(parent); this.removeLayoutComponent(proxyPanel); proxyPanel.removeAll(); return result; } /** * Returns the maximum amount of space the layout can use. * * @param the Container for which this layout manager is being used * @return a Dimension object containing the layout's maximum size * @throws ClassCastException if parent is not a JRootPane */ public Dimension maximumLayoutSize(Container target) { JRootPane rootPane = (JRootPane) target; JPanel proxyPanel = new JPanel(); proxyPanel.setLayout(new BorderLayout()); JPanel contentProxy = null; if (rootPane.getContentPane() != null) { contentProxy = new JPanel(); contentProxy.setMinimumSize(rootPane.getContentPane().getMinimumSize()); contentProxy.setMaximumSize(rootPane.getContentPane().getMaximumSize()); contentProxy.setPreferredSize(rootPane.getContentPane().getPreferredSize()); proxyPanel.add(contentProxy, CENTER); } JPanel menuProxy = null; if (rootPane.getJMenuBar() != null) { menuProxy = new JPanel(); menuProxy.setMinimumSize(rootPane.getJMenuBar().getMinimumSize()); menuProxy.setMaximumSize(rootPane.getJMenuBar().getMaximumSize()); menuProxy.setPreferredSize(rootPane.getJMenuBar().getPreferredSize()); proxyPanel.add(menuProxy, NORTH); } this.addLayoutComponent(proxyPanel, CENTER); Dimension result = super.maximumLayoutSize(target); this.removeLayoutComponent(proxyPanel); proxyPanel.removeAll(); return result; } /** * Instructs the layout manager to perform the layout for the specified * container. * * @param the Container for which this layout manager is being used * @throws ClassCastException if parent is not a JRootPane */ public void layoutContainer(Container parent) { JRootPane rootPane = (JRootPane) parent; Rectangle b = parent.getBounds(); Insets i = rootPane.getInsets(); int w = b.width - i.right - i.left; int h = b.height - i.top - i.bottom; // layout panes if (rootPane.getLayeredPane() != null) { rootPane.getLayeredPane().setBounds(i.left, i.top, w, h); } if (rootPane.getGlassPane() != null) { rootPane.getGlassPane().setBounds(i.left, i.top, w, h); } // handle proxy panel JPanel proxyPanel = new JPanel(); proxyPanel.setLayout(new BorderLayout()); this.addLayoutComponent(proxyPanel, CENTER); super.layoutContainer(parent); // use proxy sizes to set sizes of layeredPane's children Rectangle proxyRect = proxyPanel.getBounds(); if (rootPane.getJMenuBar() != null) { Rectangle menuRect = proxyPanel.getBounds(); menuRect.height = rootPane.getJMenuBar().getPreferredSize().height; rootPane.getJMenuBar().setBounds(menuRect); proxyRect.y += menuRect.height; proxyRect.height -= menuRect.height; } if (rootPane.getContentPane() != null) { rootPane.getContentPane().setBounds(proxyRect); } this.removeLayoutComponent(proxyPanel); proxyPanel.removeAll(); } /** * Passes NORTH, SOUTH, EAST, WEST and CENTER to super implementation, and * ignores all others. */ public void addLayoutComponent(Component comp, Object constraints) { if (NORTH.equals(constraints)) { super.addLayoutComponent(comp, constraints); } else if (SOUTH.equals(constraints)) { super.addLayoutComponent(comp, constraints); } else if (EAST.equals(constraints)) { super.addLayoutComponent(comp, constraints); } else if (WEST.equals(constraints)) { super.addLayoutComponent(comp, constraints); } else if (CENTER.equals(constraints)) { super.addLayoutComponent(comp, constraints); } // otherwise, ignore } }