import java.net.URL;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class jtabpane01 extends JDialog 
{

	private boolean close_Jdialog = false;
	
	private	JPanel	panel1;
	private	JPanel	panel2;
	private	JPanel	panel3;
	private	JPanel	panel4;
	
	private JTable table;
	private static final int BOOLEAN_COLUMN_INDEX = 0;
	private String[] toolTipStr = {"Select/Deselect all" , "aqw"      ,"Java","French","Latin"};
	
	public jtabpane01(JFrame f, boolean modal) 
	{
	       super(f, modal);
	      
	       // fermer la fenetre
	       addWindowListener(new java.awt.event.WindowAdapter() 
	       {
	           public void windowClosing(java.awt.event.WindowEvent e) 
	           {
	        	   dispose();
	           }
	       });
	      
	       try { 
	    	   
	            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
	        } 
	        catch (Exception e) { 
	            System.out.println("Look and Feel not set"); 
	        } 
	       
	       init();
	}
  
	private void init()
	{
		  	setResizable( false );
			//setBounds(0, 0, 926, 550);
		  	setBounds(0, 0, 1000, 650);
		  	getContentPane().setLayout(null);
			{
				JPanel buttonPane = new JPanel();
				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);
				}
			}
		  	
		  	
		  	
			setTitle("Tabpane - debug ...");
			//getContentPane().setLayout(null);
	     	
			ImageIcon icon = null;
			icon = createImageIcon("resources/settings.png", "");
			setIconImage( icon.getImage() );
			
			setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		
			
			
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			tabbedPane.setBounds(0, 0, 984, 465);
			getContentPane().add(tabbedPane);
			
			//JPanel panel = new JPanel();
			//tabbedPane.addTab("New tab", null, panel, null);
			
			
			JScrollPane scrollPane = new JScrollPane();
			//scrollPane.setBounds(109, 74, 350, 211);
			scrollPane.setBounds(38, 11, 716, 252);
			
			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"},
		            { 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 == BOOLEAN_COLUMN_INDEX) 
		            {
		                return Boolean.class;
		            } 
		            else 
		            {
		                return String.class;
		            }
		        }
		    };
		    
		    table = new JTable(model);
			
		    // 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);
		    //table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		    
	        scrollPane.setViewportView(table);
	        
	        
	    	final TableColumn booleanColumn = table.getTableHeader().getColumnModel().getColumn(BOOLEAN_COLUMN_INDEX);

			 
			// Création de la checkBox qui sera affichée en en-tête de colonne.
			final JCheckBox checkBoxHeader = new JCheckBox();

			checkBoxHeader.setSelected(true);
			// Création et affectation du cell renderer qui aura en charge la représentation de l'en-tête de colonne
			// (affichant une checkbox en plus du rendu par défaut)
			booleanColumn.setHeaderRenderer(new CheckBoxHeaderRenderer(checkBoxHeader));

			// Ajout du "forwarder". Listener qui va être informé des clics sue l'en-tête de colonne et les transmettre à la
			// checkbox.
			final HeaderMouseEventsForwarder eventFrowarder = new HeaderMouseEventsForwarder(checkBoxHeader, BOOLEAN_COLUMN_INDEX);
			table.getTableHeader().addMouseListener(eventFrowarder);

			// Ajout du listener qui va cocher/décocher toutes les checkbox
			checkBoxHeader.addActionListener(new ActionListener() 
			{
				@Override
				public void actionPerformed(final ActionEvent e) 
				{
					if (!(e.getSource() instanceof JCheckBox))
					{
						return;
					}
			
					final boolean selected = ((JCheckBox) e.getSource()).isSelected();
					final TableModel model = table.getModel();
					for (int i = 0; i < model.getRowCount(); i++) 
					{
						model.setValueAt(selected, i, BOOLEAN_COLUMN_INDEX);
					}

				}
			});

	        /****************************************************************/
	        
	        JPanel panel = new JPanel();
			tabbedPane.addTab("New tab", null, panel, null);
			panel.add(scrollPane);
	        
			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);
			
			JButton btnNewButton = new JButton("New button");
			btnNewButton.setBounds(76, 93, 89, 23);
			panel_3.add(btnNewButton);
			
	/*		
			JPanel mainPanel = new JPanel();
			mainPanel.setLayout(new BorderLayout());
			getContentPane().add(mainPanel);
			
			itemTabPanel1();
			itemTabPanel2();
			itemTabPanel3();
			itemTabPanel4();

			JTabbedPane tabPane = new JTabbedPane();
			tabPane.addTab( "Tab 1", panel1);
			tabPane.addTab( "Tab 2", panel2);
			tabPane.addTab( "Tab 3", panel3);
			tabPane.addTab( "Tab 4", panel4);
			mainPanel.add(tabPane);
	*/		 
			/*******************/
			
			
			JSeparator separator = new JSeparator();
			separator.setBounds(240, 302, 440, 2);
			getContentPane().add(separator);
		
			/*******************/
		
			JButton btn_ok = new JButton("OK");
			btn_ok.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					close_Jdialog = false;
					System.out.println("Btn Cancel  \n");
					dispose();
				}
			});
			btn_ok.setBounds(135, 550, 89, 23);
			getContentPane().add(btn_ok);
	
			/*******************/
			
			JButton btn_cancel = new JButton("Cancel");
			btn_cancel.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					close_Jdialog = false;
					System.out.println("Btn Cancel  \n");
					dispose();
				}
			});
			btn_cancel.setBounds(359, 550, 89, 23);
			getContentPane().add(btn_cancel);
		
			
	}
    
	public ImageIcon createImageIcon(String path,  String description) 
	{
		URL imgURL = getClass().getResource(path);

		if (imgURL != null)
		{
  			return new ImageIcon(imgURL, description);
		} 
		else 
		{
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}
	
	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;
	    }
	}
    
	
/*	
	public void itemTabPanel1()
	{
		panel1 = new JPanel();
		panel1.setLayout(null);
		
		JButton btn1 = new JButton("Button 1");
		btn1.setBounds(10, 11, 89, 23);
		panel1.add(btn1);
		
		JButton btn2 = new JButton("Button 2");
		btn2.setBounds(10, 45, 89, 23);
		panel1.add(btn2);
	}

	public void itemTabPanel2()
	{
		panel2 = new JPanel();
		panel2.setLayout(null);
		
		JButton btn3 = new JButton("Button 3");
		btn3.setBounds(10, 11, 89, 23);
		panel2.add(btn3);
		
		JButton btn4 = new JButton("Button 4");
		btn4.setBounds(10, 45, 89, 23);
		panel2.add(btn4);	
	}

	public void itemTabPanel3()
	{
		panel3 = new JPanel();
		panel3.setLayout(null);;
		
		JButton btn5 = new JButton("Button 5");
		btn5.setBounds(10, 11, 89, 23);
		panel3.add(btn5);
		
		JButton btn6 = new JButton("Button 6");
		btn6.setBounds(10, 45, 89, 23);
		panel3.add(btn6);		
		
	}
	
	public void itemTabPanel4()
	{
		panel4 = new JPanel();
		panel4.setLayout(null);;
		
		JButton btn7 = new JButton("Button 7");
		btn7.setBounds(10, 11, 89, 23);
		panel4.add(btn7);
		
		JButton btn8 = new JButton("Button 8");
		btn8.setBounds(10, 45, 89, 23);
		panel4.add(btn8);	
	}
*/	
}