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: ChartLegend.java,v $ 023 Revision 1.6 2004/05/05 22:24:14 markl 024 comment block updates 025 026 Revision 1.5 2004/05/05 18:42:11 markl 027 comment header updates 028 029 Revision 1.4 2003/01/19 09:33:21 markl 030 Javadoc & comment header updates. 031 032 Revision 1.3 2001/03/12 07:23:29 markl 033 Javadoc cleanup. 034 035 Revision 1.2 2000/10/15 09:40:28 markl 036 Added javadoc and final API polishing. 037 038 Revision 1.1 2000/10/13 02:04:18 markl 039 Added remaining classes, and integrated components with models. 040 ---------------------------------------------------------------------------- 041*/ 042 043package kiwi.ui.graph; 044 045import java.awt.*; 046import java.util.*; 047import javax.swing.*; 048import javax.swing.border.*; 049 050import kiwi.ui.*; 051 052/** A component that displays a chart legend. For long legends, consider 053 * placing the <code>ChartLegend</code> within a <code>JScrollPane</code>. 054 * 055 * <p><center><img src="snapshot/ChartLegend.gif"><br> 056 * <i>An example ChartLegend.</i> 057 * </center> 058 * 059 * @see kiwi.ui.graph.ChartView 060 * 061 * @author Mark Lindner 062 */ 063 064public class ChartLegend extends JList 065 { 066 private static EmptyBorder border = new EmptyBorder(0, 5, 0, 5); 067 068 /** Construct a new <code>ChartLegend</code> for the specified chart 069 * definition. 070 * 071 * @param def The chart definition. 072 */ 073 074 public ChartLegend(Chart def) 075 { 076 Enumeration e = def.getValues(); 077 Vector data = new Vector(); 078 while(e.hasMoreElements()) 079 data.addElement(e.nextElement()); 080 081 setListData(data); 082 setCellRenderer(new ChartLegendRenderer()); 083 } 084 085 /* The list renderer. 086 */ 087 088 private class ChartLegendRenderer extends JLabel implements ListCellRenderer 089 { 090 private ColorSwatch swatch = new ColorSwatch(Color.gray, 10, 10); 091 092 ChartLegendRenderer() 093 { 094 setIcon(swatch); 095 setBorder(border); 096 } 097 098 public Component getListCellRendererComponent(JList list, Object value, 099 int index, 100 boolean isSelected, 101 boolean hasFocus) 102 { 103 ChartValue val = (ChartValue)value; 104 swatch.setColor(val.getColor()); 105 setText(val.getLabel()); 106 return(this); 107 } 108 } 109 110 } 111 112/* end of source file */