Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
*/
public class BorderLayout extends Layout {

private static final String LAYOUT_KEY = BorderLayout.class.getName() + ".layoutData";
private static final ToIntFunction<Point> WIDTH = p -> p.x;
private static final ToIntFunction<Point> HEIGHT = p -> p.y;

Expand Down Expand Up @@ -119,9 +120,9 @@ public class BorderLayout extends Layout {
*/
public double widthDistributionFactor = 0.5;
/**
* If the height of the {@link SWT#TOP} and {@link SWT#BOTTOM} region exceeds the
* available space this factor is used to distribute the size to the controls,
* valid values range between [0 ... 1]
* If the height of the {@link SWT#TOP} and {@link SWT#BOTTOM} region exceeds
* the available space this factor is used to distribute the size to the
* controls, valid values range between [0 ... 1]
*
* The default value is 0.5 (equal distribution of available space)
*
Expand All @@ -134,7 +135,7 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f
return new Point(wHint, hHint);
}
Stream<Entry<Control, BorderData>> children = Arrays.stream(composite.getChildren())//
.map(control-> borderDataControl(control, flushCache));
.map(control -> borderDataControl(control, flushCache));
Map<Integer, List<Entry<Control, BorderData>>> regionMap = children
.collect(Collectors.groupingBy(BorderLayout::region));
int width;
Expand Down Expand Up @@ -253,7 +254,7 @@ protected void layout(Composite composite, boolean flushCache) {
int clientWidth = clientArea.width - 2 * marginWidth;
int clientHeight = clientArea.height - 2 * marginHeight;
Stream<Entry<Control, BorderData>> children = Arrays.stream(composite.getChildren())//
.map(control-> borderDataControl(control, flushCache));
.map(control -> borderDataControl(control, flushCache));
Map<Integer, List<Entry<Control, BorderData>>> regionMap = children
.collect(Collectors.groupingBy(BorderLayout::region));
regionMap.getOrDefault(SWT.NONE, Collections.emptyList())
Expand Down Expand Up @@ -383,7 +384,7 @@ protected void layout(Composite composite, boolean flushCache) {
}
}

private static <C extends Control> Entry<C, BorderData> borderDataControl(C control, boolean flushCache) {
private <C extends Control> Entry<C, BorderData> borderDataControl(C control, boolean flushCache) {
Object layoutData = control.getLayoutData();
if (layoutData instanceof BorderData) {
BorderData borderData = (BorderData) layoutData;
Expand All @@ -392,14 +393,18 @@ private static <C extends Control> Entry<C, BorderData> borderDataControl(C cont
}
return new SimpleEntry<>(control, borderData);
} else {
return new SimpleEntry<>(control, null);
BorderData borderData = flushCache ? null : (BorderData) control.getData(LAYOUT_KEY);
if (borderData == null) {
control.setData(LAYOUT_KEY, borderData = new BorderData());
}
return new SimpleEntry<>(control, borderData);
}
}

private static int region(Entry<Control, BorderData> entry) {
BorderData borderData = entry.getValue();
if (borderData == null) {
//we assume all controls without explicit data to be placed in the center area
// we assume all controls without explicit data to be placed in the center area
return SWT.CENTER;
}
return borderData.getRegion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static void main(String[] args) {
region(new Button(shell, SWT.PUSH), SWT.RIGHT).setText("East 2");
new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL).setText("Center 1");
new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL).setText("Center 2");
shell.pack(true);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
Expand Down