Revision 43476 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

View differences:

JComboBoxColorScheme.java
24 24
package org.gvsig.app.gui.styling;
25 25

  
26 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 27

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

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

  
47

  
48 31
/**
49 32
 * Creates a JComboBox where the user has the option to select different
50 33
 * colors.
51
 *
52
 * @autor jaume dominguez faus - jaume.dominguez@iver.es
53 34
 */
54 35
public class JComboBoxColorScheme extends JComboBox {
55 36
	/**
56 37
	 * 
57 38
	 */
58 39
	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>();
40
    protected final ColorSchemeSelector colorSchemaSelector;
66 41

  
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
		}
42
    public JComboBoxColorScheme(boolean interpolateValues) {
43
        super();
44
        this.colorSchemaSelector = new ColorSchemeSelector(this, interpolateValues);
45
    }
108 46

  
109
	}
110

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

  
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
	}
51
    public void setSelectedColors(Color[] colors) {
52
        this.colorSchemaSelector.setSelectedColors(colors);
53
    }
197 54
	
198 55
}

Also available in: Unified diff