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

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import de.javasoft.plaf.synthetica.SyntheticaLookAndFeel;
import de.javasoft.swing.JYComboBox;
import de.javasoft.swing.jycombobox.ColorComboBoxEditor;
import de.javasoft.swing.jycombobox.ColorComboBoxRenderer;
import de.javasoft.swing.jycombobox.ColorPopupPanel;
import de.javasoft.swing.jycombobox.FontComboBoxEditor;
import de.javasoft.swing.jycombobox.FontComboBoxRenderer;
import de.javasoft.swing.jycombobox.FontPopupPanel;

/**
 * JYComboBox demo application - demonstrates basic usage
 * including how to set predefined popups.  
 */
@SuppressWarnings("serial")
public class SimpleComboBox extends JFrame
{
  
  public SimpleComboBox()
  {
    super("SimpleComboBox Demo");
    setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
    
    add(new JLabel("Regular ListComboBox (Resizable)"));

    Object[] items = new Object[]{
       "Item 1234567890", 
       "Item 2", 
       "Item 3", 
    };
    JYComboBox listCombo = new JYComboBox(items);
//@highlight
    listCombo.setPopupResizable(true);
    //set initial popup size
    listCombo.setPopupWidth(200);
    listCombo.setPopupHeight(100);
//@/highlight
    add(listCombo);

    add(Box.createVerticalStrut(10));

    add(new JLabel("ColorComboBox"));
    
    JYComboBox colorCombo = new JYComboBox();
//@highlight
    colorCombo.setItem(Color.RED);
    ColorPopupPanel colorPopup = new ColorPopupPanel(colorCombo);
    colorPopup.setNonColorButtonVisible(false);
    //colorPopup.setOtherColorButtonVisible(false);
    colorCombo.setPopupComponent(colorPopup);
    colorCombo.setRenderer(new ColorComboBoxRenderer(colorCombo.getRenderer()));
    colorCombo.setEditor(new ColorComboBoxEditor(colorCombo.getEditor(), null, false, false));    
//@/highlight
    add(colorCombo);

    add(Box.createVerticalStrut(10));
    
    add(new JLabel("WebColorComboBox"));

    JYComboBox webColorCombo = new JYComboBox();
//@highlight
    webColorCombo.setItem(Color.GREEN);
    ColorPopupPanel webColorPopup = new ColorPopupPanel(webColorCombo);
    webColorPopup.setColorRows(6);
    webColorPopup.setColors(webColorPopup.createWebColorPalette());
    webColorCombo.setPopupComponent(webColorPopup);
    // render hexadezimal, including alpha
    webColorCombo.setRenderer(new ColorComboBoxRenderer(webColorCombo.getRenderer(), true, true));
    webColorCombo.setEditor(new ColorComboBoxEditor(webColorCombo.getEditor(), null, true, false));    
//@/highlight
    add(webColorCombo);

    add(Box.createVerticalStrut(10));

    add(new JLabel("FontComboBox"));

    JYComboBox fontCombo = new JYComboBox();
//@highlight
    fontCombo.setItem(UIManager.getFont("Label.font"));
    FontPopupPanel fontPopup = new FontPopupPanel(fontCombo);
    fontCombo.setPopupComponent(fontPopup);
    fontCombo.setRenderer(new FontComboBoxRenderer(fontCombo.getRenderer()));
    fontCombo.setEditor(new FontComboBoxEditor(fontCombo.getEditor(), null, false));
//@/highlight
    add(fontCombo);
    
    add(Box.createVerticalStrut(10));
    add(new JSeparator());
    add(Box.createVerticalStrut(5));

    JCheckBox cb = new JCheckBox(new AbstractAction("Editable"){
      public void actionPerformed(ActionEvent evt)
      {
        JCheckBox checkBox = (JCheckBox)evt.getSource();
        ArrayList<JYComboBox> combos = new ArrayList<JYComboBox>();  
        SyntheticaLookAndFeel.findComponents(JYComboBox.class, checkBox.getParent(), combos);
        for (JYComboBox c : combos)
          c.setEditable(checkBox.isSelected());
      }
    });
    add(cb);
    
    for (Component c : getContentPane().getComponents())
      ((JComponent)c).setAlignmentX(LEFT_ALIGNMENT);
    
    ((JComponent)getContentPane()).setBorder(new EmptyBorder(10,10,10,10));
    

    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pack();
    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 SimpleComboBox();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
}
