View Javadoc
1   /*
2   
3       dsh-identify  Lightweight components for identifiable beans.
4       Copyright (c) 2003-2019 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.identify;
25  
26  import java.awt.Component;
27  import java.awt.Color;
28  
29  import javax.swing.DefaultListCellRenderer;
30  import javax.swing.JLabel;
31  import javax.swing.JList;
32  import javax.swing.UIManager;
33  
34  /**
35   * Stripe list cell renderer.
36   *
37   * @since 1.2
38   * @author  Michael Heuer
39   */
40  public class StripeListCellRenderer extends DefaultListCellRenderer {
41      /** Background color for odd rows. */
42      private Color oddRowBackgroundColor = DEFAULT_ODD_ROW_BACKGROUND_COLOR;
43  
44      /** Default background color for odd rows, <code>2a5703, 5% alpha</code>. */
45      public static final Color DEFAULT_ODD_ROW_BACKGROUND_COLOR = new Color(42, 87, 3, 12); // 2a5703, 5% alpha
46  
47      /**
48       * Return the background color for odd rows. Defaults to DEFAULT_ODD_ROW_BACKGROUND_COLOR.
49       *
50       * @return the background color for odd rows
51       */
52      public final Color getOddRowBackgroundColor()
53      {
54          return oddRowBackgroundColor;
55      }
56  
57      /**
58       * Set the background color for odd rows to <code>oddRowBackgroundColor</code>.
59       *
60       * <p>This is a bound property.</p>
61       *
62       * @param oddRowBackgroundColor background color for odd rows, must not be null
63       */
64      public final void setOddRowBackgroundColor(final Color oddRowBackgroundColor)
65      {
66          if (oddRowBackgroundColor == null)
67          {
68              throw new IllegalArgumentException("oddRowBackgroundColor must not be null");
69          }
70          Color oldOddRowBackgroundColor = this.oddRowBackgroundColor;
71          this.oddRowBackgroundColor = oddRowBackgroundColor;
72          firePropertyChange("oddRowBackgroundColor", oldOddRowBackgroundColor, this.oddRowBackgroundColor);
73      }
74  
75      @Override
76      public Component getListCellRendererComponent(final JList list, final Object value, final int index, boolean isSelected, boolean hasFocus)
77      {
78          JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
79  
80          if (isSelected)
81          {
82              label.setForeground(UIManager.getColor("List.selectionForeground"));
83              label.setBackground(UIManager.getColor("List.selectionBackground"));
84          }
85          else
86          {
87              label.setForeground(UIManager.getColor("List.foreground"));
88  
89              if (index % 2 == 1)
90              {
91                  label.setBackground(oddRowBackgroundColor);
92              }
93              else
94              {
95                  label.setBackground(UIManager.getColor("List.background"));
96              }
97          }
98          return label;
99      }
100 
101     /**
102      * Install a stripe list cell renderer for the specified list.
103      *
104      * @param <T> list element type
105      * @param list list, must not be null
106      */
107     public static <T> void install(final JList<T> list)
108     {
109         if (list == null)
110         {
111             throw new IllegalArgumentException("list must not be null");
112         }
113         StripeListCellRenderertml#StripeListCellRenderer">StripeListCellRenderer renderer = new StripeListCellRenderer();
114         list.setCellRenderer(renderer);
115     }
116 }