Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / JComboBoxColorScheme.java @ 13189

History | View | Annotate | Download (7.78 KB)

1 12826 jaume
/* 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 com.iver.cit.gvsig.gui.styling;
42
43 12864 jaume
import java.awt.Color;
44 12826 jaume
import java.awt.Component;
45 12864 jaume
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 12835 jaume
import java.io.File;
53
import java.util.ArrayList;
54 12826 jaume
55
import javax.swing.JComboBox;
56 12864 jaume
import javax.swing.JComponent;
57 12826 jaume
import javax.swing.JList;
58
import javax.swing.ListCellRenderer;
59
60 12864 jaume
import org.gvsig.raster.datastruct.ColorItem;
61 12835 jaume
import org.gvsig.raster.datastruct.ColorTable;
62
import org.gvsig.raster.datastruct.serializer.ColorTableLibraryPersistence;
63
64 12864 jaume
import com.iver.andami.PluginServices;
65
66 12826 jaume
/**
67 13189 jvidal
 * Creates a JComboBox where the user has the option to select different
68
 * colors.
69
 *
70 12826 jaume
 * @autor jaume dominguez faus - jaume.dominguez@iver.es
71
 */
72 12864 jaume
public class JComboBoxColorScheme extends JComboBox {
73 12835 jaume
        private String palettesPath = System.getProperty("user.home") +
74
        File.separator +
75
        "gvSIG" +
76
        File.separator +
77
        "ColorSchemes";
78 13189 jvidal
79 12906 jaume
        private ActionListener innerActionUpdateTooltip = new ActionListener() {
80 12864 jaume
                public void actionPerformed(ActionEvent e) {
81
                        Object o = getSelectedItem();
82
                        if (o != null) {
83
                                ArrayList aux = (ArrayList) o;
84
                                setToolTipText((String) aux.get(0));
85
                        } else {
86
                                setToolTipText(PluginServices.getText(this, "select_a_color_scheme"));
87
                        }
88
                }
89
        };
90 13189 jvidal
        /**
91
         * Constructor method
92
         *
93
         * @param interpolateValues
94
         */
95 12864 jaume
        public JComboBoxColorScheme(boolean interpolateValues) {
96 12826 jaume
                super();
97 12864 jaume
                setPreferredSize(new Dimension(150, 20));
98 12835 jaume
                ArrayList fileList = ColorTableLibraryPersistence.getPaletteFileList(palettesPath);
99
100 13189 jvidal
101 12835 jaume
                for (int i = 0; i < fileList.size(); i++) {
102
                        ArrayList paletteItems = new ArrayList();
103 12864 jaume
                        String paletteName = ColorTableLibraryPersistence.loadPalette(
104 13189 jvidal
                                                palettesPath,
105 12864 jaume
                                                (String) fileList.get(i),
106
                                                paletteItems);
107 12835 jaume
108
                        if (paletteItems.size() <= 0)
109
                                continue;
110
111
                        ColorTable colorTable = new ColorTable();
112
                        colorTable.setName(paletteName);
113
                        colorTable.createPaletteFromColorItems(paletteItems, true);
114 12864 jaume
                        colorTable.setInterpolated(interpolateValues);
115 12835 jaume
116
                        ArrayList array = new ArrayList();
117
                        array.add(paletteName);
118
                        array.add(new ColorTablePaint(colorTable));
119
120
                        addItem(array);
121
                }
122 12906 jaume
                addActionListener(innerActionUpdateTooltip);
123 12826 jaume
                setRenderer(new ListCellRenderer() {
124
                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
125 12835 jaume
                                ArrayList array = (ArrayList) value;
126
127 12864 jaume
                                ColorSchemeItemPainter paintItem = new ColorSchemeItemPainter((String) array.get(0), (ColorTablePaint) array.get(1), isSelected);
128
                                paintItem.setPreferredSize(getPreferredSize());
129
                                return (Component) paintItem;
130 12826 jaume
                        }
131
                });
132 13189 jvidal
133 12826 jaume
        }
134 13189 jvidal
        /**
135
         * Returns an array composed with the selected colors
136
         * @return
137
         */
138 12864 jaume
        public ColorItem[] getSelectedColors() {
139
                Object o = getSelectedItem();
140 13189 jvidal
141 12864 jaume
                if (o == null) return null;
142 13189 jvidal
143 12906 jaume
                ColorTable ct = ((ColorTablePaint) ((ArrayList) o).get(1)).colorTable;
144 12864 jaume
                return (ColorItem[]) ct.getColorItems().toArray(new ColorItem[0]) ;
145
        }
146 13189 jvidal
147
148 12864 jaume
        /**
149
         *
150 13189 jvidal
         * Class to paint a color palette in a box. It can be indicated if the palette
151
         * is selected and if it is painted with interpolations
152
         *
153 12864 jaume
         * @version 30/07/2007
154
         * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
155
         */
