/**
 * Licensed under the MICE s.r.l.
 * End User License Agreement
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.micegroup.it/voila/license.html
 */
package it.mice.voila.runtime.displaytag.basedec;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;

import org.displaytag.decorator.TableDecorator;

/**
 * @author ZZY9V4
 *         <p/>
 *         To change this generated comment edit the template variable "typecomment":
 *         Window>Preferences>Java>Templates.
 *         To enable and disable the creation of type comments go to
 *         Window>Preferences>Java>Code Generation.
 */
public abstract class AbstractSelectRowDec extends TableDecorator {

    /**
     * Called just within the TR tag. Can be subclassed to provide specific data to customize TR tag such as style, jscript events, etc...
     *
     * @param defaultContent generally the style associated to the Row. Can be retained or throwed to provide a custom logic also for css style.
     * @return decorated content for the TR tag, defaultContent in the default implementation
     */
    public String decorateRowTag(String defaultContent) {
        String rt;
        try {
            Object currentElement = getCurrentRowObject();
            rt = " id=\"riga_" + getViewIndex() + "\" onclick=\"selectRow('riga_" + getViewIndex() + "', '" + getRowId(currentElement) + "')\"";
            if (getViewIndex() % 2 == 0) {
                rt += " class=\"odd\"";
            } else {
                rt += " class=\"even\"";
            }
            return rt;
        } catch (Exception e) {
            e.printStackTrace();
            return defaultContent;
        }
    }

    public void finish() {
        JspWriter out = getPageContext().getOut();
        try {
            out.write("");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public abstract Object getRowId(Object currentElement);
}
