/**
 * 
 */
package view.tool.jEasyTable;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.border.Border;

import view.tool.dateField.DateField;
import view.tool.inputField.JValidTextField;
import view.tool.jEasyTable.factory.JEasyTableCellFactoryClick;
import view.tool.jEasyTable.factory.JEasyTableCellFactoryDoubleClick;
import view.tool.jEasyTable.factory.JEasyTableCellFactorySelectable;
import view.tool.jEasyTable.factory.JEasyTableFactory;

/**
 * @author alexis
 * Table that acts like JTable but alot more dynamique
 */
public class JEasyScrollTable extends JScrollPane implements Observer{
	/**
	 * 
	 */
	/*constante*/
	public static int NONE_SELECTION = 0;
	public static int SINGLE_SELECTION = 1;
	public static int UNLIMITED_SELECTION = -1;
	private static final long serialVersionUID = 5251722666988021877L;
	
	private final Border grayBorder = BorderFactory.createLineBorder(Color.gray);
	private final Border lightgrayBorder = BorderFactory.createLineBorder(Color.lightGray);
	private GridBagLayout tableLayout = new GridBagLayout();
	private GridBagConstraints c = new GridBagConstraints();
	private String[] column_names;
	private Map<Integer,JEasyTableFactory> listenerByColumn = new HashMap<Integer,JEasyTableFactory>();
	private int column;
	private int row;
	
	private JViewport firstRow;
	private JPanel headerTable;
	private JPanel header;
	private JPanel table;
	private List<Integer> selected_index;
	private int maxSelection;
	
	public enum SelectionType {
		None {
			public void selectionBasedOnType(JEasyScrollTable table, int line, boolean _isSelected) throws Exception {
		}},
	    Single {
			public void selectionBasedOnType(JEasyScrollTable table, int line, boolean _isSelected) throws Exception {
				table.unselectAll();
				table.selected_index = new ArrayList<Integer>();
				  
				if(!_isSelected)
			  	{
			  		for(int j = 0; j < table.column+1; j++)
			  		{
						JEasyTableCell cell = table.getCellAt(j,line);
						cell.setSelected(true);
					}
					table.selected_index.add(line);
				}
			}},
	    Multi {
		    public void selectionBasedOnType(JEasyScrollTable table, int line, boolean _isSelected) throws Exception {
		    	if(_isSelected || table.maxSelection > table.selected_index.size()-1)
				{
					for(int j = 0; j < table.column+1; j++)
					{
						JEasyTableCell cell = table.getCellAt(j,line);
						cell.setSelected(!_isSelected);
					}
					if(!_isSelected)
					{
						table.selected_index.add(line);
					}
					else
					{
						table.selected_index.remove(table.selected_index.indexOf(line));
					}
				}
		    }},
	    Unlimited {
			public void selectionBasedOnType(JEasyScrollTable table, int line, boolean _isSelected) throws Exception {
		  	    for(int j = 0; j < table.column+1; j++)
		  		{
		  			JEasyTableCell cell = table.getCellAt(j,line);
		  			cell.setSelected(!_isSelected);
		  		}
		  		if(!_isSelected)
		  		{
		  			table.selected_index.add(line);
		  		}
		  		else
		  		{
		  			table.selected_index.remove(table.selected_index.indexOf(line));
		  		}
			}};

	    public abstract void selectionBasedOnType(JEasyScrollTable table, int line, boolean _isSelected) throws Exception ;
	}
	private SelectionType selectType;
	
	/**************************************************************************
	 * constructeur
	 *************************************************************************/
	
