Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / JComboBoxColorScheme.java @ 34294

History | View | Annotate | Download (7.23 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.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.io.File;
52
import java.util.ArrayList;
53

    
54
import javax.swing.JComboBox;
55
import javax.swing.JComponent;
56
import javax.swing.JList;
57
import javax.swing.ListCellRenderer;
58

    
59
import org.gvsig.gui.IColorTables;
60
import org.gvsig.gui.IColorTables.IColorTablePaint;
61
import org.gvsig.i18n.Messages;
62
import org.gvsig.symbology.swing.SymbologySwingLocator;
63

    
64

    
65
/**
66
 * Creates a JComboBox where the user has the option to select different
67
 * colors.
68
 *
69
 * @autor jaume dominguez faus - jaume.dominguez@iver.es
70
 */
71
public class JComboBoxColorScheme extends JComboBox {
72
        /**
73
         * 
74
         */
75
        private static final long serialVersionUID = -749998859270723991L;
76
        private String palettesPath = System.getProperty("user.home") +
77
        File.separator +
78
        "gvSIG" +
79
        File.separator +
80
        "ColorSchemes";
81
        private boolean interpolated = false;
82
        private ArrayList fileList = new ArrayList();
83

    
84
        private ActionListener innerActionUpdateTooltip = new ActionListener() {
85
                public void actionPerformed(ActionEvent e) {
86
                        Object o = getSelectedItem();
87
                        if (o != null) {
88
                                ArrayList aux = (ArrayList) o;
89
                                setToolTipText((String) aux.get(0));
90
                        } else {
91
                                setToolTipText(Messages.getText("select_a_color_scheme"));
92
                        }
93
                }
94
        };
95
        /**
96
         * Constructor method
97
         *
98
         * @param interpolateValues
99
         */
100
        public JComboBoxColorScheme(boolean interpolateValues) {
101
                super();
102
                interpolated = interpolateValues;
103
                setPreferredSize(new Dimension(150, 20));
104
                
105
                IColorTables colorTables = SymbologySwingLocator.getSwingManager().createColorTables();
106
                if(colorTables != null){
107
                        colorTables.load(palettesPath, interpolateValues);
108
                        for (int i=0; i<=colorTables.size(); i++) {
109
                                IColorTablePaint colorTable = colorTables.get(i);
110
                                ArrayList array = new ArrayList();
111
                                array.add(colorTable.getTableName());
112
                                array.add(colorTable);
113
                                addItem(array);
114
                        }
115
                }
116
                addActionListener(innerActionUpdateTooltip);
117
                setRenderer(new ListCellRenderer() {
118
                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
119
                                ArrayList array = (ArrayList) value;
120

    
121
                                ColorSchemeItemPainter paintItem = new ColorSchemeItemPainter((String) array.get(0), (IColorTablePaint) array.get(1), isSelected);
122
                                paintItem.setPreferredSize(getPreferredSize());
123
                                return paintItem;
124
                        }
125
                });
126

    
127
        }
128
        /**
129
         * Returns an array composed with the selected colors
130
         * @return
131
         */
132
//        public ColorItem[] getSelectedColors() {
133
//                Object o = getSelectedItem();
134
//
135
//                if (o == null) {
136
//                        return null;
137
//                }
138
//
139
//                ColorTable ct = ((ColorTablePaint) ((ArrayList) o).get(1)).colorTable;
140
//                return (ColorItem[]) ct.getColorItems().toArray(new ColorItem[0]) ;
141
//        }
142

    
143
        /**
144
         * Returns an array composed with the selected colors
145
         * @return
146
         */
147
        public Color[] getSelectedColors() {
148
                Object o = getSelectedItem();
149

    
150
                if (o == null) {
151
                        return null;
152
                }
153

    
154
                return ((IColorTablePaint) ((ArrayList) o).get(1)).getColors();
155
        }
156

    
157
//        public void setSelectedColors(ColorItem[] colors) {
158
//
159
//                fileList.clear();
160
//
161
//                if (colors == null) {
162
//                        setSelectedIndex(0);
163
//                        return;
164
//                }
165
//                else {
166
//
167
//                        for (int i = 0; i < getItemCount(); i++) {
168
//                                fileList.add(getItemAt(i));
169
//                        }
170
//
171
//                        for (int i = 0; i < fileList.size(); i++) {
172
//                                Object o = fileList.get(i);
173
//                                ColorTable ct = ((ColorTablePaint) ((ArrayList) o).get(1)).colorTable;
174
//                                ColorItem[] myColors = (ColorItem[]) ct.getColorItems().toArray(new ColorItem[0]) ;
175
//
176
//                                boolean isEqual = true;
177
//                                if(myColors.length == colors.length) {
178
//                                        for (int j = 0; isEqual && j < myColors.length; j++) {
179
//                                                Color c1 = myColors[j].getColor();
180
//                                                Color c2 = colors[j].getColor();
181
//                                                isEqual = c1.getRGB() == c2.getRGB() && c1.getAlpha() == c2.getAlpha();
182
//                                        }
183
//                                        if(isEqual) {
184
//                                                setSelectedIndex(i);
185
//                                                repaint();
186
//                                                return;
187
//                                        }
188
//                                }
189
//                        }
190
//                        if(getItemCount()> 0) {
191
//                                setSelectedItem(0);
192
//                        }
193
//                }
194
//
195
//        }
196

    
197
        public void setSelectedColors(Color[] colors) {
198

    
199
                fileList.clear();
200

    
201
                if (colors == null) {
202
                        setSelectedIndex(0);
203
                        return;
204
                }
205
                else {
206

    
207
                        for (int i = 0; i < getItemCount(); i++) {
208
                                fileList.add(getItemAt(i));
209
                        }
210

    
211
                        for (int i = 0; i < fileList.size(); i++) {
212
                                Object o = fileList.get(i);
213
                                Color[] myColors = ((IColorTablePaint) ((ArrayList) o).get(1)).getColors();
214

    
215
                                boolean isEqual = true;
216
                                if(myColors.length == colors.length) {
217
                                        for (int j = 0; isEqual && j < myColors.length; j++) {
218
                                                Color c1 = myColors[j];
219
                                                Color c2 = colors[j];
220
                                                isEqual = c1.getRGB() == c2.getRGB() && c1.getAlpha() == c2.getAlpha();
221
                                        }
222
                                        if(isEqual) {
223
                                                setSelectedIndex(i);
224
                                                repaint();
225
                                                return;
226
                                        }
227
                                }
228
                        }
229
                        if(getItemCount()> 0)
230
                                setSelectedItem(0);
231
                }
232

    
233
        }
234

    
235

    
236
        private class ColorSchemeItemPainter extends JComponent {
237
                private static final long serialVersionUID = -6448740563809113949L;
238
                private boolean isSelected = false;
239
                private IColorTablePaint colorTablePaint = null;
240
                /**
241
                 * Constructor method
242
                 *
243
                 * @param name
244
                 * @param colorTablePaint
245
                 * @param isSelected
246
                 */
247
                public ColorSchemeItemPainter(String name, IColorTablePaint colorTablePaint, boolean isSelected) {
248
                        super();
249
                        this.colorTablePaint = colorTablePaint;
250
                        this.isSelected = isSelected;
251
                        setToolTipText(name);
252
                }
253

    
254
                public void paintComponent(Graphics g) {
255
                        Graphics2D g2 = (Graphics2D) g;
256
                        if (isSelected) {
257
                                Color color1 = new Color(89, 153, 229);
258
                                Color color2 = new Color(31, 92, 207);
259
                                g2.setPaint(new GradientPaint(0, 1, color1, 0, getHeight() - 1, color2, false));
260
                                g2.fillRect(0, 1, getWidth(), getHeight() - 1);
261
                                g2.setColor(new Color(61, 123, 218));
262
                                g2.drawLine(0, 0, getWidth(), 0);
263
                                g2.setColor(Color.white);
264
                        } else {
265
                                g2.setColor(Color.white);
266
                                g2.fillRect(0, 0, getWidth(), getHeight());
267
                                g2.setColor(Color.black);
268
                        }
269

    
270
                        colorTablePaint.paint(g2, isSelected, getBounds());
271
                }
272
        }
273
        
274
}