1 /*
   2  * Copyright (c) 2014, 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  * @key headful
  27  * @bug 8044371
  28  * @summary setOneTouchExpandable functionality of JSplitPane will reduce vertical Scrollbar
  29  * @author Anton Nashatyrev
  30  * @library ..
  31  */
  32 
  33 
  34 import javax.swing.*;
  35 import javax.swing.plaf.metal.MetalLookAndFeel;
  36 import java.awt.*;
  37 import java.awt.event.AdjustmentEvent;
  38 import java.awt.event.AdjustmentListener;
  39 
  40 public class bug8044371 implements AdjustmentListener {
  41     JSplitPane sptPane;
  42     int lastAdjust = 0;
  43     int initialAdjust = 0;
  44 
  45     public static void main(String[] args) throws Throwable {
  46         UIManager.setLookAndFeel(MetalLookAndFeel.class.getName());
  47         SwingTest.start(bug8044371.class);
  48     }
  49 
  50     public bug8044371(JFrame frame) {
  51         JPanel p = new JPanel();
  52         final JScrollPane jScrollPane = new JScrollPane(new JPanel()
  53         {{setPreferredSize(new Dimension(1500,1500));}},
  54                 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  55                 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  56         jScrollPane.getViewport().scrollRectToVisible(new Rectangle(1500, 1500, 0, 0));
  57         jScrollPane.getVerticalScrollBar().addAdjustmentListener(this);
  58 
  59         sptPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jScrollPane, p);
  60         sptPane.setDividerLocation(90);
  61         sptPane.setOneTouchExpandable(true);
  62         frame.getContentPane().add(sptPane);
  63     }
  64 
  65     public void adjustmentValueChanged(AdjustmentEvent e) {
  66         System.out.println( "adjustmentValueChanged: "  + e.getValue());
  67         lastAdjust = e.getValue();
  68     }
  69 
  70     public void step1() {
  71         if (lastAdjust == 0) {
  72             throw new RuntimeException("Adjustment == 0");
  73         }
  74         initialAdjust =  lastAdjust;
  75         sptPane.setDividerLocation(0);
  76     }
  77 
  78     public void step2() {
  79         if (lastAdjust < initialAdjust) {
  80             throw new RuntimeException("Failed: Adjustment decreased: " + lastAdjust);
  81         }
  82         sptPane.setDividerLocation(90);
  83     }
  84     public void step3() {
  85         if (lastAdjust < initialAdjust) {
  86             throw new RuntimeException("Failed: Adjustment decreased: " + lastAdjust);
  87         }
  88     }
  89 }