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

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

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.MatteBorder;
import javax.swing.plaf.FontUIResource;

import org.jdesktop.swingx.JXCollapsiblePane;
import org.jdesktop.swingx.JXHyperlink;
import org.jdesktop.swingx.VerticalLayout;

import de.javasoft.swing.JYPropertyTable;

/**
 * Demonstrates how to group and display properties in different tables.
 */
@SuppressWarnings("serial")
public class GroupedPropertyTable extends JFrame
{
  private JLabel testLabel;
  
  public GroupedPropertyTable()
  {
    super("Grouped Property Table");
    createAndAddComponents(getContentPane());

    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(400,300);
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /**
   * Create components and add them to the container.
   */
  private void createAndAddComponents(final Container container)
  {   
    testLabel = new JLabel("Test");
    testLabel.setBackground(Color.RED);

//@highlight
    JYPropertyTable generalTable = new JYPropertyTable();
    initGeneralTable(generalTable);

    JYPropertyTable layoutTable = new JYPropertyTable();
    initLayoutTable(layoutTable);
    
    GroupPanel p = new GroupPanel();
    p.addComponent("General", generalTable);
    p.addComponent("Layout", layoutTable);
//@/highlight

    container.add(p);    
  }

  private void initGeneralTable(JYPropertyTable table)
  {
    table.addProperty("Text", testLabel.getText());
    table.addProperty("Font", testLabel.getFont());
    table.addProperty("Foreground", testLabel.getForeground());
    table.addProperty("Background", testLabel.getBackground());    
    table.addProperty("Opaque", testLabel.isOpaque());
  }

  private void initLayoutTable(JYPropertyTable table)
  {
    table.addProperty("HorizontalAlignment", testLabel.getHorizontalAlignment());
    table.addProperty("VerticalAlignment", testLabel.getVerticalAlignment());
    table.addProperty("PreferredSize", testLabel.getPreferredSize());    
    table.addProperty("Visible", testLabel.isVisible());    
  }
  
  /**
   * 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 GroupedPropertyTable();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }
  
  //
  //Special panel to display multiple tables
  //
  private static class GroupPanel extends JPanel
  {
    public GroupPanel() 
    {
      setLayout(new VerticalLayout());
      setOpaque(false);
    }
    
    public void addComponent(String title, JComponent component)
    {
      JXCollapsiblePane collapsible = new JXCollapsiblePane();
      collapsible.add(component);
      collapsible.setAnimated(false);

      final Action toggleAction = collapsible.getActionMap().get(JXCollapsiblePane.TOGGLE_ACTION);

      JXHyperlink link = new JXHyperlink(toggleAction){
        @Override
        public void updateUI()
        {
          super.updateUI();
          setFont(new FontUIResource(getFont().deriveFont(Font.BOLD)));

          Color foreground = UIManager.getColor("Label.foreground");
          setUnclickedColor(foreground);
          setClickedColor(foreground);
          setBorder(new MatteBorder(0, 0, 1, 0, foreground));      
          setBorderPainted(true);

          toggleAction.putValue(JXCollapsiblePane.COLLAPSE_ICON, UIManager.getIcon("Tree.expandedIcon"));
          toggleAction.putValue(JXCollapsiblePane.EXPAND_ICON, UIManager.getIcon("Tree.collapsedIcon"));
        }
      };
      link.setText(title);
      link.setFocusPainted(false);
      
      add(link);
      add(collapsible);      
    }    
  }  
}
