root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / styling / JComboBoxColorScheme.java @ 29596
History | View | Annotate | Download (8.81 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.app.gui.styling; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Component; |
45 |
import java.awt.Dimension; |
46 |
import java.awt.GradientPaint; |
47 |
import java.awt.Graphics; |
48 |
import java.awt.Graphics2D; |
49 |
import java.awt.Rectangle; |
50 |
import java.awt.event.ActionEvent; |
51 |
import java.awt.event.ActionListener; |
52 |
import java.io.File; |
53 |
import java.util.ArrayList; |
54 |
|
55 |
import javax.swing.JComboBox; |
56 |
import javax.swing.JComponent; |
57 |
import javax.swing.JList; |
58 |
import javax.swing.ListCellRenderer; |
59 |
|
60 |
import org.gvsig.andami.PluginServices; |
61 |
import org.gvsig.raster.datastruct.ColorItem; |
62 |
import org.gvsig.raster.datastruct.ColorTable; |
63 |
import org.gvsig.raster.datastruct.persistence.ColorTableLibraryPersistence; |
64 |
|
65 |
|
66 |
/**
|
67 |
* Creates a JComboBox where the user has the option to select different
|
68 |
* colors.
|
69 |
*
|
70 |
* @autor jaume dominguez faus - jaume.dominguez@iver.es
|
71 |
*/
|
72 |
public class JComboBoxColorScheme extends JComboBox { |
73 |
private String palettesPath = System.getProperty("user.home") + |
74 |
File.separator +
|
75 |
"gvSIG" +
|
76 |
File.separator +
|
77 |
"ColorSchemes";
|
78 |
private boolean interpolated = false; |
79 |
private ArrayList fileList = new ArrayList(); |
80 |
|
81 |
private ActionListener innerActionUpdateTooltip = new ActionListener() { |
82 |
public void actionPerformed(ActionEvent e) { |
83 |
Object o = getSelectedItem();
|
84 |
if (o != null) { |
85 |
ArrayList aux = (ArrayList) o; |
86 |
setToolTipText((String) aux.get(0)); |
87 |
} else {
|
88 |
setToolTipText(PluginServices.getText(this, "select_a_color_scheme")); |
89 |
} |
90 |
} |
91 |
}; |
92 |
/**
|
93 |
* Constructor method
|
94 |
*
|
95 |
* @param interpolateValues
|
96 |
*/
|
97 |
public JComboBoxColorScheme(boolean interpolateValues) { |
98 |
super();
|
99 |
interpolated = interpolateValues; |
100 |
setPreferredSize(new Dimension(150, 20)); |
101 |
ArrayList fileList = ColorTableLibraryPersistence.getPaletteFileList(palettesPath);
|
102 |
|
103 |
|
104 |
for (int i = 0; i < fileList.size(); i++) { |
105 |
ArrayList paletteItems = new ArrayList(); |
106 |
String paletteName = ColorTableLibraryPersistence.loadPalette(
|
107 |
palettesPath, |
108 |
(String) fileList.get(i),
|
109 |
paletteItems); |
110 |
|
111 |
if (paletteItems.size() <= 0) { |
112 |
continue;
|
113 |
} |
114 |
|
115 |
ColorTable colorTable = new ColorTable();
|
116 |
colorTable.setName(paletteName); |
117 |
colorTable.createPaletteFromColorItems(paletteItems, true);
|
118 |
colorTable.setInterpolated(interpolateValues); |
119 |
|
120 |
ArrayList array = new ArrayList(); |
121 |
array.add(paletteName); |
122 |
array.add(new ColorTablePaint(colorTable));
|
123 |
|
124 |
addItem(array); |
125 |
} |
126 |
addActionListener(innerActionUpdateTooltip); |
127 |
setRenderer(new ListCellRenderer() { |
128 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
129 |
ArrayList array = (ArrayList) value; |
130 |
|
131 |
ColorSchemeItemPainter paintItem = new ColorSchemeItemPainter((String) array.get(0), (ColorTablePaint) array.get(1), isSelected); |
132 |
paintItem.setPreferredSize(getPreferredSize()); |
133 |
return paintItem;
|
134 |
} |
135 |
}); |
136 |
|
137 |
} |
138 |
/**
|
139 |
* Returns an array composed with the selected colors
|
140 |
* @return
|
141 |
*/
|
142 |
public ColorItem[] getSelectedColors() { |
143 |
Object o = getSelectedItem();
|
144 |
|
145 |
if (o == null) { |
146 |
return null; |
147 |
} |
148 |
|
149 |
ColorTable ct = ((ColorTablePaint) ((ArrayList) o).get(1)).colorTable; |
150 |
return (ColorItem[]) ct.getColorItems().toArray(new ColorItem[0]) ; |
151 |
} |
152 |
|
153 |
public void setSelectedColors(ColorItem[] colors) { |
154 |
|
155 |
fileList.clear(); |
156 |
|
157 |
if (colors == null) { |
158 |
setSelectedIndex(0);
|
159 |
return;
|
160 |
} |
161 |
else {
|
162 |
|
163 |
for (int i = 0; i < getItemCount(); i++) { |
164 |
fileList.add(getItemAt(i)); |
165 |
} |
166 |
|
167 |
for (int i = 0; i < fileList.size(); i++) { |
168 |
Object o = fileList.get(i);
|
169 |
ColorTable ct = ((ColorTablePaint) ((ArrayList) o).get(1)).colorTable; |
170 |
ColorItem[] myColors = (ColorItem[]) ct.getColorItems().toArray(new ColorItem[0]) ; |
171 |
|
172 |
boolean isEqual = true; |
173 |
if(myColors.length == colors.length) {
|
174 |
for (int j = 0; isEqual && j < myColors.length; j++) { |
175 |
Color c1 = myColors[j].getColor();
|
176 |
Color c2 = colors[j].getColor();
|
177 |
isEqual = c1.getRGB() == c2.getRGB() && c1.getAlpha() == c2.getAlpha(); |
178 |
} |
179 |
if(isEqual) {
|
180 |
setSelectedIndex(i); |
181 |
repaint(); |
182 |
return;
|
183 |
} |
184 |
} |
185 |
} |
186 |
if(getItemCount()> 0) { |
187 |
setSelectedItem(0);
|
188 |
} |
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
/**
|
194 |
*
|
195 |
* Class to paint a color palette in a box. It can be indicated if the palette
|
196 |
* is selected and if it is painted with interpolations
|
197 |
*
|
198 |
* @version 30/07/2007
|
199 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
200 |
*/
|
201 |
private class ColorTablePaint { |
202 |
private ColorTable colorTable;
|
203 |
|
204 |
/**
|
205 |
* Build a ColorTablePaint with a color table (parameter or the method)
|
206 |
*
|
207 |
* @param colorTable
|
208 |
*/
|
209 |
public ColorTablePaint(ColorTable colorTable) {
|
210 |
this.colorTable = colorTable;
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* Defines if the values are interpolated between them or not.
|
215 |
*
|
216 |
* @param value
|
217 |
*/
|
218 |
public void setInterpolated(boolean value) { |
219 |
colorTable.setInterpolated(value); |
220 |
} |
221 |
|
222 |
/**
|
223 |
* Obtains the color array of the color palette
|
224 |
*
|
225 |
* @return
|
226 |
*/
|
227 |
public ArrayList getColorItems() { |
228 |
return colorTable.getColorItems();
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* Obtains the ColorTable
|
233 |
* @return
|
234 |
*/
|
235 |
public ColorTable getColorTable() {
|
236 |
return colorTable;
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* Specifies the colors of the color table, defining it the values are
|
241 |
* interpolated and if the palette will be compressed or not.
|
242 |
*
|
243 |
* @param value
|
244 |
* @param interpolated
|
245 |
* @param compress
|
246 |
*/
|
247 |
public void setColorItems(ArrayList value, boolean interpolated, boolean compress) { |
248 |
colorTable.createPaletteFromColorItems(value, compress); |
249 |
setInterpolated(interpolated); |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Method to paint the color table
|
254 |
*
|
255 |
* @param g
|
256 |
* @param isSelected
|
257 |
*/
|
258 |
public void paint(Graphics2D g, boolean isSelected, Rectangle bounds) { |
259 |
Rectangle area = bounds;
|
260 |
area.y=0;
|
261 |
area.x=0;
|
262 |
int x1 = area.x;
|
263 |
int x2 = area.x + area.width - 1; |
264 |
|
265 |
if (colorTable.getColorItems().size()>=1) { |
266 |
double min = ((ColorItem) colorTable.getColorItems().get(0)).getValue(); |
267 |
double max = ((ColorItem) colorTable.getColorItems().get(colorTable.getColorItems().size()-1)).getValue(); |
268 |
for (int i = area.x; i < (area.x + area.width); i++) { |
269 |
double pos = min + (((max - min) * (i - area.x)) / (area.width - 2)); |
270 |
|
271 |
byte[] col3 = colorTable.getRGBAByBand(pos); |
272 |
g.setColor(new Color(col3[0] & 0xff, col3[1] & 0xff, col3[2] & 0xff)); |
273 |
g.drawLine(i, area.y, i, area.y + area.height); |
274 |
} |
275 |
} else {
|
276 |
g.setColor(new Color(224, 224, 224)); |
277 |
g.fillRect(x1, area.y, x2 - x1, area.height - 1);
|
278 |
} |
279 |
if (isSelected) {
|
280 |
g.setColor(Color.black);
|
281 |
} else {
|
282 |
g.setColor(new Color(96, 96, 96)); |
283 |
} |
284 |
g.drawRect(x1, area.y, x2 - x1, area.height - 1);
|
285 |
} |
286 |
} |
287 |
|
288 |
private class ColorSchemeItemPainter extends JComponent { |
289 |
private static final long serialVersionUID = -6448740563809113949L; |
290 |
private boolean isSelected = false; |
291 |
private ColorTablePaint colorTablePaint = null; |
292 |
/**
|
293 |
* Constructor method
|
294 |
*
|
295 |
* @param name
|
296 |
* @param colorTablePaint
|
297 |
* @param isSelected
|
298 |
*/
|
299 |
public ColorSchemeItemPainter(String name, ColorTablePaint colorTablePaint, boolean isSelected) { |
300 |
super();
|
301 |
this.colorTablePaint = colorTablePaint;
|
302 |
this.isSelected = isSelected;
|
303 |
setToolTipText(name); |
304 |
} |
305 |
|
306 |
public void paintComponent(Graphics g) { |
307 |
Graphics2D g2 = (Graphics2D) g; |
308 |
if (isSelected) {
|
309 |
Color color1 = new Color(89, 153, 229); |
310 |
Color color2 = new Color(31, 92, 207); |
311 |
g2.setPaint(new GradientPaint(0, 1, color1, 0, getHeight() - 1, color2, false)); |
312 |
g2.fillRect(0, 1, getWidth(), getHeight() - 1); |
313 |
g2.setColor(new Color(61, 123, 218)); |
314 |
g2.drawLine(0, 0, getWidth(), 0); |
315 |
g2.setColor(Color.white);
|
316 |
} else {
|
317 |
g2.setColor(Color.white);
|
318 |
g2.fillRect(0, 0, getWidth(), getHeight()); |
319 |
g2.setColor(Color.black);
|
320 |
} |
321 |
|
322 |
colorTablePaint.paint(g2, isSelected, getBounds()); |
323 |
} |
324 |
} |
325 |
} |