/*
 * 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.table.editable;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * Concrete implementation of an editable table, using an ArrayListDataModel.
 *
 * @author philip.breau@icesoft.com
 */
public class EditableTableBean extends AbstractEditableTableBean {
	
	private List<Serializable> list;
	
	private static final Logger LOG = Logger.getLogger(EditableTableBean.class.getName());

    public EditableTableBean(Class clazz) {
        super(clazz);
        createDefaultDataModel();
    }

    public EditableTableBean(List list) {
    	super(list);
    	this.list = list;
        this.data = new ListDataModel(list);
    }


    public EditableTableBean(Class clazz, IEditableTableEventListener eventListener) {
        super(clazz, eventListener);
        createDefaultDataModel();
    }

    public EditableTableBean(List list, IEditableTableEventListener eventListener) {
        super(list, eventListener);
        this.data = new ListDataModel(list);
        this.list = list;
    }

    protected void createDefaultDataModel() {
        this.data = new ListDataModel(new ArrayList());
    }

    public List getListData() {
        return (List) data.getWrappedData();
    }

    public void addRow(int index, Object row) {
        ((List) this.data.getWrappedData()).add(index, row);
    }

    public void removeRow(Object row) {
        ((List) this.data.getWrappedData()).remove(row);
    }
    
    @Override
    public DataModel getData(){
    	if( data == null ){
    		this.data = new ListDataModel(list);
    	}
    	return data;
    }


}
