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 static final int VALUE_WIDTH = 250;
68
69
70 private final CellConstraints cc = new CellConstraints();
71
72
73 private final DefaultFormBuilder builder;
74
75
76 private final ButtonBarBuilder2 bbBuilder;
77
78
79 private boolean hasButtons;
80
81
82 private JComponent preview;
83
84
85
86
87
88
89
90 public PropertiesShowPanelFactory(int keyWidth) {
91 this(keyWidth, VALUE_WIDTH);
92 }
93
94
95
96
97
98
99
100
101
102 public PropertiesShowPanelFactory(int keyWidth, int valueWidth) {
103 FormLayout layout = new FormLayout("right:" + keyWidth + "dlu, 6dlu, f:"
104 + valueWidth + "dlu, 4dlu, right:default:grow, 40dlu", "15dlu");
105 builder = new DefaultFormBuilder(layout);
106 builder.setDefaultDialogBorder();
107 bbBuilder = new ButtonBarBuilder2();
108 bbBuilder.setLeftToRightButtonOrder(true);
109 init(null, 0);
110 }
111
112
113
114
115 public PropertiesShowPanelFactory() {
116 this(KEY_WIDTH, VALUE_WIDTH);
117 }
118
119
120
121
122
123
124
125 public PropertiesShowPanelFactory(List<JButton> buttons) {
126 this(KEY_WIDTH);
127 init(buttons, 0);
128 }
129
130
131
132
133
134
135
136 public PropertiesShowPanelFactory(JButton button) {
137 this();
138 List<JButton> buttons = new ArrayList<JButton>();
139 buttons.add(button);
140 init(buttons, 0);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154 public PropertiesShowPanelFactory(int keyWidth, List<JButton> buttons,
155 int numOfGeneralButtons) {
156 this(keyWidth);
157 init(buttons, numOfGeneralButtons);
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 public PropertiesShowPanelFactory(int keyWidth, int valueWidth,
174 List<JButton> buttons, int numOfGeneralButtons) {
175 this(keyWidth, valueWidth);
176 init(buttons, numOfGeneralButtons);
177 }
178
179
180
181
182
183
184
185
186
187
188 public PropertiesShowPanelFactory(List<JButton> buttons,
189 int numOfGeneralButtons) {
190 this();
191 init(buttons, numOfGeneralButtons);
192 }
193
194
195
196
197
198
199
200
201
202 private void init(List<JButton> buttons, int numOfGeneralButtons) {
203 if (buttons == null) {
204 return;
205 }
206 for (int i = 0; i < buttons.size(); i++) {
207 JButton button = buttons.get(i);
208 if (i == buttons.size() - numOfGeneralButtons) {
209 bbBuilder.addUnrelatedGap();
210 } else {
211 bbBuilder.addRelatedGap();
212 }
213 bbBuilder.addButton(button);
214 }
215 if (buttons.size() > 0) {
216 bbBuilder.getLayout().setColumnSpec(1, ColumnSpec.decode("right:d:grow"));
217 hasButtons = true;
218 }
219 }
220
221
222
223
224
225
226
227 public void sep(String separator) {
228 builder.addSeparator(separator);
229 builder.nextLine();
230 }
231
232
233
234
235
236
237
238
239
240 public void app(String label, Component component) {
241 builder.append(label, component);
242 builder.nextLine();
243 }
244
245
246
247
248
249
250
251
252
253 public void app(String label, Object object) {
254 builder.append(label, new JLabel(object.toString()));
255 builder.nextLine();
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269 public void app(String label, Component component, int rowSpan) {
270 app(label, component, rowSpan, "40dlu");
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public void app(String label, Component component, int rowSpan,
286 String rowHeight) {
287 builder.append(label);
288 for (int i = 0; i < rowSpan - 1; i++) {
289 builder.appendRow(RowSpec.decode(rowHeight));
290 }
291 builder.add(new JScrollPane(component),
292 cc.xywh(builder.getColumn(), builder.getRow(), 1, rowSpan));
293 builder.nextLine(rowSpan);
294 }
295
296
297
298
299
300
301 public JPanel constructPanel() {
302 builder.appendSeparator(hasButtons ? "Options" : "");
303 builder.nextLine();
304 builder.appendRow(RowSpec.decode("top:20dlu"));
305 builder.add(bbBuilder.getPanel(), cc.xywh(BUTTON_BAR_COLUMN_INDEX,
306 builder.getRow(), BUTTON_BAR_COLUMN_WIDTH, BUTTON_BAR_ROW_HEIGHT));
307
308 if (preview != null) {
309 builder.nextLine();
310 builder.appendRow(RowSpec.decode("10dlu"));
311 sep("Summary:");
312 builder.appendRow(RowSpec.decode("top:d:grow"));
313 builder.add(preview, cc.xywh(PREVIEW_COLUMN_INDEX, builder.getRow(),
314 PREVIEW_COLUMN_WIDTH, PREVIEW_ROW_HEIGHT));
315 }
316 return builder.getPanel();
317 }
318
319
320
321
322
323
324
325 public void appPreview(JComponent component) {
326 preview = component;
327 }
328
329 }