Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / JComboBoxColorScheme.java @ 40560

History | View | Annotate | Download (5.71 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.gui.styling;
25

    
26
import java.awt.Color;
27
import java.awt.Component;
28
import java.awt.Dimension;
29
import java.awt.GradientPaint;
30
import java.awt.Graphics;
31
import java.awt.Graphics2D;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34
import java.io.File;
35
import java.util.ArrayList;
36
import java.util.List;
37

    
38
import javax.swing.JComboBox;
39
import javax.swing.JComponent;
40
import javax.swing.JList;
41
import javax.swing.ListCellRenderer;
42

    
43
import org.gvsig.gui.ColorTablePainter;
44
import org.gvsig.i18n.Messages;
45
import org.gvsig.symbology.swing.SymbologySwingLocator;
46

    
47

    
48
/**
49
 * Creates a JComboBox where the user has the option to select different
50
 * colors.
51
 *
52
 * @autor jaume dominguez faus - jaume.dominguez@iver.es
53
 */
54
public class JComboBoxColorScheme extends JComboBox {
55
        /**
56
         * 
57
         */
58
        private static final long serialVersionUID = -749998859270723991L;
59
        private String palettesPath = System.getProperty("user.home") +
60
        File.separator +
61
        "gvSIG" +
62
        File.separator +
63
        "ColorSchemes";
64
//        private boolean interpolated = false;
65
        private List<ColorTablePainter> colorTables = new ArrayList<ColorTablePainter>();
66

    
67
        private ActionListener innerActionUpdateTooltip = new ActionListener() {
68
                public void actionPerformed(ActionEvent e) {
69
                        ColorTablePainter colorTable = (ColorTablePainter)getSelectedItem();
70
                        if (colorTable != null) {
71
                                setToolTipText(colorTable.getTableName());
72
                        } else {
73
                                setToolTipText(Messages.getText("select_a_color_scheme"));
74
                        }
75
                }
76
        };
77
        
78
        /**
79
         * Constructor method
80
         *
81
         * @param interpolateValues
82
         */
83
        public JComboBoxColorScheme(boolean interpolateValues) {
84
                super();
85
//                interpolated = interpolateValues;
86
                setPreferredSize(new Dimension(150, 20));
87
                
88
                List<ColorTablePainter> colorTables = SymbologySwingLocator.getSwingManager().createColorTables();
89
                if(colorTables != null){
90
                        for (int i=0; i<colorTables.size(); i++) {
91
                                ColorTablePainter colorTable = colorTables.get(i);
92
//                                ArrayList array = new ArrayList();
93
//                                array.add(colorTable.getTableName());
94
//                                array.add(colorTable);
95
//                                addItem(array);
96
                                addItem(colorTable);
97
                        }
98
                        addActionListener(innerActionUpdateTooltip);
99
                        setRenderer(new ListCellRenderer() {
100
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
101
                                        ColorTablePainter colorTable = (ColorTablePainter) value;
102
                                        ColorSchemeItemPainter paintItem = new ColorSchemeItemPainter(colorTable.getTableName(), colorTable, isSelected);
103
                                        paintItem.setPreferredSize(getPreferredSize());
104
                                        return paintItem;
105
                                }
106
                        });
107
                }
108

    
109
        }
110

    
111
        /**
112
         * Returns an array composed with the selected colors
113
         * @return
114
         */
115
        public Color[] getSelectedColors() {
116
                ColorTablePainter colorTable = (ColorTablePainter) getSelectedItem();
117
                if (colorTable == null) {
118
                        return null;
119
                }
120
                return colorTable.getColors();
121
        }
122

    
123
        public void setSelectedColors(Color[] colors) {
124

    
125
                colorTables.clear();
126
                if (colors == null) {
127
                        setSelectedIndex(0);
128
                        return;
129
                } else {
130
                        for (int i = 0; i < getItemCount(); i++) {
131
                                colorTables.add((ColorTablePainter) getItemAt(i));
132
                        }
133

    
134
                        for (int i = 0; i < colorTables.size(); i++) {
135
                                ColorTablePainter colorTable = colorTables.get(i);
136
                                Color[] myColors = colorTable.getColors();
137

    
138
                                boolean isEqual = true;
139
                                if(myColors.length == colors.length) {
140
                                        for (int j = 0; isEqual && j < myColors.length; j++) {
141
                                                Color c1 = myColors[j];
142
                                                Color c2 = colors[j];
143
                                                isEqual = c1.getRGB() == c2.getRGB() && c1.getAlpha() == c2.getAlpha();
144
                                        }
145
                                        if(isEqual) {
146
                                                setSelectedIndex(i);
147
                                                repaint();
148
                                                return;
149
                                        }
150
                                }
151
                        }
152
                        if(getItemCount()> 0) {
153
                                setSelectedItem(0);
154
                        }
155
                }
156

    
157
        }
158

    
159

    
160
        private class ColorSchemeItemPainter extends JComponent {
161
                private static final long serialVersionUID = -6448740563809113949L;
162
                private boolean isSelected = false;
163
                private ColorTablePainter colorTablePaint = null;
164
                /**
165
                 * Constructor method
166
                 *
167
                 * @param name
168
                 * @param colorTablePaint
169
                 * @param isSelected
170
                 */
171
                public ColorSchemeItemPainter(String name, ColorTablePainter colorTablePaint, boolean isSelected) {
172
                        super();
173
                        this.colorTablePaint = colorTablePaint;
174
                        this.isSelected = isSelected;
175
                        setToolTipText(name);
176
                }
177

    
178
                public void paintComponent(Graphics g) {
179
                        Graphics2D g2 = (Graphics2D) g;
180
                        if (isSelected) {
181
                                Color color1 = new Color(89, 153, 229);
182
                                Color color2 = new Color(31, 92, 207);
183
                                g2.setPaint(new GradientPaint(0, 1, color1, 0, getHeight() - 1, color2, false));
184
                                g2.fillRect(0, 1, getWidth(), getHeight() - 1);
185
                                g2.setColor(new Color(61, 123, 218));
186
                                g2.drawLine(0, 0, getWidth(), 0);
187
                                g2.setColor(Color.white);
188
                        } else {
189
                                g2.setColor(Color.white);
190
                                g2.fillRect(0, 0, getWidth(), getHeight());
191
                                g2.setColor(Color.black);
192
                        }
193

    
194
                        colorTablePaint.paint(g2, isSelected);
195
                }
196
        }
197
        
198
}