Statistics
| Revision:

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

History | View | Annotate | Download (5.96 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.ColorTablePainter;
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 List<ColorTablePainter> colorTables = new ArrayList<ColorTablePainter>();
83

    
84
        private ActionListener innerActionUpdateTooltip = new ActionListener() {
85
                public void actionPerformed(ActionEvent e) {
86
                        ColorTablePainter colorTable = (ColorTablePainter)getSelectedItem();
87
                        if (colorTable != null) {
88
                                setToolTipText(colorTable.getTableName());
89
                        } else {
90
                                setToolTipText(Messages.getText("select_a_color_scheme"));
91
                        }
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
                List<ColorTablePainter> colorTables = SymbologySwingLocator.getSwingManager().createColorTables();
106
                if(colorTables != null){
107
                        for (int i=0; i<colorTables.size(); i++) {
108
                                ColorTablePainter colorTable = colorTables.get(i);
109
//                                ArrayList array = new ArrayList();
110
//                                array.add(colorTable.getTableName());
111
//                                array.add(colorTable);
112
//                                addItem(array);
113
                                addItem(colorTable);
114
                        }
115
                        addActionListener(innerActionUpdateTooltip);
116
                        setRenderer(new ListCellRenderer() {
117
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
118
                                        ColorTablePainter colorTable = (ColorTablePainter) value;
119
                                        ColorSchemeItemPainter paintItem = new ColorSchemeItemPainter(colorTable.getTableName(), colorTable, isSelected);
120
                                        paintItem.setPreferredSize(getPreferredSize());
121
                                        return paintItem;
122
                                }
123
                        });
124
                }
125

    
126
        }
127

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

    
140
        public void setSelectedColors(Color[] colors) {
141

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

    
151
                        for (int i = 0; i < colorTables.size(); i++) {
152
                                ColorTablePainter colorTable = colorTables.get(i);
153
                                Color[] myColors = colorTable.getColors();
154

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

    
174
        }
175

    
176

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

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

    
211
                        colorTablePaint.paint(g2, isSelected);
212
                }
213
        }
214
        
215
}