package main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
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 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.event.ChangeEvent;
import javax.swing.event.ChangeListener;
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 FontChooser02 extends JDialog implements ActionListener 
{
	JColorChooser colorChooser;
	JComboBox fontName, fontSize;
	JCheckBox fontBold, fontItalic;
	
	private JTextField textField;

	
	//JTextField fontSize;
	JLabel previewLabel;
	SimpleAttributeSet attributes;
	Font newFont;
	Color newColor;
	
	public FontChooser02(Frame parent) 
	{
		super(parent, "Font Chooser", true);
		setSize(650, 450);
		attributes = new SimpleAttributeSet();

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

		Container c = getContentPane();

		JPanel fontPanel = new JPanel();
		
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		
		String[] AllfontNames = ge.getAvailableFontFamilyNames();
		
		fontName = new JComboBox(new String[] { "TimesRoman", "Helvetica", "Courier" });
		//fontName = new JComboBox(AllfontNames);
		
		String[] AllfontSizes = new String[] { 	"8", "9", "10", "11", "12", "14", "16",
			        							"18", "20", "22", "24", "26", "28", "30"};
		
		fontName.setSelectedIndex(1);
		fontName.addActionListener(this);
		
		
		textField = new JTextField();
		textField.setBounds(24, 31, 127, 20);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JComboBox comboBox_font = new JComboBox(new String[] { "TimesRoman", "Helvetica", "Courier" });
		comboBox_font.setBounds(177, 31, 58, 20);
		//getContentPane().add(comboBox);
		fontPanel.add(comboBox_font);
		
		JComboBox comboBox_size = new JComboBox(AllfontSizes);
		comboBox_size.setBounds(261, 31, 58, 20);
		//getContentPane().add(comboBox_1);
		fontPanel.add(comboBox_size);
		
		//fontSize = new JTextField("12", 4);
		fontSize = new JComboBox(AllfontSizes);
		
		//fontSize.setHorizontalAlignment(SwingConstants.RIGHT);
		fontSize.addActionListener(this);
		
		fontBold = new JCheckBox("Bold");
		fontBold.setSelected(true);
		fontBold.addActionListener(this);
		fontItalic = new JCheckBox("Italic");
		fontItalic.addActionListener(this);

	    fontPanel.add(fontName);
	    fontPanel.add(new JLabel(" Size: "));
	    fontPanel.add(fontSize);
	    fontPanel.add(fontBold);
	    fontPanel.add(fontItalic);
	
	    c.add(fontPanel, BorderLayout.NORTH);
   
	    colorChooser = new JColorChooser(Color.black);
	    colorChooser.getSelectionModel().addChangeListener(new ChangeListener() 
	    {
	    	public void stateChanged(ChangeEvent e) 
	    	{
	    		updatePreviewColor();
	    	}
        });
    
	    c.add(colorChooser, BorderLayout.CENTER);

	    JPanel previewPanel = new JPanel(new BorderLayout());
	    previewLabel = new JLabel("Here's a sample of this font.");
	    previewLabel.setForeground(colorChooser.getColor());
	    previewPanel.add(previewLabel, BorderLayout.CENTER);

	    JButton okButton = new JButton("Ok");
	    okButton.addActionListener(new ActionListener() 
	    {
	    	public void actionPerformed(ActionEvent ae) 
	    	{
	    		closeAndSave();
	    	}
	    });
    
	    JButton cancelButton = new JButton("Cancel");
	    cancelButton.addActionListener(new ActionListener() 
	    {
	    	public void actionPerformed(ActionEvent ae) 
	    	{
	    		closeAndCancel();
	    	}
	    });

	    JPanel controlPanel = new JPanel();
	    controlPanel.add(okButton);
	    controlPanel.add(cancelButton);
	    previewPanel.add(controlPanel, BorderLayout.SOUTH);
	
	    previewPanel.setMinimumSize(new Dimension(100, 100));
	    previewPanel.setPreferredSize(new Dimension(100, 100));
	
	    c.add(previewPanel, BorderLayout.SOUTH);
  }

	public void actionPerformed(ActionEvent ae) 
	{
		
		if (!StyleConstants.getFontFamily(attributes).equals(fontName.getSelectedItem())) 
		{
			StyleConstants.setFontFamily(attributes, (String) fontName.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) fontSize.getSelectedItem())) 
		{
			StyleConstants.setFontSize(attributes, Integer.parseInt((String) fontSize.getSelectedItem()));
		}
    
		
		if (StyleConstants.isBold(attributes) != fontBold.isSelected()) 
		{
			StyleConstants.setBold(attributes, fontBold.isSelected());
		}
		
		if (StyleConstants.isItalic(attributes) != fontItalic.isSelected()) 
		{
			StyleConstants.setItalic(attributes, fontItalic.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);
	    previewLabel.setFont(f);
	}

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

	public Font getNewFont() 
	{
		return newFont;
	}

	public Color getNewColor() 
	{
		return newColor;
	}

	public AttributeSet getAttributes() 
	{
		return attributes;
	}

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

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

  	 }
}