001 package sears.gui; 002 003 import java.awt.event.MouseAdapter; 004 import java.awt.event.MouseEvent; 005 006 import javax.swing.Icon; 007 import javax.swing.JButton; 008 009 import sears.gui.resources.SearsResources; 010 011 /** 012 * An arrow button is a JButton with an arrow set to its icon. 013 * <br>When user press down the button, the arrow flip to down and stay in this state. 014 * <br>Press again and it returns to its normal state. 015 * @see JButton 016 */ 017 public class ArrowButton extends JButton { 018 019 private static final long serialVersionUID = -379552681655716687L; 020 021 // Arrow icons: 022 private static final Icon ARROW_UP = SearsResources.getIcon("ArrowUp"); 023 private static final Icon ARROW_DOWN = SearsResources.getIcon("ArrowDown"); 024 // to know the arrow state 025 private boolean isTurnOn; 026 027 /** 028 * Constructs a new <tt>ArrowButton</tt> object 029 * @param text the text to display on the button (if null, no text will appear) 030 * @see JButton 031 */ 032 public ArrowButton(String text) { 033 super(text); 034 createArrowButton(); 035 } 036 037 // set the immutable constraints 038 private void createArrowButton() { 039 isTurnOn = false; 040 setIcon(ARROW_UP); 041 042 setBorderPainted(false); 043 addMouseListener(new MouseAdapter() { 044 public void mouseClicked(MouseEvent e) { 045 if( !isTurnOn ) { 046 turnOn(); 047 } else { 048 turnOff(); 049 } 050 } 051 }); 052 } 053 054 /** 055 * To know the arrow state 056 * @return true if arrow is down, false if up, the normal state is sets to up 057 */ 058 public boolean isTurnOn() { 059 return isTurnOn; 060 } 061 062 /** 063 * Turns on the arrow 064 */ 065 protected void turnOn() { 066 if( !isTurnOn ) { 067 isTurnOn = true; 068 setIcon(ARROW_DOWN); 069 } 070 } 071 072 /** 073 * Turns off the arrow 074 */ 075 protected void turnOff() { 076 if( isTurnOn ) { 077 isTurnOn = false; 078 setIcon(ARROW_UP); 079 } 080 } 081 }