/**
 * Copyright (c) 2011, Martin Foster <martin@ethereal-realms.org>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Ethereal-Realms.org nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.lyra.Widgets;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * The Music Player Daemon (MPD) makes use of seconds in duration and
 * Unix time in the protocol.  Unfortunately this is not necessarily human
 * friendly.  This will correct the elapsed time display for 
 * <i>TableColumn</i> instances which require a nice format.
 * <p>
 * @author Martin Foster
 * @version 1.0.1,  %Revision, 95%, %LastChangedDate, 12/03/11 9:59PM%
 * @see CustomColorRenderer
 */
public class DateTableCellRenderer extends CustomColorRenderer {

	// Serial Compliance
	private static final long serialVersionUID = 1L;
	
	// Language Element
	private String Separator;
	
	/**
     * Default constructor for the class.   Requires passage of a language
     * element in order to display properly.
     * <p>
     * @param global Global Map containing colours and user settings.
     * @param separator String containing element between Minute and Seconds.
     */
    public DateTableCellRenderer(Map<String,String> global, String separator) {
    	
    	// Let the super class do all the work.
    	super(global); 

    	// Pass up
    	Separator = separator;
    }
    // End of constructor class

    
    /* (non-Javadoc)
     * @see javax.swing.table.DefaultTableCellRenderer#setValue(java.lang.Object)
     */
    public void setValue(Object value) {

    	/*
    	 * We only deal with the format of the string itself.  The rest is
    	 * done by the Super class element itself.
    	 */
    	if (value != null) {
    		
    		// Parse the string and extract Minute and Seconds
			long total  = Long.parseLong(value.toString());
			long imin   = TimeUnit.SECONDS.toMinutes(total);
			long isec   = TimeUnit.SECONDS.toSeconds(total - (imin * 60));
    		
			// Format the minutes appending 0 if necessary
			String smin = (imin < 10) 
			  ? "0" + Long.toString(imin)  
			  : Long.toString(imin);
			
			// Format the seconds appending 0 if necessary
			String ssec = (isec < 10) 
			  ? "0" + Long.toString(isec)  
			  : Long.toString(isec);
			
			// Permit the super class to continue with its processing
    		super.setValue(smin + Separator + ssec);
    	}
    	
		/*
		 * There is no runtime available for this element.  Which 
		 * should normally never happen.   So we set it to a blank
		 * string and pass it along.
		 */
    	else {
    		super.setValue("");
    	}
    }
    // End of setValue Method
}

