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

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.text.DecimalFormat;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

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

/**
 * Simple JYFormattedTextField demo with leading button and prompt.   
 */
@SuppressWarnings("serial")
public class FormattedTextFieldWithLeadingButton extends JFrame
{
  public FormattedTextFieldWithLeadingButton()
  {
    super("JYFormattedTextField With Leading Button");
    setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20));
    
    JYFormattedTextField textField = new JYFormattedTextField();
    textField.setColumns(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")));
    
//@highlight
    //add leading components
    textField.addLeadingComponent(button);
    //add some space
    textField.addLeadingComponent((JComponent)Box.createHorizontalStrut(4));
    //set custom prompt text
    textField.setPromptText("Input Amount...");

    NumberFormatter defaultFormatter = new NumberFormatter(new DecimalFormat("#.##"));
    defaultFormatter.setValueClass(Float.class);
    NumberFormatter displayFormatter = new NumberFormatter(new DecimalFormat("$ #,###.00"));
    displayFormatter.setValueClass(Float.class);
    NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("#.##")); 
    editFormatter.setValueClass(Float.class);   
    DefaultFormatterFactory factory = new DefaultFormatterFactory(defaultFormatter,displayFormatter,editFormatter);
    textField.setFormatterFactory(factory);
    //textField.setValue(new Float(1599.99));

    //print prompt if text field is empty - independent from focus
    textField.setPromptStrategy(PromptStrategy.NO_TEXT);
//@/highlight

    add(textField);
    add(new JButton("OK"));
    
    //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 FormattedTextFieldWithLeadingButton();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
}
