001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: StatusIconCellRenderer.java,v $ 023 Revision 1.3 2004/05/12 21:38:09 markl 024 updated to handle values of type Integer and IntegerHolder. 025 026 Revision 1.2 2004/03/20 05:16:01 markl 027 made renderer tolerant of invalid value types 028 029 Revision 1.1 2004/03/16 06:02:30 markl 030 new class 031 ---------------------------------------------------------------------------- 032*/ 033 034package kiwi.ui; 035 036import java.awt.*; 037import javax.swing.*; 038import javax.swing.border.*; 039import javax.swing.table.*; 040 041import kiwi.util.*; 042 043/** A cell renderer that displays one of a set of status icons. The renderer 044 * assumes that the values being rendered are objects of type 045 * <code>Integer</code> or <code>kiwi.util.IntegerHolder</code>; for values 046 * of other types, the renderer displays nothing. 047 * 048 * @author Mark Lindner 049 * @since Kiwi 2.0 050 */ 051 052public class StatusIconCellRenderer extends AbstractCellRenderer 053 implements ListCellRenderer, TableCellRenderer 054 { 055 private Icon icons[]; 056 private JLabel renderer; 057 058 /** Construct a new <code>StatusIconCellRenderer</code>. 059 * 060 * @param icons The set of icons that represent the various states. The array 061 * must contain at least one icon. 062 * @throws java.lang.IllegalArgumentException If <code>icons</code> is 063 * <code>null</code> or a zero-length array. 064 */ 065 066 public StatusIconCellRenderer(Icon icons[]) 067 { 068 renderer = new JLabel(); 069 renderer.setHorizontalAlignment(SwingConstants.CENTER); 070 071 if((icons == null) || (icons.length < 1)) 072 throw(new IllegalArgumentException("Cannot be null or an empty array.")); 073 074 this.icons = icons; 075 } 076 077 /** Get the actual cell renderer component. */ 078 079 protected JComponent getCellRenderer(JComponent component, Object value, 080 int row, int column) 081 { 082 int val = -1; 083 084 if(value != null) 085 { 086 if(value.getClass() == Integer.class) 087 val = ((Integer)value).intValue(); 088 else if(value.getClass() == IntegerHolder.class) 089 val = ((IntegerHolder)value).getValue(); 090 } 091 092 if((val < 0) || (val >= icons.length)) 093 renderer.setIcon(null); 094 else 095 renderer.setIcon(icons[val]); 096 097 return(renderer); 098 } 099 100 } 101 102/* end of source file */