Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / colorbutton / ColorButton.java @ 13022

History | View | Annotate | Download (6.24 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.gui.beans.colorbutton;
20

    
21
import java.awt.Color;
22
import java.awt.Cursor;
23
import java.awt.Dimension;
24
import java.awt.Graphics;
25
import java.awt.Image;
26
import java.awt.Shape;
27
import java.awt.event.MouseEvent;
28
import java.awt.event.MouseListener;
29
import java.util.ArrayList;
30
import java.util.EventObject;
31
import java.util.Iterator;
32

    
33
import javax.swing.JComponent;
34

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

    
44
        private ArrayList         actionCommandListeners = new ArrayList();
45

    
46
        private Color             color                  = Color.black;
47
        private ColorButtonDialog colorButtonPanel       = null;
48
        private Image             bufferImage            = null;
49
        private int               width                  = 0;
50
        private int               height                 = 0;
51
        private Graphics          bufferGraphics;
52

    
53
        public ColorButton() {
54
                setPreferredSize(new Dimension(40, 22));
55
                setCursor(new Cursor(Cursor.HAND_CURSOR));
56
                setColor(Color.black);
57
                addMouseListener(this);
58
        }
59

    
60
        public void setColor(Color value) {
61
                color = value;
62
                refreshImage();
63
        }
64

    
65
        public Color getColor() {
66
                return color;
67
        }
68

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

    
85
        public void actionValueChanged(EventObject e) {
86
                setColor(colorButtonPanel.getColor());
87
                callValueChangedListeners();
88
        }
89

    
90
        public void actionValueDragged(EventObject e) {
91
                setColor(colorButtonPanel.getColor());
92
                callValueDraggedListeners();
93
        }
94

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

    
108
                if ((width != width2) || (height != height2)) {
109
                        bufferImage = createImage(width2, height2);
110
                        if (bufferImage == null)
111
                                return null;
112
                        bufferGraphics = bufferImage.getGraphics();
113
                }
114

    
115
                width = width2;
116
                height = height2;
117

    
118
                return bufferGraphics;
119
        }
120

    
121
        /**
122
         * Redibujar el componente en el graphics temporal
123
         */
124
        private void redrawBuffer() {
125
                if (getBufferGraphics() == null)
126
                        return;
127

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

    
145
                        newColor = color;
146
                } else {
147
                        newColor = getBackground();
148
                }
149

    
150
                if (newColor != null) {
151
                        getBufferGraphics().setColor(newColor);
152
                        getBufferGraphics().fillRect(1, 1, width - 2, height - 2);
153
                }
154

    
155
                if (isEnabled())
156
                        getBufferGraphics().setColor(Color.black);
157
                else
158
                        getBufferGraphics().setColor(new Color(172, 168, 153));
159
                getBufferGraphics().drawRect(0, 0, width - 1, height - 1);
160
        }
161

    
162
        /**
163
         * Redibujar el componente en el graphics temporal y representarlo en el
164
         * componente
165
         */
166
        private void refreshImage() {
167
                redrawBuffer();
168
                if (bufferImage != null)
169
                        getGraphics().drawImage(bufferImage, 0, 0, this);
170
                super.paint(getGraphics());
171
        }
172

    
173
        /*
174
         * (non-Javadoc)
175
         * @see javax.swing.JComponent#paint(java.awt.Graphics)
176
         */
177
        public void paint(Graphics g) {
178
                redrawBuffer();
179
                g.drawImage(bufferImage, 0, 0, this);
180
                super.paint(g);
181
        }
182

    
183

    
184
        /**
185
         * A?adir un listener a la lista de eventos
186
         * @param listener
187
         */
188
        public void addValueChangedListener(ColorButtonListener listener) {
189
                if (!actionCommandListeners.contains(listener))
190
                        actionCommandListeners.add(listener);
191
        }
192

    
193
        /**
194
         * Borrar un listener de la lista de eventos
195
         * @param listener
196
         */
197
        public void removeValueChangedListener(ColorButtonListener listener) {
198
                actionCommandListeners.remove(listener);
199
        }
200

    
201
        /**
202
         * Invocar a los eventos asociados al componente
203
         */
204
        private void callValueChangedListeners() {
205
                Iterator acIterator = actionCommandListeners.iterator();
206
                while (acIterator.hasNext()) {
207
                        ColorButtonListener listener = (ColorButtonListener) acIterator.next();
208
                        listener.actionValueChanged(new ColorButtonEvent(this));
209
                }
210
        }
211

    
212
        /**
213
         * Invocar a los eventos asociados al componente
214
         */
215
        private void callValueDraggedListeners() {
216
                Iterator acIterator = actionCommandListeners.iterator();
217
                while (acIterator.hasNext()) {
218
                        ColorButtonListener listener = (ColorButtonListener) acIterator.next();
219
                        listener.actionValueDragged(new ColorButtonEvent(this));
220
                }
221
        }
222

    
223

    
224
        public void mouseClicked(MouseEvent e) {}
225
        public void mouseReleased(MouseEvent e) {}
226
        public void mouseEntered(MouseEvent e) {}
227
        public void mouseExited(MouseEvent e) {}
228
}