Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / JComboBoxColorScheme.java @ 33213

History | View | Annotate | Download (8.44 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package com.iver.cit.gvsig.gui.styling;
23

    
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Dimension;
27
import java.awt.GradientPaint;
28
import java.awt.Graphics;
29
import java.awt.Graphics2D;
30
import java.awt.Rectangle;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.util.ArrayList;
34

    
35
import javax.swing.JComboBox;
36
import javax.swing.JComponent;
37
import javax.swing.JList;
38
import javax.swing.ListCellRenderer;
39

    
40
import org.gvsig.raster.datastruct.ColorItem;
41
import org.gvsig.raster.datastruct.ColorTable;
42
import org.gvsig.raster.datastruct.persistence.ColorTableLibraryPersistence;
43

    
44
import com.iver.andami.PluginServices;
45

    
46
/**
47
 * Creates a JComboBox where the user has the option to select different
48
 * colors.
49
 *
50
 * @autor jaume dominguez faus - jaume.dominguez@iver.es
51
 */
52
public class JComboBoxColorScheme extends JComboBox {
53
        private String palettesPath = com.iver.andami.Launcher.getAppHomeDir() + "ColorSchemes";
54
        private boolean interpolated = false;
55
        private ArrayList fileList = new ArrayList();
56

    
57
        private ActionListener innerActionUpdateTooltip = new ActionListener() {
58
                public void actionPerformed(ActionEvent e) {
59
                        Object o = getSelectedItem();
60
                        if (o != null) {
61
                                ArrayList aux = (ArrayList) o;
62
                                setToolTipText((String) aux.get(0));
63
                        } else {
64
                                setToolTipText(PluginServices.getText(this, "select_a_color_scheme"));
65
                        }
66
                }
67
        };
68
        /**
69
         * Constructor method
70
         *
71
         * @param interpolateValues
72
         */
73
        public JComboBoxColorScheme(boolean interpolateValues) {
74
                super();
75
                interpolated = interpolateValues;
76
                setPreferredSize(new Dimension(150, 20));
77
                ArrayList fileList = ColorTableLibraryPersistence.getPaletteFileList(palettesPath);
78

    
79

    
80
                for (int i = 0; i < fileList.size(); i++) {
81
                        ArrayList paletteItems = new ArrayList();
82
                        String paletteName = ColorTableLibraryPersistence.loadPalette(
83
                                        palettesPath,
84
                                        (String) fileList.get(i),
85
                                        paletteItems);
86

    
87
                        if (paletteItems.size() <= 0)
88
                                continue;
89

    
90
                        ColorTable colorTable = new ColorTable();
91
                        colorTable.setName(paletteName);
92
                        colorTable.createPaletteFromColorItems(paletteItems, true);
93
                        colorTable.setInterpolated(interpolateValues);
94

    
95
                        ArrayList array = new ArrayList();
96
                        array.add(paletteName);
97
                        array.add(new ColorTablePaint(colorTable));
98

    
99
                        addItem(array);
100
                }
101
                addActionListener(innerActionUpdateTooltip);
102
                setRenderer(new ListCellRenderer() {
103
                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
104
                                ArrayList array = (ArrayList) value;
105

    
106
                                ColorSchemeItemPainter paintItem = new ColorSchemeItemPainter((String) array.get(0), (ColorTablePaint) array.get(1), isSelected);
107
                                paintItem.setPreferredSize(getPreferredSize());
108
                                return (Component) paintItem;
109
                        }
110
                });
111

    
112
        }
113
        /**
114
         * Returns an array composed with the selected colors
115
         * @return
116
         */
117
        public ColorItem[] getSelectedColors() {
118
                Object o = getSelectedItem();
119

    
120
                if (o == null) return null;
121

    
122
                ColorTable ct = ((ColorTablePaint) ((ArrayList) o).get(1)).colorTable;
123
                return (ColorItem[]) ct.getColorItems().toArray(new ColorItem[0]) ;
124
        }
125

    
126
        public void setSelectedColors(ColorItem[] colors) {
127

    
128
                fileList.clear();
129

    
130
                if (colors == null) {
131
                        setSelectedIndex(0);
132
                        return;
133
                }
134
                else {
135

    
136
                        for (int i = 0; i < getItemCount(); i++) {
137
                                fileList.add(getItemAt(i));
138
                        }
139

    
140
                        for (int i = 0; i < fileList.size(); i++) {
141
                                Object o = fileList.get(i);
142
                                ColorTable ct = ((ColorTablePaint) ((ArrayList) o).get(1)).colorTable;
143
                                ColorItem[] myColors = (ColorItem[]) ct.getColorItems().toArray(new ColorItem[0]) ;
144

    
145
                                boolean isEqual = true;
146
                                if(myColors.length == colors.length) {
147
                                        for (int j = 0; isEqual && j < myColors.length; j++) {
148
                                                Color c1 = myColors[j].getColor();
149
                                                Color c2 = colors[j].getColor();
150
                                                isEqual = c1.getRGB() == c2.getRGB() && c1.getAlpha() == c2.getAlpha();
151
                                        }
152
                                        if(isEqual) {
153
                                                setSelectedIndex(i);
154
                                                repaint();
155
                                                return;
156
                                        }
157
                                }
158
                        }
159
                        if(getItemCount()> 0)
160
                                setSelectedItem(0);
161
                }
162

    
163
        }
164

    
165
        /**
166
         *
167
         * Class to paint a color palette in a box. It can be indicated if the palette
168
         * is selected and if it is painted with interpolations
169
         *
170
         * @version 30/07/2007
171
         * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
172
         */
173
        private class ColorTablePaint {
174
                private ColorTable colorTable;
175

    
176
                /**
177
                 * Build a ColorTablePaint with a color table (parameter or the method)
178
                 *
179
                 * @param colorTable
180
                 */
181
                public ColorTablePaint(ColorTable colorTable) {
182
                        this.colorTable = colorTable;
183
                }
184

    
185
                /**
186
                 * Defines if the values are interpolated between them or not.
187
                 *
188
                 * @param value
189
                 */
190
                public void setInterpolated(boolean value) {
191
                        colorTable.setInterpolated(value);
192
                }
193

    
194
                /**
195
                 * Obtains the color array of the color palette
196
                 *
197
                 * @return
198
                 */
199
                public ArrayList getColorItems() {
200
                        return colorTable.getColorItems();
201
                }
202

    
203
                /**
204
                 * Obtains the ColorTable
205
                 * @return
206
                 */
207
                public ColorTable getColorTable() {
208
                        return colorTable;
209
                }
210

    
211
                /**
212
                 * Specifies the colors of the color table, defining it the values are
213
                 * interpolated and if the palette will be compressed or not.
214
                 *
215
                 * @param value
216
                 * @param interpolated
217
                 * @param compress
218
                 */
219
                public void setColorItems(ArrayList value, boolean interpolated, boolean compress) {
220
                        colorTable.createPaletteFromColorItems(value, compress);
221
                        setInterpolated(interpolated);
222
                }
223

    
224
                /**
225
                 * Method to paint the color table
226
                 *
227
                 * @param g
228
                 * @param isSelected
229
                 */
230
                public void paint(Graphics2D g, boolean isSelected, Rectangle bounds) {
231
                        Rectangle area = bounds;
232
                        area.y=0;
233
                        area.x=0;
234
                        int x1 = area.x;
235
                        int x2 = area.x + area.width - 1;
236

    
237
                        if (colorTable.getColorItems().size()>=1) {
238
                                double min = ((ColorItem) colorTable.getColorItems().get(0)).getValue();
239
                                double max = ((ColorItem) colorTable.getColorItems().get(colorTable.getColorItems().size()-1)).getValue();
240
                                for (int i = area.x; i < (area.x + area.width); i++) {
241
                                        double pos = min + (((max - min) * (i - area.x)) / (area.width - 2));
242

    
243
                                        byte[] col3 = colorTable.getRGBAByBand(pos);
244
                                        g.setColor(new Color(col3[0] & 0xff, col3[1] & 0xff, col3[2] & 0xff));
245
                                        g.drawLine(i, area.y, i, area.y + area.height);
246
                                }
247
                        } else {
248
                                g.setColor(new Color(224, 224, 224));
249
                                g.fillRect(x1, area.y, x2 - x1, area.height - 1);
250
                        }
251
                        if (isSelected)
252
                                g.setColor(Color.black);
253
                        else
254
                                g.setColor(new Color(96, 96, 96));
255
                        g.drawRect(x1, area.y, x2 - x1, area.height - 1);
256
                }
257
        }
258

    
259
        private class ColorSchemeItemPainter extends JComponent {
260
                private static final long serialVersionUID = -6448740563809113949L;
261
                private boolean isSelected = false;
262
                private ColorTablePaint colorTablePaint = null;
263
                /**
264
                 * Constructor method
265
                 *
266
                 * @param name
267
                 * @param colorTablePaint
268
                 * @param isSelected
269
                 */
270
                public ColorSchemeItemPainter(String name, ColorTablePaint colorTablePaint, boolean isSelected) {
271
                        super();
272
                        this.colorTablePaint = colorTablePaint;
273
                        this.isSelected = isSelected;
274
                        setToolTipText(name);
275
                }
276

    
277
                public void paintComponent(Graphics g) {
278
                        Graphics2D g2 = (Graphics2D) g;
279
                        if (isSelected) {
280
                                Color color1 = new Color(89, 153, 229);
281
                                Color color2 = new Color(31, 92, 207);
282
                                g2.setPaint(new GradientPaint(0, 1, color1, 0, getHeight() - 1, color2, false));
283
                                g2.fillRect(0, 1, getWidth(), getHeight() - 1);
284
                                g2.setColor(new Color(61, 123, 218));
285
                                g2.drawLine(0, 0, getWidth(), 0);
286
                                g2.setColor(Color.white);
287
                        } else {
288
                                g2.setColor(Color.white);
289
                                g2.fillRect(0, 0, getWidth(), getHeight());
290
                                g2.setColor(Color.black);
291
                        }
292

    
293
                        colorTablePaint.paint((Graphics2D) g2, isSelected, getBounds());
294
                }
295
        }
296
}