Coverage Report - net.wotonomy.ui.swing.components.BetterRootLayout
 
Classes in this File Line Coverage Branch Coverage Complexity
BetterRootLayout
0% 
0% 
4
 
 1  
 /*
 2  
 Wotonomy: OpenStep design patterns for pure Java applications.
 3  
 Copyright (C) 2000 Intersect Software Corporation
 4  
 
 5  
 This library is free software; you can redistribute it and/or
 6  
 modify it under the terms of the GNU Lesser General Public
 7  
 License as published by the Free Software Foundation; either
 8  
 version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
 This library is distributed in the hope that it will be useful,
 11  
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 Lesser General Public License for more details.
 14  
 
 15  
 You should have received a copy of the GNU Lesser General Public
 16  
 License along with this library; if not, see http://www.gnu.org
 17  
 */
 18  
 
 19  
 package net.wotonomy.ui.swing.components;
 20  
 
 21  
 import java.awt.BorderLayout;
 22  
 import java.awt.Component;
 23  
 import java.awt.Container;
 24  
 import java.awt.Dimension;
 25  
 import java.awt.Insets;
 26  
 import java.awt.Rectangle;
 27  
 
 28  
 import javax.swing.JPanel;
 29  
 import javax.swing.JRootPane;
 30  
 
 31  
 /** 
 32  
 * A custom layout for a JRootPane that handles the the layout of a 
 33  
 * JRootPane's layeredPane, glassPane, and menuBar, and in addition
 34  
 * handles four decorative components arranged in a border layout.
 35  
 * Add the decorative components to the JRootPane using the directional
 36  
 * constants; CENTER is reserved for the content pane and menu bar. 
 37  
 *
 38  
 * @author michael@mpowers.net
 39  
 * @version $Revision: 904 $
 40  
 */
 41  0
 public class BetterRootLayout extends BorderLayout
 42  
 {
 43  
         /**
 44  
          * Returns the amount of space the layout would like to have.
 45  
          *
 46  
          * @param the Container for which this layout manager is being used
 47  
          * @return a Dimension object containing the layout's preferred size
 48  
          * @throws ClassCastException if parent is not a JRootPane
 49  
          */ 
 50  
         public Dimension preferredLayoutSize(Container parent) 
 51  
         {
 52  0
                 JRootPane rootPane = (JRootPane) parent;
 53  
                 
 54  0
                 JPanel proxyPanel = new JPanel();
 55  0
                 proxyPanel.setLayout( new BorderLayout() );
 56  
                 
 57  0
                 JPanel contentProxy = null;
 58  0
                 if(rootPane.getContentPane() != null) {
 59  0
                         contentProxy = new JPanel();
 60  0
                         contentProxy.setMinimumSize( 
 61  0
                                 rootPane.getContentPane().getMinimumSize() );
 62  0
                         contentProxy.setMaximumSize( 
 63  0
                                 rootPane.getContentPane().getMaximumSize() );
 64  0
                         contentProxy.setPreferredSize( 
 65  0
                                 rootPane.getContentPane().getPreferredSize() );
 66  0
                         proxyPanel.add( contentProxy, CENTER );
 67  
                 }
 68  0
                 JPanel menuProxy = null;
 69  0
                 if(rootPane.getJMenuBar() != null) {
 70  0
                         menuProxy = new JPanel();
 71  0
                         menuProxy.setMinimumSize( 
 72  0
                                 rootPane.getJMenuBar().getMinimumSize() );
 73  0
                         menuProxy.setMaximumSize( 
 74  0
                                 rootPane.getJMenuBar().getMaximumSize() );
 75  0
                         menuProxy.setPreferredSize( 
 76  0
                                 rootPane.getJMenuBar().getPreferredSize() );
 77  0
                         proxyPanel.add( menuProxy, NORTH );
 78  
                 }
 79  
                 
 80  0
                 this.addLayoutComponent( proxyPanel, CENTER );
 81  
                 
 82  0
                 Dimension result = super.preferredLayoutSize( parent );
 83  
 
 84  0
                 this.removeLayoutComponent( proxyPanel );
 85  
                 
 86  0
                 proxyPanel.removeAll();
 87  
                 
 88  0
                 return result;
 89  
         }
 90  
         
 91  
         /**
 92  
          * Returns the minimum amount of space the layout needs.
 93  
          *
 94  
          * @param the Container for which this layout manager is being used
 95  
          * @return a Dimension object containing the layout's minimum size
 96  
          * @throws ClassCastException if parent is not a JRootPane
 97  
          */ 
 98  
         public Dimension minimumLayoutSize(Container parent) 
 99  
         {
 100  0
                 JRootPane rootPane = (JRootPane) parent;
 101  
                 
 102  0
                 JPanel proxyPanel = new JPanel();
 103  0
                 proxyPanel.setLayout( new BorderLayout() );
 104  
                 
 105  0
                 JPanel contentProxy = null;
 106  0
                 if(rootPane.getContentPane() != null) {
 107  0
                         contentProxy = new JPanel();
 108  0
                         contentProxy.setMinimumSize( 
 109  0
                                 rootPane.getContentPane().getMinimumSize() );
 110  0
                         contentProxy.setMaximumSize( 
 111  0
                                 rootPane.getContentPane().getMaximumSize() );
 112  0
                         contentProxy.setPreferredSize( 
 113  0
                                 rootPane.getContentPane().getPreferredSize() );
 114  0
                         proxyPanel.add( contentProxy, CENTER );
 115  
                 }
 116  0
                 JPanel menuProxy = null;
 117  0
                 if(rootPane.getJMenuBar() != null) {
 118  0
                         menuProxy = new JPanel();
 119  0
                         menuProxy.setMinimumSize( 
 120  0
                                 rootPane.getJMenuBar().getMinimumSize() );
 121  0
                         menuProxy.setMaximumSize( 
 122  0
                                 rootPane.getJMenuBar().getMaximumSize() );
 123  0
                         menuProxy.setPreferredSize( 
 124  0
                                 rootPane.getJMenuBar().getPreferredSize() );
 125  0
                         proxyPanel.add( menuProxy, NORTH );
 126  
                 }
 127  
                 
 128  0
                 this.addLayoutComponent( proxyPanel, CENTER );
 129  
                 
 130  0
                 Dimension result = super.minimumLayoutSize( parent );
 131  
 
 132  0
                 this.removeLayoutComponent( proxyPanel );
 133  
                 
 134  0
                 proxyPanel.removeAll();
 135  
                 
 136  0
                 return result;
 137  
         }
 138  
         
 139  
         /**
 140  
          * Returns the maximum amount of space the layout can use.
 141  
          *
 142  
          * @param the Container for which this layout manager is being used
 143  
          * @return a Dimension object containing the layout's maximum size
 144  
          * @throws ClassCastException if parent is not a JRootPane
 145  
          */ 
 146  
         public Dimension maximumLayoutSize(Container target) 
 147  
         {
 148  0
                 JRootPane rootPane = (JRootPane) target;
 149  
                 
 150  0
                 JPanel proxyPanel = new JPanel();
 151  0
                 proxyPanel.setLayout( new BorderLayout() );
 152  
                 
 153  0
                 JPanel contentProxy = null;
 154  0
                 if(rootPane.getContentPane() != null) {
 155  0
                         contentProxy = new JPanel();
 156  0
                         contentProxy.setMinimumSize( 
 157  0
                                 rootPane.getContentPane().getMinimumSize() );
 158  0
                         contentProxy.setMaximumSize( 
 159  0
                                 rootPane.getContentPane().getMaximumSize() );
 160  0
                         contentProxy.setPreferredSize( 
 161  0
                                 rootPane.getContentPane().getPreferredSize() );
 162  0
                         proxyPanel.add( contentProxy, CENTER );
 163  
                 }
 164  0
                 JPanel menuProxy = null;
 165  0
                 if(rootPane.getJMenuBar() != null) {
 166  0
                         menuProxy = new JPanel();
 167  0
                         menuProxy.setMinimumSize( 
 168  0
                                 rootPane.getJMenuBar().getMinimumSize() );
 169  0
                         menuProxy.setMaximumSize( 
 170  0
                                 rootPane.getJMenuBar().getMaximumSize() );
 171  0
                         menuProxy.setPreferredSize( 
 172  0
                                 rootPane.getJMenuBar().getPreferredSize() );
 173  0
                         proxyPanel.add( menuProxy, NORTH );
 174  
                 }
 175  
                 
 176  0
                 this.addLayoutComponent( proxyPanel, CENTER );
 177  
                 
 178  0
                 Dimension result = super.maximumLayoutSize( target );
 179  
 
 180  0
                 this.removeLayoutComponent( proxyPanel );
 181  
                 
 182  0
                 proxyPanel.removeAll();
 183  
                 
 184  0
                 return result;
 185  
         }
 186  
         
 187  
         /**
 188  
          * Instructs the layout manager to perform the layout for the specified
 189  
          * container.
 190  
          *
 191  
          * @param the Container for which this layout manager is being used
 192  
          * @throws ClassCastException if parent is not a JRootPane
 193  
          */ 
 194  
         public void layoutContainer(Container parent) 
 195  
         {
 196  0
                 JRootPane rootPane = (JRootPane) parent;
 197  
                 
 198  0
                 Rectangle b = parent.getBounds();
 199  0
                 Insets i = rootPane.getInsets();
 200  0
                 int w = b.width - i.right - i.left;
 201  0
                 int h = b.height - i.top - i.bottom;
 202  
         
 203  
                 // layout panes
 204  
 
 205  0
                 if(rootPane.getLayeredPane() != null) {
 206  0
                         rootPane.getLayeredPane().setBounds(i.left, i.top, w, h);
 207  
                 }
 208  0
                 if(rootPane.getGlassPane() != null) {
 209  0
                         rootPane.getGlassPane().setBounds(i.left, i.top, w, h);
 210  
                 }
 211  
                 
 212  
                 // handle proxy panel
 213  
                 
 214  0
                 JPanel proxyPanel = new JPanel();
 215  0
                 proxyPanel.setLayout( new BorderLayout() );
 216  
                 
 217  0
                 this.addLayoutComponent( proxyPanel, CENTER );
 218  
                 
 219  0
                 super.layoutContainer( parent );
 220  
 
 221  
                 // use proxy sizes to set sizes of layeredPane's children
 222  
 
 223  0
                 Rectangle proxyRect = proxyPanel.getBounds();
 224  0
                 if(rootPane.getJMenuBar() != null) {
 225  0
                         Rectangle menuRect = proxyPanel.getBounds();
 226  0
                         menuRect.height = rootPane.getJMenuBar().getPreferredSize().height;
 227  0
                         rootPane.getJMenuBar().setBounds( menuRect );
 228  0
                         proxyRect.y += menuRect.height;
 229  0
                         proxyRect.height -= menuRect.height;
 230  
                 }
 231  0
                 if(rootPane.getContentPane() != null) {
 232  0
                         rootPane.getContentPane().setBounds( proxyRect );
 233  
                 }
 234  
 
 235  0
                 this.removeLayoutComponent( proxyPanel );
 236  
                 
 237  0
                 proxyPanel.removeAll();
 238  0
         }
 239  
         
 240  
         /**
 241  
         * Passes NORTH, SOUTH, EAST, WEST and CENTER to super implementation,
 242  
         * and ignores all others.
 243  
         */
 244  
         public void addLayoutComponent(Component comp, Object constraints) 
 245  
         {
 246  0
                 if ( NORTH.equals( constraints ) )
 247  
                 {
 248  0
                         super.addLayoutComponent( comp, constraints );
 249  0
                 }
 250  
                 else
 251  0
                 if ( SOUTH.equals( constraints ) )
 252  
                 {
 253  0
                         super.addLayoutComponent( comp, constraints );
 254  0
                 }
 255  
                 else
 256  0
                 if ( EAST.equals( constraints ) )
 257  
                 {
 258  0
                         super.addLayoutComponent( comp, constraints );
 259  0
                 }
 260  
                 else
 261  0
                 if ( WEST.equals( constraints ) )
 262  
                 {
 263  0
                         super.addLayoutComponent( comp, constraints );
 264  0
                 }
 265  
                 else
 266  0
                 if ( CENTER.equals( constraints ) )
 267  
                 {
 268  0
                         super.addLayoutComponent( comp, constraints );
 269  
                 }
 270  
                 
 271  
                 // otherwise, ignore
 272  0
         }
 273  
 }
 274