001    package antichess.viewgui;
002    
003    import java.text.DecimalFormat;
004    
005    import javax.swing.BorderFactory;
006    import javax.swing.JLabel;
007    
008    import antichess.GameClock;
009    import antichess.GameTimerListener;
010    import antichess.Player;
011    /**
012     * A TimerLabel is a JLabel automatically refreshed by a ChessClock
013     * It displays the time in a minutes:seconds format.  If the specified
014     * ChessClock does not contain a timer for this player the TimerLabel
015     * displays the word Untimed. 
016     * 
017     * @author nlharr
018     *
019     */
020    public class TimerLabel extends JLabel implements GameTimerListener{
021            //Fields
022            //stores whether the GameClock has a timer for this player
023            private boolean hasTimer;
024            
025            
026            
027            /**
028             * Constructs a new TimerLabel that listens to a TimerLabel contained
029             * in a GameClock
030             * 
031             * If the clock does not have a player associated with the 
032             * 
033             */
034            public TimerLabel(GameClock clock, Player player){
035                    super();
036                    if (!clock.hasPlayer(player)){
037                            hasTimer = false;
038                            this.setText("<html><font size=+2 color=white>Untimed</font>");
039                            
040                    }else{
041                            hasTimer = true;
042                            clock.addListener(this, player);
043                            this.setText("<html><font size=+3 color=white>"+getTimeFormat(clock.getTime(player))+"</font>");
044                    }
045                    
046                    
047                    this.setBorder(BorderFactory.createTitledBorder(""));
048                    
049            }
050            /**
051             * Refreshes the text value displayed in the field
052             */
053            public void refreshTimer(long times){
054                    if (hasTimer){
055                            this.setText("<html><font size=+3 color=white>"+getTimeFormat(times)+"</font>");
056                    }               
057            }
058            /**
059             * @return a string that formats the inp in milliseconds into a 
060             * proper time format "##:0#" minutes:seconds
061             */
062            public String getTimeFormat(long inp){
063                    DecimalFormat secondFormatter = new DecimalFormat("00");
064                    DecimalFormat minuteFormatter = new DecimalFormat("#0");
065                    
066                    String minutes = minuteFormatter.format(Math.floor(inp/60000));
067                    String seconds = secondFormatter.format(Math.floor((inp-60000*Math.floor(inp/60000))/1000));
068                    return minutes+":"+seconds;
069            }
070            
071            
072            private static final long serialVersionUID = 8713470460423722237L;
073    }