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

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.jdesktop.swingx.JXHyperlink;

/**
 * A simple Hyperlink application used to demonstrate and explain  
 * basic functionality.
 */
@SuppressWarnings("serial")
public class SimpleHyperlink extends JFrame
{
  public SimpleHyperlink()
  {
    super("Simple Hyperlink");
    
    //create and add the hyperlinkpanel
    JPanel panel = new JPanel();    
    panel.setBorder(new EmptyBorder(20,20,20,20));
    createAndAddComponents(panel);
    add(panel);

    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);        //center
    setVisible(true);
  }

  /**
   * Create components and add them to the container.
   */
  private void createAndAddComponents(JPanel panel)
  {
//@highlight
    JXHyperlink hl1 = new JXHyperlink(new HyperlinkAction("Default Hyperlink"));
    hl1.setBorder(new EmptyBorder(2,2,2,2));

    JXHyperlink hl2 = new JXHyperlink(new HyperlinkAction("Colored Hyperlink"));
    hl2.setBorder(new EmptyBorder(2, 2, 2, 2));
    hl2.setUnclickedColor(Color.RED);
    hl2.setClickedColor(Color.MAGENTA);

    JXHyperlink hl3 = new JXHyperlink(new HyperlinkAction("Bold Hyperlink"));
    hl3.setBorder(new EmptyBorder(2, 2, 2, 2));
    hl3.setFont(hl3.getFont().deriveFont(Font.BOLD));
    
    Icon icon = new ImageIcon(getClass().getResource("/de/javasoft/synthetica/democenter/examples/hyperlink/link.png"));
    JXHyperlink hl4 = new JXHyperlink(new HyperlinkAction("Hyperlink with trailing icon"));
    hl4.setBorder(new EmptyBorder(2,2,2,2));
    hl4.setIcon(icon);
    hl4.setHorizontalTextPosition(SwingConstants.LEADING);
//@/highlight

    panel.add(hl1);
    panel.add(hl2);
    panel.add(hl3);    
    panel.add(hl4);    
  }

  /**
   * Hyperlink action class. 
   */
  class HyperlinkAction extends AbstractAction
  {
    public HyperlinkAction(String name)
    {
      super(name);
    }

    public void actionPerformed(ActionEvent evt)
    {
//@highlight
      JXHyperlink hl = (JXHyperlink) evt.getSource();
      hl.setClicked(true);
//@/highlight
    }    
  }  

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