<%@ jet package="cruise.ui.jfx.templates.impl" 
imports="java.util.* cruise.model.abstractObjects.BackingObject cruise.umple.compiler.* cruise.ui.jfx.* "
class="KeyTable" skeleton="../jfx.skeleton" %>
<%  BackingObject bckObject = (BackingObject) argument; 
    String preffix=JFXProvider.properties.getProperty(cruise.model.Constants.PACKAGE_PREFIX);
%>
package <%=preffix +  ((bckObject.getPackageName().trim().length()>0)?("." + bckObject.getPackageName()):"")%>;

import dao.utils.DAOUtils;
import <%=(bckObject.getPackageName()!=null&&bckObject.getPackageName().trim().length()>0)?(bckObject.getPackageName().trim()+"."):""%><%=bckObject.getClassName()%>;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class <%=bckObject.getClassName()%>KeyTable {


    List<<%=bckObject.getClassName()%>> data =  DAOUtils.getDAOFactory().get<%=bckObject.getClassName()%>DAO().findAll();
    JTable table;
    
    public <%=bckObject.getClassName()%>KeyTable(List<<%=bckObject.getClassName()%>> data){
        this.data=data;
    }

    public <%=bckObject.getClassName()%> getSelected<%=bckObject.getClassName()%>() {

        int index = table.getSelectedRow();

        <%=bckObject.getClassName()%> a<%=bckObject.getClassName()%> = (<%=bckObject.getClassName()%>)data.get(index == -1 ? 0 : index);

        return a<%=bckObject.getClassName()%>;
    }

    public void deleteSelected<%=bckObject.getClassName()%>() {
        int index = table.getSelectedRow();
        data.remove(index == -1 ? 0 : index);
        restructureTable();
    }

    public void refresh() {
        restructureTable();
    }


    private void restructureTable() {
        table.setModel(new <%=bckObject.getClassName()%>KeyModel(data));
        table.validate();
        table.repaint();
    }

    public JComponent create() {
        JScrollPane scrollPane;
		table = new JTable();
        table.setModel(new <%=bckObject.getClassName()%>KeyModel(data));
        table.getTableHeader().setReorderingAllowed(false);
		scrollPane = new JScrollPane( table );
        return scrollPane;
}



static class <%=bckObject.getClassName()%>KeyModel extends AbstractTableModel {
     String[] colum = new String[<%=bckObject.getKeysAttributes().size() +1%>];
	    List<<%=bckObject.getClassName()%>> data;
	    public <%=bckObject.getClassName()%>KeyModel( List<<%=bckObject.getClassName()%>> data){
		this.data=data;
	    <% int columnCount=0;
	    for (AttributeVariable attVar: bckObject.getKeysAttributes()) { %>
	    colum[<%=columnCount++%>]="<%=attVar.getUpperCaseName()%>";     <%}%>
	    colum[<%=columnCount%>]="Type";
     }
     @Override
     public int getRowCount() {
         return data.size();
     }
     @Override
     public int getColumnCount() {
         return colum.length;
     }
     @Override
     public Object getValueAt(int rowIndex, int columnIndex) {
         <%=bckObject.getClassName()%> a<%=bckObject.getClassName()%> = (<%=bckObject.getClassName()%>)data.get(rowIndex);
	     <% columnCount=0;
	    for (AttributeVariable attVar: bckObject.getKeysAttributes()) { %>
	      if(columnIndex ==<%=columnCount++%>) {
	             return a<%=bckObject.getClassName()%>.get<%=attVar.getUpperCaseName()%>();
	        } 
	     <%}%>
	    return a<%=bckObject.getClassName()%>.getClass().getName();
     }
     @Override
     public String getColumnName(int column) {
         return colum[column];
     }
}
}
