1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.panels;
17
18 import java.awt.Component;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.swing.JButton;
23 import javax.swing.JComponent;
24 import javax.swing.JLabel;
25 import javax.swing.JPanel;
26 import javax.swing.JScrollPane;
27
28 import com.jgoodies.forms.builder.ButtonBarBuilder2;
29 import com.jgoodies.forms.builder.DefaultFormBuilder;
30 import com.jgoodies.forms.layout.CellConstraints;
31 import com.jgoodies.forms.layout.ColumnSpec;
32 import com.jgoodies.forms.layout.FormLayout;
33 import com.jgoodies.forms.layout.RowSpec;
34
35
36
37
38
39
40
41
42
43 public class PropertiesShowPanelFactory {
44
45
46 private static final int PREVIEW_COLUMN_INDEX = 1;
47
48
49 private static final int PREVIEW_ROW_HEIGHT = 1;
50
51
52 private static final int PREVIEW_COLUMN_WIDTH = 5;
53
54
55 private static final int BUTTON_BAR_ROW_HEIGHT = 1;
56
57
58 private static final int BUTTON_BAR_COLUMN_WIDTH = 5;
59
60
61 private static final int BUTTON_BAR_COLUMN_INDEX = 1;
62
63
64 private static final int KEY_WIDTH = 100;
65
66
67 private final CellConstraints cc = new CellConstraints();
68
69
70 private final DefaultFormBuilder builder;
71
72
73 private final ButtonBarBuilder2 bbBuilder;
74
75
76 private boolean hasButtons;
77
78
79 private JComponent preview;
80
81
82
83
84
85
86
87 public PropertiesShowPanelFactory(int keyWidth) {
88 FormLayout layout = new FormLayout("right:" + keyWidth
89 + "dlu, 6dlu, f:250dlu, 4dlu, right:default:grow, 40dlu", "15dlu");
90 builder = new DefaultFormBuilder(layout);
91 builder.setDefaultDialogBorder();
92 bbBuilder = new ButtonBarBuilder2();
93 bbBuilder.setLeftToRightButtonOrder(true);
94 init(null, 0);
95 }
96
97
98
99
100 public PropertiesShowPanelFactory() {
101 this(KEY_WIDTH);
102 }
103
104
105
106
107
108
109
110 public PropertiesShowPanelFactory(List<JButton> buttons) {
111 this(KEY_WIDTH);
112 init(buttons, 0);
113 }
114
115
116
117
118
119
120
121 public PropertiesShowPanelFactory(JButton button) {
122 this();
123 List<JButton> buttons = new ArrayList<JButton>();
124 buttons.add(button);
125 init(buttons, 0);
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 public PropertiesShowPanelFactory(int keyWidth, List<JButton> buttons,
140 int numOfGeneralButtons) {
141 this(keyWidth);
142 init(buttons, numOfGeneralButtons);
143 }
144
145
146
147
148
149
150
151
152
153
154 public PropertiesShowPanelFactory(List<JButton> buttons,
155 int numOfGeneralButtons) {
156 this();
157 init(buttons, numOfGeneralButtons);
158 }
159
160
161
162
163
164
165
166
167
168 private void init(List<JButton> buttons, int numOfGeneralButtons) {
169 if (buttons == null) {
170 return;
171 }
172 for (int i = 0; i < buttons.size(); i++) {
173 JButton button = buttons.get(i);
174 if (i == buttons.size() - numOfGeneralButtons) {
175 bbBuilder.addUnrelatedGap();
176 } else {
177 bbBuilder.addRelatedGap();
178 }
179 bbBuilder.addButton(button);
180 }
181 if (buttons.size() > 0) {
182 bbBuilder.getLayout().setColumnSpec(1, ColumnSpec.decode("right:d:grow"));
183 hasButtons = true;
184 }
185 }
186
187
188
189
190
191
192
193 public void sep(String separator) {
194 builder.addSeparator(separator);
195 builder.nextLine();
196 }
197
198
199
200
201
202
203
204
205
206 public void app(String label, Component component) {
207 builder.append(label, component);
208 builder.nextLine();
209 }
210
211
212
213
214
215
216
217
218
219 public void app(String label, Object object) {
220 builder.append(label, new JLabel(object.toString()));
221 builder.nextLine();
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235 public void app(String label, Component component, int rowSpan) {
236 app(label, component, rowSpan, "40dlu");
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251 public void app(String label, Component component, int rowSpan,
252 String rowHeight) {
253 builder.append(label);
254 for (int i = 0; i < rowSpan - 1; i++) {
255 builder.appendRow(RowSpec.decode(rowHeight));
256 }
257 builder.add(new JScrollPane(component),
258 cc.xywh(builder.getColumn(), builder.getRow(), 1, rowSpan));
259 builder.nextLine(rowSpan);
260 }
261
262
263
264
265
266
267 public JPanel constructPanel() {
268 builder.appendSeparator(hasButtons ? "Options" : "");
269 builder.nextLine();
270 builder.appendRow(RowSpec.decode("top:20dlu"));
271 builder.add(bbBuilder.getPanel(), cc.xywh(BUTTON_BAR_COLUMN_INDEX,
272 builder.getRow(), BUTTON_BAR_COLUMN_WIDTH, BUTTON_BAR_ROW_HEIGHT));
273
274 if (preview != null) {
275 builder.nextLine();
276 builder.appendRow(RowSpec.decode("10dlu"));
277 sep("Summary:");
278 builder.appendRow(RowSpec.decode("top:d:grow"));
279 builder.add(preview, cc.xywh(PREVIEW_COLUMN_INDEX, builder.getRow(),
280 PREVIEW_COLUMN_WIDTH, PREVIEW_ROW_HEIGHT));
281 }
282 return builder.getPanel();
283 }
284
285
286
287
288
289
290
291 public void appPreview(JComponent component) {
292 preview = component;
293 }
294
295 }