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 / ColorSchemeSelector.java @ 43510

History | View | Annotate | Download (6.82 KB)

1 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40560 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40560 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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 40560 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20 40435 jjdelcerro
 *
21 40560 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
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.util.ArrayList;
35
import java.util.List;
36
37
import javax.swing.JComboBox;
38
import javax.swing.JComponent;
39
import javax.swing.JList;
40
import javax.swing.ListCellRenderer;
41
42
import org.gvsig.gui.ColorTablePainter;
43
import org.gvsig.i18n.Messages;
44
import org.gvsig.symbology.swing.SymbologySwingLocator;
45
46 43476 jjdelcerro
public class ColorSchemeSelector {
47 40435 jjdelcerro
48 43476 jjdelcerro
    private List<ColorTablePainter> colorTables = new ArrayList<>();
49 40435 jjdelcerro
50 43476 jjdelcerro
    private final JComboBox wrappedComboBox;
51 40435 jjdelcerro
52 43476 jjdelcerro
    /**
53
     * Constructor method
54
     *
55
     * @param wrappedComboBox
56
     * @param interpolateValues
57
     */
58
    public ColorSchemeSelector(final JComboBox wrappedComboBox, boolean interpolateValues) {
59
        this.wrappedComboBox = wrappedComboBox;
60
        this.wrappedComboBox.removeAllItems();
61
        this.wrappedComboBox.setPreferredSize(new Dimension(150, 20));
62
63
        // Remove listeners to prevent strange behavior when you call this class
64
        // repeatedly with the same combo.
65
        for( ActionListener l : this.wrappedComboBox.getActionListeners()) {
66
            this.wrappedComboBox.removeActionListener(l);
67
        }
68
        this.colorTables = SymbologySwingLocator.getSwingManager().createColorTables();
69
        if( colorTables != null ) {
70
            for( int i = 0; i < colorTables.size(); i++ ) {
71
                ColorTablePainter colorTable = colorTables.get(i);
72
                wrappedComboBox.addItem(colorTable);
73
            }
74
            this.wrappedComboBox.addActionListener(new ActionListener() {
75
                @Override
76
                public void actionPerformed(ActionEvent e) {
77
                    doUpdateToolTip();
78
                }
79
            });
80
            this.wrappedComboBox.setRenderer(new ListCellRenderer() {
81
                @Override
82
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
83
                    ColorTablePainter colorTable = (ColorTablePainter) value;
84
                    ColorSchemeItemPainter paintItem = new ColorSchemeItemPainter(colorTable.getTableName(), colorTable, isSelected);
85
                    paintItem.setPreferredSize(wrappedComboBox.getPreferredSize());
86
                    return paintItem;
87
                }
88
            });
89
        }
90 40435 jjdelcerro
91 43476 jjdelcerro
    }
92 40435 jjdelcerro
93 43476 jjdelcerro
    private void doUpdateToolTip() {
94
        ColorTablePainter colorTable = (ColorTablePainter) this.wrappedComboBox.getSelectedItem();
95
        if( colorTable != null ) {
96
            this.wrappedComboBox.setToolTipText(colorTable.getTableName());
97
        } else {
98
            this.wrappedComboBox.setToolTipText(Messages.getText("select_a_color_scheme"));
99
        }
100
    }
101 40435 jjdelcerro
102 43476 jjdelcerro
    /**
103
     * Returns an array composed with the selected colors
104
     *
105
     * @return
106
     */
107
    public Color[] getSelectedColors() {
108
        ColorTablePainter colorTable = (ColorTablePainter) this.wrappedComboBox.getSelectedItem();
109
        if( colorTable == null ) {
110
            return null;
111
        }
112
        return colorTable.getColors();
113
    }
114 40435 jjdelcerro
115 43476 jjdelcerro
    public void setSelectedColors(Color[] colors) {
116 40435 jjdelcerro
117 43476 jjdelcerro
        if( colors == null ) {
118
            this.wrappedComboBox.setSelectedIndex(0);
119
            return;
120
        }
121
        colorTables.clear();
122
        for( int i = 0; i < this.wrappedComboBox.getItemCount(); i++ ) {
123
            colorTables.add((ColorTablePainter) this.wrappedComboBox.getItemAt(i));
124
        }
125 40435 jjdelcerro
126 43476 jjdelcerro
        int n = 0;
127
        int colorsCode = getCode(colors);
128
        for( ColorTablePainter colorTable : colorTables ) {
129
            Color[] curcolors = colorTable.getColors();
130
            if( getCode(curcolors) == colorsCode ) {
131
                this.wrappedComboBox.setSelectedIndex(n);
132
                this.wrappedComboBox.repaint();
133
                return;
134
            }
135
            n++;
136
        }
137
        if( this.wrappedComboBox.getItemCount() > 0 ) {
138
            this.wrappedComboBox.setSelectedItem(0);
139
        }
140 40435 jjdelcerro
141 43476 jjdelcerro
    }
142
143
    private int getCode(Color[] table) {
144
        StringBuilder builder = new StringBuilder();
145
        for( Color color : table ) {
146
            builder.append(color.getRed());
147
            builder.append(color.getGreen());
148
            builder.append(color.getBlue());
149
            builder.append(color.getAlpha());
150
        }
151
        return builder.toString().hashCode();
152
    }
153 40435 jjdelcerro
154 43476 jjdelcerro
    private class ColorSchemeItemPainter extends JComponent {
155 40435 jjdelcerro
156 43476 jjdelcerro
        private static final long serialVersionUID = -6448740563809113949L;
157
        private boolean isSelected = false;
158
        private ColorTablePainter colorTablePaint = null;
159 40435 jjdelcerro
160 43476 jjdelcerro
        /**
161
         * Constructor method
162
         *
163
         * @param name
164
         * @param colorTablePaint
165
         * @param isSelected
166
         */
167
        public ColorSchemeItemPainter(String name, ColorTablePainter colorTablePaint, boolean isSelected) {
168
            super();
169
            this.colorTablePaint = colorTablePaint;
170
            this.isSelected = isSelected;
171
            setToolTipText(name);
172
        }
173
174
        @Override
175
        public void paintComponent(Graphics g) {
176
            Graphics2D g2 = (Graphics2D) g;
177
            if( isSelected ) {
178
                Color color1 = new Color(89, 153, 229);
179
                Color color2 = new Color(31, 92, 207);
180
                g2.setPaint(new GradientPaint(0, 1, color1, 0, getHeight() - 1, color2, false));
181
                g2.fillRect(0, 1, getWidth(), getHeight() - 1);
182
                g2.setColor(new Color(61, 123, 218));
183
                g2.drawLine(0, 0, getWidth(), 0);
184
                g2.setColor(Color.white);
185
            } else {
186
                g2.setColor(Color.white);
187
                g2.fillRect(0, 0, getWidth(), getHeight());
188
                g2.setColor(Color.black);
189
            }
190
191
            colorTablePaint.paint(g2, isSelected);
192
        }
193
    }
194
195 40435 jjdelcerro
}