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.Image;
27  import java.awt.Component;
28  
29  import javax.swing.JTable;
30  import javax.swing.JLabel;
31  import javax.swing.ImageIcon;
32  
33  import javax.swing.table.DefaultTableCellRenderer;
34  
35  import org.dishevelled.iconbundle.IconSize;
36  import org.dishevelled.iconbundle.IconState;
37  import org.dishevelled.iconbundle.IconBundle;
38  import org.dishevelled.iconbundle.IconTextDirection;
39  
40  /**
41   * Table cell renderer that displays the name property value
42   * and appropriate icon from an icon bundle for a given bean.
43   *
44   * @author  Michael Heuer
45   */
46  public class IdTableCellRenderer
47      extends StripeTableCellRenderer
48  {
49      /** Default icon size. */
50      public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
51  
52      /** Icon size. */
53      private IconSize iconSize;
54  
55      /** ImageIcon wrapper for image from icon bundle. */
56      private transient ImageIcon imageIcon;
57  
58  
59      /**
60       * Create a new table cell renderer for identifiable beans with
61       * the default icon size.
62       *
63       * @see #DEFAULT_ICON_SIZE
64       */
65      public IdTableCellRenderer()
66      {
67          this(DEFAULT_ICON_SIZE);
68      }
69  
70      /**
71       * Create a new table cell renderer for identifiable beans with
72       * the specified icon size.
73       *
74       * @param iconSize icon size, must not be null
75       */
76      public IdTableCellRenderer(final IconSize iconSize)
77      {
78          super();
79          setIconSize(iconSize);
80      }
81  
82  
83      /**
84       * Return the icon size for this table cell renderer.
85       *
86       * @return the icon size for this table cell renderer
87       */
88      public final IconSize getIconSize()
89      {
90          return iconSize;
91      }
92  
93      /**
94       * Set the icon size for this table cell renderer to <code>iconSize</code>.
95       *
96       * @param iconSize icon size, must not be null
97       */
98      public final void setIconSize(final IconSize iconSize)
99      {
100         if (iconSize == null)
101         {
102             throw new IllegalArgumentException("iconSize must not be null");
103         }
104         this.iconSize = iconSize;
105     }
106 
107     @Override
108     public Component getTableCellRendererComponent(final JTable table,
109                                                    final Object value,
110                                                    final boolean isSelected,
111                                                    final boolean hasFocus,
112                                                    final int row,
113                                                    final int column)
114     {
115         JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
116 
117         String name = IdentifyUtils.getNameFor(value);
118         label.setText(name);
119 
120         IconBundle iconBundle = IdentifyUtils.getIconBundleFor(value);
121 
122         if (iconBundle == null)
123         {
124             label.setIcon(null);
125         }
126         else
127         {
128             IconTextDirection textDirection = IdentifyUtils.determineTextDirection(label);
129             Image image = iconBundle.getImage(label, textDirection, IconState.NORMAL, iconSize);
130 
131             if (imageIcon == null)
132             {
133                 imageIcon = new ImageIcon(image);
134             }
135             else
136             {
137                 imageIcon.setImage(image);
138             }
139 
140             label.setIcon(imageIcon);
141         }
142 
143         return label;
144     }
145 }