1
- package tudelft .ti2806 .pl3 .detailView ;
1
+ package tudelft .ti2806 .pl3 .detailview ;
2
2
3
3
import tudelft .ti2806 .pl3 .data .wrapper .WrapperClone ;
4
4
5
- import java .awt .Component ;
6
5
import java .awt .Container ;
7
6
import java .awt .Dimension ;
7
+ import java .util .Arrays ;
8
+ import java .util .Collection ;
9
+ import java .util .Comparator ;
8
10
import java .util .HashSet ;
9
11
import java .util .Iterator ;
10
12
import java .util .Set ;
11
13
import javax .swing .BorderFactory ;
12
14
import javax .swing .BoxLayout ;
13
15
import javax .swing .JLabel ;
16
+ import javax .swing .JList ;
14
17
import javax .swing .JPanel ;
18
+ import javax .swing .JScrollPane ;
19
+ import javax .swing .ListSelectionModel ;
15
20
16
21
/**
17
22
* This view is added when more details about a node are needed.
18
23
* @author mathieu
19
24
*/
20
25
public class DetailView extends JPanel {
21
26
private static final int BORDER = 10 ;
22
- private static final int MAX_ROWS = 5 ;
23
27
private static final int POSITION_OFFSET = 30 ;
28
+ public static final String REF_GENOME = "REF" ;
29
+
30
+ /**
31
+ * Custom string comparator to prioritize the TKK_REF genome string.
32
+ */
33
+ private Comparator <String > comparator = (o1 , o2 ) -> {
34
+ if (o1 .contains (REF_GENOME )) {
35
+ return -1 ;
36
+ } else if (o2 .contains (REF_GENOME )) {
37
+ return 1 ;
38
+ }
39
+ return o1 .compareTo (o2 );
40
+ };
24
41
25
42
/**
26
43
* Constructs a DetailView and set a border and the preferred layout.
27
44
*/
28
45
public DetailView () {
29
- BoxLayout layout = new BoxLayout (this , BoxLayout .Y_AXIS );
46
+ BoxLayout layout = new BoxLayout (this , BoxLayout .X_AXIS );
30
47
setLayout (layout );
31
48
setBorder (BorderFactory .createEmptyBorder (BORDER , BORDER , BORDER , BORDER ));
32
49
}
@@ -43,67 +60,39 @@ public DetailView() {
43
60
public void setNode (WrapperClone node , int x , int y ) {
44
61
removeAll ();
45
62
46
- Set <String > genomes = new HashSet <>();
47
- node .getGenome ().forEach (genome -> genomes .add (genome .getIdentifier ()));
48
- addLabels ("Genomes:" , genomes , "TKK_REF" );
49
-
50
- if (node .getLabels ().size () > 0 ) {
51
- add (" " );
52
- }
53
-
54
- Set <String > labels = new HashSet <>();
55
- node .getLabels ().forEach (label -> labels .add (label .getText ()));
56
- addLabels ("Labels:" , labels );
63
+ addList (node .getGenome (), "Genomes: " );
64
+ addList (node .getLabels (), "Labels: " );
57
65
58
66
Dimension size = getPreferredSize ();
59
67
setBounds (x , y , size .width , size .height );
60
68
}
61
69
62
70
/**
63
- * Same as addLabels(String, Set, String) but without a filter.
64
- */
65
- private void addLabels (String title , Set <String > set ) {
66
- addLabels (title , set , null );
67
- }
68
-
69
- /**
70
- * Add JLabels for every object in the given set.
71
+ * Adds a collection as a scrollable list to itself.
72
+ * @param collection
73
+ * the collection to be added as list.
71
74
* @param title
72
- * Label to be displayed above the list of labels.
73
- * @param set
74
- * The set of objects to be displayed in the list.
75
- * @param filter
76
- * A string that will always be displayed if found in the set.
75
+ * if not null, this string will be added above the list as header.
77
76
*/
78
- private void addLabels ( String title , Set < String > set , String filter ) {
79
- if (set .size () > 0 ) {
80
- add ( title ) ;
77
+ private void addList ( Collection collection , String title ) {
78
+ if (collection .size () == 0 ) {
79
+ return ;
81
80
}
82
-
83
- Iterator <String > iterator = set .iterator ();
84
- int i = 0 ;
85
- while (iterator .hasNext ()) {
86
- String nextString = iterator .next ();
87
- if (filter != null && filter .equals (nextString )) {
88
- filter = null ;
89
- if (i >= MAX_ROWS ) {
90
- add (nextString );
91
- i ++;
92
- break ;
93
- }
94
- }
95
- if (i < MAX_ROWS ) {
96
- add (nextString );
97
- i ++;
98
- }
81
+ Set <String > stringSet = new HashSet <>(collection .size ());
82
+ Iterator iterator = collection .iterator ();
83
+ for (int i = 0 ; i < collection .size (); i ++) {
84
+ stringSet .add (iterator .next ().toString ());
99
85
}
100
- if (i < set .size ()) {
101
- add ("..." );
86
+ String [] array = stringSet .toArray (new String [stringSet .size ()]);
87
+ Arrays .sort (array , comparator );
88
+ JList <Object > list = new JList <>(array );
89
+ list .setSelectionMode (ListSelectionModel .SINGLE_INTERVAL_SELECTION );
90
+ list .setLayoutOrientation (JList .VERTICAL );
91
+ JScrollPane listScroller = new JScrollPane (list );
92
+ if (title != null ) {
93
+ listScroller .setColumnHeaderView (new JLabel (title ));
102
94
}
103
- }
104
-
105
- public Component add (String label ) {
106
- return super .add (new JLabel (label ));
95
+ add (listScroller );
107
96
}
108
97
109
98
@ Override
0 commit comments