-
Notifications
You must be signed in to change notification settings - Fork 96
/
PWRow.java
130 lines (117 loc) · 3.75 KB
/
PWRow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************************************
* Copyright (c) 2011 Laurent CARON
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Laurent CARON (laurent.caron at gmail dot com) - Initial implementation and API
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.preferencewindow;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.nebula.widgets.opal.preferencewindow.widgets.PWButton;
import org.eclipse.nebula.widgets.opal.preferencewindow.widgets.PWLabel;
import org.eclipse.nebula.widgets.opal.preferencewindow.widgets.PWWidget;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
/**
* Instances of this class are rows
*/
public class PWRow extends PWRowGroup {
protected final List<PWWidget> widgets;
/**
* Constructor
*/
public PWRow() {
this.widgets = new ArrayList<PWWidget>();
}
@Override
public PWContainer add(final PWWidget widget) {
this.widgets.add(widget);
addColumn(widget.getNumberOfColumns());
return this;
}
@Override
public PWContainer add(final PWContainer element) {
if (element instanceof PWRow || element instanceof PWGroup) {
return this.parent.add(element);
} else {
throw new UnsupportedOperationException("Can only add a PWGroup or a PWRow.");
}
}
@Override
public void build(final Composite parent) {
final int size = this.widgets.size();
int columIndex = 0;
for (int i = 0; i < size; i++) {
final PWWidget widget = this.widgets.get(i);
final Control control = widget.checkAndBuild(parent);
if (control != null && control.getLayoutData() == null) {
final int colSpan;
final boolean grabExcessSpace;
final int alignment;
if (size == 1) {
if (widget.isSingleWidget()) {
colSpan = this.parentNumberOfColums;
} else {
colSpan = this.parentNumberOfColums - widget.getNumberOfColumns() + 1;
}
grabExcessSpace = true;
} else {
if (i == size - 1) {
colSpan = this.parentNumberOfColums - columIndex;
grabExcessSpace = widget.isGrabExcessSpace();
} else {
colSpan = 1;
grabExcessSpace = widget instanceof PWButton && i == 0 ? true : widget.isGrabExcessSpace();
}
}
columIndex += widget.getNumberOfColumns();
if (i == 0 && grabExcessSpace && size > 1) {
if (widget instanceof PWLabel || widget instanceof PWButton) {
alignment = GridData.END;
} else {
alignment = GridData.BEGINNING;
}
} else {
alignment = widget.getAlignment();
}
final GridData gd = new GridData(alignment, GridData.BEGINNING, grabExcessSpace, false, colSpan, 1);
gd.horizontalIndent = widget.getIndent();
gd.minimumWidth = widget.getWidth();
if (widget.getHeight() != -1) {
gd.minimumHeight = widget.getHeight();
}
control.setLayoutData(gd);
}
}
}
@Override
protected void checkParent(final PWContainer parent) {
if (parent instanceof PWTab || parent instanceof PWGroup) {
return;
}
throw new UnsupportedOperationException("Bad parent, should be only PWTab or PWGroup");
}
@Override
public void enableOrDisable() {
if (this.enabler == null) {
return;
}
final boolean enabled = this.enabler.isEnabled();
for (final PWWidget widget : this.widgets) {
final boolean widgetEnable = widget.enableOrDisable();
for (final Control c : widget.getControls()) {
if (!c.isDisposed()) {
c.setEnabled(enabled && widgetEnable);
}
}
}
}
}