Statistics
| Revision:

gvsig-3d / 2.1 / branches / org.gvsig.view3d_vector_and_extrusion_2.3 / org.gvsig.view3d / org.gvsig.view3d / org.gvsig.view3d.swing / org.gvsig.view3d.swing.impl / src / main / java / org / gvsig / view3d / swing / impl / properties / layer / ColorChooserButton.java @ 708

History | View | Annotate | Download (3.45 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright © 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.view3d.swing.impl.properties.layer;
25

    
26
import java.awt.Color;
27
import java.awt.Graphics2D;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.awt.image.BufferedImage;
31
import java.util.ArrayList;
32
import java.util.List;
33

    
34
import javax.swing.ImageIcon;
35
import javax.swing.JButton;
36
import javax.swing.JColorChooser;
37

    
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.i18n.I18nManager;
40

    
41
/**
42
 * Button for color selection.
43
 * 
44
 * <p>As off: http://stackoverflow.com/questions/26565166/how-to-display-a-color-selector-when-clicking-a-button
45
 * 
46
 */
47
public class ColorChooserButton extends JButton {
48
    private I18nManager i18nManager = ToolsLocator.getI18nManager();
49
    private Color current;
50

    
51
    public ColorChooserButton( Color c ) {
52
        setSelectedColor(c);
53
        addActionListener(new ActionListener(){
54
            @Override
55
            public void actionPerformed( ActionEvent arg0 ) {
56
                Color newColor = JColorChooser.showDialog(null, i18nManager.getTranslation("Choose a Color"), current);
57
                setSelectedColor(newColor);
58
            }
59
        });
60
    }
61

    
62
    public Color getSelectedColor() {
63
        return current;
64
    }
65

    
66
    public void setSelectedColor( Color newColor ) {
67
        setSelectedColor(newColor, true);
68
    }
69

    
70
    public void setSelectedColor( Color newColor, boolean notify ) {
71

    
72
        if (newColor == null)
73
            return;
74

    
75
        current = newColor;
76
        setIcon(createIcon(current, 16, 16));
77
        repaint();
78

    
79
        if (notify) {
80
            // Notify everybody that may be interested.
81
            for( ColorChangedListener l : listeners ) {
82
                l.colorChanged(newColor);
83
            }
84
        }
85
    }
86

    
87
    public static interface ColorChangedListener {
88
        public void colorChanged( Color newColor );
89
    }
90

    
91
    private List<ColorChangedListener> listeners = new ArrayList<ColorChangedListener>();
92

    
93
    public void addColorChangedListener( ColorChangedListener toAdd ) {
94
        listeners.add(toAdd);
95
    }
96

    
97
    public static ImageIcon createIcon( Color main, int width, int height ) {
98
        BufferedImage image = new BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
99
        Graphics2D graphics = image.createGraphics();
100
        graphics.setColor(main);
101
        graphics.fillRect(0, 0, width, height);
102
        graphics.setXORMode(Color.DARK_GRAY);
103
        graphics.drawRect(0, 0, width - 1, height - 1);
104
        image.flush();
105
        ImageIcon icon = new ImageIcon(image);
106
        return icon;
107
    }
108
}