|
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"); |
|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | -package org.utplsql.sqldev.ui.runner |
| 16 | +package org.utplsql.sqldev.ui.runner; |
17 | 17 |
|
18 | | -import java.awt.Component |
19 | | -import java.awt.Container |
20 | | -import java.awt.Dimension |
21 | | -import java.awt.FlowLayout |
22 | | -import java.awt.Insets |
23 | | -import javax.swing.JScrollPane |
24 | | -import javax.swing.SwingUtilities |
| 18 | +import java.awt.Component; |
| 19 | +import java.awt.Container; |
| 20 | +import java.awt.Dimension; |
| 21 | +import java.awt.FlowLayout; |
| 22 | +import java.awt.Insets; |
| 23 | +import javax.swing.JScrollPane; |
| 24 | +import javax.swing.SwingUtilities; |
25 | 25 |
|
26 | | -/** |
| 26 | +/** |
27 | 27 | * FlowLayout subclass that fully supports wrapping of components. |
28 | 28 | * Converted to Xtend based on http://www.camick.com/java/source/WrapLayout.java |
29 | 29 | */ |
30 | | -class WrapLayout extends FlowLayout { |
31 | | - |
32 | | - /** |
33 | | - * Constructs a new <code>WrapLayout</code> with a left |
34 | | - * alignment and a default 5-unit horizontal and vertical gap. |
35 | | - */ |
36 | | - new() { |
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 | | - new(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 | | - new(int align, int hgap, 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 Dimension preferredLayoutSize(Container target) { |
75 | | - return layoutSize(target, true) |
76 | | - } |
77 | | - |
78 | | - /** |
79 | | - * Returns the minimum dimensions needed to layout the <i>visible</i> |
80 | | - * components contained in the specified target container. |
81 | | - * @param target the component which needs to be laid out |
82 | | - * @return the minimum dimensions to lay out the |
83 | | - * subcomponents of the specified container |
84 | | - */ |
85 | | - override Dimension minimumLayoutSize(Container target) { |
86 | | - var Dimension minimum = layoutSize(target, false) |
87 | | - minimum.width -= (getHgap() + 1) |
88 | | - return minimum |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * Returns the minimum or preferred dimension needed to layout the target |
93 | | - * container. |
94 | | - * @param target target to get layout size for |
95 | | - * @param preferred should preferred size be calculated |
96 | | - * @return the dimension to layout the target container |
97 | | - */ |
98 | | - def private Dimension layoutSize(Container target, boolean preferred) { |
99 | | - synchronized (target.getTreeLock()) { |
100 | | - // Each row must fit with the width allocated to the containter. |
101 | | - // When the container width = 0, the preferred width of the container |
102 | | - // has not yet been calculated so lets ask for the maximum. |
103 | | - var int targetWidth = target.getSize().width |
104 | | - var Container container = target |
105 | | - while (container.getSize().width === 0 && container.getParent() !== null) { |
106 | | - container = container.getParent() |
107 | | - } |
108 | | - targetWidth = container.getSize().width |
109 | | - if(targetWidth === 0) targetWidth = Integer.MAX_VALUE |
110 | | - var int hgap = getHgap() |
111 | | - var int vgap = getVgap() |
112 | | - var Insets insets = target.getInsets() |
113 | | - var int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2) |
114 | | - var int maxWidth = targetWidth - horizontalInsetsAndGap |
115 | | - // Fit components into the allowed width |
116 | | - var Dimension dim = new Dimension(0, 0) |
117 | | - var int rowWidth = 0 |
118 | | - var int rowHeight = 0 |
119 | | - var int nmembers = target.getComponentCount() |
120 | | - for (var int i = 0; i < nmembers; i++) { |
121 | | - var Component m = target.getComponent(i) |
122 | | - if (m.isVisible()) { |
123 | | - var Dimension d = if(preferred) m.getPreferredSize() else m.getMinimumSize() |
124 | | - // Can't add the component to current row. Start a new row. |
125 | | - if (rowWidth + d.width > maxWidth) { |
126 | | - addRow(dim, rowWidth, rowHeight) |
127 | | - rowWidth = 0 |
128 | | - rowHeight = 0 |
129 | | - } |
130 | | - // Add a horizontal gap for all components after the first |
131 | | - if (rowWidth !== 0) { |
132 | | - rowWidth += hgap |
133 | | - } |
134 | | - rowWidth += d.width |
135 | | - rowHeight = Math.max(rowHeight, d.height) |
136 | | - } |
137 | | - } |
138 | | - addRow(dim, rowWidth, rowHeight) |
139 | | - dim.width += horizontalInsetsAndGap |
140 | | - dim.height += insets.top + insets.bottom + vgap * 2 |
141 | | - // When using a scroll pane or the DecoratedLookAndFeel we need to |
142 | | - // make sure the preferred size is less than the size of the |
143 | | - // target containter so shrinking the container size works |
144 | | - // correctly. Removing the horizontal gap is an easy way to do this. |
145 | | - var Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane, target) |
146 | | - if (scrollPane !== null && target.isValid()) { |
147 | | - dim.width -= (hgap + 1) |
148 | | - } |
149 | | - return dim |
150 | | - } |
151 | | - } |
152 | | - |
153 | | - /* |
154 | | - * A new row has been completed. Use the dimensions of this row |
155 | | - * to update the preferred size for the container. |
156 | | - * |
157 | | - * @param dim update the width and height when appropriate |
158 | | - * @param rowWidth the width of the row to add |
159 | | - * @param rowHeight the height of the row to add |
160 | | - */ |
161 | | - def private void addRow(Dimension dim, int rowWidth, int rowHeight) { |
162 | | - dim.width = Math.max(dim.width, rowWidth) |
163 | | - if (dim.height > 0) { |
164 | | - dim.height += getVgap() |
165 | | - } |
166 | | - dim.height += rowHeight |
167 | | - } |
| 30 | +@SuppressWarnings("all") |
| 31 | +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(); |
| 133 | + } |
| 134 | + Dimension d = _xifexpression; |
| 135 | + if (((rowWidth + d.width) > maxWidth)) { |
| 136 | + this.addRow(dim, rowWidth, rowHeight); |
| 137 | + rowWidth = 0; |
| 138 | + rowHeight = 0; |
| 139 | + } |
| 140 | + if ((rowWidth != 0)) { |
| 141 | + int _rowWidth = rowWidth; |
| 142 | + rowWidth = (_rowWidth + hgap); |
| 143 | + } |
| 144 | + int _rowWidth_1 = rowWidth; |
| 145 | + rowWidth = (_rowWidth_1 + d.width); |
| 146 | + rowHeight = Math.max(rowHeight, d.height); |
| 147 | + } |
| 148 | + } |
| 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 | + } |
| 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); |
| 178 | + } |
| 179 | + int _height_1 = dim.height; |
| 180 | + dim.height = (_height_1 + rowHeight); |
| 181 | + } |
168 | 182 | } |
0 commit comments