/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.component.tabs;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import java.io.Serializable;

public class Tab implements Serializable {

    protected String id;
    protected String title;
    protected String contentPath;
    protected boolean closed = false;
    protected String actionListener;

    public String getActionListener() {
        return actionListener;
    }

    public boolean isClosed() {
        return closed;
    }

    public void setClosed(boolean closed) {
        this.closed = closed;
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        if (title != null && title.startsWith("#{")) {
            FacesContext fc = FacesContext.getCurrentInstance();
            title = fc.getApplication().createValueBinding(title).getValue(fc).toString();
        }
        return title;
    }

    public String getContentPath() {
        return contentPath;
    }

    public Tab(String id, String title, String contentPath) {
        super();
        this.id = id;
        this.title = title;
        this.contentPath = contentPath;
    }

    public Tab(String id, String title, String contentPath, String actionListener) {
        super();
        this.id = id;
        this.title = title;
        this.contentPath = contentPath;
        this.actionListener = actionListener;
    }

    public void actionListenerHandler(ActionEvent evt) {
        if (actionListener != null) {
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.getApplication().createMethodBinding(actionListener, new Class[]{ActionEvent.class})
                    .invoke(fc, new Object[]{evt});

        }
    }

    public String toString() {
        return this.getClass().getName() + " " + super.toString() + " id=" + this.id + ", title=" + this.title + ", contentPath=" + this.contentPath + ", closed=" + this.closed;
    }


}
