-
Notifications
You must be signed in to change notification settings - Fork 6
/
DialogComponentGroup.java
335 lines (307 loc) · 10.9 KB
/
DialogComponentGroup.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*******************************************************************************
* Copyright (c) 2019,2022, Vernalis (R&D) Ltd
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, Version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>
******************************************************************************/
package com.vernalis.knime.dialog.components;
import java.awt.Container;
import java.util.Optional;
import java.util.OptionalInt;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.border.Border;
import org.knime.core.node.defaultnodesettings.DefaultNodeSettingsPane;
import org.knime.core.node.defaultnodesettings.DialogComponent;
/**
* A class to allow arbitrary nesting of groups of dialog components within a
* {@link DefaultNodeSettingsPane}. Each group must have at least one component,
* added at constructor time, and will be drawn within a boxed border.
* Optionally, a title can be supplied and a direction specified (horizontal or
* vertical). If no direction is specified, then the direction will be inherited
* from the parent
*
* @author S.Roughley knime@vernalis.com
*
*/
public class DialogComponentGroup {
private static final int DEFAULT_AXIS = BoxLayout.Y_AXIS;
private final DefaultNodeSettingsPane pane;
private final Container parent;
private final JPanel panel;
/**
* Simple constructor with no title and inherited layout orientation
*
* @param nodeSettingsPane
* The {@link DefaultNodeSettingsPane} to add the components to
* @param firstDiaC
* The first {@link DialogComponent} to go in the grouping
*/
public DialogComponentGroup(DefaultNodeSettingsPane nodeSettingsPane,
DialogComponent firstDiaC) {
this(nodeSettingsPane, Optional.empty(), OptionalInt.empty(),
firstDiaC);
}
/**
* Simple constructor with title and inherited layout orientation
*
* @param nodeSettingsPane
* The {@link DefaultNodeSettingsPane} to add the components to
* @param title
* The title to display on the border
* @param firstDiaC
* The first {@link DialogComponent} to go in the grouping
*/
public DialogComponentGroup(DefaultNodeSettingsPane nodeSettingsPane,
String title, DialogComponent firstDiaC) {
this(nodeSettingsPane, Optional.ofNullable(title), OptionalInt.empty(),
firstDiaC);
}
/**
* Constructor with no title, specifiying layout orientation
*
* @param nodeSettingsPane
* The {@link DefaultNodeSettingsPane} to add the components to
* @param firstDiaC
* The first {@link DialogComponent} to go in the grouping
* @param horizontal
* the layout orientation
*/
public DialogComponentGroup(DefaultNodeSettingsPane nodeSettingsPane,
DialogComponent firstDiaC, boolean horizontal) {
this(nodeSettingsPane, Optional.empty(),
OptionalInt
.of(horizontal ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS),
firstDiaC);
}
/**
* Full constructor with title, and layout orientation
*
* @param nodeSettingsPane
* The {@link DefaultNodeSettingsPane} to add the components to
* @param title
* The optional title of the bounding box
* @param firstDiaC
* The first {@link DialogComponent} to go in the grouping
* @param horizontal
* the layout orientation
*/
public DialogComponentGroup(DefaultNodeSettingsPane nodeSettingsPane,
String title, DialogComponent firstDiaC, boolean horizontal) {
this(nodeSettingsPane, Optional.ofNullable(title),
OptionalInt
.of(horizontal ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS),
firstDiaC);
}
/**
* Full constructor for creation from any of the public constructors
*
* @param nodeSettingsPane
* The {@link DefaultNodeSettingsPane} to add the components to
* @param title
* The optional title of the bounding box. {@code null} means no
* border, {@link Optional#empty()} or
* {@link Optional#ofNullable(Object)} with argument {@code null}
* means an etched border with no title
* @param axis
* The axis(e.g. {@code BoxLayout.X_AXIS} or
* {@code BoxLayour.Y_AXIS}
* @param firstDiaC
* The first {@link DialogComponent} to go in the grouping
*
* @since v1.32.0 Made public in this version
*/
public DialogComponentGroup(DefaultNodeSettingsPane nodeSettingsPane,
Optional<String> title, OptionalInt axis,
DialogComponent firstDiaC) {
if (firstDiaC == null) {
throw new NullPointerException(
"A non-null DialogComponent must be supplied");
}
this.pane = nodeSettingsPane;
// First we add the dialog component to the NodeSettingsPane - this
// takes care of settings load/save, and gives us a way into the
// components
pane.addDialogComponent(firstDiaC);
parent = firstDiaC.getComponentPanel().getParent();
// Now create the panel
// Start by finding the layout axis
int layoutAxis;
if (axis.isPresent()) {
layoutAxis = axis.getAsInt();
} else {
if (parent.getLayout() instanceof BoxLayout) {
layoutAxis = ((BoxLayout) parent.getLayout()).getAxis();
} else {
layoutAxis = DEFAULT_AXIS;
}
}
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, layoutAxis));
if (title != null) {
// And create the border
Border border = BorderFactory.createEtchedBorder();
if (title.isPresent()) {
border = BorderFactory.createTitledBorder(border, title.get());
}
panel.setBorder(border);
}
// Add the panel to the dialog
parent.add(panel);
// And then add the component into the dialog
panel.add(firstDiaC.getComponentPanel());
}
/**
* Private constructor for creation from the
* {@link #addSubGroup(DialogComponent, Optional, OptionalInt)} method
*
* @param pane
* The node settings pane
* @param newParent
* The new parent container
* @param newPanel
* The new panel
*/
private DialogComponentGroup(DefaultNodeSettingsPane pane,
Container newParent, JPanel newPanel) {
this.pane = pane;
this.parent = newParent;
this.panel = newPanel;
}
/**
* Method to add a new dialog component to the group
*
* @param diaC
* The {@link DialogComponent} to add
*
* @return This group
*/
public final DialogComponentGroup addComponent(DialogComponent diaC) {
if (diaC == null) {
throw new NullPointerException(
"A non-null DialogComponent must be supplied");
}
pane.addDialogComponent(diaC);
panel.add(diaC.getComponentPanel());
return this;
}
/**
* Method to add new dialog components to the group
*
* @param diaC
* The {@link DialogComponent}s to add
*
* @return This group
*/
public final DialogComponentGroup addComponents(DialogComponent... diaC) {
if (diaC == null) {
throw new NullPointerException(
"A non-null DialogComponent must be supplied");
}
for (DialogComponent dComp : diaC) {
addComponent(dComp);
}
return this;
}
/**
* Method to create a new {@link DialogComponentGroup} nested within the
* current group, with no title and inherited layout
*
* @param diaC
* The {@link DialogComponent} to initialised the new group with
*
* @return The new {@link DialogComponentGroup} subgroup
*/
public final DialogComponentGroup addSubGroup(DialogComponent diaC) {
return addSubGroup(diaC, Optional.empty(), OptionalInt.empty());
}
/**
* Method to create a new {@link DialogComponentGroup} nested within the
* current group, with a title and inherited layout
*
* @param diaC
* The {@link DialogComponent} to initialised the new group with
* @param title
* The title
*
* @return The new {@link DialogComponentGroup} subgroup
*/
public final DialogComponentGroup addSubGroup(DialogComponent diaC,
String title) {
return addSubGroup(diaC, Optional.ofNullable(title),
OptionalInt.empty());
}
/**
* Method to create a new {@link DialogComponentGroup} nested within the
* current group, with no title and supplied layout
*
* @param diaC
* The {@link DialogComponent} to initialised the new group with
* @param horizontal
* The layout orientation
*
* @return The new {@link DialogComponentGroup} subgroup
*/
public final DialogComponentGroup addSubGroup(DialogComponent diaC,
boolean horizontal) {
return addSubGroup(diaC, Optional.empty(), OptionalInt
.of(horizontal ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS));
}
/**
* Method to create a new {@link DialogComponentGroup} nested within the
* current group, with a title and supplied layout
*
* @param diaC
* The {@link DialogComponent} to initialised the new group with
* @param title
* The title
* @param horizontal
* The layout orientation
*
* @return The new {@link DialogComponentGroup} subgroup
*/
public final DialogComponentGroup addSubGroup(DialogComponent diaC,
String title, boolean horizontal) {
return addSubGroup(diaC, Optional.ofNullable(title), OptionalInt
.of(horizontal ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS));
}
private DialogComponentGroup addSubGroup(DialogComponent diaC,
Optional<String> title, OptionalInt axis) {
addComponent(diaC);
Container newParent = diaC.getComponentPanel().getParent();
// Now create the panel
// Start by finding the layout axis
int layoutAxis;
if (axis.isPresent()) {
layoutAxis = axis.getAsInt();
} else {
if (newParent.getLayout() instanceof BoxLayout) {
layoutAxis = ((BoxLayout) newParent.getLayout()).getAxis();
} else {
layoutAxis = DEFAULT_AXIS;
}
}
// And create the border
Border border = BorderFactory.createEtchedBorder();
if (title.isPresent()) {
border = BorderFactory.createTitledBorder(border, title.get());
}
JPanel newPanel = new JPanel();
newPanel.setLayout(new BoxLayout(newPanel, layoutAxis));
newPanel.setBorder(border);
// Add the panel to the dialog
newParent.add(newPanel);
// And then add the component into the dialog
newPanel.add(diaC.getComponentPanel());
return new DialogComponentGroup(this.pane, newParent, newPanel);
}
}