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

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import de.javasoft.swing.DetailsDialog;
import de.javasoft.swing.ExtendedFileChooser;

/**
 * A simple FileChooser application used to demonstrate and explain  
 * essential functionality of the ExtendedFileChooser component.
 */
@SuppressWarnings("serial")
public class SimpleFileChooser extends JFrame
{
  public SimpleFileChooser()
  {
    super("Simple FileChooser Demo");
    
    //create and add the fileChooser panel and its components
    JPanel panel = new JPanel();
    init(panel);
    add(panel);
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);        //center
    this.setVisible(true);
  }

  /**
   * Add components to the panel - use BoxLayout.
   */
  private void init(JPanel panel)
  {
    final JTextField texField = new JTextField(40);
    texField.setAlignmentX(Box.CENTER_ALIGNMENT);
    texField.setEditable(false);

    JPanel selectedPanel = new JPanel(new BorderLayout());
    selectedPanel.setBorder(new TitledBorder("Selected File"));
    selectedPanel.add(texField);
    
    JButton button = new JButton(new AbstractAction("Open ExtendedFileChooser")
    {
      public void actionPerformed(ActionEvent evt)
      {
        Component c = (Component)evt.getSource();
//@highlight
        ExtendedFileChooser chooser = new ExtendedFileChooser();
        if (chooser.showOpenDialog(c) == ExtendedFileChooser.APPROVE_OPTION)
        {  
          try
          {
            texField.setText(chooser.getSelectedFile().getCanonicalPath());
            //if text is longer than the textField, show the leading part  
            texField.setCaretPosition(0);
          }
          catch (IOException e)
          {
            //open an ErrorDialog and show error details
            DetailsDialog.showDialog(SwingUtilities.getWindowAncestor(c), null, null, e);
          }
        }
//@/highlight
      }
    });
    button.setAlignmentX(Box.CENTER_ALIGNMENT);
    
    panel.setBorder(new EmptyBorder(10,10,10,10));
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    panel.add(button);
    panel.add(Box.createVerticalStrut(10));
    panel.add(selectedPanel);    
  }

  /**
   * 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 SimpleFileChooser();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
  
}
