001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.widgets; 003 004import java.awt.event.HierarchyBoundsListener; 005import java.awt.event.HierarchyEvent; 006import java.beans.PropertyChangeEvent; 007import java.beans.PropertyChangeListener; 008 009import javax.swing.JSplitPane; 010 011/** 012 * Auto adjusting split pane when parent is resized. 013 * @since 11772 (extracted from {@code CombinePrimitiveResolverDialog}) 014 */ 015public class AutoAdjustingSplitPane extends JSplitPane implements PropertyChangeListener, HierarchyBoundsListener { 016 private double dividerLocation; 017 018 /** 019 * Constructs a new {@code AutoAdjustingSplitPane}. 020 * @param newOrientation {@code JSplitPane.HORIZONTAL_SPLIT} or {@code JSplitPane.VERTICAL_SPLIT} 021 */ 022 public AutoAdjustingSplitPane(int newOrientation) { 023 super(newOrientation); 024 addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, this); 025 addHierarchyBoundsListener(this); 026 } 027 028 @Override 029 public void ancestorResized(HierarchyEvent e) { 030 setDividerLocation((int) (dividerLocation * getHeight())); 031 } 032 033 @Override 034 public void ancestorMoved(HierarchyEvent e) { 035 // do nothing 036 } 037 038 @Override 039 public void propertyChange(PropertyChangeEvent evt) { 040 if (JSplitPane.DIVIDER_LOCATION_PROPERTY.equals(evt.getPropertyName())) { 041 int newVal = (Integer) evt.getNewValue(); 042 if (getHeight() != 0) { 043 dividerLocation = (double) newVal / (double) getHeight(); 044 } 045 } 046 } 047}