OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ColorField extends JPanel {
    private Color color = Color.WHITE;
    private JPanel colorPanel = new JPanel();

    public ColorField(String chooserTitle) {
        this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        int h = new JTextField("O").getPreferredSize().height;
        this.colorPanel.setPreferredSize(new Dimension(h * 2, h));
        this.colorPanel.setMinimumSize(new Dimension(h, h));
        this.colorPanel.setMaximumSize(new Dimension(h * 2, h));
        this.colorPanel.setBackground(this.color);
        this.colorPanel.setOpaque(true);
        this.colorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        this.colorPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                final Color c = JColorChooser.showDialog(ColorField.this.colorPanel, chooserTitle, getColor());
                Color oldValue = getColor();
                setColor(c);
                firePropertyChange("value", oldValue, c);

            }
        });
        this.add(this.colorPanel);
    }

    public void setColor(Color c) {
        this.color = c;
        this.colorPanel.setBackground(c);
    }

    public Color getColor() {
        return this.color;
    }

}