//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import org.huihoo.god4j.editor.panels.JavaButton;
import org.huihoo.god4j.util.ImageLoader;




/**
 * @author roger
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class AboutDialog extends JDialog {
   private JButton okButton;
   private JPanel aboutPnl;
   private JPanel creditsPnl;
   private JPanel infoPnl;
   private JTabbedPane tabContainer;
   private JToolBar buttonToolbar;
   private JPanel txtPnl;
   private JLabel txtGoDJ;

   /**
    * CardLayout for displaying different panes on one JDialog
    **/
//   private CardLayout card = new CardLayout();
   private ActionListener actionHandler = new ActionHandler();

   public static void showAbout(Frame owner, String title, ImageIcon logo){
      AboutDialog about =new AboutDialog(owner, title, logo);
      about.show();
      about=null;
   }

   private AboutDialog(Frame owner, String title, ImageIcon logo) {
      super(owner, title, true);
      createUI();
      setResizable(false);
      pack();
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension winsize = this.getSize();
      setLocation(screenSize.width/2 - (winsize.width/2),
                  screenSize.height/2 - (winsize.height/2));
      setLocationRelativeTo(owner);
   }

   private void createUI(){
      createButtonPane();
      createTabbedPane();
      getContentPane().add(tabContainer,BorderLayout.CENTER);
      getContentPane().add(buttonToolbar,BorderLayout.SOUTH);
      getRootPane().setDefaultButton(okButton);
   }

   private void createButtonPane(){
      Dimension dim = getDimension(new String[]{"OK","Credits","About"});
      okButton = new JavaButton("OK", "OK", dim);
      okButton.addActionListener(actionHandler);
      buttonToolbar = new JToolBar(JToolBar.HORIZONTAL);
      buttonToolbar.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
      buttonToolbar.setFloatable(false);
      buttonToolbar.add(Box.createHorizontalGlue());
      buttonToolbar.add(okButton);
      buttonToolbar.add(Box.createHorizontalGlue());
   }

   private void createTabbedPane(){
      createAboutPane();
      createCreditsPane();
      createInfoPane();
      tabContainer = new JTabbedPane();

      tabContainer.addTab("About",aboutPnl);
      tabContainer.addTab("Credits",creditsPnl);
      tabContainer.addTab("Info",infoPnl);

   }

   private void createAboutPane(){
      JLabel aboutLabel = new JLabel(ImageLoader.getImageIcon(ImageLoader.getImageName("AboutPicture")),JLabel.CENTER);
      aboutLabel.setBorder(BorderFactory.createEtchedBorder());
      aboutPnl = new JPanel(new BorderLayout());
      txtPnl=new JPanel(new BorderLayout());
      txtPnl.setFont(new Font("Serif",Font.PLAIN,14));
      txtPnl.add(BorderLayout.NORTH,new JLabel("version: 1.0",SwingConstants.CENTER));
      JLabel vendor=new JLabel("(c) HUIHOO - Huihoo Power",SwingConstants.CENTER);
      vendor.setFont(new Font("SansSerif", Font.BOLD, 14));
      txtPnl.add(BorderLayout.CENTER, vendor);
      txtPnl.add(BorderLayout.SOUTH,
         new JLabel("http://www.huihoo.org      e-mail: dertyang@hotmail.com",
            SwingConstants.CENTER));
      aboutPnl.add(aboutLabel, BorderLayout.CENTER);
      aboutPnl.add(txtPnl,BorderLayout.SOUTH);
   }

   private void createCreditsPane() {
      creditsPnl = new JPanel(new BorderLayout());
      JPanel contextPane = new JPanel(new GridBagLayout());
      Insets ins = new Insets(1, 1, 1, 1);
      txtGoDJ= new JLabel("GoD4J");
      txtGoDJ.setFont(new Font(txtGoDJ.getFont().getFontName(),Font.BOLD,
         14));
      contextPane.add(txtGoDJ,
         new GridBagConstraints(0,0,3,1,0d,0d,GridBagConstraints.CENTER,
            GridBagConstraints.NONE,new Insets(10,1,1,1),0,0));
      contextPane.add(new JLabel("Java XPDL Editor"),
         new GridBagConstraints(0,1,3,1,0d,0d,GridBagConstraints.CENTER,
            GridBagConstraints.NONE,ins,0,0));
      JLabel sponsoredLabel = new JLabel("Sponsored by:");
      sponsoredLabel.setFont(new Font(txtGoDJ.getFont().getFontName(),Font.BOLD,
         12));
      contextPane.add(sponsoredLabel,
         new GridBagConstraints(0,2,1,1,0d,0d,GridBagConstraints.WEST,
            GridBagConstraints.NONE,new Insets(15,1,1,1),0,0));
      JLabel sponsore1logo =new JLabel(ImageLoader.getImageIcon(ImageLoader.getImageName("Sponsore1Logo")));
      sponsore1logo.setBorder(BorderFactory.createEtchedBorder());
      contextPane.add(sponsore1logo,
         new GridBagConstraints(0,3,1,5,0d,0d,GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL,new Insets(10,1,1,30),0,0));
      contextPane.add(new JLabel("(c) HUIHOO - Huihoo Power"),
         new GridBagConstraints(1,4,2,1,0d,0d,GridBagConstraints.CENTER,
            GridBagConstraints.NONE,new Insets(25,1,1,1),0,0));
  
      creditsPnl.add(contextPane, BorderLayout.NORTH);
   }

   private void createInfoPane() {
      infoPnl = new JPanel(new BorderLayout());
      JPanel contextPane = new JPanel(new GridBagLayout());
      Insets ins = new Insets(1, 1, 1, 1);
      JLabel thanks = new JLabel("Many thanks to all who made this project available:");
      thanks.setFont(new Font(thanks.getFont().getFontName(),Font.BOLD,thanks.getFont().getSize()));
      contextPane.add(thanks,
         new GridBagConstraints(0,0,2,1,0d,0d,GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL,ins,0,0));
            
      contextPane.add(new JLabel("jgraph - "),
         new GridBagConstraints(0,6,1,1,0d,0d,GridBagConstraints.EAST,
            GridBagConstraints.NONE,new Insets(8,1,1,1),0,0));
      contextPane.add(new JLabel("www.jgraph.com"),
         new GridBagConstraints(1,6,1,1,0d,0d,GridBagConstraints.WEST,
            GridBagConstraints.NONE,new Insets(8,1,1,1),0,0));
      
      contextPane.add(new JLabel("WFMC - "),
         new GridBagConstraints(0,7,1,1,0d,0d,GridBagConstraints.EAST,
            GridBagConstraints.NONE,ins,0,0));
      contextPane.add(new JLabel("www.wfmc.org"),
         new GridBagConstraints(1,7,1,1,0d,0d,GridBagConstraints.WEST,
            GridBagConstraints.NONE,ins,0,0));
  
//		contextPane.add(new JLabel("Huihoo - "),
//				 new GridBagConstraints(0,8,1,1,0d,0d,GridBagConstraints.EAST,
//						GridBagConstraints.NONE,ins,0,0));
//						
//			contextPane.add(new JLabel("www.huihoo.org"),
//				 new GridBagConstraints(1,8,1,1,0d,0d,GridBagConstraints.WEST,
//						GridBagConstraints.NONE,ins,0,0));
						          
      JTextArea txtProject = new JTextArea();
      txtProject.setText(
            "Many thanks to:\n\n"+
            "adaliangce\n"+
						"Allen Long\n"+
            "fcxiao2000");
      txtProject.setMinimumSize(new Dimension(200,100));
      txtProject.setPreferredSize(new Dimension(250,150));
      txtProject.setMaximumSize(new Dimension(350,250));
      txtProject.setFont(new Font(txtGoDJ.getFont().getFontName(),
            txtGoDJ.getFont().getStyle(),10));
      txtProject.setBackground(Color.LIGHT_GRAY);
      txtProject.setBorder(BorderFactory.createLoweredBevelBorder());
      txtProject.setEditable(false);

      contextPane.add(txtProject,
       new GridBagConstraints(0,9,3,1,0d,0d,GridBagConstraints.CENTER,
            GridBagConstraints.NONE,new Insets(15,1,1,1),0,0));


      infoPnl.add(contextPane,BorderLayout.NORTH);
   }

   private Dimension getDimension(String[] s){
      String[] labels = s;
      int longest=0;
      int w=0;
      for (int i=0; i<labels.length; i++) {
         try {
            w=getFontMetrics(getFont()).stringWidth(labels[i]);
            if (w>longest)
               longest=w;
         } catch(Exception e) {}
      }
      w=longest+22;
      int h = getFontMetrics(getFont()).getHeight()+2;
      Dimension dim = new Dimension(w,h);
      return dim;
   }

   // Close on escape
   protected JRootPane createRootPane() {
      KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
      JRootPane rootPane = new JRootPane();
      rootPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
               setVisible(false);
            }
         }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
      return rootPane;
   }

   private class ActionHandler implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         if (e.getSource() == okButton) {
               dispose();
         }
      }
   }
}