001 /* 002 * LAPIS lightweight structured text processing system 003 * 004 * Copyright (C) 1998-2002 Carnegie Mellon University, 005 * Copyright (C) 2003 Massachusetts Institute of Technology. 006 * All rights reserved. 007 * 008 * This library is free software; you can redistribute it 009 * and/or modify it under the terms of the GNU General 010 * Public License as published by the Free Software 011 * Foundation, version 2. 012 * 013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/ 014 */ 015 package lapisx.swing; 016 017 import java.awt.Color; 018 import java.awt.event.ActionEvent; 019 import java.awt.event.ActionListener; 020 021 import javax.swing.JLabel; 022 import javax.swing.Timer; 023 024 /** 025 * Label that can flash (change color briefly) on demand. 026 * Useful for status messages that need to call attention to themselves. 027 */ 028 public class FlashingLabel extends JLabel { 029 Timer timer; 030 Color flashColor; 031 Color normalColor; 032 033 static int DELAY = 500; // milliseconds 034 035 /** 036 * Make a FlashingLabel. 037 * @param text Text for label 038 */ 039 public FlashingLabel (String text) { 040 super (text); 041 timer = new Timer (DELAY, new ActionListener () { 042 public void actionPerformed (ActionEvent event) { 043 stopFlashing (); 044 } 045 }); 046 timer.setCoalesce (true); 047 timer.setRepeats (false); 048 049 flashColor = Color.red; 050 normalColor = getForeground (); 051 } 052 053 public void setForeground (Color c) { 054 super.setForeground (c); 055 normalColor = c; 056 } 057 058 /** 059 * Set the color used when the label flashes. 060 * @param c flash color 061 */ 062 public void setFlashColor (Color c) { 063 flashColor = c; 064 } 065 066 /** 067 * Get the color used when the label flashes. 068 * @return flash color 069 */ 070 public Color getFlashColor (Color c) { 071 return flashColor; 072 } 073 074 /** 075 * Flash the label. Automatically stops flashing after a delay. 076 */ 077 public void startFlashing () { 078 if (flashColor == null) 079 return; 080 super.setForeground (flashColor); 081 timer.restart (); 082 } 083 084 /** 085 * Force the label to stop flashing, if it's currently flashing. 086 * Normally happens automatically after a delay, so it's 087 * not usually necessary to call this. 088 */ 089 public void stopFlashing () { 090 super.setForeground (normalColor); 091 timer.stop (); 092 } 093 }