Statistics
| Revision:

root / tags / v2_0_0_Build_2031 / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / JComboBoxColorScheme.java @ 36104

History | View | Annotate | Download (6.05 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
import java.util.List;
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.gui.IColorTables;
61
import org.gvsig.gui.IColorTables.IColorTablePaint;
62
import org.gvsig.i18n.Messages;
63
import org.gvsig.symbology.swing.SymbologySwingLocator;
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
        /**
74
         * 
75
         */
76
        private static final long serialVersionUID = -749998859270723991L;
77
        private String palettesPath = System.getProperty("user.home") +
78
        File.separator +
79
        "gvSIG" +
80
        File.separator +
81
        "ColorSchemes";
82
//        private boolean interpolated = false;
83
        private List<IColorTablePaint> colorTables = new ArrayList<IColorTablePaint>();
84

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

    
128
        }
129

    
130
        /**
131
         * Returns an array composed with the selected colors
132
         * @return
133
         */
134
        public Color[] getSelectedColors() {
135
                IColorTablePaint colorTable = (IColorTablePaint) getSelectedItem();
136
                if (colorTable == null) {
137
                        return null;
138
                }
139
                return colorTable.getColors();
140
        }
141

    
142
        public void setSelectedColors(Color[] colors) {
143

    
144
                colorTables.clear();
145
                if (colors == null) {
146
                        setSelectedIndex(0);
147
                        return;
148
                } else {
149
                        for (int i = 0; i < getItemCount(); i++) {
150
                                colorTables.add((IColorTablePaint) getItemAt(i));
151
                        }
152

    
153
                        for (int i = 0; i < colorTables.size(); i++) {
154
                                IColorTablePaint colorTable = colorTables.get(i);
155
                                Color[] myColors = colorTable.getColors();
156

    
157
                                boolean isEqual = true;
158
                                if(myColors.length == colors.length) {
159
                                        for (int j = 0; isEqual && j < myColors.length; j++) {
160
                                                Color c1 = myColors[j];
161
                                                Color c2 = colors[j];
162
                                                isEqual = c1.getRGB() == c2.getRGB() && c1.getAlpha() == c2.getAlpha();
163
                                        }
164
                                        if(isEqual) {
165
                                                setSelectedIndex(i);
166
                                                repaint();
167
                                                return;
168
                                        }
169
                                }
170
                        }
171
                        if(getItemCount()> 0) {
172
                                setSelectedItem(0);
173
                        }
174
                }
175

    
176
        }
177

    
178

    
179
        private class ColorSchemeItemPainter extends JComponent {
180
                private static final long serialVersionUID = -6448740563809113949L;
181
                private boolean isSelected = false;
182
                private IColorTablePaint colorTablePaint = null;
183
                /**
184
                 * Constructor method
185
                 *
186
                 * @param name
187
                 * @param colorTablePaint
188
                 * @param isSelected
189
                 */
190
                public ColorSchemeItemPainter(String name, IColorTablePaint colorTablePaint, boolean isSelected) {
191
                        super();
192
                        this.colorTablePaint = colorTablePaint;
193
                        this.isSelected = isSelected;
194
                        setToolTipText(name);
195
                }
196

    
197
                public void paintComponent(Graphics g) {
198
                        Graphics2D g2 = (Graphics2D) g;
199
                        if (isSelected) {
200
                                Color color1 = new Color(89, 153, 229);
201
                                Color color2 = new Color(31, 92, 207);
202
                                g2.setPaint(new GradientPaint(0, 1, color1, 0, getHeight() - 1, color2, false));
203
                                g2.fillRect(0, 1, getWidth(), getHeight() - 1);
204
                                g2.setColor(new Color(61, 123, 218));
205
                                g2.drawLine(0, 0, getWidth(), 0);
206
                                g2.setColor(Color.white);
207
                        } else {
208
                                g2.setColor(Color.white);
209
                                g2.fillRect(0, 0, getWidth(), getHeight());
210
                                g2.setColor(Color.black);
211
                        }
212

    
213
                        colorTablePaint.paint(g2, isSelected, getBounds());
214
                }
215
        }
216
        
217
}