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