root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / colorbutton / ColorButton.java @ 17327
History | View | Annotate | Download (6.29 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<ColorButtonListener> actionCommandListeners = new ArrayList<ColorButtonListener>(); |
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 = null; |
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 |
callValueChangedListeners(); |
83 |
} |
84 |
} |
85 |
|
86 |
public void actionValueChanged(EventObject e) { |
87 |
setColor(colorButtonPanel.getColor()); |
88 |
callValueChangedListeners(); |
89 |
} |
90 |
|
91 |
public void actionValueDragged(EventObject e) { |
92 |
setColor(colorButtonPanel.getColor()); |
93 |
callValueDraggedListeners(); |
94 |
} |
95 |
|
96 |
/**
|
97 |
* Crea un graphics con las dimensiones del componente si no estaba creado y
|
98 |
* lo devuelve para poder usarlo para dibujar en ?l.
|
99 |
* @return Graphics
|
100 |
*/
|
101 |
private Graphics getBufferGraphics() { |
102 |
int width2 = getBounds().width;
|
103 |
int height2 = getBounds().height;
|
104 |
if (width2 <= 0) |
105 |
width2 = 1;
|
106 |
if (height2 <= 0) |
107 |
height2 = 1;
|
108 |
|
109 |
if ((width != width2) || (height != height2)) {
|
110 |
bufferImage = createImage(width2, height2); |
111 |
if (bufferImage == null) |
112 |
return null; |
113 |
bufferGraphics = bufferImage.getGraphics(); |
114 |
} |
115 |
|
116 |
width = width2; |
117 |
height = height2; |
118 |
|
119 |
return bufferGraphics;
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Redibujar el componente en el graphics temporal
|
124 |
*/
|
125 |
private void redrawBuffer() { |
126 |
if (getBufferGraphics() == null) |
127 |
return;
|
128 |
|
129 |
Color newColor = color;
|
130 |
if (isEnabled()) {
|
131 |
Shape oldClip = getBufferGraphics().getClip();
|
132 |
getBufferGraphics().setClip(1, 1, width - 2, height - 2); |
133 |
if ((color == null) || (color.getAlpha() != 255)) { |
134 |
for (int i = 0; (i * 4) <= width; i++) { |
135 |
for (int j = 0; (j * 4) <= height; j++) { |
136 |
if ((i + j) % 2 == 0) |
137 |
getBufferGraphics().setColor(Color.white);
|
138 |
else
|
139 |
getBufferGraphics().setColor(new Color(204, 204, 204)); |
140 |
getBufferGraphics().fillRect(1 + i * 4, 1 + j * 4, 4, 4); |
141 |
} |
142 |
} |
143 |
} |
144 |
getBufferGraphics().setClip(oldClip); |
145 |
|
146 |
newColor = color; |
147 |
} else {
|
148 |
newColor = getBackground(); |
149 |
} |
150 |
|
151 |
if (newColor != null) { |
152 |
getBufferGraphics().setColor(newColor); |
153 |
getBufferGraphics().fillRect(1, 1, width - 2, height - 2); |
154 |
} |
155 |
|
156 |
if (isEnabled())
|
157 |
getBufferGraphics().setColor(Color.black);
|
158 |
else
|
159 |
getBufferGraphics().setColor(new Color(172, 168, 153)); |
160 |
getBufferGraphics().drawRect(0, 0, width - 1, height - 1); |
161 |
} |
162 |
|
163 |
/**
|
164 |
* Redibujar el componente en el graphics temporal y representarlo en el
|
165 |
* componente
|
166 |
*/
|
167 |
private void refreshImage() { |
168 |
redrawBuffer(); |
169 |
if (bufferImage != null) |
170 |
getGraphics().drawImage(bufferImage, 0, 0, this); |
171 |
super.paint(getGraphics());
|
172 |
} |
173 |
|
174 |
/*
|
175 |
* (non-Javadoc)
|
176 |
* @see javax.swing.JComponent#paint(java.awt.Graphics)
|
177 |
*/
|
178 |
public void paint(Graphics g) { |
179 |
redrawBuffer(); |
180 |
g.drawImage(bufferImage, 0, 0, this); |
181 |
super.paint(g);
|
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<ColorButtonListener> acIterator = actionCommandListeners.iterator();
|
206 |
while (acIterator.hasNext()) {
|
207 |
ColorButtonListener listener = 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<ColorButtonListener> acIterator = actionCommandListeners.iterator();
|
217 |
while (acIterator.hasNext()) {
|
218 |
ColorButtonListener listener = 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 |
} |