6
6
import org .jetbrains .annotations .Nullable ;
7
7
8
8
import javax .swing .*;
9
+ import javax .swing .event .DocumentEvent ;
10
+ import javax .swing .event .DocumentListener ;
11
+ import javax .swing .table .DefaultTableModel ;
12
+ import javax .swing .table .TableRowSorter ;
9
13
import java .util .List ;
14
+ import java .util .stream .IntStream ;
10
15
11
16
/**
12
- * Dialog for choosing branches
17
+ * Dialog for choosing branches
13
18
*
14
19
* @author Opher Vishnia / opherv.com / [email protected]
15
20
*/
16
21
17
22
public class GitflowBranchChooseDialog extends DialogWrapper {
18
23
private JPanel contentPane ;
19
- private JList branchList ;
20
-
24
+ private JTextField searchField ;
25
+ //using a single column table because it has built in sorting and filtering capabilities.
26
+ private JTable branchList ;
27
+ private JScrollPane scrollpane ;
28
+ private JPanel branchPanel ;
21
29
22
30
public GitflowBranchChooseDialog (Project project , List <String > branchNames ) {
23
31
super (project , true );
24
32
25
33
setModal (true );
26
34
27
35
setTitle ("Choose Branch" );
28
- branchList .setListData (branchNames .toArray ());
29
-
36
+ initBranchList (branchNames );
30
37
init ();
31
38
}
32
39
33
40
@ Nullable
34
41
@ Override
35
42
protected ValidationInfo doValidate () {
36
- if (branchList .getSelectedValue () == null ) {
37
- return new ValidationInfo ("No branch selected!" );
43
+ if (branchList .getSelectedRow () == - 1 ) {
44
+ return new ValidationInfo ("No branch selected!" );
38
45
} else {
39
46
return null ;
40
47
}
@@ -46,7 +53,53 @@ protected JComponent createCenterPanel() {
46
53
return contentPane ;
47
54
}
48
55
49
- public String getSelectedBranchName (){
50
- return branchList .getSelectedValue ().toString ();
56
+ public String getSelectedBranchName () {
57
+ int selectedRow = branchList .getSelectedRow ();
58
+ return (String ) branchList .getValueAt (selectedRow , 0 );
59
+ }
60
+
61
+ private void initBranchList (List <String > branchNames ) {
62
+ branchList .setTableHeader (null );
63
+ DefaultTableModel model = (DefaultTableModel ) branchList .getModel ();
64
+ //only one column. No Header.
65
+ model .setColumnCount (1 );
66
+ for (String branchName : branchNames ) {
67
+ model .addRow (new String []{branchName });
68
+ }
69
+ //sort on first (and only column)
70
+ TableRowSorter <DefaultTableModel > rowSorter = new TableRowSorter <>();
71
+ rowSorter .setModel (model );
72
+ branchList .setRowSorter (rowSorter );
73
+ branchList .getRowSorter ().toggleSortOrder (0 );
74
+ //add filtering capabilities.
75
+ searchField .getDocument ().addDocumentListener (
76
+ new DocumentListener () {
77
+ @ Override
78
+ public void insertUpdate (DocumentEvent documentEvent ) {
79
+ filter ();
80
+ }
81
+
82
+ @ Override
83
+ public void removeUpdate (DocumentEvent documentEvent ) {
84
+ filter ();
85
+ }
86
+
87
+ @ Override
88
+ public void changedUpdate (DocumentEvent documentEvent ) {
89
+ filter ();
90
+ }
91
+
92
+ private void filter () {
93
+ String text = searchField .getText ();
94
+ if (text .trim ().length () == 0 ) {
95
+ rowSorter .setRowFilter (null );
96
+ } else {
97
+ rowSorter .setRowFilter (RowFilter .regexFilter ("(?i)" + text ));
98
+ }
99
+ }
100
+ }
101
+ );
51
102
}
103
+
104
+
52
105
}
0 commit comments