1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.util.function.IntSupplier;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.preferences.AbstractProperty;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Defines the sizing policy used for download tabs.
|
---|
11 | * @author Michael Zangl
|
---|
12 | * @since 12705
|
---|
13 | */
|
---|
14 | public interface DownloadSourceSizingPolicy {
|
---|
15 | /**
|
---|
16 | * Gets the height of the download source panel.
|
---|
17 | * @return The height the component should have.
|
---|
18 | */
|
---|
19 | int getComponentHeight();
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Check whether the user should be allowed to adjust the height of this download source panel
|
---|
23 | * @return <code>true</code> if the height should be adjustable
|
---|
24 | */
|
---|
25 | boolean isHeightAdjustable();
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Stores the height
|
---|
29 | * @param height the height in pixel
|
---|
30 | */
|
---|
31 | default void storeHeight(int height) {
|
---|
32 | throw new UnsupportedOperationException(
|
---|
33 | "Setting the height is not supported for " + this.getClass().getCanonicalName());
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * The download source has a fixed size provided by the component
|
---|
38 | * @author Michael Zangl
|
---|
39 | */
|
---|
40 | class FixedDownloadSourceSizePolicy implements DownloadSourceSizingPolicy {
|
---|
41 | private final Component base;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Create a new fixed download source policy
|
---|
45 | * @param base The component of which the size should be taken.
|
---|
46 | */
|
---|
47 | public FixedDownloadSourceSizePolicy(Component base) {
|
---|
48 | this.base = base;
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public int getComponentHeight() {
|
---|
53 | return (int) base.getPreferredSize().getHeight();
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public boolean isHeightAdjustable() {
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * The height of this component is given by a preference entry.
|
---|
64 | * <p>
|
---|
65 | * Mind that using a preferred component size is not possible in this case, since the preference entry needs to have a onstant default value.
|
---|
66 | */
|
---|
67 | class AdjustableDownloadSizePolicy implements DownloadSourceSizingPolicy {
|
---|
68 |
|
---|
69 | private final AbstractProperty<Integer> preference;
|
---|
70 | private final IntSupplier minHeight;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Create a new {@link AdjustableDownloadSizePolicy}
|
---|
74 | * @param preference The preference to use
|
---|
75 | */
|
---|
76 | public AdjustableDownloadSizePolicy(AbstractProperty<Integer> preference) {
|
---|
77 | this(preference, () -> 1);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Create a new {@link AdjustableDownloadSizePolicy}
|
---|
82 | * @param preference The preference to use
|
---|
83 | * @param minHeight A supplier that gives the minimum height of the component. Must be positive or 0.
|
---|
84 | * @since 14418
|
---|
85 | */
|
---|
86 | public AdjustableDownloadSizePolicy(AbstractProperty<Integer> preference, IntSupplier minHeight) {
|
---|
87 | this.preference = preference;
|
---|
88 | this.minHeight = minHeight;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public int getComponentHeight() {
|
---|
93 | int computedMinHeight = this.minHeight.getAsInt();
|
---|
94 | if (computedMinHeight < 0) {
|
---|
95 | throw new IllegalStateException("Illegal minimum component height:" + computedMinHeight);
|
---|
96 | }
|
---|
97 | return Math.max(computedMinHeight, preference.get());
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public boolean isHeightAdjustable() {
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public void storeHeight(int height) {
|
---|
107 | preference.put(height);
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|
111 | }
|
---|