package designer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.ScrollPaneConstants;

public class tab extends JDialog {
	private JTable table;
	
	private String[] toolTipStr = {"Select/Deselect all" , "aqw"      ,"Java","French","Latin"};

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			tab dialog = new tab();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public tab() 
	{
		setBounds(0, 0, 1000, 650);
		getContentPane().setLayout(null);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setToolTipText("mlm\u00F9l");
			buttonPane.setBounds(0, 579, 984, 33);
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 984, 465);
		getContentPane().add(tabbedPane);
		
		
		
		JScrollPane scrollPane = new JScrollPane();
		//scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBounds(0, 74, 350, 211);
		
		String[] columnNames =  { "", "Test", "Deux" , "rien", "plus"};
		
	    Object[][] data = 
	    	{
	            { Boolean.TRUE, "USA", "Washington DC", "179", "false"},
	            { Boolean.TRUE, "Canada", "Ottawa", "32", "true"},
	            { Boolean.TRUE, "United Kingdom", "London", "60", "false"},
	            { Boolean.TRUE, "Germany", "Berlin", "83", "true"},
	            { Boolean.TRUE, "France", "Paris", "28", "true"},
	            { Boolean.FALSE, "Norway", "Oslo", "79", "false"},
	            { Boolean.TRUE, "India", "New Delhi", "1079", "false"}
	        };
		
		
	    TableModel model = new DefaultTableModel(data,  columnNames)
	    {		
	    	@Override
		    public boolean isCellEditable(int row, int column)
		    {
	    		// make read only fields except column 0,13,14
		        return column == 0 ;
	        }
	    	
	    	@Override
	        public Class<?> getColumnClass(int columnIndex) 
	    	{
	            if (columnIndex == 0) 
	            {
	                return Boolean.class;
	            } 
	            else 
	            {
	                return String.class;
	            }
	        }
	    };
	    
	    table = new JTable(new DefaultTableModel(
	    	new Object[][] {
	    		{Boolean.TRUE, "USA", "Washington DC", "179", "false"},
	    		{Boolean.TRUE, "Canada", "Ottawa", "32", "true"},
	    		{Boolean.TRUE, "United Kingdom", "London", "60", "false"},
	    		{Boolean.TRUE, "Germany", "Berlin", "83", "true"},
	    		{Boolean.TRUE, "France", "Paris", "28", "true"},
	    		{Boolean.FALSE, "Norway", "Oslo", "79", "false"},
	    		{Boolean.TRUE, "India", "New Delhi", "1079", "false"},
	    	},
	    	new String[] {
	    		"", "Test", "Deux", "rien", "plus"
	    	}
	    ));
	
		
		
	    // pas désactiver le classement
	    //table.getTableHeader().setEnabled(false);
	    
	    // bloquer le deplacement des colonnes
	    table.getTableHeader().setReorderingAllowed(false);
	    // dimension
	    table.setRowHeight(30);
	    table.setFont(new Font("Serif", Font.BOLD, 20));
		
	    // on n'a pas le droit de faire une modif
	    table.setEnabled(true);
	    
	    TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
	    table.setRowSorter(sorter);
	    
	    //ToolTipHeader header = new ToolTipHeader(table.getColumnModel());
	    //header.setToolTipStrings(toolTipStr);
	    //header.setToolTipText("Default ToolTip TEXT");
	    //table.setTableHeader(header);
	    
	  //  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	    
        scrollPane.setViewportView(table);

		
		
		
		
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("New tab", null, panel, null);
		panel.add(scrollPane);
		//panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_1, null);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_2, null);
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_3, null);
		panel_3.setLayout(null);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.setBounds(76, 93, 89, 23);
		panel_3.add(btnNewButton);
		
		
		class ToolTipHeader extends JTableHeader {
		    String[] toolTips;
		   
		    public ToolTipHeader(TableColumnModel model) {
		      super(model);
		    }
		     
		    public String getToolTipText(MouseEvent e) {
		      int col  = columnAtPoint(e.getPoint());
		      int modelCol = getTable().convertColumnIndexToModel(col);
		      String retStr;
		      try {
		        retStr = toolTips[modelCol];
		      } catch (NullPointerException ex) {
		        retStr = "";
		      } catch (ArrayIndexOutOfBoundsException ex) {
		        retStr = "";
		      }
		      if (retStr.length() < 1) {
		        retStr = super.getToolTipText(e);
		      }
		      return retStr;
		    }  
		     
		    public void setToolTipStrings(String[] toolTips) {
		      this.toolTips = toolTips;
		    }
		  }
	    
		
	}
}