156
        private class ColorTablePaint {
157
                private ColorTable colorTable;
158
159
                /**
160 13189 jvidal
                 * Build a ColorTablePaint with a color table (parameter or the method)
161
                 *
162 12864 jaume
                 * @param colorTable
163
                 */
164
                public ColorTablePaint(ColorTable colorTable) {
165
                        this.colorTable = colorTable;
166
                }
167
168
                /**
169 13189 jvidal
                 * Defines if the values are interpolated between them or not.
170
                 *
171 12864 jaume
                 * @param value
172
                 */
173
                public void setInterpolated(boolean value) {
174
                        colorTable.setInterpolated(value);
175
                }
176
177
                /**
178 13189 jvidal
                 * Obtains the color array of the color palette
179
                 *
180 12864 jaume
                 * @return
181
                 */
182
                public ArrayList getColorItems() {
183
                        return colorTable.getColorItems();
184
                }
185
186
                /**
187 13189 jvidal
                 * Obtains the ColorTable
188 12864 jaume
                 * @return
189
                 */
190
                public ColorTable getColorTable() {
191
                        return colorTable;
192
                }
193
194
                /**
195 13189 jvidal
                 * Specifies the colors of the color table, defining it the values are
196
                 * interpolated and if the palette will be compressed or not.
197
                 *
198 12864 jaume
                 * @param value
199
                 * @param interpolated
200
                 * @param compress
201
                 */
202
                public void setColorItems(ArrayList value, boolean interpolated, boolean compress) {
203
                        colorTable.createPaletteFromColorItems(value, compress);
204
                        setInterpolated(interpolated);
205
                }
206
207
                /**
208 13189 jvidal
                 * Method to paint the color table
209
                 *
210 12864 jaume
                 * @param g
211
                 * @param isSelected
212
                 */
213
                public void paint(Graphics2D g, boolean isSelected, Rectangle bounds) {
214
                        Rectangle area = bounds;
215
                        area.y=0;
216
                        area.x=0;
217
                        int x1 = area.x;
218
                        int x2 = area.x + area.width - 1;
219
220
                        if (colorTable.getColorItems().size()>=1) {
221
                                double min = ((ColorItem) colorTable.getColorItems().get(0)).getValue();
222
                                double max = ((ColorItem) colorTable.getColorItems().get(colorTable.getColorItems().size()-1)).getValue();
223
                                for (int i = area.x; i < (area.x + area.width); i++) {
224
                                        double pos = min + (((max - min) * (i - area.x)) / (area.width - 2));
225
226
                                        byte[] col3 = colorTable.getRGBByBand(pos);
227
                                        g.setColor(new Color(col3[0] & 0xff, col3[1] & 0xff, col3[2] & 0xff));
228
                                        g.drawLine(i, area.y, i, area.y + area.height);
229
                                }
230
                        } else {
231
                                g.setColor(new Color(224, 224, 224));
232
                                g.fillRect(x1, area.y, x2 - x1, area.height - 1);
233
                        }
234
                        if (isSelected)
235
                                g.setColor(Color.black);
236
                        else
237
                                g.setColor(new Color(96, 96, 96));
238
                        g.drawRect(x1, area.y, x2 - x1, area.height - 1);
239
                }
240
        }
241 13189 jvidal
242 12864 jaume
        private class ColorSchemeItemPainter extends JComponent {
243
                private static final long serialVersionUID = -6448740563809113949L;
244
                private boolean isSelected = false;
245
                private ColorTablePaint colorTablePaint = null;
246 13189 jvidal
                /**
247
                 * Constructor method
248
                 *
249
                 * @param name
250
                 * @param colorTablePaint
251
                 * @param isSelected
252
                 */
253 12864 jaume
                public ColorSchemeItemPainter(String name, ColorTablePaint colorTablePaint, boolean isSelected) {
254
                        super();
255
                        this.colorTablePaint = colorTablePaint;
256
                        this.isSelected = isSelected;
257
                        setToolTipText(name);
258
                }
259
260
                public void paintComponent(Graphics g) {
261
                        Graphics2D g2 = (Graphics2D) g;
262
                        if (isSelected) {
263
                                Color color1 = new Color(89, 153, 229);
264
                                Color color2 = new Color(31, 92, 207);
265
                                g2.setPaint(new GradientPaint(0, 1, color1, 0, getHeight() - 1, color2, false));
266
                                g2.fillRect(0, 1, getWidth(), getHeight() - 1);
267
                                g2.setColor(new Color(61, 123, 218));
268
                                g2.drawLine(0, 0, getWidth(), 0);
269
                                g2.setColor(Color.white);
270
                        } else {
271
                                g2.setColor(Color.white);
272
                                g2.fillRect(0, 0, getWidth(), getHeight());
273
                                g2.setColor(Color.black);
274
                        }
275
276
                        colorTablePaint.paint((Graphics2D) g2, isSelected, getBounds());
277
                }
278
        }
279 12826 jaume
}