1 package com.github.celldynamics.quimp.plugin.utils;
2
3 import java.awt.BorderLayout;
4 import java.awt.Choice;
5 import java.awt.Component;
6 import java.awt.Dimension;
7 import java.awt.FlowLayout;
8 import java.awt.GridLayout;
9 import java.awt.Label;
10 import java.awt.Panel;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.awt.event.WindowAdapter;
14 import java.awt.event.WindowEvent;
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.Map;
20 import java.util.Set;
21
22 import javax.swing.BorderFactory;
23 import javax.swing.JButton;
24 import javax.swing.JCheckBox;
25 import javax.swing.JFrame;
26 import javax.swing.JPanel;
27 import javax.swing.JScrollPane;
28 import javax.swing.JSpinner;
29 import javax.swing.JTextPane;
30 import javax.swing.SpinnerNumberModel;
31
32 import org.apache.commons.lang3.text.WordUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.github.celldynamics.quimp.plugin.ParamList;
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 public abstract class QWindowBuilder {
88
89
90
91
92 static final Logger LOGGER = LoggerFactory.getLogger(QWindowBuilder.class.getName());
93
94
95
96
97 public static final char DELIMITER = ':';
98
99
100
101 protected JFrame pluginWnd;
102
103
104
105
106 protected boolean windowState;
107
108
109
110
111 protected JPanel pluginPanel;
112
113
114
115
116 protected ComponentList ui;
117
118
119 private ParamList def;
120
121
122 private final HashSet<String> reservedKeys =
123 new HashSet<String>(Arrays.asList(new String[] { "help", "name" }));
124
125
126
127 private final int uiType = 0;
128
129
130 private final int srMin = 1;
131
132
133 private final int srMax = 2;
134
135
136 private final int srStep = 3;
137
138
139 private final int srDefault = 4;
140
141
142 private final int srFract = 5;
143
144
145
146
147 protected JButton applyB;
148
149
150
151
152 protected JButton cancelB;
153
154
155
156
157 public QWindowBuilder() {
158 LOGGER.trace("Entering constructor");
159 ui = new ComponentList();
160 def = null;
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 public void buildWindow(final ParamList def) {
264 if (def.size() < 2) {
265 throw new IllegalArgumentException("Window must contain title and" + " at least one control");
266 }
267 this.def = def;
268 ui.clear();
269
270 pluginWnd = new JFrame();
271 pluginPanel = new JPanel();
272
273
274
275 pluginPanel.setLayout(new BorderLayout());
276
277 Panel north = new Panel();
278
279 int siz = def.size();
280
281
282 Set<String> s = def.keySet();
283 for (String k : reservedKeys) {
284 if (s.contains(k)) {
285 siz--;
286 }
287 }
288 GridLayout gridL = new GridLayout(siz, 3);
289 north.setLayout(gridL);
290 gridL.setVgap(5);
291 gridL.setHgap(5);
292
293 String helpText;
294
295
296
297 for (Map.Entry<String, String> e : def.entrySet()) {
298 String key = e.getKey();
299 if (reservedKeys.contains(key)) {
300 continue;
301 }
302 String[] uiparams = StringParser.getParams(e.getValue(), DELIMITER);
303 if (uiparams.length == 0) {
304 throw new IllegalArgumentException("Probably wrong syntax in UI definition");
305 }
306 switch (uiparams[uiType].toLowerCase()) {
307 case "spinner":
308 {
309 helpText = spinnerVerify(uiparams);
310 SpinnerNumberModel model = new SpinnerNumberModel(Double.parseDouble(uiparams[srDefault]),
311 Double.parseDouble(uiparams[srMin]),
312 Double.parseDouble(uiparams[srMax]),
313 Double.parseDouble(uiparams[srStep]));
314 JSpinner sp = new JSpinner(model);
315 ui.put(key, sp);
316
317 String lab = WordUtils.capitalize(key.replaceAll("_", " "));
318 ui.put(key + "label", new Label(lab));
319 ui.put(key + "help", new Label(helpText));
320 }
321 break;
322 case "spinnerd":
323 {
324 helpText = spinnerdVerify(uiparams);
325 SpinnerNumberModel model = new SpinnerNumberModel(Double.parseDouble(uiparams[srDefault]),
326 Double.parseDouble(uiparams[srMin]),
327 Double.parseDouble(uiparams[srMax]),
328 Double.parseDouble(uiparams[srStep]));
329 JSpinner sp = new JSpinner(model);
330 String c = "";
331 Double val = Double.parseDouble(uiparams[srFract]);
332 if (val == 0) {
333 c = "0";
334 } else {
335 c = "0." + String.join("", Collections.nCopies(val.intValue(), "0"));
336 }
337 sp.setEditor(new JSpinner.NumberEditor(sp, c));
338 ui.put(key, sp);
339 String lab = WordUtils.capitalize(key.replaceAll("_", " "));
340 ui.put(key + "label", new Label(lab));
341 ui.put(key + "help", new Label(helpText));
342 }
343 break;
344 case "choiceh": {
345 helpText = choiceVerify(uiparams);
346 Choice c = new Choice();
347 for (int i = uiType + 1; i < uiparams.length - 1; i++) {
348 c.add(uiparams[i]);
349 }
350 c.select(0);
351 ui.put(key, c);
352 String lab = WordUtils.capitalize(key.replaceAll("_", " "));
353 ui.put(key + "label", new Label(lab));
354 ui.put(key + "help", new Label(helpText));
355 }
356 break;
357 case "choice": {
358 if (uiparams.length < 1) {
359 throw new IllegalArgumentException(
360 "Probably wrong syntax in UI definition for " + uiparams[uiType]);
361 }
362 Choice c1 = new Choice();
363 for (int i = uiType + 1; i < uiparams.length; i++) {
364 c1.add(uiparams[i]);
365 }
366 c1.select(0);
367 ui.put(key, c1);
368 String lab = WordUtils.capitalize(key.replaceAll("_", " "));
369 ui.put(key + "label", new Label(lab));
370 ui.put(key + "help", new Label(""));
371 }
372 break;
373 case "button": {
374 helpText = buttonVerify(uiparams);
375 JButton b = new JButton(uiparams[1]);
376 ui.put(key, b);
377 String lab = WordUtils.capitalize(key.replaceAll("_", " "));
378 ui.put(key + "label", new Label(lab));
379 ui.put(key + "help", new Label(helpText));
380 }
381 break;
382 case "checkbox": {
383 helpText = checkboxVerify(uiparams);
384 JCheckBox cb = new JCheckBox(WordUtils.capitalize(uiparams[1]),
385 Boolean.parseBoolean(uiparams[2]));
386 ui.put(key, cb);
387 String lab = WordUtils.capitalize(key.replaceAll("_", " "));
388 ui.put(key + "label", new Label(lab));
389 ui.put(key + "help", new Label(helpText));
390 }
391 break;
392 default:
393
394 throw new IllegalArgumentException("Unknown ui type" + " provided: " + key);
395 }
396 }
397
398
399 for (Map.Entry<String, Component> me : ui.entrySet()) {
400 north.add(me.getValue());
401 }
402
403
404 if (def.containsKey("name")) {
405
406 pluginPanel.setBorder(BorderFactory.createTitledBorder("Plugin " + def.get("name")));
407 }
408 if (def.containsKey("help")) {
409 JTextPane helpArea = new JTextPane();
410
411
412 helpArea.setPreferredSize(new Dimension(80, 200));
413 helpArea.setContentType("text/html");
414 JScrollPane helpPanel = new JScrollPane(helpArea);
415 helpArea.setEditable(false);
416 pluginPanel.add(helpPanel, BorderLayout.CENTER);
417 helpArea.setText(def.get("help"));
418 }
419
420
421 Panel south = new Panel();
422 south.setLayout(new FlowLayout());
423 applyB = new JButton("Apply");
424 south.add(applyB);
425 cancelB = new JButton("Cancel");
426 south.add(cancelB);
427
428 cancelB.addActionListener(new ActionListener() {
429 @Override
430 public void actionPerformed(ActionEvent e) {
431 pluginWnd.setVisible(false);
432 windowState = false;
433 }
434 });
435
436
437 pluginPanel.add(north, BorderLayout.NORTH);
438 pluginPanel.add(south, BorderLayout.SOUTH);
439 pluginWnd.add(pluginPanel);
440 pluginWnd.pack();
441
442 pluginWnd.addWindowListener(new WindowAdapter() {
443 @Override
444 public void windowClosing(WindowEvent we) {
445 LOGGER.trace("Window closing");
446 windowState = false;
447 pluginWnd.setVisible(false);
448 }
449 });
450 windowState = false;
451 }
452
453
454
455
456
457
458
459 private String spinnerVerify(String[] uiparams) {
460 String helpText = "";
461 if (uiparams.length != 5) {
462 if (uiparams.length != 6) {
463 throw new IllegalArgumentException(
464 "Probably wrong syntax in UI definition for " + uiparams[uiType]);
465 } else {
466 helpText = uiparams[5];
467 }
468 }
469 return helpText;
470 }
471
472
473
474
475
476
477
478 private String spinnerdVerify(String[] uiparams) {
479 String helpText = "";
480 if (uiparams.length != 6) {
481 if (uiparams.length != 7) {
482 throw new IllegalArgumentException(
483 "Probably wrong syntax in UI definition for " + uiparams[uiType]);
484 } else {
485 helpText = uiparams[5];
486 }
487 }
488 return helpText;
489 }
490
491
492
493
494
495
496
497 private String choiceVerify(String[] uiparams) {
498 String helpText = "";
499 if (uiparams.length < 2) {
500 throw new IllegalArgumentException(
501 "Probably wrong syntax in UI definition for " + uiparams[uiType]);
502 } else {
503 helpText = uiparams[uiparams.length - 1];
504 }
505 return helpText;
506 }
507
508
509
510
511
512
513
514 private String buttonVerify(String[] uiparams) {
515 String helpText = "";
516 if (uiparams.length != 2) {
517 if (uiparams.length != 3) {
518 throw new IllegalArgumentException(
519 "Probably wrong syntax in UI definition for " + uiparams[uiType]);
520 } else {
521 helpText = uiparams[2];
522 }
523 }
524 return helpText;
525 }
526
527
528
529
530
531
532
533 private String checkboxVerify(String[] uiparams) {
534 String helpText = "";
535 if (uiparams.length != 3) {
536 if (uiparams.length != 4) {
537 throw new IllegalArgumentException(
538 "Probably wrong syntax in UI definition for " + uiparams[uiType]);
539 } else {
540 helpText = uiparams[3];
541 }
542 }
543 return helpText;
544 }
545
546
547
548
549
550
551 public void showWindow(boolean state) {
552 pluginWnd.setVisible(state);
553 windowState = state;
554 }
555
556
557
558
559
560
561
562
563 public boolean toggleWindow(boolean val) {
564 if (!val) {
565 windowState = false;
566 showWindow(windowState);
567 } else {
568 windowState = !windowState;
569 showWindow(windowState);
570 }
571 return windowState;
572 }
573
574
575
576
577
578
579 public boolean isWindowVisible() {
580 return windowState;
581 }
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599 public void setValues(final ParamList vals) {
600
601 for (Map.Entry<String, String> e : vals.entrySet()) {
602 String key = e.getKey();
603 String val = e.getValue();
604
605 String[] ret = def.getParsed(key, DELIMITER);
606 if (ret.length == 0) {
607 return;
608 }
609 switch (ret[uiType].toLowerCase()) {
610
611 case "spinnerd":
612 case "spinner": {
613
614 JSpinner comp = (JSpinner) ui.get(key);
615 comp.setValue(Double.parseDouble(val));
616 SpinnerNumberModel sm = (SpinnerNumberModel) comp.getModel();
617 if (sm.getNextValue() == null) {
618 sm.setMaximum(Double.parseDouble(val));
619 } else if (sm.getPreviousValue() == null) {
620 sm.setMinimum(Double.parseDouble(val));
621 }
622 break;
623 }
624 case "choiceh":
625 case "choice": {
626 Choice comp = (Choice) ui.get(key);
627 comp.select(val);
628 break;
629 }
630 case "button": {
631 break;
632 }
633 case "checkbox": {
634 JCheckBox c = (JCheckBox) ui.get(key);
635 c.setSelected(Boolean.parseBoolean(val));
636 break;
637 }
638 default:
639 throw new IllegalArgumentException(
640 "Unknown UI type in setValues: " + def.getParsed(key, DELIMITER)[uiType]);
641 }
642 }
643 }
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661 public ParamList getValues() {
662 ParamLists/quimp/plugin/ParamList.html#ParamList">ParamList ret = new ParamList();
663
664 Iterator<Map.Entry<String, Component>> entryIterator = ui.entrySet().iterator();
665 while (entryIterator.hasNext()) {
666 Map.Entry<String, Component> m = entryIterator.next();
667 String key = m.getKey();
668
669 switch (def.getParsed(key, DELIMITER)[uiType].toLowerCase()) {
670 case "spinnerd":
671 case "spinner": {
672 JSpinner val = (JSpinner) m.getValue();
673 ret.put(key, String.valueOf(val.getValue()));
674
675 break;
676 }
677 case "choiceh":
678 case "choice": {
679 Choice val = (Choice) m.getValue();
680 ret.put(key, val.getSelectedItem());
681 break;
682 }
683 case "button": {
684 break;
685 }
686 case "checkbox": {
687 JCheckBox c = (JCheckBox) m.getValue();
688 ret.put(key, String.valueOf(c.isSelected()));
689 break;
690 }
691 default:
692 throw new IllegalArgumentException(
693 "Unknown UI type in getValues: " + def.getParsed(key, DELIMITER)[uiType]);
694 }
695 entryIterator.next();
696 entryIterator.next();
697 }
698 return ret;
699 }
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714 public int getIntegerFromUI(final String key) {
715 return (int) getDoubleFromUI(key);
716 }
717
718
719
720
721
722
723
724
725
726 public double getDoubleFromUI(final String key) {
727
728 ParamList uiParam = getValues();
729 return uiParam.getDoubleValue(key);
730 }
731
732
733
734
735
736
737
738
739
740 public boolean getBooleanFromUI(final String key) {
741
742 ParamList uiParam = getValues();
743 return uiParam.getBooleanValue(key);
744 }
745
746
747
748
749
750
751
752
753
754 public String getStringFromUI(final String key) {
755
756 ParamList uiParam = getValues();
757 return uiParam.getStringValue(key);
758 }
759
760
761
762
763
764
765 public class ComponentList extends LinkedStringMap<Component> {
766
767
768 private static final long serialVersionUID = -5157229346595354602L;
769
770 }
771 }