1 /*
   2  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25    @test
  26    @bug 4370316
  27    @summary GridLayout does not centre its component properly
  28     (summary was GridLayout does not fill its Container)
  29    @library ../../regtesthelpers
  30    @build Util
  31    @author Andrei Dmitriev : area=awt.layout
  32    @run main LayoutExtraGaps
  33 */
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import test.java.awt.regtesthelpers.Util;
  38 
  39 public class LayoutExtraGaps extends Frame {
  40     final static int compCount = 30;
  41 
  42     public LayoutExtraGaps() {
  43         super("GridLayoutTest");
  44         Panel yellowPanel = new Panel(new GridLayout(compCount, 1, 3, 3));
  45         yellowPanel.setBackground(Color.yellow);
  46 
  47         for(int i = 0; i < compCount ; i++) {
  48             Label redLabel = new Label(""+i);
  49             redLabel.setBackground(Color.red);
  50             yellowPanel.add(redLabel);
  51         }
  52 
  53         Panel bluePanel = new Panel(new GridLayout(1, compCount, 3, 3));
  54         bluePanel.setBackground(Color.blue);
  55 
  56         for(int i = 0; i < compCount; i++) {
  57             Label greenLabel = new Label(""+i);
  58             greenLabel.setBackground(Color.green);
  59             bluePanel.add(greenLabel);
  60         }
  61 
  62         //RTL orientation
  63         Panel blackPanel = new Panel(new GridLayout(compCount, 1, 3, 3));
  64         blackPanel.setBackground(Color.black);
  65         blackPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  66 
  67         for(int i = 0; i < compCount ; i++) {
  68             Label redLabel = new Label(""+i);
  69             redLabel.setBackground(Color.red);
  70             blackPanel.add(redLabel);
  71         }
  72 
  73         Panel redPanel = new Panel(new GridLayout(1, compCount, 3, 3));
  74         redPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  75         redPanel.setBackground(Color.red);
  76 
  77         for(int i = 0; i < compCount; i++) {
  78             Label greenLabel = new Label(""+i);
  79             greenLabel.setBackground(Color.green);
  80             redPanel.add(greenLabel);
  81         }
  82 
  83         setLayout(new GridLayout(2, 2, 20, 20));
  84 
  85         add(yellowPanel);
  86         add(bluePanel);
  87         add(redPanel);
  88         add(blackPanel);
  89         pack();
  90         setSize((int)getPreferredSize().getWidth() + 50, (int)getPreferredSize().getHeight() + 50);
  91         setVisible(true);
  92 
  93         Util.waitForIdle(Util.createRobot());
  94 
  95         if (isComponentCentredLTR(yellowPanel) && isComponentCentredLTR(bluePanel)
  96                 && isComponentCentredLTR(blackPanel) && isComponentCentredRTL(redPanel))
  97         {
  98             System.out.println("Test passed.");
  99         } else {
 100             throw new RuntimeException("Test failed. GridLayout doesn't center component.");
 101         }
 102     }
 103 
 104     /**
 105      * Checks if the components under Panel p are properly centred (i.e.
 106      * opposite borders between the Panel and component are equal). Panel p
 107      * must not be affect by RTL orientation (RTL only affects horizontal
 108      * positioning and components lay out from top right corner).
 109      *
 110      * @param      p the panel where the components exist and is not affected
 111      *             by right to left orientation
 112      * @return     true if components of panel p are properly centre, false
 113      *             otherwise
 114      */
 115     public static boolean isComponentCentredLTR(Panel p) {
 116         double borderLeft;
 117         double borderRight;
 118         double borderTop;
 119         double borderBottom;
 120 
 121         //The first component(rectangle) in panel p.
 122         Rectangle firstRec = p.getComponent(0).getBounds();
 123 
 124         //The last component(rectangle) in panel p.
 125         Rectangle lastRec = p.getComponent(compCount - 1).getBounds();
 126 
 127         System.out.println("bounds of the first rectangle in "+ p.getName() + " = " + firstRec);
 128         System.out.println("bounds of the last rectangle in "+ p.getName() + " = " + lastRec);
 129 
 130         borderLeft = firstRec.getX();
 131         borderRight = p.getWidth() - lastRec.getWidth() - lastRec.getX();
 132         borderTop = firstRec.getY();
 133         borderBottom = p.getHeight() - lastRec.getHeight() - lastRec.getY();
 134 
 135         return areBordersEqual(borderLeft, borderRight) &&
 136                 areBordersEqual(borderTop, borderBottom);
 137     }
 138 
 139     /**
 140      * Checks if the components under Panel p are properly centred (i.e.
 141      * opposite borders between the Panel and component are equal). Panel p
 142      * must be affect by RTL orientation (RTL only affects horizontal positioning
 143      * and components lay out from top right corner).
 144      *
 145      * @param      p the panel where the components exist and is affected by
 146      *             right to left orientation
 147      * @return     true if components of panel p are properly centre, false
 148      *             otherwise
 149      */
 150     public static boolean isComponentCentredRTL(Panel p) {
 151         double borderLeft;
 152         double borderRight;
 153         double borderTop;
 154         double borderBottom;
 155 
 156         //The first component(rectangle) in panel p.
 157         Rectangle firstRec = p.getComponent(0).getBounds();
 158 
 159         //The last component(rectangle) in panel p.
 160         Rectangle lastRec = p.getComponent(compCount - 1).getBounds();
 161 
 162         System.out.println("bounds of the first rectangle in "+ p.getName() + " = " + firstRec);
 163         System.out.println("bounds of the last rectangle in "+ p.getName() + " = " + lastRec);
 164 
 165         borderLeft = lastRec.getX();
 166         borderRight = p.getWidth() - firstRec.getWidth() - firstRec.getX();
 167         borderTop = lastRec.getY();
 168         borderBottom = p.getHeight() - firstRec.getHeight() - firstRec.getY();
 169 
 170         return areBordersEqual(borderLeft, borderRight) &&
 171                 areBordersEqual(borderTop, borderBottom);
 172     }
 173 
 174     /**
 175      * Given two borders border1 and border2 check if they are equal.
 176      *
 177      * @param      border1 one of the borders being compared
 178      * @param      border2 the other border being compared
 179      * @return     true if border1 and border2 are equal to each other (i.e.
 180      *             their width/height difference is at most 1, assuming the
 181      *             smallest pixel is of size 1), false otherwise
 182      */
 183     public static boolean areBordersEqual(double border1, double border2) {
 184         return Math.abs(border1 - border2) <= 1;
 185     }
 186 
 187     public static void main(String[] args) {
 188         new LayoutExtraGaps();
 189     }
 190 }