svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / panels / ColorChooserPanel.java @ 11433
History | View | Annotate | Download (6.54 KB)
1 |
|
---|---|
2 |
/*
|
3 |
* The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
|
4 |
* for visualizing and manipulating spatial features with geometry and attributes.
|
5 |
*
|
6 |
* Copyright (C) 2003 Vivid Solutions
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Vivid Solutions
|
25 |
* Suite #1A
|
26 |
* 2328 Government Street
|
27 |
* Victoria BC V8T 5G5
|
28 |
* Canada
|
29 |
*
|
30 |
* (250)385-6040
|
31 |
* www.vividsolutions.com
|
32 |
*/
|
33 |
|
34 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
35 |
*
|
36 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
37 |
*
|
38 |
* This program is free software; you can redistribute it and/or
|
39 |
* modify it under the terms of the GNU General Public License
|
40 |
* as published by the Free Software Foundation; either version 2
|
41 |
* of the License, or (at your option) any later version.
|
42 |
*
|
43 |
* This program is distributed in the hope that it will be useful,
|
44 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
45 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
46 |
* GNU General Public License for more details.
|
47 |
*
|
48 |
* You should have received a copy of the GNU General Public License
|
49 |
* along with this program; if not, write to the Free Software
|
50 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
51 |
*
|
52 |
* For more information, contact:
|
53 |
*
|
54 |
* Generalitat Valenciana
|
55 |
* Conselleria d'Infraestructures i Transport
|
56 |
* Av. Blasco Ib??ez, 50
|
57 |
* 46010 VALENCIA
|
58 |
* SPAIN
|
59 |
*
|
60 |
* +34 963862235
|
61 |
* gvsig@gva.es
|
62 |
* www.gvsig.gva.es
|
63 |
*
|
64 |
* or
|
65 |
*
|
66 |
* IVER T.I. S.A
|
67 |
* Salamanca 50
|
68 |
* 46005 Valencia
|
69 |
* Spain
|
70 |
*
|
71 |
* +34 963163400
|
72 |
* dac@iver.es
|
73 |
*/
|
74 |
package com.iver.cit.gvsig.gui.panels; |
75 |
|
76 |
import java.awt.Color; |
77 |
import java.awt.Dimension; |
78 |
import java.awt.GridBagConstraints; |
79 |
import java.awt.GridBagLayout; |
80 |
import java.awt.Insets; |
81 |
import java.awt.event.ActionEvent; |
82 |
import java.awt.event.ActionListener; |
83 |
import java.util.ArrayList; |
84 |
import java.util.Iterator; |
85 |
|
86 |
import javax.swing.BorderFactory; |
87 |
import javax.swing.JButton; |
88 |
import javax.swing.JColorChooser; |
89 |
import javax.swing.JPanel; |
90 |
import javax.swing.SwingUtilities; |
91 |
|
92 |
import com.iver.cit.gvsig.gui.GUIUtil; |
93 |
|
94 |
|
95 |
/**
|
96 |
* A custom Colour Scheme Browser - custom choices based on JColorChooser.
|
97 |
*/
|
98 |
|
99 |
public class ColorChooserPanel extends JPanel { |
100 |
GridBagLayout gridBagLayout1 = new GridBagLayout(); |
101 |
JButton changeButton = new JButton(); |
102 |
|
103 |
|
104 |
//{DEFECT-PREVENTION} In accessors, it's better to use "workbench = newWorkbench"
|
105 |
//rather than "this.workbench = workbench", because otherwise if you misspell the
|
106 |
//argument, Java won't complain! We should do this everywhere. [Jon Aquino]
|
107 |
JPanel outerColorPanel = new JPanel(); |
108 |
ColorPanel colorPanel = new ColorPanel();
|
109 |
GridBagLayout gridBagLayout2 = new GridBagLayout(); |
110 |
private Color color = Color.black; |
111 |
private int alpha; |
112 |
private ArrayList actionListeners = new ArrayList(); |
113 |
|
114 |
public ColorChooserPanel() {
|
115 |
try {
|
116 |
jbInit(); |
117 |
colorPanel.setLineColor(null);
|
118 |
changeButton.setToolTipText("Browse");
|
119 |
} catch (Exception ex) { |
120 |
ex.printStackTrace(); |
121 |
} |
122 |
} |
123 |
|
124 |
void jbInit() throws Exception { |
125 |
this.setLayout(gridBagLayout1);
|
126 |
changeButton.setMargin(new Insets(0, 0, 0, 0)); |
127 |
changeButton.setText(" ... ");
|
128 |
changeButton.addActionListener(new java.awt.event.ActionListener() {
|
129 |
public void actionPerformed(ActionEvent e) { |
130 |
changeButton_actionPerformed(e); |
131 |
} |
132 |
}); |
133 |
outerColorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
|
134 |
outerColorPanel.setPreferredSize(new Dimension(60, 20)); |
135 |
outerColorPanel.setBackground(Color.white);
|
136 |
outerColorPanel.setLayout(gridBagLayout2); |
137 |
colorPanel.setMargin(0);
|
138 |
colorPanel.setPreferredSize(new Dimension(45, 8)); |
139 |
this.add(changeButton,
|
140 |
new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, |
141 |
GridBagConstraints.CENTER, GridBagConstraints.NONE, |
142 |
new Insets(0, 2, 0, 0), 0, 0)); |
143 |
this.add(outerColorPanel,
|
144 |
new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, |
145 |
GridBagConstraints.CENTER, GridBagConstraints.NONE, |
146 |
new Insets(0, 0, 0, 0), 0, 0)); |
147 |
outerColorPanel.add(colorPanel, |
148 |
new GridBagConstraints(0, 0, 1, 1, 0, 0, |
149 |
GridBagConstraints.CENTER, GridBagConstraints.NONE, |
150 |
new Insets(0, 0, 0, 0), 0, 0)); |
151 |
} |
152 |
|
153 |
void changeButton_actionPerformed(ActionEvent e) { |
154 |
Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), "Choose Colour", color); |
155 |
|
156 |
if (newColor == null) { |
157 |
return;
|
158 |
} |
159 |
|
160 |
setColor(newColor); |
161 |
fireActionPerformed(); |
162 |
} |
163 |
|
164 |
public void setColor(Color color) { |
165 |
this.color = color;
|
166 |
if (color==null) |
167 |
this.color=Color.black; |
168 |
updateColorPanel(); |
169 |
} |
170 |
|
171 |
private void updateColorPanel() { |
172 |
colorPanel.setFillColor(new Color(color.getRed(), color.getGreen(), color.getBlue(),alpha)); |
173 |
colorPanel.repaint(); |
174 |
} |
175 |
|
176 |
public void addActionListener(ActionListener l) { |
177 |
actionListeners.add(l); |
178 |
} |
179 |
|
180 |
public void removeActionListener(ActionListener l) { |
181 |
actionListeners.remove(l); |
182 |
} |
183 |
|
184 |
protected void fireActionPerformed() { |
185 |
for (Iterator i = actionListeners.iterator(); i.hasNext();) { |
186 |
ActionListener l = (ActionListener) i.next(); |
187 |
l.actionPerformed(new ActionEvent(this, 0, null)); |
188 |
} |
189 |
} |
190 |
|
191 |
public Color getColor() { |
192 |
return color;
|
193 |
} |
194 |
|
195 |
//{CLARITY} Might be clearer to say "alpha" rather than "alpha".
|
196 |
//This occurs in other places too. [Jon Aquino]
|
197 |
public void setAlpha(int alpha) { |
198 |
this.alpha = alpha;
|
199 |
color=GUIUtil.alphaColor(color,alpha); |
200 |
updateColorPanel(); |
201 |
} |
202 |
|
203 |
public void setEnabled(boolean newEnabled) { |
204 |
changeButton.setEnabled(newEnabled); |
205 |
} |
206 |
} |