1 /*
   2  * Copyright (c) 2013, 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 /* @test
  24    @bug 4816114
  25    @summary REGRESSION: Regression in divider location behavior when JSplitPane is resized
  26    @author Andrey Pikalev
  27    @run main bug4816114
  28 */
  29 
  30 import javax.swing.*;
  31 import java.awt.*;
  32 import java.lang.reflect.*;
  33 
  34 
  35 public class bug4816114 {
  36 
  37     JFrame fr;
  38     JSplitPane splitPane;
  39 
  40     boolean[] resized = new boolean[] { false, false, false,
  41                                         false, false, false };
  42     static int step = 0;
  43     boolean h_passed = false;
  44     boolean v_passed = false;
  45 
  46     static bug4816114 test = new bug4816114();
  47 
  48     public static void main(String[] args) throws InterruptedException, InvocationTargetException, AWTException {
  49         SwingUtilities.invokeAndWait(new Runnable() {
  50             public void run() {
  51                 test.createAndShowGUI();
  52             }
  53         });
  54         Robot robot = new Robot();
  55         robot.waitForIdle();
  56         Thread.sleep(1000);
  57         Thread.sleep(2000);
  58 
  59         step++;
  60         test.doTest(150, 300);
  61 
  62         step++;
  63         test.doTest(650, 300);
  64 
  65         SwingUtilities.invokeAndWait(new Runnable() {
  66             public void run() {
  67                 test.splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  68             }
  69         });
  70 
  71         step++;
  72         test.doTest(300, 650);
  73 
  74         step++;
  75         test.doTest(300, 150);
  76 
  77         step++;
  78         test.doTest(300, 650);
  79 
  80         if ( !test.isPassed() ) {
  81             throw new Error("The divider location is wrong.");
  82         }
  83     }
  84     public void createAndShowGUI() {
  85         fr = new JFrame("Test");
  86 
  87         splitPane = new TestSplitPane();
  88         splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  89         splitPane.setResizeWeight(0);
  90         splitPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
  91 
  92         JButton leftButton = new JButton("LEFT");
  93         leftButton.setPreferredSize(new Dimension(300, 300));
  94         leftButton.setMinimumSize(new Dimension(150, 150));
  95         splitPane.setLeftComponent(leftButton);
  96 
  97         JButton rightButton = new JButton("RIGHT");
  98         rightButton.setPreferredSize(new Dimension(300, 300));
  99         rightButton.setMinimumSize(new Dimension(150, 150));
 100         splitPane.setRightComponent(rightButton);
 101 
 102         fr.getContentPane().add(splitPane, BorderLayout.CENTER);
 103 
 104         fr.pack();
 105         fr.setVisible(true);
 106     }
 107 
 108     void doTest(final int width, final int height)  throws InterruptedException, InvocationTargetException {
 109         SwingUtilities.invokeAndWait(new Runnable() {
 110             public void run() {
 111                 splitPane.setPreferredSize(new Dimension(width, height));
 112                 fr.pack();
 113             }
 114         });
 115 
 116         synchronized (bug4816114.this) {
 117             while (!resized[step]) {
 118                 bug4816114.this.wait();
 119             }
 120         }
 121     }
 122 
 123    synchronized void setPassed(int orientation, boolean passed) {
 124        if (orientation == JSplitPane.HORIZONTAL_SPLIT) {
 125            this.h_passed = passed;
 126        }
 127        else {
 128            this.v_passed = passed;
 129        }
 130    }
 131 
 132     synchronized boolean isPassed() {
 133         return h_passed && v_passed;
 134     }
 135 
 136 
 137     class TestSplitPane extends JSplitPane {
 138         public void setDividerLocation(int location) {
 139             super.setDividerLocation(location);
 140 
 141             if ( splitPane.getDividerLocation() == 151 ) {
 142                 setPassed(getOrientation(), true);
 143             }
 144 
 145             synchronized (bug4816114.this) {
 146                 resized[step] = true;
 147                 bug4816114.this.notifyAll();
 148             }
 149         }
 150     }
 151 }