	/**
	 * Constructor
	 * @param column_names titles of the column of the table
	 * @throws Exception 
	 */
	public JEasyScrollTable(String[] column_names) throws Exception {
		if(column_names.length == 0)
			throw new Exception("column_names size should be atleast 1");
		
		firstRow = new JViewport();
		table = new JPanel(tableLayout);
		header = new JPanel(tableLayout);
		headerTable = new JPanel(new GridBagLayout());
		
		headerTable.setBorder(lightgrayBorder);
		
		this.column_names = column_names;
		column = column_names.length-1;
		row = 0;
		selected_index = new ArrayList<Integer>();
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = c.weighty = 1.0;
		c.gridx = 0;
		c.gridy = 0;
		headerTable.add(table,c);
		
		firstRow.add(header);
		addHeader();
		
		this.setViewportView(headerTable);
		this.setColumnHeader(firstRow);
		
		table.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
            	super.componentResized(e);
            	updateCellSize();
            }
		});
	}
	
	/**
	 * add the header (titles) of the table
	 */
	private void addHeader(){
		for(int i = 0; i < column_names.length; i++)
		{
			c.fill = GridBagConstraints.BOTH;
			c.weightx = c.weighty = 1.0;
			c.gridx = i;
			c.gridy = 0;
			JLabel cell = new JLabel(column_names[i]);
			cell.setFont(new Font(cell.getFont().getName(),cell.getFont().getStyle(),cell.getFont().getSize()+4));
			cell.setBackground(Color.lightGray);
			cell.setOpaque(true);
			cell.setBorder(grayBorder);
			cell.setSize(new Dimension(100,30));
			cell.setMaximumSize(new Dimension(100,30));
			cell.setMinimumSize(new Dimension(100,30));
			cell.setPreferredSize(new Dimension(100,30));
			header.add(cell,c);
		}
	}
	
	/**
	 * remove last row of the table
	 * @throws Exception 
	 */
	public void removeLastRow() throws Exception{
		if(row < 0)
			throw new Exception("No data remaining");
		
		for(int i = 0; i <= column; i++)
		{
			int temp = ((column+1)*(row+1))-i-1;
			table.remove(temp);
		}
		row--;
		updateVisual();
	}
	
	/**
	 * remove row from table
	 * @param index of the row to remove
	 * @throws Exception 
	 */
	public void removeRow(int index) throws Exception{
		if(row < 0 || index > row || index < 0)
			throw new Exception("Index out of bound");
	
		JComponent[][] temp = new JComponent[row][column+1];
		int currentColumn = 0;
		int currentRow = 0;
		boolean justSkipped = false;
		boolean hasSkipped = false;
		
		for(int i = 0; i < (column+1)*(row+1); i++)
		{
			if(currentColumn == column+1)
			{
				currentRow++;
				currentColumn = 0;
				
				if(justSkipped == true)
				{
					currentRow--;
					hasSkipped = true;
					justSkipped = false;
				}
			}
			if(hasSkipped || currentRow != index)
			{
				temp[currentRow][currentColumn] = ((JEasyTableCell)table.getComponent(i)).getContent();
			}
			else
			{
        		
				justSkipped = true;
			}
			currentColumn++;
		}
		setContent(temp);
	}
	
	
	/**
	 * add row at the end of the table
	 * @param content row to add to the table
	 * @throws Exception 
	 */
	public void addRow(JComponent[] content) throws Exception{
		if(content.length-1 != column)
			throw new Exception("JComponent[].length should be equal to the number of columns");
		
		for(int i = 0; i < content.length; i++)
		{
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = c.weighty = 1.0;
			c.gridx = i;
			c.gridy = row;
			
			JEasyTableCell tempPane;
			
			if(listenerByColumn.containsKey(i))
			{
				if(listenerByColumn.get(i) instanceof JEasyTableCellFactorySelectable)
					tempPane = listenerByColumn.get(i).build(content[i],row);
				else
					tempPane = listenerByColumn.get(i).build(content[i]);
			}
			else
        		
			{
				tempPane = new JEasyTableCell(content[i]);
			}
			table.add(tempPane,c);
		}
		row++;
		updateVisual();
	}
	
	/**
	 * Adds a row at given position
	 * @param content row of the table
	 * @param index index to place it
	 * @throws Exception 
	 */
	public void addRowAt(JComponent[] content, int index) throws Exception{
		if(row < 0 || index > row || index < 0)
			throw new Exception("Index out of bound exception");
		if(content.length-1 != column)
			throw new Exception("JComponent[].length should be equal to the number of columns");
			
		JComponent[][] temp = new JComponent[row+2][column+1];
		int currentColumn = 0;
		int currentRow = 0;
		boolean isAdded = false;

		for(int i = 0; i < (column+1)*(row+1); i++)
		{
			if(currentColumn == column+1)
			{
				currentRow++;
				currentColumn = 0;
			}
			if(currentRow == index && !isAdded)
			{
				isAdded = true;
				for(int k = 0; k < column+1; k++)
					temp[currentRow][k] = content[k];
				currentRow++;
			}
			temp[currentRow][currentColumn] = ((JEasyTableCell)table.getComponent(i)).getContent();

			currentColumn++;
		}
		setContent(temp);
	}
	
	/**
	 * Clear the content
	 */
	public void clear(){
		/*this.setContent(new JComponent[][]{{}});*/
		table.removeAll();
		row = 0;
		updateVisual();
	}
	
	/**
	 * update the graphical display
	 */
	private void updateVisual(){
		updateCellSize();
		headerTable.revalidate();
		headerTable.repaint();
		this.setMinimumSize(headerTable.getMinimumSize());
		header.validate();
		this.revalidate();
		this.repaint();
	}
	
	private void updateCellSize(){
		int width;
		int height;
		
		for(int k = 0; k < table.getComponentCount(); k++)
		{
			width = table.getComponent(0).getSize().width;
			height = 20;
			
			table.getComponent(k).setSize(new Dimension(width,height));
			table.getComponent(k).setPreferredSize(new Dimension(width,height));
			table.getComponent(k).setMinimumSize(new Dimension(width,height));
			table.getComponent(k).setMaximumSize(new Dimension(width,height));
		}
		
		/*Integer[] width = new Integer[column];
		int maxWidth = this.getViewport().getSize().width;
		int totalWidth = 0;
		int mainRatio;
		
		for(int h = 0; h < column; h++)
		{
			width[h] = header.getComponent(h).getWidth();
			
			for(int k = 0; k < row; k++)
			{
				int index = k*column+h;
				
				if(width[h] > table.getComponent(index).getPreferredSize().width)
				{
					width[h] = table.getComponent(index).getPreferredSize().width;
				}
			}
			
			totalWidth += width[h];
			System.out.println(width[h]);
		}
		
		mainRatio = maxWidth / totalWidth;
		
		for(int h = 0; h < column; h++)
		{
			header.getComponent(h).setSize(new Dimension(width[h]*mainRatio,header.getComponent(h).getSize().height));
			header.getComponent(h).setMinimumSize(new Dimension(width[h]*mainRatio,header.getComponent(h).getSize().height));
			header.getComponent(h).setMaximumSize(new Dimension(width[h]*mainRatio,header.getComponent(h).getSize().height));
			header.getComponent(h).setPreferredSize(new Dimension(width[h]*mainRatio,header.getComponent(h).getSize().height));
			/*int index = (k*column+1)+h;
			System.out.println("index :"+index+" row :"+k+" column :"+h);
			height = 20;
			
			table.getComponent(index).setSize(new Dimension(width[h],height));
			table.getComponent(index).setPreferredSize(new Dimension(width[h],height));
			table.getComponent(index).setMinimumSize(new Dimension(width[h],height));
			table.getComponent(index).setMaximumSize(new Dimension(width[h],height));
		}*/
	}
	
	/**************************************************************************
	 * getter
	 *************************************************************************/
	
	/**
	 * @return row count
	 */
	public int getRowCount(){
		return row;
	}
	
	/* (non-Javadoc)
	 * @see java.awt.Container#getComponentAt(int, int)
	 */
	public JComponent getComponentAt(int column_index, int row_index){
		if(column_index < 0 || column_index > column || row_index < 0 || row_index > row)
			return null;//throw new Exception("Index out of bound");
		
		int index = ((row_index) * (column+1))+column_index;
		return ((JEasyTableCell)table.getComponent(index)).getContent();
	}
	
	/**
	 * @param column_index
	 * @param row_index
	 * @return the cell at a specific position
	 * @throws Exception 
	 */
	private JEasyTableCell getCellAt(int column_index, int row_index) throws Exception{
		if(column_index < 0 || column_index > column || row_index < 0 || row_index > row)
			throw new Exception("Index out of bound");
		
		int index = ((row_index) * (column+1))+column_index;
		
		return (JEasyTableCell)table.getComponent(index);
	}
	
	/**
	 * @param row_index index of the row you want to get
	 * @return row of JComponent
	 * @throws Exception 
	 */
	public JComponent[] getRowComponentAt(int row_index) throws Exception{
		if(row_index < 0 || row_index > row)
			throw new Exception("Index out of bound");
		
		int index = ((row_index) * (column+1));
		JComponent[] componentRow = new JComponent[column+1];
		for(int i = 0; i < column+1; i++)
			componentRow[i] = ((JEasyTableCell)table.getComponent(index+i)).getContent();
		return componentRow;
	}
	
	/**
	 * @return index of the selected row
	 * return empty list when nothing selected
	 */
	public List<Integer> getSelectedRowIndex(){
		return selected_index;
	}
	
	/**
	 * @return if 1 or more row are selected
	 */
	public boolean hasRowSelected(){
		if(selected_index.size() > 0)
			return true;
		return false;
	}
	
	/**
	 * @return the content of the selected row
	 */
	public JComponent[][] getSelectedRow(){
		JComponent[][] rowComponent = new JComponent[selected_index.size()][column];
		for(int j = 0; j < selected_index.size(); j++)
		{
			for(int i = 0; i < column; i++)
			{
				rowComponent[j][i] = this.getComponentAt(i, selected_index.get(j));
			}
		}
		
		return rowComponent;
	}
	
	/**************************************************************************
	 * setter
	 *************************************************************************/
	
	/**
	 * place content at row and column
	 * @param content JComponent you want to place
	 * @param column_index index of the column
	 * @param row_index index of the row
	 * @throws Exception 
	 */
	public void setComponentAt(JComponent content, int column_index, int row_index) throws Exception{
		if(column_index > column || row_index > row || column_index < 0 || row_index < 0)
			throw new Exception("Index out of bound");
		
		int index = ((row_index) * (column+1))+column_index;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = c.weighty = 1.0;
		c.gridx = column_index;
		c.gridy = row_index;
		table.remove(index);
		
		JEasyTableCell tempPane;
		
		if(listenerByColumn.containsKey(column_index))
		{
			if(listenerByColumn.get(column_index) instanceof JEasyTableCellFactorySelectable)
				tempPane = listenerByColumn.get(column_index).build(content,row_index);
			else
				tempPane = listenerByColumn.get(column_index).build(content);
		}
		else
		{
			tempPane = new JEasyTableCell(content);
		}
		
		table.add(tempPane,c,index);
		updateVisual();
	}
	
	/**
	 * remplace any previous content with content in parameter
	 * @param content to set in the table
	 */
	public void setContent(JComponent[][] content){
		table.removeAll();
		
		for(int j = 0; j < content.length; j++)
		{
			for(int i = 0; i < content[0].length; i++)
			{
				c.fill = GridBagConstraints.HORIZONTAL;
				c.weightx = c.weighty = 1.0;
				c.gridx = i;
				c.gridy = j;
				
				JEasyTableCell tempPane;
				
				if(listenerByColumn.containsKey(i))
				{
					if(listenerByColumn.get(i) instanceof JEasyTableCellFactorySelectable)
						tempPane = listenerByColumn.get(i).build(content[j][i],j);
					else
						tempPane = listenerByColumn.get(i).build(content[j][i]);
				}
				else
				{
					tempPane = new JEasyTableCell(content[j][i]);
				}
					
				table.add(tempPane,c);
			}
		}
		
		row = content.length-1;
		updateVisual();
	}
	
	/**
	 * remplace row at index
	 * @param content to set at row
	 * @param row_index index of the row to remplace
	 * @throws Exception 
	 */
	public void setComponentRowAt(JComponent[] content, int row_index) throws Exception{
		if(content.length-1 != column)
			throw new Exception("JComponent[].length should be equal to the number of columns");
		if(row_index < 0 || row_index > row)
			throw new Exception("Index out of bound");
			
			
		int index = ((row_index) * (column+1));
		for(int i = 0; i < column+1; i++)
		{
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = c.weighty = 1.0;
			c.gridx = i;
			c.gridy = row_index/*+1*/;
			table.remove(index+(column-i));
			
			JEasyTableCell tempPane;
			
			if(listenerByColumn.containsKey(i))
			{
				if(listenerByColumn.get(i) instanceof JEasyTableCellFactorySelectable)
					tempPane = listenerByColumn.get(i).build(content[i],row_index);
				else
					tempPane = listenerByColumn.get(i).build(content[i]);
			}
			else
			{
				tempPane = new JEasyTableCell(content[i]);
			}
			table.add(tempPane,c,index+(column-i));
		}
		updateVisual();
	}
	
	/**
	 * add click listener at a specific column
	 * @param column_index
	 * @param listener
	 * @throws Exception
	 */
	public void setClickListenerAt(int column_index, Observer listener) throws Exception{
		if(column_index <= column && column != 0 && row == 0)
		{
			listenerByColumn.put(column_index, new JEasyTableCellFactoryClick(listener));
		}
		else
		{
			throw new Exception("asd");
		}
	}
	
	/**
	 * add doubleclick listener at a specific column
	 * @param column_index
	 * @param listener
	 * @throws Exception
	 */
	public void setDoubleClickListenerAt(int column_index, Observer listener) throws Exception{
		if(column_index > column || column == 0 || column_index < 0)
			throw new Exception("Index out of bound");
		
		listenerByColumn.put(column_index, new JEasyTableCellFactoryDoubleClick(listener));
	}
	
	/**
	 * Allow or disallow the table to have scrollbars
	 * @param isScrollable
	 */
	public void setScrollable(boolean isScrollable){
		if(isScrollable)
		{
			this.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);//HORIZONTAL_SCROLLBAR_AS_NEEDED);
			this.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
		}
		else
		{
			this.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
			this.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER);
		}
	}
	
	/**
	 * add click listener at all the table, the click listener is used to select a row
	 * @param column_index
	 * @param listener
	 * @throws Exception
	 */
	public void setSelectable(int maximum_row_selection) throws Exception{
		if(maximum_row_selection < -1)
			throw new Exception("Received negative value, should be positive");

		this.maxSelection = maximum_row_selection-1;
		for(int i = 0; i < column+1; i++)
		{
			listenerByColumn.put(i, new JEasyTableCellFactorySelectable(this));
		}
		
		if(maximum_row_selection == JEasyScrollTable.NONE_SELECTION)
		{
			selectType = SelectionType.None;
		}
		else if(maximum_row_selection == 1)
		{
			selectType = SelectionType.Single;
		}
		else if(maximum_row_selection == JEasyScrollTable.UNLIMITED_SELECTION)
		{
			selectType = SelectionType.Unlimited;
		}
		else
		{
			selectType = SelectionType.Multi;
		}
	}
	
	/**
	 * change a line selection base on its current state (toggle)
	 * @param line index
	 * @throws Exception
	 */
	private void changeLineSelection(int line) throws Exception{
		if(line > row || line < 0)
			throw new Exception("Index out of bound");

		boolean selectionState = this.getCellAt(0, line).isSelected();
		selectType.selectionBasedOnType(this,line,selectionState);
	}
	
	/**
	 * set a line to selected
	 * @param line
	 * @throws Exception
	 */
	public void setLineSelected(int line) throws Exception{
		if(line > row || line < 0)
			throw new Exception("Index out of bound");
		
		if(!this.getCellAt(0, line).isSelected())
		{
			this.changeLineSelection(line);
		}
	}
	
	/**
	 * select all the lines
	 * @throws Exception
	 */
	public void selectAll() throws Exception{
		for(int i = 0; i < row+1; i++)
		{
			this.setLineSelected(i);
		}
	}
	
	/**
	 * remove the current selection
	 * @throws Exception
	 */
	public void unselectAll() throws Exception{
		this.selected_index = new ArrayList<Integer>();
		for(int i = 0; i < row; i++)
		{
			for(int j = 0; j < column+1; j++)
			{
				JEasyTableCell cell = this.getCellAt(j,i);
				cell.setSelected(false);
			}
		}
	}
	
	@Override
	public void update(Observable o, Object e) {
		try {
			this.changeLineSelection((Integer)e);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		JFrame a = new JFrame();
		a.setSize(300,300);
		a.setLayout(new GridBagLayout());
		
		GridBagConstraints contraint = new GridBagConstraints();
		contraint.fill = GridBagConstraints.BOTH;
		contraint.weightx = 1.0;
		contraint.weighty = 1.0;
		contraint.gridx = 0;
		contraint.gridy = 0;
		
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		JComponent[][] comp = new JLabel[6][3];
		for(int i = 0; i < 6; i++)
		{
			for(int j = 0; j < 3; j++)
			{
				if(j == 2)
					comp[i][j] = new JLabel("i"+i+" j"+j+"-------------------------");
				else
					comp[i][j] = new JLabel("i"+i+" j"+j);
			}
		}
		JEasyScrollTable tab = new JEasyScrollTable(new String[]{"aaaaaaaaaaaaaaaaa","b","cc"});
		
		tab.setScrollable(true);
		tab.setClickListenerAt(0, new Observer(){
			@Override
			public void update(Observable o, Object arg) {
				System.out.println(((JLabel)arg).getText());
			}});
		tab.setDoubleClickListenerAt(2, new Observer(){
			@Override
			public void update(Observable o, Object arg) {
				System.out.println(((JLabel)arg).getText()+"B");
			}});

		tab.setSelectable(JEasyScrollTable.UNLIMITED_SELECTION);
		
		tab.setContent(comp);
		tab.clear();
		for(int i = 0; i < comp.length;i++)
			tab.addRow(comp[i]);
		
		tab.removeRow(1);
		tab.removeLastRow();
		
		JComponent[] compAdd = new JLabel[3];
		for(int i = 0; i < compAdd.length; i++)
		{
			compAdd[i] = new JLabel("new "+i);
		}
		JComponent[] compAdd2 = new JLabel[3];
		for(int i = 0; i < compAdd.length; i++)
		{
			compAdd2[i] = new JLabel("new TIME "+i);
		}
		tab.addRowAt(compAdd2,3);

		tab.addRow(compAdd);
		
		JComponent[] row3 = tab.getRowComponentAt(3);
		for(int i = 0; i < row3.length; i++)
			((JLabel)row3[i]).setText("TEST"+i);
		
		JLabel tex = (JLabel) tab.getComponentAt(0,0);
		tex.setText("test");
		JComboBox<String> j = new JComboBox<String>(new String[]{"choix con","choix encore plus con","choix le plus con"});
		tab.setComponentAt(DateField.fromCalendar(Calendar.getInstance()), 1, 3);
		tab.setComponentAt(j, 1, 4);
		
		tab.setComponentAt(JValidTextField.JValidTextFieldWithTextDisallowCertainCharacter("[PENIS]", "BANANA",16), 1, 4);
		
		JComponent[] row2 = new JComponent[3];
		for(int i = 0; i < row2.length; i++)
			row2[i] = new JLabel("Tessst"+i);
		tab.setComponentRowAt(row2,2);
		JComponent[] row1 = new JComponent[3];
		for(int i = 0; i < row1.length; i++)
			row1[i] = new JLabel("TessstT"+i);
		tab.setComponentRowAt(row1,1);
		/*line selection*/
		//tab.setLineSelected(1);
		tab.selectAll();
		tab.unselectAll();
		/***************/
		a.add(tab,contraint);
		a.pack();
		a.setVisible(true);
	}
}