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 @ 38371
History | View | Annotate | Download (5.96 KB)
1 | 34294 | fdiaz | /* 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 | 35976 | jjdelcerro | import java.util.List; |
54 | 34294 | fdiaz | |
55 | import javax.swing.JComboBox; |
||
56 | import javax.swing.JComponent; |
||
57 | import javax.swing.JList; |
||
58 | import javax.swing.ListCellRenderer; |
||
59 | |||
60 | 38371 | nbrodin | import org.gvsig.gui.ColorTablePainter; |
61 | 34294 | fdiaz | 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 | 35976 | jjdelcerro | // private boolean interpolated = false;
|
82 | 38371 | nbrodin | private List<ColorTablePainter> colorTables = new ArrayList<ColorTablePainter>(); |
83 | 34294 | fdiaz | |
84 | private ActionListener innerActionUpdateTooltip = new ActionListener() { |
||
85 | public void actionPerformed(ActionEvent e) { |
||
86 | 38371 | nbrodin | ColorTablePainter colorTable = (ColorTablePainter)getSelectedItem(); |
87 | 35976 | jjdelcerro | if (colorTable != null) { |
88 | setToolTipText(colorTable.getTableName()); |
||
89 | 34294 | fdiaz | } else {
|
90 | setToolTipText(Messages.getText("select_a_color_scheme"));
|
||
91 | } |
||
92 | } |
||
93 | }; |
||
94 | 35976 | jjdelcerro | |
95 | 34294 | fdiaz | /**
|
96 | * Constructor method
|
||
97 | *
|
||
98 | * @param interpolateValues
|
||
99 | */
|
||
100 | public JComboBoxColorScheme(boolean interpolateValues) { |
||
101 | super();
|
||
102 | 35976 | jjdelcerro | // interpolated = interpolateValues;
|
103 | 34294 | fdiaz | setPreferredSize(new Dimension(150, 20)); |
104 | |||
105 | 38371 | nbrodin | List<ColorTablePainter> colorTables = SymbologySwingLocator.getSwingManager().createColorTables();
|
106 | 34294 | fdiaz | if(colorTables != null){ |
107 | 34631 | fdiaz | for (int i=0; i<colorTables.size(); i++) { |
108 | 38371 | nbrodin | ColorTablePainter colorTable = colorTables.get(i); |
109 | 35976 | jjdelcerro | // ArrayList array = new ArrayList();
|
110 | // array.add(colorTable.getTableName());
|
||
111 | // array.add(colorTable);
|
||
112 | // addItem(array);
|
||
113 | addItem(colorTable); |
||
114 | 34294 | fdiaz | } |
115 | 35976 | jjdelcerro | addActionListener(innerActionUpdateTooltip); |
116 | setRenderer(new ListCellRenderer() { |
||
117 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
||
118 | 38371 | nbrodin | ColorTablePainter colorTable = (ColorTablePainter) value; |
119 | 35976 | jjdelcerro | ColorSchemeItemPainter paintItem = new ColorSchemeItemPainter(colorTable.getTableName(), colorTable, isSelected);
|
120 | paintItem.setPreferredSize(getPreferredSize()); |
||
121 | return paintItem;
|
||
122 | } |
||
123 | }); |
||
124 | 34294 | fdiaz | } |
125 | |||
126 | } |
||
127 | |||
128 | /**
|
||
129 | * Returns an array composed with the selected colors
|
||
130 | * @return
|
||
131 | */
|
||
132 | public Color[] getSelectedColors() { |
||
133 | 38371 | nbrodin | ColorTablePainter colorTable = (ColorTablePainter) getSelectedItem(); |
134 | 35976 | jjdelcerro | if (colorTable == null) { |
135 | 34294 | fdiaz | return null; |
136 | } |
||
137 | 35976 | jjdelcerro | return colorTable.getColors();
|
138 | 34294 | fdiaz | } |
139 | |||
140 | public void setSelectedColors(Color[] colors) { |
||
141 | |||
142 | 35976 | jjdelcerro | colorTables.clear(); |
143 | 34294 | fdiaz | if (colors == null) { |
144 | setSelectedIndex(0);
|
||
145 | return;
|
||
146 | 35976 | jjdelcerro | } else {
|
147 | 34294 | fdiaz | for (int i = 0; i < getItemCount(); i++) { |
148 | 38371 | nbrodin | colorTables.add((ColorTablePainter) getItemAt(i)); |
149 | 34294 | fdiaz | } |
150 | |||
151 | 35976 | jjdelcerro | for (int i = 0; i < colorTables.size(); i++) { |
152 | 38371 | nbrodin | ColorTablePainter colorTable = colorTables.get(i); |
153 | 35976 | jjdelcerro | Color[] myColors = colorTable.getColors(); |
154 | 34294 | fdiaz | |
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 | 35976 | jjdelcerro | if(getItemCount()> 0) { |
170 | 34294 | fdiaz | setSelectedItem(0);
|
171 | 35976 | jjdelcerro | } |
172 | 34294 | fdiaz | } |
173 | |||
174 | } |
||
175 | |||
176 | |||
177 | private class ColorSchemeItemPainter extends JComponent { |
||
178 | private static final long serialVersionUID = -6448740563809113949L; |
||
179 | private boolean isSelected = false; |
||
180 | 38371 | nbrodin | private ColorTablePainter colorTablePaint = null; |
181 | 34294 | fdiaz | /**
|
182 | * Constructor method
|
||
183 | *
|
||
184 | * @param name
|
||
185 | * @param colorTablePaint
|
||
186 | * @param isSelected
|
||
187 | */
|
||
188 | 38371 | nbrodin | public ColorSchemeItemPainter(String name, ColorTablePainter colorTablePaint, boolean isSelected) { |
189 | 34294 | fdiaz | 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 | 38371 | nbrodin | colorTablePaint.paint(g2, isSelected); |
212 | 34294 | fdiaz | } |
213 | } |
||
214 | |||
215 | } |