Statistics
| Revision:

gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.vector / org.gvsig.view3d.vector.swing / org.gvsig.view3d.vector.swing.impl / src / main / java / org / gvsig / view3d / vector / swing / impl / JColorChooserButton.java @ 760

History | View | Annotate | Download (3.69 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.vector.swing.impl;
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
import java.util.Random;
34

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

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

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

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

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

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

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

    
73
        if (newColor == null) {
74
            Random rand = new Random();
75
            float r = rand.nextFloat();
76
            float g = rand.nextFloat();
77
            float b = rand.nextFloat();
78
            this.current = new Color(r, g, b);
79
        } else {
80
            this.current = newColor;
81
        }
82
        
83
        setIcon(createIcon(current, 16, 16));
84
        repaint();
85

    
86
        if (notify) {
87
            // Notify everybody that may be interested.
88
            for( ColorChangedListener l : listeners ) {
89
                l.colorChanged(newColor);
90
            }
91
        }
92
    }
93

    
94
    public static interface ColorChangedListener {
95
        public void colorChanged( Color newColor );
96
    }
97

    
98
    private List<ColorChangedListener> listeners = new ArrayList<ColorChangedListener>();
99

    
100
    public void addColorChangedListener( ColorChangedListener toAdd ) {
101
        listeners.add(toAdd);
102
    }
103

    
104
    public static ImageIcon createIcon( Color main, int width, int height ) {
105
        BufferedImage image = new BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
106
        Graphics2D graphics = image.createGraphics();
107
        graphics.setColor(main);
108
        graphics.fillRect(0, 0, width, height);
109
        graphics.setXORMode(Color.DARK_GRAY);
110
        graphics.drawRect(0, 0, width - 1, height - 1);
111
        image.flush();
112
        ImageIcon icon = new ImageIcon(image);
113
        return icon;
114
    }
115
}