package main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/*
public class FontPicker extends JFrame {

  Color c;

  public FontPicker() {
    super("JColorChooser Test Frame");
    setSize(200, 100);
    final JButton go = new JButton("Show FontChooser");
    go.addActionListener(new ActionListener() {
      final FontChooser chooser = new FontChooser(FontPicker.this);

      boolean first = true;

      public void actionPerformed(ActionEvent e) {
        chooser.setVisible(true);
        // If we got a real font choice, then update our go button
        if (chooser.getNewFont() != null) {
          go.setFont(chooser.getNewFont());
          go.setForeground(chooser.getNewColor());
        }
      }
    });
    getContentPane().add(go);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  public static void main(String args[]) {
    FontPicker fp = new FontPicker();
    fp.setVisible(true);
  }
}
*/

class FontChooser03 extends JDialog implements ActionListener 
{
	JColorChooser colorChooser;
	
	JComboBox comboBox_font;
	JComboBox comboBox_size;
	
	JCheckBox checkbox_bold; 
	JCheckBox checkbox_italic; 
	
	JLabel lblNewLabel;
	
	//JTextField fontSize;
	JLabel previewLabel;
	SimpleAttributeSet attributes;
	Font newFont;
	Color newColor;
	
	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	
	public FontChooser03(Frame parent) 
	{
		super(parent, "Add note", true);
		//setSize(650, 450);
		attributes = new SimpleAttributeSet();

		addWindowListener(new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e) 
			{
				closeAndCancel();
			}
		});

		
		setBounds(100, 100, 689, 547);
		setResizable( false );
		
		getContentPane().setLayout(null);
		
		ImageIcon icon = null;
		icon = createImageIcon("resources/settings.png", "");
		//setIconImage( icon.getImage() );
	
		String[] AllfontSizes = new String[] { 	"8", "9", "10", "11", "12", "14", "16",
				"18", "20", "22", "24", "26", "28", "30"};
		
		textField = new JTextField();
		textField.setBounds(24, 31, 127, 20);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		comboBox_font = new JComboBox(new String[] { "TimesRoman", "Helvetica", "Courier" });
		comboBox_font.setBounds(177, 31, 100, 20);
		getContentPane().add(comboBox_font);
		comboBox_font.setSelectedIndex(1);
		comboBox_font.addActionListener(this);
		
		comboBox_size = new JComboBox(AllfontSizes);
		comboBox_size.setBounds(300, 31, 80, 20);
		getContentPane().add(comboBox_size);
		comboBox_size.addActionListener(this);
		
		checkbox_bold = new JCheckBox(" Bold");
		checkbox_bold.setSelected(true);
		checkbox_bold.setBounds(394, 30, 59, 23);
		getContentPane().add(checkbox_bold);
		checkbox_bold.addActionListener(this);
		
		checkbox_italic = new JCheckBox(" Italic");
		checkbox_italic.setBounds(455, 30, 97, 23);
		getContentPane().add(checkbox_italic);
		checkbox_italic.addActionListener(this);
		
		colorChooser = new JColorChooser(Color.black);
		colorChooser.setBounds(34, 62, 603, 317);
		getContentPane().add(colorChooser);
	    colorChooser.getSelectionModel().addChangeListener(new ChangeListener() 
	    {
	    	public void stateChanged(ChangeEvent e) 
	    	{
	    		updatePreviewColor();
	    	}
        });
		
		
		lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(61, 390, 250, 30);
		getContentPane().add(lblNewLabel);
		
		
		textField.getDocument().addDocumentListener(new DocumentListener() 
		{
			 
			@Override
			public void changedUpdate(DocumentEvent arg0) 
			{
				// TODO Auto-generated method stub
				
				String str = textField.getText();
				
				lblNewLabel.setForeground(colorChooser.getColor());
				lblNewLabel.setText(textField.getText());
				
			}
			@Override
			public void insertUpdate(DocumentEvent arg0) 
			{
				// TODO Auto-generated method stub
				lblNewLabel.setForeground(colorChooser.getColor());
				lblNewLabel.setText(textField.getText());
				
				
				String name = StyleConstants.getFontFamily(attributes);
			    boolean bold = StyleConstants.isBold(attributes);
			    boolean ital = StyleConstants.isItalic(attributes);
			    int size = StyleConstants.getFontSize(attributes);

			    Font f = new Font(name, (bold ? Font.BOLD : 0) + (ital ? Font.ITALIC : 0), size);
			    lblNewLabel.setFont(f);
				
			}
			@Override
			public void removeUpdate(DocumentEvent arg0) 
			{
				// TODO Auto-generated method stub
				lblNewLabel.setText(textField.getText());
			}

			 
		});
		
		
		JButton okButton = new JButton("Ok");
		okButton.setBounds(165, 427, 89, 23);
		okButton.addActionListener(new ActionListener() 
		{
		  	public void actionPerformed(ActionEvent ae) 
		   	{
		   		closeAndSave();
		   	}
		});
		getContentPane().add(okButton);
	    
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setBounds(419, 427, 89, 23);
		cancelButton.addActionListener(new ActionListener() 
		{
		   	public void actionPerformed(ActionEvent ae) 
		   	{
		   		closeAndCancel();
		   	}
		});
		getContentPane().add(cancelButton);
		
	}
	
	public ImageIcon createImageIcon(String path,  String description) 
	{
		URL imgURL = getClass().getResource(path);

		if (imgURL != null)
		{
  			return new ImageIcon(imgURL, description);
		} 
		else 
		{
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	public void actionPerformed(ActionEvent ae) 
	{
		
		if (!StyleConstants.getFontFamily(attributes).equals(comboBox_font.getSelectedItem())) 
		{
			StyleConstants.setFontFamily(attributes, (String) comboBox_font.getSelectedItem());
		}

		// Check the font size (no error checking yet)
		/*if (StyleConstants.getFontSize(attributes) != Integer.parseInt(fontSize
        .getText())) {
      	StyleConstants.setFontSize(attributes, Integer.parseInt(fontSize
          .getText()));
    	}*/
    
		if (StyleConstants.getFontSize(attributes) != Integer.parseInt((String) comboBox_size.getSelectedItem())) 
		{
			StyleConstants.setFontSize(attributes, Integer.parseInt((String) comboBox_size.getSelectedItem()));
		}
    
		
		if (StyleConstants.isBold(attributes) != checkbox_bold.isSelected()) 
		{
			StyleConstants.setBold(attributes, checkbox_bold.isSelected());
		}
		
		if (StyleConstants.isItalic(attributes) != checkbox_italic.isSelected()) 
		{
			StyleConstants.setItalic(attributes, checkbox_italic.isSelected());
		}
		
		updatePreviewFont();
	}

	protected void updatePreviewFont() 
	{
		String name = StyleConstants.getFontFamily(attributes);
	    boolean bold = StyleConstants.isBold(attributes);
	    boolean ital = StyleConstants.isItalic(attributes);
	    int size = StyleConstants.getFontSize(attributes);

	    Font f = new Font(name, (bold ? Font.BOLD : 0) + (ital ? Font.ITALIC : 0), size);
	    lblNewLabel.setFont(f);
	}

	protected void updatePreviewColor() 
	{
		lblNewLabel.setForeground(colorChooser.getColor());
		lblNewLabel.repaint();
	}

	public Font getNewFont() 
	{
		return newFont;
	}

	public Color getNewColor() 
	{
		return newColor;
	}

	public AttributeSet getAttributes() 
	{
		return attributes;
	}

  	public void closeAndSave() 
  	{
  		newFont = lblNewLabel.getFont();
  		newColor = lblNewLabel.getForeground();
  		setVisible(false);
  	}

  	public void closeAndCancel() 
  	{
  		newFont = null;
  		newColor = null;
  		setVisible(false);
  	}
  	
  	 public Object [] getDonnees()
  	 {
  	      return new Object[]{newFont, newColor};

  	 }
}