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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import de.javasoft.swing.JYPreferencesPanel;

/**
 * Demonstrates basic usage of the PreferencesPanel.
 */
@SuppressWarnings("serial")
public class SimplePreferencesDialog extends JDialog
{
  public SimplePreferencesDialog()
  {
    super((JFrame)null, "Preferences", true, null);

//@highlight
    JYPreferencesPanel pp = new JYPreferencesPanel();
    String iconPath = "/resources/icons/large/";
    pp.addTab("Display", new ImageIcon(getClass().getResource(iconPath+"display.png")), new JLabel("Display"));
    pp.addTab("System", new ImageIcon(getClass().getResource(iconPath+"filesave.png")), new JLabel("System"));
    pp.addTab("Printer", new ImageIcon(getClass().getResource(iconPath+"fileprint.png")), new JLabel("Printer"));
    pp.addTab("User", new ImageIcon(getClass().getResource(iconPath+"user_male.png")), new JLabel("User"));
    add(pp);
//@/highlight

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
    buttonPanel.setBorder(new EmptyBorder(10,10,10,10));

    buttonPanel.add(Box.createHorizontalGlue());
    buttonPanel.add(new PrefButton("OK", false));
    buttonPanel.add(Box.createHorizontalStrut(5));
    buttonPanel.add(new PrefButton("Cancel", true));
    add(buttonPanel, BorderLayout.SOUTH);
    
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(640, 480);
    setLocationRelativeTo(null);        //center
    setVisible(true);
  }
  
  /**
   * 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 SimplePreferencesDialog();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }
  
  /**
   * PreferenceButton class
   */
  private class PrefButton extends JButton
  {
    public PrefButton(String text, boolean cancel)
    {
      setAction(new AbstractAction(text)
      {
        public void actionPerformed(ActionEvent evt)
        {
          dispose();
        }        
      });
    }
    @Override
    public Dimension getPreferredSize()
    {
      return new Dimension(Math.max(80, super.getMinimumSize().width) , super.getPreferredSize().height);
    }
  }
  
}
