/*
 * ColorSelectorPanel.java
 *
 * Created on 5 ottobre 2004, 23.31
 */

package it.businesslogic.ireport.gui.sheet;
import java.awt.*;
import java.util.*;
import javax.swing.*;
/**
 *
 * @author  Administrator
 */
public class ColorSelectorPanel extends javax.swing.JPanel {
    
    private Color color = Color.BLACK;
    private String value = "[0,0,0]";
    /** Creates new form ColorSelectorPanel */
    public ColorSelectorPanel() {
        initComponents();
        
        setColor(Color.GRAY);
    }

    public String getValue() {
        return value;
    }

    public void setValue(String newValue) {
        
        Color newColor = parseColorString(newValue);
        if (newColor == null) return;
        this.setColor(newColor);
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
        this.value = "[" + color.getRed() + "," + color.getGreen() + "," +  color.getBlue()+ "]";
        this.jPanelColor.setBackground(color );
        this.jPanelColor.invalidate();
        this.jPanelColor.updateUI();
        jTextFieldColorValue.setText( getValue() );
    }
    
    public static Color parseColorString(String newValue)
    {
        if (newValue == null) return null;
        
        newValue = newValue.trim();
        if (!newValue.startsWith("[") || !newValue.endsWith("]"))
        {
            return null;
        }
        
        int r = 0;
        int g = 0;
        int b = 0;
        String rgbValues = newValue.substring(1,newValue.length()-1);  
        try {
        
            StringTokenizer st = new StringTokenizer(rgbValues, ",",false);
            r = Integer.parseInt( st.nextToken() );
            g = Integer.parseInt( st.nextToken() );
            b = Integer.parseInt( st.nextToken() );
        } catch (Exception ex) { return null; }
        
        Color c = new Color(r,g,b);
        return c;
        
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPanelColor = new javax.swing.JPanel();
        jTextFieldColorValue = new javax.swing.JTextField();
        jButtonSelect = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(45, 10));
        setPreferredSize(new java.awt.Dimension(75, 22));
        jPanelColor.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jPanelColor.setMaximumSize(new java.awt.Dimension(18, 18));
        jPanelColor.setMinimumSize(new java.awt.Dimension(18, 8));
        jPanelColor.setPreferredSize(new java.awt.Dimension(18, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
        add(jPanelColor, gridBagConstraints);

        jTextFieldColorValue.setFont(new java.awt.Font("Dialog", 0, 11));
        jTextFieldColorValue.setText("[0,0,0]");
        jTextFieldColorValue.setBorder(null);
        jTextFieldColorValue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldColorValueActionPerformed(evt);
            }
        });
        jTextFieldColorValue.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldColorValueFocusLost(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        add(jTextFieldColorValue, gridBagConstraints);

        jButtonSelect.setFont(new java.awt.Font("Dialog", 0, 11));
        jButtonSelect.setText("...");
        jButtonSelect.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jButtonSelect.setMinimumSize(new java.awt.Dimension(19, 10));
        jButtonSelect.setPreferredSize(new java.awt.Dimension(19, 22));
        jButtonSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSelectActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
        add(jButtonSelect, gridBagConstraints);

    }//GEN-END:initComponents

    private void jTextFieldColorValueFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldColorValueFocusLost
        jTextFieldColorValueActionPerformed(null);
    }//GEN-LAST:event_jTextFieldColorValueFocusLost

    private void jButtonSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectActionPerformed

        Color c =JColorChooser.showDialog(this,"Pick a color...",this.getColor());
        if (c != null) setColor(c);
        
    }//GEN-LAST:event_jButtonSelectActionPerformed

    private void jTextFieldColorValueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldColorValueActionPerformed
       
        Color newColor = parseColorString(jTextFieldColorValue.getText());
        if (newColor == null)
        {
            this.jTextFieldColorValue.setText(getValue());
        }
        if (newColor != null) setColor(newColor);
        
    }//GEN-LAST:event_jTextFieldColorValueActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSelect;
    private javax.swing.JPanel jPanelColor;
    private javax.swing.JTextField jTextFieldColorValue;
    // End of variables declaration//GEN-END:variables
    
}
