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

import java.awt.EventQueue;
import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import de.javasoft.swing.JYTextField;
import de.javasoft.swing.JYTextField.PromptStrategy;

/**
 * Demonstrates text field with leading button and prompt.  
 */
@SuppressWarnings("serial")
public class TextFieldWithLeadingButton extends JFrame
{
  public TextFieldWithLeadingButton()
  {
    super("JYTextField With Leading Button");
    setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20));
    
    JYTextField textField = new JYTextField(30);

    //create star icon button
    JButton button = new JButton();
    button.setFocusable(false);
    button.setBorder(new EmptyBorder(0,0,0,0));
    button.setBorderPainted(false);
    button.setContentAreaFilled(false);
    button.setIcon(new ImageIcon(getClass().getResource("/resources/icons/star14x14.png")));
    
    JCheckBox checkBox = new JCheckBox();
    checkBox.setFocusable(false);
    checkBox.setOpaque(false);
    checkBox.setSelected(true);
    
//@highlight
    //add leading components
    textField.addLeadingComponent(button);
    textField.addLeadingComponent(checkBox);    

    //add some space
    textField.addLeadingComponent((JComponent)Box.createHorizontalStrut(4));
    
    //set custom prompt text
    textField.setPromptText("JYTextField - type your text...");
    
    //print prompt if text field is empty - independent from focus
    textField.setPromptStrategy(PromptStrategy.NO_TEXT);
//@/highlight

    add(textField);
    
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(400, 250);
    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 TextFieldWithLeadingButton();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
}
