Coverage Report - net.mtu.eggplant.util.gui.BetterBoxLayout
 
Classes in this File Line Coverage Branch Coverage Complexity
BetterBoxLayout
0%
0/48
0%
0/10
1.417
BetterBoxLayout$1
N/A
N/A
1.417
BetterBoxLayout$BetterBoxLayoutOrientation
0%
0/1
N/A
1.417
 
 1  
 /*
 2  
  * Copyright (c) 2000
 3  
  *      Jon Schewe.  All rights reserved
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  * 1. Redistributions of source code must retain the above copyright
 9  
  *    notice, this list of conditions and the following disclaimer.
 10  
  * 2. Redistributions in binary form must reproduce the above copyright
 11  
  *    notice, this list of conditions and the following disclaimer in the
 12  
  *    documentation and/or other materials provided with the distribution.
 13  
  *
 14  
  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 15  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17  
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 18  
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19  
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20  
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22  
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23  
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24  
  * SUCH DAMAGE.
 25  
  *
 26  
  * I'd appreciate comments/suggestions on the code jpschewe@mtu.net
 27  
  */
 28  
 package net.mtu.eggplant.util.gui;
 29  
 
 30  
 import java.awt.Component;
 31  
 import java.awt.Container;
 32  
 import java.awt.Dimension;
 33  
 import java.awt.Insets;
 34  
 import java.awt.LayoutManager2;
 35  
 
 36  
 /**
 37  
  * Lets box be it's preferred size and lays out components inside accordingly.
 38  
  * 
 39  
  * @version $Revision$
 40  
  */
 41  
 public class BetterBoxLayout implements LayoutManager2 {
 42  
 
 43  
   /**
 44  
    * Layout components along the Y axis.
 45  
    **/
 46  0
   public static final BetterBoxLayoutOrientation VERTICAL = new BetterBoxLayoutOrientation();
 47  
 
 48  
   /**
 49  
    * Layout components along the X axis.
 50  
    **/
 51  0
   public static final BetterBoxLayoutOrientation HORIZONTAL = new BetterBoxLayoutOrientation();
 52  
 
 53  
   /**
 54  
    * Create a BetterBoxLayout with a HORIZONTAL orientation, ie. components laid
 55  
    * out along the X axis.
 56  
    **/
 57  
   public BetterBoxLayout() {
 58  0
     this(HORIZONTAL);
 59  0
   }
 60  
 
 61  
   /**
 62  
    * Create a BetterBoxLayout with given orientation.
 63  
    **/
 64  0
   public BetterBoxLayout(final BetterBoxLayoutOrientation orientation) {
 65  0
     _orientation = orientation;
 66  0
   }
 67  
 
 68  
   private BetterBoxLayoutOrientation _orientation;
 69  
 
 70  
   public void addLayoutComponent(final String name, final Component comp) {
 71  
 
 72  0
   }
 73  
 
 74  
   public void addLayoutComponent(final Component comp, final Object constraints) {
 75  
 
 76  0
   }
 77  
 
 78  
   public void layoutContainer(final Container target) {
 79  0
     final Insets insets = target.getInsets();
 80  0
     final Dimension targetSize = target.getSize();
 81  0
     final Component[] components = target.getComponents();
 82  
 
 83  0
     if (_orientation == VERTICAL) {
 84  0
       final int componentWidth = targetSize.width
 85  
           - insets.left - insets.right;
 86  0
       final int x = insets.left;
 87  0
       int y = insets.top;
 88  0
       for (int i = 0; i < components.length; i++) {
 89  0
         final int height = components[i].getPreferredSize().height;
 90  0
         components[i].setBounds(x, y, componentWidth, height);
 91  0
         y += height;
 92  
       }
 93  0
     } else {
 94  0
       final int componentHeight = targetSize.height
 95  
           - insets.top - insets.bottom;
 96  0
       int x = insets.left;
 97  0
       final int y = insets.top;
 98  0
       for (int i = 0; i < components.length; i++) {
 99  0
         final int width = components[i].getPreferredSize().width;
 100  0
         components[i].setBounds(x, y, width, componentHeight);
 101  0
         x += width;
 102  
       }
 103  
     }
 104  0
   }
 105  
 
 106  
   public Dimension minimumLayoutSize(final Container target) {
 107  0
     return preferredLayoutSize(target);
 108  
   }
 109  
 
 110  
   public Dimension preferredLayoutSize(final Container target) {
 111  0
     final Insets insets = target.getInsets();
 112  0
     final Dimension dim = new Dimension(0, 0);
 113  0
     final Component[] components = target.getComponents();
 114  0
     for (int i = 0; i < components.length; i++) {
 115  0
       final Dimension preferredSize = components[i].getPreferredSize();
 116  0
       if (_orientation == VERTICAL) {
 117  0
         dim.height += preferredSize.height;
 118  0
         dim.width = Math.max(preferredSize.width, dim.width);
 119  
       } else {
 120  0
         dim.height = Math.max(preferredSize.height, dim.height);
 121  0
         dim.width += preferredSize.width;
 122  
       }
 123  
     }
 124  
 
 125  0
     dim.height += insets.top
 126  
         + insets.bottom;
 127  0
     dim.width += insets.left
 128  
         + insets.right;
 129  0
     return dim;
 130  
   }
 131  
 
 132  
   public Dimension maximumLayoutSize(final Container target) {
 133  0
     return preferredLayoutSize(target);
 134  
   }
 135  
 
 136  
   public void removeLayoutComponent(final Component comp) {
 137  
 
 138  0
   }
 139  
 
 140  
   public float getLayoutAlignmentX(final Container target) {
 141  0
     return 0F;
 142  
   }
 143  
 
 144  
   public float getLayoutAlignmentY(final Container target) {
 145  0
     return 0F;
 146  
   }
 147  
 
 148  
   public void invalidateLayout(final Container target) {
 149  
 
 150  0
   }
 151  
 
 152  
   /**
 153  
    * Private class to simulate enum types.
 154  
    **/
 155  0
   private static final class BetterBoxLayoutOrientation {
 156  
   }
 157  
 
 158  
 }