package de.javasoft.synthetica.democenter.examples.table;

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.Highlighter;
import org.jdesktop.swingx.decorator.HighlighterFactory;

import de.javasoft.synthetica.addons.SyntheticaAddonsUtilities;

/**
 * A simple JXTable application used to demonstrate and explain  
 * basic functionality.
 */
@SuppressWarnings("serial")
public class SimpleTable extends JFrame
{
  public SimpleTable()
  {
    super("Simple JXTable");
    createAndAddComponents(getContentPane());

    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /**
   * Create components and add them to the container.
   */
  private void createAndAddComponents(Container container)
  {
    Object[][] data = 
    {
      {"Mercury", "-170C...+350C", new Integer(0), new Integer(0)},
      {"Venus", "+475C", new Integer(0), new Integer(0)},
      {"Earth", "+22C", new Integer(1), new Integer(0)},
      {"Mars", "-23C", new Integer(2), new Integer(0)},
      {"Jupiter", "-123C", new Integer(16), new Integer(1)},
      {"Saturn", "-18C", new Integer(23), "---"},
      {"Uranus", "-218C", new Integer(18), new Integer(10)},
      {"Neptune", "-228C", new Integer(13), new Integer(3)},
      {"Pluto", "-230C", new Integer(3), new Integer(0)}
    };
    Object[] columns = {"Planet", "Temperature", "Moons", "Rings"};    

//@highlight
    JXTable table = new JXTable(data, columns);
    table.setColumnControlVisible(true);
    //for dark themes we define a different color
    Color oddRowColor = SyntheticaAddonsUtilities.isDefaultTextBackgroundDark() ? UIManager.getColor("Panel.background") : Color.WHITE;
    Color evenRowColor = SyntheticaAddonsUtilities.isDefaultTextBackgroundDark() ? new Color(0x2C3747) : new Color(0xF7F7F9);
    Highlighter hl = HighlighterFactory.createAlternateStriping(oddRowColor, evenRowColor); 
    table.setHighlighters(new Highlighter[]{hl});
//@/highlight
    
    //workaround to avoid border fallback to default scrollpane-border after a LAF switch - Java 1.5/JTable bug.
    JScrollPane scrollPane = new JScrollPane(table){
      @Override
      public void updateUI()
      {
        super.updateUI();
        setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
      }
    };
    container.add(scrollPane);
  }

  /**
   * Static main method for application startup. 
   */
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel");
          new SimpleTable();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
}
