-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeJ_.java
More file actions
1593 lines (1373 loc) · 47.4 KB
/
TreeJ_.java
File metadata and controls
1593 lines (1373 loc) · 47.4 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//package runTreeJ;
/**
* @see https://imagej.net/plugins/treej
* @author Elise laruelle
* @since 2015
* @version v2 (includes the annotation feature)
*/
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.gui.GenericDialog;
import ij.gui.ImageCanvas;
import ij.gui.StackWindow;
import ij.io.LogStream;
import ij.io.DirectoryChooser;
import ij.plugin.PlugIn;
import ij.process.ImageConverter;
import ij.WindowManager;
import ij.measure.Calibration;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.plaf.basic.BasicFileChooserUI;
import javax.swing.plaf.FileChooserUI;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
import javax.swing.SwingWorker;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.event.WindowEvent;
import java.awt.Checkbox;
import java.awt.Dimension;
import java.awt.TextField;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;
import java.lang.System;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.Math;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.DocumentException;
public class TreeJ_ implements PlugIn {
/**
* This class create node to build the tree.
* adapted from http://stackoverflow.com/questions/4965335/how-to-print-binary-tree-diagram/8948691#8948691
*
*/
public static class Node {
Integer value;
String Note;
Node left, right;
public Node() {
}
public Node(int cellLabel) {
this.value = cellLabel;
this.Note = "";
}
public Node(int motherLabel, Node daughter1, Node daughter2) {
this.value = motherLabel;
this.left = daughter1;
this.right = daughter2;
this.Note = "";
}
public Node(int motherLabel, Node daughter1, Node daughter2, String note) {
this.value = motherLabel;
this.left = daughter1;
this.right = daughter2;
this.Note = note;
}
public void insertOneDaughter(Node Daughter) {
if (this.left == null) {
this.left = Daughter;
} else if (this.right == null) {
this.right = Daughter;
} else {
IJ.log("erreur : more than 2 daughter cells ?! ");
}
}
public void printTree(StringWriter out) {
if (this.right != null) {
this.right.printTree(out, true, "");
}
printNodeValue(out);
if (this.left != null) {
this.left.printTree(out, false, "");
}
}
private void printNodeValue(StringWriter out) {
if (this.value == null) {
out.write("<null>");
} else {
if (this.Note.isEmpty()) {
out.write(Integer.toString(this.value));
} else {
out.write(this.Note + "(" + Integer.toString(this.value) + ")");
}
}
out.write('\n');
}
// use string and not stringbuffer on purpose as we need to change the indent at
// each recursion
private void printTree(StringWriter out, boolean isRight, String indent) {
if (this.right != null) {
this.right.printTree(out, true, indent + (isRight ? " " : " | "));
}
out.write(indent);
if (isRight) {
out.write(" /");
} else {
out.write(" \\");
}
out.write("----- ");
printNodeValue(out);
if (this.left != null) {
this.left.printTree(out, false, indent + (isRight ? " | " : " "));
}
}
}
// ------------------ End Node class
/**
* The Tree class contains attributes and methods to build the tree and to maintain it.
* @param fathers contains the mother cell label per cell (the treeV format)
* @param notes contains the annotation per cell (includes in the treeV format)
* @param listNode contains the node address per cell
* @param nextFather is the next label number
*/
public static class Tree {
int max;
// variables to use for the Lineage
java.util.List<Integer> fathers;
java.util.List<String> notes;
java.util.List<Node> listNode;
int nextFather;
StringWriter str;
public Tree() {
this.max = 0;
}
public void initialize(ImageStack inputStackCopy) {
TreeSet<Integer> labelsTree = new TreeSet<Integer>();// from David Legland
// ijpb-plugins/src/main/java/inra/ijpb/morphology/LabelImages.java
int[] array;
int sizeX = inputStackCopy.getWidth();
int sizeY = inputStackCopy.getHeight();
int sizeZ = inputStackCopy.getSize();
IJ.log("step 3");
// iterate on image pixels
for (int z = 0; z < sizeZ; z++) {
IJ.showProgress(z, sizeZ);
for (int y = 0; y < sizeY; y++)
for (int x = 0; x < sizeX; x++)
labelsTree.add((int) inputStackCopy.getVoxel(x, y, z));
}
IJ.showProgress(1);
IJ.log("step 4");
/**
* remove 0 if it exists if (labels.contains(0)) labels.remove(0);
**/
// convert to array of integers
array = new int[labelsTree.size()];
this.fathers = new ArrayList<Integer>(labelsTree.size());
this.notes = new ArrayList<String>(labelsTree.size() + 1);
Iterator<Integer> iterator = labelsTree.iterator();
IJ.log("step 5");
for (int i = 0; i < labelsTree.size(); i++) {
array[i] = iterator.next();
if (array[i] > this.max) {
this.max = array[i];
}
}
this.listNode = new ArrayList<Node>();
IJ.log("step 6");
for (int i = 0; i <= this.max; i++) {
this.newEmptyCell(i);
}
// notes.add("");
IJ.log("nb of label " + labelsTree.size() + "");
IJ.log("label number max : " + max + "");
IJ.log("nb of label (with zero (as background?)) : " + fathers.size() + "");
IJ.log("step 7");
this.nextFather = this.max + 1;
this.str = new StringWriter();
}
public void newEmptyCell(int label) {
this.listNode.add(new Node(label));
this.fathers.add(0);
this.notes.add("");
}
public void newMotherCell(int label, Node Daughter1, Node Daughter2, String note) {
this.listNode.add(new Node(label, Daughter1, Daughter2, note));
this.fathers.add(0);
this.notes.add(note);
}
public void resetTree() {
for (int i = 0; i < this.listNode.size(); i++) {
this.listNode.get(i).left = null;
this.listNode.get(i).right = null;
this.listNode.get(i).Note = "";
}
}
public void clearTree() {
for (int i = 0; i < this.listNode.size(); i++) {
this.fathers.clear();
this.notes.clear();
this.resetTree();
}
}
public void unlinkFromMother(int motherLabel) {
do {
this.listNode.get(motherLabel).left = null;
this.listNode.get(motherLabel).right = null;
this.listNode.get(motherLabel).Note = "";
this.notes.set(motherLabel, "");
for (int i = 0; i < this.fathers.size(); i++) {
if (this.fathers.get(i) == motherLabel) {
this.fathers.set(i, 0);
}
}
motherLabel = this.fathers.get(motherLabel);
} while (motherLabel != 0);
}
public void writeStrTree() {
this.str.getBuffer().setLength(0);
for (int i = 0; i < this.fathers.size(); i++) {
if (this.fathers.get(i) == 0 && this.listNode.get(i).left != null) {
this.listNode.get(i).printTree(this.str);
}
}
}
public void writeToPDF(String filePathName) {
this.writeStrTree();
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(filePathName));
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
document.open();
try {
document.add(new Paragraph(this.str.toString()));
} catch (DocumentException e) {
e.printStackTrace();
}
document.close();
}
public void writeToTreeV(File file) {
try (PrintWriter writer = new PrintWriter(file)) {
int i;
for (i = 1; i < this.fathers.size(); i++) {
writer.print(Integer.toString(this.fathers.get(i)));
writer.print("\t");
}
writer.print("\n");
for (i = 1; i < this.fathers.size(); i++) {
writer.print(this.notes.get(i));
writer.print("\t");
}
writer.close();
IJ.log("file save as : " + file.getName());
} catch (FileNotFoundException ex) {
IJ.log("error");
System.out.println("file error in filiationTool");
IJ.error("file error in filiationTool", "FileNotFoundException");
}
}
public void writeTreeToNwk(File file) {
try {
PrintWriter writer = new PrintWriter(file);
for (int i = 1; i < this.fathers.size(); i++) {
if (this.fathers.get(i) == 0 && this.listNode.get(i).left != null) {
IJ.log("root" + i);
String a = this.buildStrTree(this.listNode.get(i)) + ";\n";
writer.print(a);
}
}
writer.close();
IJ.log("file save as : " + file.getName());
} catch (FileNotFoundException ex) {
IJ.log("error");
System.out.println("file error in filiationTool");
IJ.error("file error in filiationTool", "FileNotFoundException");
}
}
private String buildStrTree(Node t) {
// for nwk format
String strTree = "";
if (t.left == null && t.right == null) {
IJ.log(Integer.toString(t.value));
strTree = Integer.toString(t.value);
} else {
strTree = "(" + this.buildStrTree(t.left) + "," + this.buildStrTree(t.right) + ")"
+ Integer.toString(t.value);
}
return strTree;
}
public void readTreeV(String file) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
try {
String line = br.readLine();
int lineNumber = 0;
while (line != null) {
if (lineNumber == 0) {
String everything = line.toString();
// System.out.println(everything);
String[] everythingParse = everything.split("[\t| ]");
// System.out.println(everythingParse.length);
// fathers = new ArrayList<Integer>(everythingParse.length);
this.fathers.add(0); // <--- because the treeV array mean [P(1) P(2) ...P(n)] where P() mean
// the father and here father is [P(index)] (index start from zero)
this.notes.add("");
for (int i = 0; i < everythingParse.length; i++) {
this.fathers.add(Integer.parseInt(everythingParse[i]));
this.notes.add("");
}
} else if (lineNumber == 1) {
// extract notes
String everything = line.toString();
// System.out.println(everything);
String tmp = "";
int i = 1;
while (everything.length() != 0) {
if (i > this.fathers.size()) {
System.out.println("error : more tags than cells ");
}
if (everything.charAt(0) != '\t') {
tmp = tmp.concat(String.valueOf(everything.charAt(0)));
} else {
this.notes.set(i, tmp);
tmp = "";
i = i + 1;
}
everything = everything.substring(1);
}
/*
* // alternative way for (int i = 1; i < fathers.size(); i++) {
*
* if ( everything.charAt(0) != '\t') {
* notes.set(i,String.valueOf(everything.charAt(0))); everything =
* everything.substring(1); listNode.get(i).Note =
* String.valueOf(everything.charAt(0)); } everything = everything.substring(1);
*
* }
*/
} else {
System.out.println("error : more than two lines in the treeV file : not processed ");
}
line = br.readLine();
lineNumber = lineNumber + 1;
}
br.close();
// notes.add("");
} catch (IOException ex) {
System.out.println("error :IOException");
return;
}
} catch (FileNotFoundException ex) {
System.out.println("error :FileNotFoundException");
return;
}
}
public void readNwk(String file) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
try {
String line = br.readLine();
br.close();
// parse string :
java.util.List<String> everythingParse = new ArrayList<String>();
String tmp = "";
int fmax = 0;
while (line.length() != 0) {
if (Character.isDigit(line.charAt(0))) {
while (Character.isDigit(line.charAt(0))) {
tmp = tmp.concat(String.valueOf(line.charAt(0)));
line = line.substring(1);
}
everythingParse.add(tmp);
if (Integer.parseInt(tmp) > fmax) {
fmax = Integer.parseInt(tmp);
}
tmp = "";
} else {
everythingParse.add(String.valueOf(line.charAt(0)));
line = line.substring(1);
}
}
System.out.println(everythingParse);
// create null father list :
for (int i = 0; i <= fmax; i++) {
this.fathers.add(0);
this.notes.add("");
}
// search the father for each label :
int i = 0;
for (int c = 0; c < everythingParse.size(); c++) {
if (Character.isDigit(everythingParse.get(c).charAt(0))) {
System.out.println("digit found");
i = c + 1;
while (i < everythingParse.size() && (!everythingParse.get(i).equals(")"))
&& (!everythingParse.get(i).equals(";"))) {
i = i + 1;
}
i = i + 1;
if (i >= everythingParse.size() || (everythingParse.get(i).equals(";"))) {
continue;
}
if (Character.isDigit(everythingParse.get(i).charAt(0))) {
System.out.println(Integer.parseInt(everythingParse.get(c)));
System.out.println(Integer.parseInt(everythingParse.get(i)));
this.fathers.set(Integer.parseInt(everythingParse.get(c)),
Integer.parseInt(everythingParse.get(i)));
} else if (!everythingParse.get(c).equals(";")) {
System.out.println(
"error : nwk tree has not the shape : (Sister1Label,Sister2Label)fatherLabel; ");
}
}
}
} catch (IOException ex) {
System.out.println("error :IOException");
return;
}
} catch (FileNotFoundException ex) {
System.out.println("error :FileNotFoundException");
return;
}
}
}
/**
* The CustomWindow class contains the TreeJ interface and methods for user interactions
*/
private class CustomWindow extends StackWindow {
/** to launch threads for the swing plugin methods and events */
SwingWorker<Void, Void> worker = null;
/** main panel */
Panel all = new Panel();
/** input image panel */
JPanel lineagePanel = new JPanel();
JPanel unitPanel = new JPanel();
JPanel treePanel = new JPanel();
JPanel outputImagePanel = new JPanel();
/** elements of the GUI */
JTextArea treeArea;
JScrollPane scrollPane;
JTextField daughter1Text;
JTextField daughter2Text;
JTextField unitLabel;
JTextField note;
JTextField unitNote;
JTextField rootLabel;
JTextField tagILabel;
/** fields label */
JLabel daughter1Label;
JLabel daughter2Label;
JLabel unitLabelText;
JLabel noteText;
JLabel unitNoteText;
JLabel rootText;
JLabel tagIText;
/** buttons */
JCheckBox assemblyCheckBox;
JCheckBox orphanCheckBox;
JButton linkButton;
JButton tagButton;
JButton reset1Button;
JButton reset2Button;
JButton unlinkButton;
JButton createFileButton;
JButton createPDFButton;
JButton extractImageButton;
JButton pursueFiliationButton;
JButton extractsubImageButton;
JButton extractTagCellsButton;
/** text of buttons */
private String linkButtonText = "Apply";
private String unlinkButtonText = "Unpair";
private String tagButtonText = "Apply";
private String reset1ButtonText = "Reset";
private String reset2ButtonText = "Reset";
private String createFileButtonText = "Save";
private String createPDFButtonText = "Export to PDF";
private String extractImageButtonText = "Current view";
private String extractsubImageButtonText = "Subtree ";
private String extractTagCellsButtonText = "From tag";
private String pursueFiliationButtonText = "Load";
/** flag to select/deselect the assembly selection options */
private boolean assemblySelection = false;
/** flag to select/deselect the orphan selection options */
private boolean orphanSelection = false;
int nOut = 0; // count of the created output images
CustomWindow(ImagePlus imp) {
super(imp, new ImageCanvas(imp));
final ImageCanvas canvas = (ImageCanvas) getCanvas();
setTitle("TreeJ");
treeArea = new JTextArea(20, 15);
scrollPane = new JScrollPane(treeArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(3, 3, 300, 200);
linkButton = new JButton(linkButtonText);
linkButton.addActionListener(listener);
linkButton.setToolTipText("construct a tree from label 1 and label 2");
unlinkButton = new JButton(unlinkButtonText);
unlinkButton.addActionListener(listener);
unlinkButton.setToolTipText("dismantle the label mother");
tagButton = new JButton(tagButtonText);
tagButton.addActionListener(listener);
tagButton.setToolTipText("tag the label cell");
createFileButton = new JButton(createFileButtonText);
createFileButton.addActionListener(listener);
createPDFButton = new JButton(createPDFButtonText);
createPDFButton.addActionListener(listener);
reset1Button = new JButton(reset1ButtonText);
reset1Button.addActionListener(listener);
reset1Button.setToolTipText("reset label numbers and tag");
reset2Button = new JButton(reset2ButtonText);
reset2Button.addActionListener(listener);
reset2Button.setToolTipText("reset label number and tag");
extractImageButton = new JButton(extractImageButtonText);
extractImageButton.addActionListener(listener);
pursueFiliationButton = new JButton(pursueFiliationButtonText);
pursueFiliationButton.addActionListener(listener);
extractsubImageButton = new JButton(extractsubImageButtonText);
extractsubImageButton.addActionListener(listener);
extractTagCellsButton = new JButton(extractTagCellsButtonText);
extractTagCellsButton.addActionListener(listener);
assemblyCheckBox = new JCheckBox("daughter cell selection", assemblySelection);
assemblyCheckBox.addActionListener(listener);
orphanCheckBox = new JCheckBox("orphan cell selection", orphanSelection);
orphanCheckBox.addActionListener(listener);
// daughter cell values
daughter1Label = new JLabel("Label 1");
// daughter1Label.setToolTipText( "label number" );
daughter1Text = new JTextField("0", 3);
daughter1Text.setToolTipText("label number");
daughter2Label = new JLabel("Label 2");
// daughter2Label.setToolTipText( "label number" );
daughter2Text = new JTextField("0", 3);
daughter2Text.setToolTipText("label number");
unitLabelText = new JLabel("Label");
// daughter1Label.setToolTipText( "label number" );
unitLabel = new JTextField("0", 3);
unitLabel.setToolTipText("label number");
noteText = new JLabel("Tag");
// noteText.setToolTipText( "" );
note = new JTextField("", 3);
note.setToolTipText(" Comments on the mother cell ");
unitNoteText = new JLabel("Tag");
// noteText.setToolTipText( "" );
unitNote = new JTextField("", 3);
unitNote.setToolTipText(" Comments on the mother cell ");
rootText = new JLabel("Root");
rootLabel = new JTextField("0", 3);
rootLabel.setMinimumSize(rootLabel.getPreferredSize());
rootLabel.setToolTipText("root label number");
tagIText = new JLabel("Tag");
tagILabel = new JTextField("", 3);
tagILabel.setMinimumSize(tagILabel.getPreferredSize());
tagILabel.setToolTipText("tag to extract");
treeArea.setBorder(BorderFactory.createTitledBorder("Tree"));
GridBagLayout treeLayout = new GridBagLayout();
GridBagConstraints treeConstraints = new GridBagConstraints();
treeConstraints.anchor = GridBagConstraints.WEST;
treeConstraints.gridwidth = 1;
treeConstraints.gridheight = 1;
treeConstraints.gridx = 0;
treeConstraints.gridy = 0;
treeArea.setLayout(treeLayout);
treeArea.setMinimumSize(treeArea.getPreferredSize());
GridBagLayout treeFLayout = new GridBagLayout();
GridBagConstraints treeFConstraints = new GridBagConstraints();
treeFConstraints.fill = GridBagConstraints.BOTH;
treeFConstraints.gridwidth = 1;
treeFConstraints.gridheight = 1;
treeFConstraints.gridx = 0;
treeFConstraints.gridy = 0;
treeFConstraints.insets = new Insets(5, 5, 6, 6);
treePanel.setLayout(treeFLayout);
treePanel.add(pursueFiliationButton, treeFConstraints);
treeFConstraints.gridx++;
treePanel.add(createFileButton, treeFConstraints);
treeFConstraints.gridx++;
treePanel.add(createPDFButton, treeFConstraints);
treePanel.setMinimumSize(treePanel.getPreferredSize());
lineagePanel.setBorder(BorderFactory.createTitledBorder("Pair cells"));
GridBagLayout lineageLayout = new GridBagLayout();
GridBagConstraints lineageConstraints = new GridBagConstraints();
lineageConstraints.fill = GridBagConstraints.BOTH;
lineageConstraints.gridwidth = 1;
lineageConstraints.gridheight = 1;
lineageConstraints.gridx = 0;
lineageConstraints.gridy = 0;
lineageConstraints.insets = new Insets(5, 5, 6, 6);
lineagePanel.setLayout(lineageLayout);
lineagePanel.add(daughter1Label, lineageConstraints);
lineageConstraints.gridx++;
lineagePanel.add(daughter1Text, lineageConstraints);
lineageConstraints.gridx--;
lineageConstraints.gridy++;
lineagePanel.add(daughter2Label, lineageConstraints);
lineageConstraints.gridx++;
lineagePanel.add(daughter2Text, lineageConstraints);
lineageConstraints.gridx--;
lineageConstraints.gridy++;
lineagePanel.add(noteText, lineageConstraints);
lineageConstraints.gridx++;
lineagePanel.add(note, lineageConstraints);
lineageConstraints.gridx++;
lineagePanel.add(tagButton, lineageConstraints);
lineageConstraints.gridx--;
lineageConstraints.gridx--;
lineageConstraints.gridy++;
lineagePanel.add(linkButton, lineageConstraints);
lineageConstraints.gridx++;
lineageConstraints.gridwidth = 2;
lineagePanel.add(reset1Button, lineageConstraints);
lineagePanel.setMinimumSize(lineagePanel.getPreferredSize());
unitPanel.setBorder(BorderFactory.createTitledBorder("Unpair or tag"));
unitPanel.setToolTipText("activate with ctrl keyboard");
GridBagLayout unitLayout = new GridBagLayout();
GridBagConstraints unitConstraints = new GridBagConstraints();
unitConstraints.fill = GridBagConstraints.BOTH;
unitConstraints.gridwidth = 1;
unitConstraints.gridheight = 1;
unitConstraints.gridx = 0;
unitConstraints.gridy = 0;
unitConstraints.insets = new Insets(5, 5, 6, 6);
unitPanel.setLayout(unitLayout);
unitConstraints.gridx = 0;
unitConstraints.gridy = 0;
unitPanel.add(unitLabelText, unitConstraints);
unitConstraints.gridx++;
unitPanel.add(unitLabel, unitConstraints);
unitConstraints.gridy++;
unitPanel.add(unlinkButton, unitConstraints);
unitConstraints.gridx--;
unitConstraints.gridy++;
unitPanel.add(unitNoteText, unitConstraints);
unitConstraints.gridx++;
unitPanel.add(unitNote, unitConstraints);
unitConstraints.gridy++;
unitConstraints.gridx--;
unitPanel.add(tagButton, unitConstraints);
unitConstraints.gridx++;
unitPanel.add(reset2Button, unitConstraints);
unitPanel.setMinimumSize(unitPanel.getPreferredSize());
outputImagePanel.setBorder(BorderFactory.createTitledBorder(" output image"));
GridBagLayout outputImageLayout = new GridBagLayout();
GridBagConstraints outputImageConstraints = new GridBagConstraints();
outputImageConstraints.anchor = GridBagConstraints.WEST;
outputImageConstraints.fill = GridBagConstraints.BOTH;
outputImageConstraints.gridwidth = 1;
outputImageConstraints.gridheight = 1;
outputImageConstraints.gridx = 0;
outputImageConstraints.gridy = 0;
outputImageConstraints.insets = new Insets(5, 5, 6, 6);
outputImagePanel.setLayout(outputImageLayout);
outputImageConstraints.gridwidth = 1;
outputImagePanel.add(extractImageButton, outputImageConstraints);
outputImageConstraints.gridy++;
outputImagePanel.add(extractsubImageButton, outputImageConstraints);
outputImageConstraints.gridx++;
outputImagePanel.add(rootText, outputImageConstraints);
outputImageConstraints.gridx++;
outputImagePanel.add(rootLabel, outputImageConstraints);
outputImageConstraints.gridx--;
outputImageConstraints.gridx--;
outputImageConstraints.gridy++;
outputImagePanel.add(extractTagCellsButton, outputImageConstraints);
outputImageConstraints.gridx++;
outputImagePanel.add(tagIText, outputImageConstraints);
outputImageConstraints.gridx++;
outputImagePanel.add(tagILabel, outputImageConstraints);
outputImagePanel.setMinimumSize(outputImagePanel.getPreferredSize());
// Main panel (including parameters panel and canvas)
GridBagLayout layout = new GridBagLayout();
GridBagConstraints allConstraints = new GridBagConstraints();
all.setLayout(layout);
// put parameter panel in place
allConstraints.anchor = GridBagConstraints.NORTHWEST;
allConstraints.fill = GridBagConstraints.BOTH;
allConstraints.gridwidth = 1;
allConstraints.gridheight = 3;
allConstraints.gridx = 0;
allConstraints.gridy = 0;
allConstraints.weightx = 1;
allConstraints.weighty = 3;
all.add(scrollPane, allConstraints);
allConstraints.gridy = 3;
allConstraints.gridheight = 1;
allConstraints.fill = GridBagConstraints.HORIZONTAL;
all.add(treePanel, allConstraints);
// put canvas in place
allConstraints.gridx++;
allConstraints.gridy = 0;
allConstraints.weightx = 1;
allConstraints.weighty = 1;
allConstraints.gridheight = 3;
all.add(canvas, allConstraints);
canvas.addMouseListener(mlAdd);
allConstraints.gridy = 3;
allConstraints.weightx = 0;
allConstraints.weighty = 0;
allConstraints.gridheight = 0;
// if the input image is 3d, put the slice selectors in place
if (null != super.sliceSelector) {
sliceSelector.setValue(inputImage.getCurrentSlice());
displayImage.setSlice(inputImage.getCurrentSlice());
all.add(super.sliceSelector, allConstraints);
if (null != super.zSelector)
all.add(super.zSelector, allConstraints);
if (null != super.tSelector)
all.add(super.tSelector, allConstraints);
if (null != super.cSelector)
all.add(super.cSelector, allConstraints);
}
allConstraints.gridy = 0;
allConstraints.gridx = 2;
allConstraints.weightx = 1;
allConstraints.weighty = 1;
allConstraints.gridheight = 1;
all.add(lineagePanel, allConstraints);
allConstraints.gridy++;
allConstraints.weightx = 1;
allConstraints.weighty = 1;
allConstraints.gridheight = 1;
all.add(unitPanel, allConstraints);
allConstraints.gridy++;
allConstraints.weightx = 1;
allConstraints.weighty = 1;
allConstraints.gridheight = 1;
all.add(outputImagePanel, allConstraints);
GridBagLayout wingb = new GridBagLayout();
GridBagConstraints winc = new GridBagConstraints();
winc.anchor = GridBagConstraints.NORTHWEST;
winc.fill = GridBagConstraints.HORIZONTAL;
winc.weightx = 1;
winc.weighty = 1;
setLayout(wingb);
add(all, winc);
// Fix minimum size to the preferred size at this point
pack();
setMinimumSize(getPreferredSize());
all.addComponentListener( new ComponentAdapter()
{
public void componentResized(ComponentEvent evt)
{
Dimension size = lineagePanel.getSize();
if (size.getWidth() == 0)
{
canvas.unzoom();
}
}
});
}
/**
* Listener for the GUI buttons
*/
private ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
final String command = e.getActionCommand();
// listen to the buttons on separate threads not to block
// the event dispatch thread
worker = new SwingWorker<Void, Void>() {
@Override
public Void doInBackground() {
// IJ.log(""+e.getSource()+"");
if (e.getSource() == linkButton) {
link(command);
} else if (e.getSource() == unlinkButton) {
unlink(command);
} else if (e.getSource() == tagButton) {
tag(command);
} else if (e.getSource() == extractImageButton) {
extractImage(command);
} else if (e.getSource() == extractsubImageButton) {
extractPartialImage(command);
} else if (e.getSource() == extractTagCellsButton) {
extractTag(command);
} else if (e.getSource() == assemblyCheckBox) {
assemblySelection = !assemblySelection;
if (assemblySelection) {
ic.addMouseListener(mlAdd);
}
} else if (e.getSource() == orphanCheckBox) {
orphanSelection = !orphanSelection;
if (orphanSelection) {
ic.addMouseListener(mlAdd);
}
} else if (e.getSource() == reset1Button) {
reset1(command);
} else if (e.getSource() == reset2Button) {
reset2(command);
}
return null;
}
};
worker.execute();
while (worker.isDone() == false) {
// IJ.log("wait");
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// IJ.log(""+e.getSource()+"");
if (e.getSource() == pursueFiliationButton) {
pursueFiliation(command);
} else if (e.getSource() == createFileButton) {
createFile(command);
} else if (e.getSource() == createPDFButton) {
createTreePDF(command);
}
}
});
}
};
MouseListener mlAdd = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent eImg) {
// IJ.log(""+eImg.getSource()+"");
int xi = eImg.getX();
int yi = eImg.getY();
int ox = imp.getWindow().getCanvas().offScreenX(xi);
int oy = imp.getWindow().getCanvas().offScreenY(yi);
int zi = displayImage.getCurrentSlice();
ImageStack imgStack = displayImage.getStack();
// markersImage.setDefault16bitRange(16);
int label = (int) imgStack.getVoxel(ox, oy, zi - 1);
// IJ.log(""+eImg.getModifiers()+"");
if (eImg.getModifiers() == 18 || eImg.getModifiers() == 20) {
unitLabel.setText(Integer.toString(label));
} else {
if (daughter1Text.getText().equals("0")) {
daughter1Text.setText(Integer.toString(label));
} else {
daughter2Text.setText(Integer.toString(label));
}
}
}
};
private void reset1(String command) {
daughter1Text.setText(Integer.toString(0));
daughter2Text.setText(Integer.toString(0));
note.setText("");
}