Statistics
| Revision:

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

History | View | Annotate | Download (6.39 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.colorbutton;
25

    
26
import java.awt.Color;
27
import java.awt.Cursor;
28
import java.awt.Dimension;
29
import java.awt.Graphics;
30
import java.awt.Image;
31
import java.awt.Shape;
32
import java.awt.event.MouseEvent;
33
import java.awt.event.MouseListener;
34
import java.util.ArrayList;
35
import java.util.EventObject;
36
import java.util.Iterator;
37

    
38
import javax.swing.JComponent;
39

    
40
import org.gvsig.gui.beans.colorchooser.ColorChooserListener;
41
/**
42
 *
43
 * @version 03/08/2007
44
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
45
 */
46
public class ColorButton extends JComponent implements MouseListener, ColorChooserListener {
47
        private static final long serialVersionUID = 6003841064431749542L;
48

    
49
        private ArrayList<ColorButtonListener> actionCommandListeners = new ArrayList<ColorButtonListener>();
50

    
51
        private Color             color            = Color.black;
52
        private ColorButtonDialog colorButtonPanel = null;
53
        private Image             bufferImage      = null;
54
        private int               width            = 0;
55
        private int               height           = 0;
56
        private Graphics          bufferGraphics   = null;
57

    
58
        public ColorButton() {
59
                setPreferredSize(new Dimension(40, 22));
60
                setCursor(new Cursor(Cursor.HAND_CURSOR));
61
                setColor(Color.black);
62
                addMouseListener(this);
63
        }
64

    
65
        public void setColor(Color value) {
66
                color = value;
67
                refreshImage();
68
        }
69

    
70
        public Color getColor() {
71
                return color;
72
        }
73

    
74
        /*
75
         * (non-Javadoc)
76
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
77
         */
78
        public void mousePressed(MouseEvent e) {
79
                if (!isEnabled())
80
                        return;
81
                colorButtonPanel = new ColorButtonDialog();
82
                Color oldColor = getColor();
83
                colorButtonPanel.setColor(oldColor);
84
                colorButtonPanel.addValueChangedListener(this);
85
                if (!colorButtonPanel.showDialog()) {
86
                        setColor(oldColor);
87
                        callValueChangedListeners();
88
                }
89
        }
90

    
91
        public void actionValueChanged(EventObject e) {
92
                setColor(colorButtonPanel.getColor());
93
                callValueChangedListeners();
94
        }
95

    
96
        public void actionValueDragged(EventObject e) {
97
                setColor(colorButtonPanel.getColor());
98
                callValueDraggedListeners();
99
        }
100

    
101
        /**
102
         * Crea un graphics con las dimensiones del componente si no estaba creado y
103
         * lo devuelve para poder usarlo para dibujar en ?l.
104
         * @return Graphics
105
         */
106
        private Graphics getBufferGraphics() {
107
                int width2 = getBounds().width;
108
                int height2 = getBounds().height;
109
                if (width2 <= 0)
110
                        width2 = 1;
111
                if (height2 <= 0)
112
                        height2 = 1;
113

    
114
                if ((width != width2) || (height != height2)) {
115
                        bufferImage = createImage(width2, height2);
116
                        if (bufferImage == null)
117
                                return null;
118
                        bufferGraphics = bufferImage.getGraphics();
119
                }
120

    
121
                width = width2;
122
                height = height2;
123

    
124
                return bufferGraphics;
125
        }
126

    
127
        /**
128
         * Redibujar el componente en el graphics temporal
129
         */
130
        private void redrawBuffer() {
131
                if (getBufferGraphics() == null)
132
                        return;
133

    
134
                Color newColor = color;
135
                if (isEnabled()) {
136
                        Shape oldClip = getBufferGraphics().getClip();
137
                        getBufferGraphics().setClip(1, 1, width - 2, height - 2);
138
                        if ((color == null) || (color.getAlpha() != 255)) {
139
                                for (int i = 0; (i * 4) <= width; i++) {
140
                                        for (int j = 0; (j * 4) <= height; j++) {
141
                                                if ((i + j) % 2 == 0)
142
                                                        getBufferGraphics().setColor(Color.white);
143
                                                else
144
                                                        getBufferGraphics().setColor(new Color(204, 204, 204));
145
                                                getBufferGraphics().fillRect(1 + i * 4, 1 + j * 4, 4, 4);
146
                                        }
147
                                }
148
                        }
149
                        getBufferGraphics().setClip(oldClip);
150

    
151
                        newColor = color;
152
                } else {
153
                        newColor = getBackground();
154
                }
155

    
156
                if (newColor != null) {
157
                        getBufferGraphics().setColor(newColor);
158
                        getBufferGraphics().fillRect(1, 1, width - 2, height - 2);
159
                }
160

    
161
                if (isEnabled())
162
                        getBufferGraphics().setColor(Color.black);
163
                else
164
                        getBufferGraphics().setColor(new Color(172, 168, 153));
165
                getBufferGraphics().drawRect(0, 0, width - 1, height - 1);
166
        }
167

    
168
        /**
169
         * Redibujar el componente en el graphics temporal y representarlo en el
170
         * componente
171
         */
172
        private void refreshImage() {
173
                redrawBuffer();
174
                if (bufferImage != null)
175
                        getGraphics().drawImage(bufferImage, 0, 0, this);
176
                super.paint(getGraphics());
177
        }
178

    
179
        /*
180
         * (non-Javadoc)
181
         * @see javax.swing.JComponent#paint(java.awt.Graphics)
182
         */
183
        public void paint(Graphics g) {
184
                redrawBuffer();
185
                g.drawImage(bufferImage, 0, 0, this);
186
                super.paint(g);
187
        }
188

    
189
        /**
190
         * A?adir un listener a la lista de eventos
191
         * @param listener
192
         */
193
        public void addValueChangedListener(ColorButtonListener listener) {
194
                if (!actionCommandListeners.contains(listener))
195
                        actionCommandListeners.add(listener);
196
        }
197

    
198
        /**
199
         * Borrar un listener de la lista de eventos
200
         * @param listener
201
         */
202
        public void removeValueChangedListener(ColorButtonListener listener) {
203
                actionCommandListeners.remove(listener);
204
        }
205

    
206
        /**
207
         * Invocar a los eventos asociados al componente
208
         */
209
        private void callValueChangedListeners() {
210
                Iterator<ColorButtonListener> acIterator = actionCommandListeners.iterator();
211
                while (acIterator.hasNext()) {
212
                        ColorButtonListener listener = acIterator.next();
213
                        listener.actionValueChanged(new ColorButtonEvent(this));
214
                }
215
        }
216

    
217
        /**
218
         * Invocar a los eventos asociados al componente
219
         */
220
        private void callValueDraggedListeners() {
221
                Iterator<ColorButtonListener> acIterator = actionCommandListeners.iterator();
222
                while (acIterator.hasNext()) {
223
                        ColorButtonListener listener = acIterator.next();
224
                        listener.actionValueDragged(new ColorButtonEvent(this));
225
                }
226
        }
227

    
228

    
229
        public void mouseClicked(MouseEvent e) {}
230
        public void mouseReleased(MouseEvent e) {}
231
        public void mouseEntered(MouseEvent e) {}
232
        public void mouseExited(MouseEvent e) {}
233
}