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: StateIndicator.java,v $ 023 Revision 1.3 2004/05/12 18:21:20 markl 024 comment block updates 025 026 Revision 1.2 2004/01/23 00:03:58 markl 027 javadoc corrections 028 029 Revision 1.1 2003/11/07 19:19:01 markl 030 New class. 031 ---------------------------------------------------------------------------- 032*/ 033 034package kiwi.ui; 035 036import java.awt.*; 037import java.util.*; 038import javax.swing.*; 039 040import kiwi.util.KiwiUtils; 041 042/** A graphical state indicator component. This component displays one 043 * of a collection of images, depending on what its current "state" 044 * is. Each state is indicated by a unique string; the component maintains a 045 * mapping of these strings to the corresponding state icons. 046 * <p> 047 * This component can be used to create status icons and other 048 * types of multi-state indicators. 049 * 050 * @see kiwi.ui.ToggleIndicator 051 * 052 * @since Kiwi 1.4.3 053 * 054 * @author Mark Lindner 055 */ 056 057public class StateIndicator extends JLabel 058 { 059 private String state = null; 060 private Hashtable icons = new Hashtable(); 061 private Icon defaultIcon; 062 063 /** Construct a new <code>StateIndicator</code>. 064 * 065 * @param defaultIcon The default icon to display when the state is unknown. 066 */ 067 068 public StateIndicator(Icon defaultIcon) 069 { 070 this.defaultIcon = defaultIcon; 071 072 setHorizontalAlignment(SwingConstants.CENTER); 073 setVerticalAlignment(SwingConstants.CENTER); 074 075 setIcon(defaultIcon); 076 } 077 078 /** 079 * Remove all states from the indicator. 080 */ 081 082 public void clearStates() 083 { 084 icons.clear(); 085 } 086 087 /** 088 * Add a new state to the indicator. 089 * 090 * @param state The name of the state. 091 * @param icon The icon to display for this state. 092 */ 093 094 public void addState(String state, Icon icon) 095 { 096 icons.put(state, icon); 097 } 098 099 /** 100 * Remove a state from the indicator. 101 * 102 * @param state The name of the state. 103 */ 104 105 public void removeState(String state) 106 { 107 icons.remove(state); 108 } 109 110 /** Set the state of the indicator. 111 * 112 * @param state The new state for the indicator. The indicator will 113 * be repainted immediately. If the specified <code>state</code> is 114 * invalid, the state will be set to <code>null</code> and the icon 115 * to default icon. 116 */ 117 118 public synchronized void setState(String state) 119 { 120 Icon icon = (Icon)icons.get(state); 121 if(icon == null) 122 { 123 icon = defaultIcon; 124 this.state = null; 125 } 126 else 127 this.state = state; 128 129 setIcon(icon); 130 KiwiUtils.paintImmediately(this); 131 } 132 133 /** Get the current state of the indicator. 134 * 135 * @return The current state of the indicator, which may be 136 * <code>null</code>. 137 */ 138 139 public synchronized String getState() 140 { 141 return(state); 142 } 143 144 } 145 146/* end of source file */