Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / colorchooser / ColorChooser.java @ 40561

History | View | Annotate | Download (6.62 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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 3
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.gui.beans.colorchooser;
25

    
26
import java.awt.Color;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.util.ArrayList;
30
import java.util.EventObject;
31
import java.util.Iterator;
32

    
33
import javax.swing.JPanel;
34

    
35
import org.gvsig.gui.beans.checkslidertext.CheckColorSliderTextContainer;
36
import org.gvsig.gui.beans.doubleslider.DoubleSliderEvent;
37
import org.gvsig.gui.beans.doubleslider.DoubleSliderListener;
38
/**
39
 *
40
 * @version 03/08/2007
41
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
42
 */
43
public class ColorChooser extends JPanel implements DoubleSliderListener {
44
        private static final long serialVersionUID = 2397304969793748653L;
45

    
46
        private ArrayList<ColorChooserListener> actionCommandListeners = new ArrayList<ColorChooserListener>();
47

    
48
        private CheckColorSliderTextContainer sliderRed   = null;
49
        private CheckColorSliderTextContainer sliderGreen = null;
50
        private CheckColorSliderTextContainer sliderBlue  = null;
51
        private CheckColorSliderTextContainer sliderAlpha = null;
52

    
53
        public ColorChooser() {
54
                initialize();
55
        }
56

    
57
        private void initialize() {
58
                setLayout(new GridBagLayout());
59
                sliderRed = new CheckColorSliderTextContainer(0, 255, 0, "R", true);
60
                sliderGreen = new CheckColorSliderTextContainer(0, 255, 0, "G", true);
61
                sliderBlue = new CheckColorSliderTextContainer(0, 255, 0, "B", true);
62
                sliderAlpha = new CheckColorSliderTextContainer(0, 255, 255, "A", true);
63
                sliderRed.setCheckboxVisible(false);
64
                sliderGreen.setCheckboxVisible(false);
65
                sliderBlue.setCheckboxVisible(false);
66
                sliderAlpha.setCheckboxVisible(false);
67
                sliderRed.addValueChangedListener(this);
68
                sliderGreen.addValueChangedListener(this);
69
                sliderBlue.addValueChangedListener(this);
70
                sliderAlpha.addValueChangedListener(this);
71
                setColor(Color.black);
72
                updateColors();
73

    
74
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
75
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
76
                gridBagConstraints.weightx = 1.0;
77
                add(sliderRed, gridBagConstraints);
78

    
79
                gridBagConstraints = new GridBagConstraints();
80
                gridBagConstraints.gridx = 0;
81
                gridBagConstraints.gridy = 1;
82
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
83
                gridBagConstraints.weightx = 1.0;
84
                add(sliderGreen, gridBagConstraints);
85

    
86
                gridBagConstraints = new GridBagConstraints();
87
                gridBagConstraints.gridx = 0;
88
                gridBagConstraints.gridy = 2;
89
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
90
                gridBagConstraints.weightx = 1.0;
91
                add(sliderBlue, gridBagConstraints);
92

    
93
                gridBagConstraints = new GridBagConstraints();
94
                gridBagConstraints.gridx = 0;
95
                gridBagConstraints.gridy = 3;
96
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
97
                gridBagConstraints.weightx = 1.0;
98
                add(sliderAlpha, gridBagConstraints);
99
        }
100

    
101
        public void actionValueChanged(DoubleSliderEvent e) {
102
                updateColors();
103
                callChangeValue();
104
        }
105

    
106
        private void updateColors() {
107
                sliderRed.setColor1(new Color(0, sliderGreen.getValue(), sliderBlue.getValue(), sliderAlpha.getValue()), false);
108
                sliderRed.setColor2(new Color(255, sliderGreen.getValue(), sliderBlue.getValue(), sliderAlpha.getValue()), true);
109
                sliderGreen.setColor1(new Color(sliderRed.getValue(), 0, sliderBlue.getValue(), sliderAlpha.getValue()), false);
110
                sliderGreen.setColor2(new Color(sliderRed.getValue(), 255, sliderBlue.getValue(), sliderAlpha.getValue()), true);
111
                sliderBlue.setColor1(new Color(sliderRed.getValue(), sliderGreen.getValue(), 0, sliderAlpha.getValue()), false);
112
                sliderBlue.setColor2(new Color(sliderRed.getValue(), sliderGreen.getValue(), 255, sliderAlpha.getValue()), true);
113
                sliderAlpha.setColor1(new Color(sliderRed.getValue(), sliderGreen.getValue(), sliderBlue.getValue(), 0), false);
114
                sliderAlpha.setColor2(new Color(sliderRed.getValue(), sliderGreen.getValue(), sliderBlue.getValue(), 255), true);
115
        }
116

    
117
        /*
118
         * (non-Javadoc)
119
         * @see org.gvsig.gui.beans.doubleslider.DoubleSliderListener#actionValueDragged(org.gvsig.gui.beans.doubleslider.DoubleSliderEvent)
120
         */
121
        public void actionValueDragged(DoubleSliderEvent e) {
122
                updateColors();
123
                callDraggedValue();
124
        }
125

    
126
        public Color getColor() {
127
                return new Color(sliderRed.getValue(), sliderGreen.getValue(), sliderBlue.getValue(), sliderAlpha.getValue());
128
        }
129

    
130
        public void setColor(Color value) {
131
                sliderRed.setValue(value.getRed());
132
                sliderGreen.setValue(value.getGreen());
133
                sliderBlue.setValue(value.getBlue());
134
                sliderAlpha.setValue(value.getAlpha());
135
                updateColors();
136
        }
137

    
138
        /**
139
         * A?adir un listener a la lista de eventos
140
         * @param listener
141
         */
142
        public void addValueChangedListener(ColorChooserListener listener) {
143
                if (!actionCommandListeners.contains(listener))
144
                        actionCommandListeners.add(listener);
145
        }
146

    
147
        /* (non-Javadoc)
148
         * @see javax.swing.JComponent#setEnabled(boolean)
149
         */
150
        public void setEnabled(boolean enabled) {
151
                super.setEnabled(enabled);
152
                sliderRed.setEnabled(enabled);
153
                sliderGreen.setEnabled(enabled);
154
                sliderBlue.setEnabled(enabled);
155
                sliderAlpha.setEnabled(enabled);
156
        }
157

    
158
        /**
159
         * Borrar un listener de la lista de eventos
160
         * @param listener
161
         */
162
        public void removeValueChangedListener(ColorChooserListener listener) {
163
                actionCommandListeners.remove(listener);
164
        }
165

    
166

    
167
        /**
168
         * Dispara el evento del cambio del control
169
         */
170
        protected void callChangeValue() {
171
                Iterator<ColorChooserListener> acIterator = actionCommandListeners.iterator();
172
                while (acIterator.hasNext()) {
173
                        ColorChooserListener listener = acIterator.next();
174
                        listener.actionValueChanged(new EventObject(this));
175
                }
176
        }
177

    
178
        /**
179
         * Dispara el evento del cambio del control
180
         */
181
        protected void callDraggedValue() {
182
                Iterator<ColorChooserListener> acIterator = actionCommandListeners.iterator();
183
                while (acIterator.hasNext()) {
184
                        ColorChooserListener listener = acIterator.next();
185
                        listener.actionValueDragged(new EventObject(this));
186
                }
187
        }
188
}