11
11
import java .awt .Component ;
12
12
import java .awt .Dimension ;
13
13
import java .awt .Font ;
14
+ import java .awt .Insets ;
14
15
import java .awt .event .ActionEvent ;
15
16
import java .awt .event .ActionListener ;
16
17
import java .awt .event .MouseAdapter ;
19
20
import java .awt .event .ComponentListener ;
20
21
import java .io .IOException ;
21
22
import java .util .Collections ;
23
+ import java .util .HashMap ;
22
24
import java .util .List ;
25
+ import java .util .Map ;
23
26
24
27
import javax .swing .BoxLayout ;
28
+ import javax .swing .DefaultListCellRenderer ;
25
29
import javax .swing .JButton ;
26
30
import javax .swing .JFrame ;
27
31
import javax .swing .JLabel ;
28
32
import javax .swing .JList ;
29
33
import javax .swing .JPanel ;
30
34
import javax .swing .JScrollPane ;
31
- import javax .swing .ListCellRenderer ;
32
35
import javax .swing .ListModel ;
33
36
import javax .swing .ListSelectionModel ;
34
37
import javax .swing .border .EmptyBorder ;
38
+ import javax .swing .border .MatteBorder ;
35
39
import org .openide .util .Exceptions ;
36
40
37
41
public class OrganizationListWindow extends JPanel {
38
42
39
43
private static JFrame frame ;
40
44
private final JLabel title ;
41
- private final JList <OrganizationCard > organizations ;
45
+ private final JList <Organization > organizations ;
42
46
private static JButton button ;
43
47
44
48
public OrganizationListWindow (List <Organization > organizations ) {
45
49
this .title = new JLabel ("Select an organization:" );
46
50
Font titleFont = this .title .getFont ();
47
51
this .title .setFont (new Font (titleFont .getName (), Font .BOLD , 20 ));
48
- OrganizationCard [] organizationCards = new OrganizationCard [ organizations . size ()] ;
52
+ this . title . setBorder ( new MatteBorder ( new Insets ( 10 , 10 , 5 , 10 ), new Color ( 242 , 241 , 240 ))) ;
49
53
Collections .sort (organizations , (a , b ) -> {
50
54
if (a .isPinned () && b .isPinned ()) {
51
55
return a .getName ().compareTo (b .getName ());
@@ -58,22 +62,25 @@ public OrganizationListWindow(List<Organization> organizations) {
58
62
}
59
63
return a .getName ().compareTo (b .getName ());
60
64
});
61
- for ( int i = 0 ; i < organizations .size (); i ++) {
62
- organizationCards [ i ] = new OrganizationCard ( organizations . get ( i ) );
63
- }
64
- this .organizations = new JList <>( organizationCards );
65
+ Organization [] orgArray = organizations . toArray ( new Organization [ organizations .size ()]);
66
+ this . organizations = new JList <>( orgArray );
67
+ this . organizations . setFixedCellHeight ( 107 );
68
+ this .organizations . setFixedCellWidth ( 346 );
65
69
this .organizations .setSelectionMode (ListSelectionModel .SINGLE_SELECTION );
66
70
setLayout (new BoxLayout (this , BoxLayout .Y_AXIS ));
67
71
this .button = new JButton ("Select" );
68
72
button .addActionListener (new SelectOrganizationListener (this ));
69
73
70
- this .organizations .setCellRenderer (new OrganizationCellRenderer ());
74
+ this .organizations .setCellRenderer (new OrganizationCellRenderer (this . organizations ));
71
75
this .organizations .setVisibleRowCount (4 );
72
76
JScrollPane pane = new JScrollPane (this .organizations );
73
77
Dimension d = pane .getPreferredSize ();
74
78
d .width = 800 ;
79
+ d .height = (int ) (d .height * 1.12 );
75
80
pane .setPreferredSize (d );
76
81
pane .setBorder (new EmptyBorder (5 , 0 , 5 , 0 ));
82
+ pane .setViewportBorder (new EmptyBorder (0 , 0 , 0 , 0 ));
83
+ pane .getVerticalScrollBar ().setUnitIncrement (10 );
77
84
this .organizations .setBackground (new Color (242 , 241 , 240 ));
78
85
79
86
this .organizations .setSelectedIndex (setDefaultSelectedIndex ());
@@ -138,9 +145,9 @@ private int setDefaultSelectedIndex() {
138
145
if (!selectedOrganization .isPresent ()) {
139
146
return 0 ;
140
147
}
141
- final ListModel <OrganizationCard > list = organizations .getModel ();
148
+ final ListModel <Organization > list = organizations .getModel ();
142
149
for (int i = 0 ; i < list .getSize (); i ++) {
143
- if (list .getElementAt (i ).getOrganization (). getName ().equals (selectedOrganization .get ().getName ())) {
150
+ if (list .getElementAt (i ).getName ().equals (selectedOrganization .get ().getName ())) {
144
151
return i ;
145
152
}
146
153
}
@@ -154,8 +161,7 @@ public SelectOrganizationListener(OrganizationListWindow window) {
154
161
155
162
@ Override
156
163
public void actionPerformed (ActionEvent e ) {
157
- final OrganizationCard organization = organizations .getSelectedValue ();
158
- setColors (organization , Color .white , Color .black );
164
+ final Organization organization = organizations .getSelectedValue ();
159
165
frame .setVisible (false );
160
166
frame .dispose ();
161
167
try {
@@ -176,32 +182,34 @@ public void actionPerformed(ActionEvent e) {
176
182
}
177
183
}
178
184
}
179
-
180
- private void setColors (OrganizationCard organization , Color background , Color foreground ) {
181
- organization .setBackground (background );
182
- for (Component c : organization .getComponents ()) {
183
- c .setForeground (foreground );
184
- }
185
- }
186
185
}
187
186
188
- class OrganizationCellRenderer extends JLabel implements ListCellRenderer {
187
+ class OrganizationCellRenderer extends DefaultListCellRenderer {
189
188
190
189
private static final Color HIGHLIGHT_COLOR = new Color (240 , 119 , 70 );
190
+ private final JList parent ;
191
+ private final Map <Organization , OrganizationCard > cachedOrgs ;
191
192
192
- public OrganizationCellRenderer () {
193
+ public OrganizationCellRenderer (JList parent ) {
194
+ this .parent = parent ;
195
+ this .cachedOrgs = new HashMap <>();
193
196
}
194
197
195
198
@ Override
196
199
public Component getListCellRendererComponent (final JList list ,
197
200
final Object value , final int index , final boolean isSelected ,
198
201
final boolean hasFocus ) {
199
- OrganizationCard organization = (OrganizationCard ) value ;
202
+ final Organization org = (Organization )value ;
203
+ if (!this .cachedOrgs .containsKey (org )) {
204
+ OrganizationCard organization = new OrganizationCard (org , parent );
205
+ this .cachedOrgs .put (org , organization );
206
+ }
207
+ OrganizationCard organizationCard = this .cachedOrgs .get (org );
200
208
if (isSelected ) {
201
- organization .setColors (Color .white , HIGHLIGHT_COLOR );
209
+ organizationCard .setColors (Color .white , HIGHLIGHT_COLOR );
202
210
} else {
203
- organization .setColors (new Color (76 , 76 , 76 ), Color .white );
211
+ organizationCard .setColors (new Color (76 , 76 , 76 ), Color .white );
204
212
}
205
- return organization ;
213
+ return organizationCard ;
206
214
}
207
215
}
0 commit comments