1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.event.HierarchyBoundsListener;
|
---|
5 | import java.awt.event.HierarchyEvent;
|
---|
6 | import java.beans.PropertyChangeEvent;
|
---|
7 | import java.beans.PropertyChangeListener;
|
---|
8 |
|
---|
9 | import javax.swing.JSplitPane;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Auto adjusting split pane when parent is resized.
|
---|
13 | * @since 11772 (extracted from {@code CombinePrimitiveResolverDialog})
|
---|
14 | */
|
---|
15 | public class AutoAdjustingSplitPane extends JSplitPane implements PropertyChangeListener, HierarchyBoundsListener {
|
---|
16 | private double dividerLocation;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Constructs a new {@code AutoAdjustingSplitPane}.
|
---|
20 | * @param newOrientation {@code JSplitPane.HORIZONTAL_SPLIT} or {@code JSplitPane.VERTICAL_SPLIT}
|
---|
21 | */
|
---|
22 | public AutoAdjustingSplitPane(int newOrientation) {
|
---|
23 | super(newOrientation);
|
---|
24 | addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, this);
|
---|
25 | addHierarchyBoundsListener(this);
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public void ancestorResized(HierarchyEvent e) {
|
---|
30 | setDividerLocation((int) (dividerLocation * getHeight()));
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public void ancestorMoved(HierarchyEvent e) {
|
---|
35 | // do nothing
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
40 | if (JSplitPane.DIVIDER_LOCATION_PROPERTY.equals(evt.getPropertyName())) {
|
---|
41 | int newVal = (Integer) evt.getNewValue();
|
---|
42 | if (getHeight() != 0) {
|
---|
43 | dividerLocation = (double) newVal / (double) getHeight();
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|