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

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

import org.jdesktop.swingx.JXHyperlink;
import org.jdesktop.swingx.JXTaskPaneContainer;

import de.javasoft.swing.JYSearchField;
import de.javasoft.swing.JYTaskPane;

/**
 * A simple TaskPane application used to demonstrate and explain  
 * basic functionality.
 */
@SuppressWarnings("serial")
public class SimpleTaskPane extends JFrame
{
  public SimpleTaskPane()
  {
    super("Simple JYTaskPane");
//@highlight
    //add taskPane at the left
    add(createTaskPaneContainer(), BorderLayout.WEST);
//@/highlight
    getContentPane().setBackground(Color.WHITE);
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(640, 480);
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /**
   * Create a simple taskPaneContainer including all taskPanes. The taskPaneContainer
   * is placed in a scrollpane.
   */
  private Component createTaskPaneContainer()
  {
    Icon iconOpen = new ImageIcon(this.getClass().getResource("/resources/icons/fileopen.png"));
//@highlight
    JYTaskPane fileTaskPane = new JYTaskPane("Special TaskPane", iconOpen);
    //enable special to highlight the title pane
    fileTaskPane.setSpecial(true);
    fileTaskPane.setMnemonic(KeyEvent.VK_S);    
//@/highlight

    //by adding an action a JXHyperlink component will be created
    Icon iconNew = new ImageIcon(this.getClass().getResource("/resources/icons/filenew.png"));
    fileTaskPane.add(createTaskPaneAction("Hyperlink Button 1", iconNew));
    fileTaskPane.add(createTaskPaneAction("Hyperlink Button 2", iconOpen));
    Icon iconSave = new ImageIcon(this.getClass().getResource("/resources/icons/filesave.png"));
    fileTaskPane.add(createTaskPaneAction("Hyperlink Button 3", iconSave));
    fileTaskPane.add(Box.createVerticalStrut(5));
    fileTaskPane.add(new JSeparator());
    fileTaskPane.add(Box.createVerticalStrut(5));
    fileTaskPane.add(new JYSearchField(10));
    
    final JYTaskPane displayTaskPane = new JYTaskPane();
    Icon iconDisplay = new ImageIcon(this.getClass().getResource("/resources/icons/display.png"));    
    displayTaskPane.add(createTaskPaneAction("Hyperlink Button 1", iconDisplay));
    displayTaskPane.add(createTaskPaneAction("Hyperlink Button 2", iconDisplay));
    displayTaskPane.add(Box.createVerticalStrut(10));
    displayTaskPane.add(new JButton("Reset Display Settings"));
//@highlight
    JPanel titlePane = new JPanel();
    JXHyperlink link = new JXHyperlink(new AbstractAction("Custom title panel")
    {      
      public void actionPerformed(ActionEvent evt)
      {
        displayTaskPane.setCollapsed(!displayTaskPane.isCollapsed());
      }
    });
    //increase focus insets
    link.setBorder(new CompoundBorder(link.getBorder(), new EmptyBorder(3,3,3,3)));
    titlePane.add(link);
    titlePane.setBackground(new Color(0xD3D3D3));
    titlePane.setBorder(new LineBorder(Color.WHITE));
    displayTaskPane.setTitlePane(titlePane);
    Border contentBorder = new CompoundBorder(new MatteBorder(0,1,1,1,Color.WHITE), new EmptyBorder(10,10,10,10));
    displayTaskPane.getContentPane().setBorder(contentBorder);
//@/highlight

    JXTaskPaneContainer taskPaneContainer = new JXTaskPaneContainer();    
    taskPaneContainer.add(fileTaskPane);
    taskPaneContainer.add(displayTaskPane);

    JScrollPane scrollPane = new JScrollPane(taskPaneContainer);
    scrollPane.setBorder(new EmptyBorder(0,0,0,0));
    
    return scrollPane; 
  }

  /**
   * Method for creating a dummy action. 
   */
  private Action createTaskPaneAction(String name, Icon icon)
  {
    return new AbstractAction(name, icon)
    {
      public void actionPerformed(ActionEvent e)
      {        
      }
    };
  }
  
  /**
   * 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 SimpleTaskPane();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }
}
