1111import java .awt .Component ;
1212import java .awt .Dimension ;
1313import java .awt .Font ;
14+ import java .awt .Insets ;
1415import java .awt .event .ActionEvent ;
1516import java .awt .event .ActionListener ;
1617import java .awt .event .MouseAdapter ;
1920import java .awt .event .ComponentListener ;
2021import java .io .IOException ;
2122import java .util .Collections ;
23+ import java .util .HashMap ;
2224import java .util .List ;
25+ import java .util .Map ;
2326
2427import javax .swing .BoxLayout ;
28+ import javax .swing .DefaultListCellRenderer ;
2529import javax .swing .JButton ;
2630import javax .swing .JFrame ;
2731import javax .swing .JLabel ;
2832import javax .swing .JList ;
2933import javax .swing .JPanel ;
3034import javax .swing .JScrollPane ;
31- import javax .swing .ListCellRenderer ;
3235import javax .swing .ListModel ;
3336import javax .swing .ListSelectionModel ;
3437import javax .swing .border .EmptyBorder ;
38+ import javax .swing .border .MatteBorder ;
3539import org .openide .util .Exceptions ;
3640
3741public class OrganizationListWindow extends JPanel {
3842
3943 private static JFrame frame ;
4044 private final JLabel title ;
41- private final JList <OrganizationCard > organizations ;
45+ private final JList <Organization > organizations ;
4246 private static JButton button ;
4347
4448 public OrganizationListWindow (List <Organization > organizations ) {
4549 this .title = new JLabel ("Select an organization:" );
4650 Font titleFont = this .title .getFont ();
4751 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 ))) ;
4953 Collections .sort (organizations , (a , b ) -> {
5054 if (a .isPinned () && b .isPinned ()) {
5155 return a .getName ().compareTo (b .getName ());
@@ -58,22 +62,25 @@ public OrganizationListWindow(List<Organization> organizations) {
5862 }
5963 return a .getName ().compareTo (b .getName ());
6064 });
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 );
6569 this .organizations .setSelectionMode (ListSelectionModel .SINGLE_SELECTION );
6670 setLayout (new BoxLayout (this , BoxLayout .Y_AXIS ));
6771 this .button = new JButton ("Select" );
6872 button .addActionListener (new SelectOrganizationListener (this ));
6973
70- this .organizations .setCellRenderer (new OrganizationCellRenderer ());
74+ this .organizations .setCellRenderer (new OrganizationCellRenderer (this . organizations ));
7175 this .organizations .setVisibleRowCount (4 );
7276 JScrollPane pane = new JScrollPane (this .organizations );
7377 Dimension d = pane .getPreferredSize ();
7478 d .width = 800 ;
79+ d .height = (int ) (d .height * 1.12 );
7580 pane .setPreferredSize (d );
7681 pane .setBorder (new EmptyBorder (5 , 0 , 5 , 0 ));
82+ pane .setViewportBorder (new EmptyBorder (0 , 0 , 0 , 0 ));
83+ pane .getVerticalScrollBar ().setUnitIncrement (10 );
7784 this .organizations .setBackground (new Color (242 , 241 , 240 ));
7885
7986 this .organizations .setSelectedIndex (setDefaultSelectedIndex ());
@@ -138,9 +145,9 @@ private int setDefaultSelectedIndex() {
138145 if (!selectedOrganization .isPresent ()) {
139146 return 0 ;
140147 }
141- final ListModel <OrganizationCard > list = organizations .getModel ();
148+ final ListModel <Organization > list = organizations .getModel ();
142149 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 ())) {
144151 return i ;
145152 }
146153 }
@@ -154,8 +161,7 @@ public SelectOrganizationListener(OrganizationListWindow window) {
154161
155162 @ Override
156163 public void actionPerformed (ActionEvent e ) {
157- final OrganizationCard organization = organizations .getSelectedValue ();
158- setColors (organization , Color .white , Color .black );
164+ final Organization organization = organizations .getSelectedValue ();
159165 frame .setVisible (false );
160166 frame .dispose ();
161167 try {
@@ -176,32 +182,34 @@ public void actionPerformed(ActionEvent e) {
176182 }
177183 }
178184 }
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- }
186185}
187186
188- class OrganizationCellRenderer extends JLabel implements ListCellRenderer {
187+ class OrganizationCellRenderer extends DefaultListCellRenderer {
189188
190189 private static final Color HIGHLIGHT_COLOR = new Color (240 , 119 , 70 );
190+ private final JList parent ;
191+ private final Map <Organization , OrganizationCard > cachedOrgs ;
191192
192- public OrganizationCellRenderer () {
193+ public OrganizationCellRenderer (JList parent ) {
194+ this .parent = parent ;
195+ this .cachedOrgs = new HashMap <>();
193196 }
194197
195198 @ Override
196199 public Component getListCellRendererComponent (final JList list ,
197200 final Object value , final int index , final boolean isSelected ,
198201 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 );
200208 if (isSelected ) {
201- organization .setColors (Color .white , HIGHLIGHT_COLOR );
209+ organizationCard .setColors (Color .white , HIGHLIGHT_COLOR );
202210 } else {
203- organization .setColors (new Color (76 , 76 , 76 ), Color .white );
211+ organizationCard .setColors (new Color (76 , 76 , 76 ), Color .white );
204212 }
205- return organization ;
213+ return organizationCard ;
206214 }
207215}
0 commit comments