|
1 | | -/** |
| 1 | +/* |
2 | 2 | * Copyright 2019 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
20 | 20 | import java.awt.Dimension; |
21 | 21 | import java.awt.FlowLayout; |
22 | 22 | import java.awt.Insets; |
| 23 | + |
23 | 24 | import javax.swing.JScrollPane; |
24 | 25 | import javax.swing.SwingUtilities; |
25 | 26 |
|
26 | 27 | /** |
27 | | - * FlowLayout subclass that fully supports wrapping of components. |
28 | | - * Converted to Xtend based on http://www.camick.com/java/source/WrapLayout.java |
| 28 | + * FlowLayout subclass that fully supports wrapping of components. |
| 29 | + * Converted to Xtend based on http://www.camick.com/java/source/WrapLayout.java |
| 30 | + * Converted back to Java with small amendments to original code |
29 | 31 | */ |
30 | | -@SuppressWarnings("all") |
31 | 32 | public class WrapLayout extends FlowLayout { |
32 | | - /** |
33 | | - * Constructs a new <code>WrapLayout</code> with a left |
34 | | - * alignment and a default 5-unit horizontal and vertical gap. |
35 | | - */ |
36 | | - public WrapLayout() { |
37 | | - super(); |
38 | | - } |
39 | | - |
40 | | - /** |
41 | | - * Constructs a new <code>FlowLayout</code> with the specified |
42 | | - * alignment and a default 5-unit horizontal and vertical gap. |
43 | | - * The value of the alignment argument must be one of |
44 | | - * <code>WrapLayout</code>, <code>WrapLayout</code>, |
45 | | - * or <code>WrapLayout</code>. |
46 | | - * @param align the alignment value |
47 | | - */ |
48 | | - public WrapLayout(final int align) { |
49 | | - super(align); |
50 | | - } |
51 | | - |
52 | | - /** |
53 | | - * Creates a new flow layout manager with the indicated alignment |
54 | | - * and the indicated horizontal and vertical gaps. |
55 | | - * <p> |
56 | | - * The value of the alignment argument must be one of |
57 | | - * <code>WrapLayout</code>, <code>WrapLayout</code>, |
58 | | - * or <code>WrapLayout</code>. |
59 | | - * @param align the alignment value |
60 | | - * @param hgap the horizontal gap between components |
61 | | - * @param vgap the vertical gap between components |
62 | | - */ |
63 | | - public WrapLayout(final int align, final int hgap, final int vgap) { |
64 | | - super(align, hgap, vgap); |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * Returns the preferred dimensions for this layout given the |
69 | | - * <i>visible</i> components in the specified target container. |
70 | | - * @param target the component which needs to be laid out |
71 | | - * @return the preferred dimensions to lay out the |
72 | | - * subcomponents of the specified container |
73 | | - */ |
74 | | - @Override |
75 | | - public Dimension preferredLayoutSize(final Container target) { |
76 | | - return this.layoutSize(target, true); |
77 | | - } |
78 | | - |
79 | | - /** |
80 | | - * Returns the minimum dimensions needed to layout the <i>visible</i> |
81 | | - * components contained in the specified target container. |
82 | | - * @param target the component which needs to be laid out |
83 | | - * @return the minimum dimensions to lay out the |
84 | | - * subcomponents of the specified container |
85 | | - */ |
86 | | - @Override |
87 | | - public Dimension minimumLayoutSize(final Container target) { |
88 | | - Dimension minimum = this.layoutSize(target, false); |
89 | | - int _width = minimum.width; |
90 | | - int _hgap = this.getHgap(); |
91 | | - int _plus = (_hgap + 1); |
92 | | - minimum.width = (_width - _plus); |
93 | | - return minimum; |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * Returns the minimum or preferred dimension needed to layout the target |
98 | | - * container. |
99 | | - * @param target target to get layout size for |
100 | | - * @param preferred should preferred size be calculated |
101 | | - * @return the dimension to layout the target container |
102 | | - */ |
103 | | - private Dimension layoutSize(final Container target, final boolean preferred) { |
104 | | - synchronized (target.getTreeLock()) { |
105 | | - int targetWidth = target.getSize().width; |
106 | | - Container container = target; |
107 | | - while (((container.getSize().width == 0) && (container.getParent() != null))) { |
108 | | - container = container.getParent(); |
109 | | - } |
110 | | - targetWidth = container.getSize().width; |
111 | | - if ((targetWidth == 0)) { |
112 | | - targetWidth = Integer.MAX_VALUE; |
113 | | - } |
114 | | - int hgap = this.getHgap(); |
115 | | - int vgap = this.getVgap(); |
116 | | - Insets insets = target.getInsets(); |
117 | | - int horizontalInsetsAndGap = ((insets.left + insets.right) + (hgap * 2)); |
118 | | - int maxWidth = (targetWidth - horizontalInsetsAndGap); |
119 | | - Dimension dim = new Dimension(0, 0); |
120 | | - int rowWidth = 0; |
121 | | - int rowHeight = 0; |
122 | | - int nmembers = target.getComponentCount(); |
123 | | - for (int i = 0; (i < nmembers); i++) { |
124 | | - { |
125 | | - Component m = target.getComponent(i); |
126 | | - boolean _isVisible = m.isVisible(); |
127 | | - if (_isVisible) { |
128 | | - Dimension _xifexpression = null; |
129 | | - if (preferred) { |
130 | | - _xifexpression = m.getPreferredSize(); |
131 | | - } else { |
132 | | - _xifexpression = m.getMinimumSize(); |
| 33 | + private static final long serialVersionUID = -4576022991725404247L; |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructs a new <code>WrapLayout</code> with a left alignment and a default |
| 37 | + * 5-unit horizontal and vertical gap. |
| 38 | + */ |
| 39 | + public WrapLayout() { |
| 40 | + super(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Constructs a new <code>FlowLayout</code> with the specified alignment and a |
| 45 | + * default 5-unit horizontal and vertical gap. The value of the alignment |
| 46 | + * argument must be one of <code>WrapLayout</code>, <code>WrapLayout</code>, or |
| 47 | + * <code>WrapLayout</code>. |
| 48 | + * |
| 49 | + * @param align |
| 50 | + * the alignment value |
| 51 | + */ |
| 52 | + public WrapLayout(final int align) { |
| 53 | + super(align); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a new flow layout manager with the indicated alignment and the |
| 58 | + * indicated horizontal and vertical gaps. |
| 59 | + * <p> |
| 60 | + * The value of the alignment argument must be one of <code>WrapLayout</code>, |
| 61 | + * <code>WrapLayout</code>, or <code>WrapLayout</code>. |
| 62 | + * |
| 63 | + * @param align |
| 64 | + * the alignment value |
| 65 | + * @param hgap |
| 66 | + * the horizontal gap between components |
| 67 | + * @param vgap |
| 68 | + * the vertical gap between components |
| 69 | + */ |
| 70 | + public WrapLayout(final int align, final int hgap, final int vgap) { |
| 71 | + super(align, hgap, vgap); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns the preferred dimensions for this layout given the <i>visible</i> |
| 76 | + * components in the specified target container. |
| 77 | + * |
| 78 | + * @param target |
| 79 | + * the component which needs to be laid out |
| 80 | + * @return the preferred dimensions to lay out the subcomponents of the |
| 81 | + * specified container |
| 82 | + */ |
| 83 | + @Override |
| 84 | + public Dimension preferredLayoutSize(final Container target) { |
| 85 | + return layoutSize(target, true); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the minimum dimensions needed to layout the <i>visible</i> components |
| 90 | + * contained in the specified target container. |
| 91 | + * |
| 92 | + * @param target |
| 93 | + * the component which needs to be laid out |
| 94 | + * @return the minimum dimensions to lay out the subcomponents of the specified |
| 95 | + * container |
| 96 | + */ |
| 97 | + @Override |
| 98 | + public Dimension minimumLayoutSize(final Container target) { |
| 99 | + Dimension minimum = layoutSize(target, false); |
| 100 | + minimum.width -= (getHgap() + 1); |
| 101 | + return minimum; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns the minimum or preferred dimension needed to layout the target |
| 106 | + * container. |
| 107 | + * |
| 108 | + * @param target |
| 109 | + * target to get layout size for |
| 110 | + * @param preferred |
| 111 | + * should preferred size be calculated |
| 112 | + * @return the dimension to layout the target container |
| 113 | + */ |
| 114 | + private Dimension layoutSize(final Container target, final boolean preferred) { |
| 115 | + synchronized (target.getTreeLock()) { |
| 116 | + // Each row must fit with the width allocated to the container. |
| 117 | + // When the container width = 0, the preferred width of the container |
| 118 | + // has not yet been calculated so lets ask for the maximum. |
| 119 | + Container container = target; |
| 120 | + while (container.getSize().width == 0 && container.getParent() != null) { |
| 121 | + container = container.getParent(); |
| 122 | + } |
| 123 | + int targetWidth = container.getSize().width; |
| 124 | + if (targetWidth == 0) { |
| 125 | + targetWidth = Integer.MAX_VALUE; |
133 | 126 | } |
134 | | - Dimension d = _xifexpression; |
135 | | - if (((rowWidth + d.width) > maxWidth)) { |
136 | | - this.addRow(dim, rowWidth, rowHeight); |
137 | | - rowWidth = 0; |
138 | | - rowHeight = 0; |
| 127 | + int hgap = getHgap(); |
| 128 | + int vgap = getVgap(); |
| 129 | + Insets insets = target.getInsets(); |
| 130 | + int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2); |
| 131 | + int maxWidth = targetWidth - horizontalInsetsAndGap; |
| 132 | + // Fit components into the allowed width |
| 133 | + Dimension dim = new Dimension(0, 0); |
| 134 | + int rowWidth = 0; |
| 135 | + int rowHeight = 0; |
| 136 | + int nmembers = target.getComponentCount(); |
| 137 | + for (int i = 0; i < nmembers; i++) { |
| 138 | + Component m = target.getComponent(i); |
| 139 | + if (m.isVisible()) { |
| 140 | + Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize(); |
| 141 | + // Can't add the component to current row. Start a new row. |
| 142 | + if (rowWidth + d.width > maxWidth) { |
| 143 | + addRow(dim, rowWidth, rowHeight); |
| 144 | + rowWidth = 0; |
| 145 | + rowHeight = 0; |
| 146 | + } |
| 147 | + // Add a horizontal gap for all components after the first |
| 148 | + if (rowWidth != 0) { |
| 149 | + rowWidth += hgap; |
| 150 | + } |
| 151 | + rowWidth += d.width; |
| 152 | + rowHeight = Math.max(rowHeight, d.height); |
| 153 | + } |
139 | 154 | } |
140 | | - if ((rowWidth != 0)) { |
141 | | - int _rowWidth = rowWidth; |
142 | | - rowWidth = (_rowWidth + hgap); |
| 155 | + addRow(dim, rowWidth, rowHeight); |
| 156 | + dim.width += horizontalInsetsAndGap; |
| 157 | + dim.height += insets.top + insets.bottom + vgap * 2; |
| 158 | + // When using a scroll pane or the DecoratedLookAndFeel we need to |
| 159 | + // make sure the preferred size is less than the size of the |
| 160 | + // target container so shrinking the container size works |
| 161 | + // correctly. Removing the horizontal gap is an easy way to do this. |
| 162 | + Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target); |
| 163 | + if (scrollPane != null && target.isValid()) { |
| 164 | + dim.width -= (hgap + 1); |
143 | 165 | } |
144 | | - int _rowWidth_1 = rowWidth; |
145 | | - rowWidth = (_rowWidth_1 + d.width); |
146 | | - rowHeight = Math.max(rowHeight, d.height); |
147 | | - } |
| 166 | + return dim; |
148 | 167 | } |
149 | | - } |
150 | | - this.addRow(dim, rowWidth, rowHeight); |
151 | | - int _width = dim.width; |
152 | | - dim.width = (_width + horizontalInsetsAndGap); |
153 | | - int _height = dim.height; |
154 | | - dim.height = (_height + ((insets.top + insets.bottom) + (vgap * 2))); |
155 | | - Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target); |
156 | | - if (((scrollPane != null) && target.isValid())) { |
157 | | - int _width_1 = dim.width; |
158 | | - dim.width = (_width_1 - (hgap + 1)); |
159 | | - } |
160 | | - return dim; |
161 | 168 | } |
162 | | - } |
163 | | - |
164 | | - /** |
165 | | - * A new row has been completed. Use the dimensions of this row |
166 | | - * to update the preferred size for the container. |
167 | | - * |
168 | | - * @param dim update the width and height when appropriate |
169 | | - * @param rowWidth the width of the row to add |
170 | | - * @param rowHeight the height of the row to add |
171 | | - */ |
172 | | - private void addRow(final Dimension dim, final int rowWidth, final int rowHeight) { |
173 | | - dim.width = Math.max(dim.width, rowWidth); |
174 | | - if ((dim.height > 0)) { |
175 | | - int _height = dim.height; |
176 | | - int _vgap = this.getVgap(); |
177 | | - dim.height = (_height + _vgap); |
| 169 | + |
| 170 | + /** |
| 171 | + * A new row has been completed. Use the dimensions of this row to update the |
| 172 | + * preferred size for the container. |
| 173 | + * |
| 174 | + * @param dim |
| 175 | + * update the width and height when appropriate |
| 176 | + * @param rowWidth |
| 177 | + * the width of the row to add |
| 178 | + * @param rowHeight |
| 179 | + * the height of the row to add |
| 180 | + */ |
| 181 | + private void addRow(final Dimension dim, final int rowWidth, final int rowHeight) { |
| 182 | + dim.width = Math.max(dim.width, rowWidth); |
| 183 | + if (dim.height > 0) { |
| 184 | + dim.height += getVgap(); |
| 185 | + } |
| 186 | + dim.height += rowHeight; |
178 | 187 | } |
179 | | - int _height_1 = dim.height; |
180 | | - dim.height = (_height_1 + rowHeight); |
181 | | - } |
182 | 188 | } |
0 commit comments