Skip to main content
summaryrefslogtreecommitdiffstats
blob: 897a399ea0c97d92248a7f09aa48f65e0298d3ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 2007 Oracle Corporation., and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Yury Kats/Oracle - initial API and implementation
 *    
 ********************************************************************************/
package org.eclipse.jst.jsf.taglibprocessing.attributevalues;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jst.jsf.metadataprocessors.features.IPossibleValues;
import org.eclipse.jst.jsf.metadataprocessors.features.PossibleValue;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;

/**
 * Meta-data processing type representing a color.
 * A color is defined as in html spec http://www.w3.org/TR/html4/types.html#type-color
 * 
 * <p><b>Provisional API - subject to change</b></p>
 * 
 * @author ykats
 */

public class ColorType extends EnumerationType implements IPossibleValues {
	
	/**
	 * List of standard colors
	 * See http://www.w3.org/TR/html4/types.html#type-color
	 */
	private final static String[] COLORS = {"Black", "Silver", "Gray", "White", "Maroon", "Red", "Purple", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
				"Fuchsia", "Green", "Lime", "Olive", "Yellow", "Navy", "Blue", "Teal","Aqua"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
		//if ordering changes, must change RGB[] ordering

	private RGB[] _rgb;

	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.taglibprocessing.attributevalues.EnumerationType#getReturnType()
	 */
	protected String getReturnType(){ return "java.lang.String";} //$NON-NLS-1$

	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.metadataprocessors.features.IValidValues#isValidValue(java.lang.String)
	 */
	public boolean isValidValue(String value) {	
		boolean bValid = true;
		String trimmedVal = value.trim();
		if (trimmedVal.length() == 0)
			bValid = false;
		else if (trimmedVal.charAt(0) == '#' && trimmedVal.length() == 7) {
			for (int i=1; i<7; i++) {
				if (trimmedVal.charAt(i) < '0' ||
					(trimmedVal.charAt(i) > '9' && trimmedVal.charAt(i) < 'A') ||
					(trimmedVal.charAt(i) > 'F' && trimmedVal.charAt(i) < 'a') ||
					trimmedVal.charAt(i) > 'f') {
					bValid = false;
					break;
				}
			}
		}
		else {
			bValid = false;
			for (int i = 0; i < COLORS.length; i++) {
				if(trimmedVal.equalsIgnoreCase(COLORS[i])) {
					bValid = true;
					break;
				}
			}
		}
		
		if(!bValid) {
			addNewValidationMessage(Messages.ColorType_invalid_color);
		}
		
		return getValidationMessages().isEmpty();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.metadataprocessors.features.IPossibleValues#getPossibleValues()
	 */
	public List getPossibleValues() {
		List ret = new ArrayList(COLORS.length);
		for (int i=0;i < COLORS.length;i++){
			PossibleValue pv = new PossibleValue(COLORS[i]);
			pv.setIcon(createImage(i));
			ret.add(pv);
		}
		return ret;
	}

	private ImageDescriptor createImage(int color) {
		PaletteData pd = getPaletteData(color);
		return ImageDescriptor.createFromImageData(new ImageData(16, 16, 1, pd));
	}

	private PaletteData getPaletteData(int color){
		if (_rgb == null){
			_rgb = new RGB[16];
			_rgb[0] = new RGB(0,0,0);//black
			_rgb[1] = new RGB(192,192,192);//silver
			_rgb[2] = new RGB(128,128,128);//gray
			_rgb[3] = new RGB(255,255,255);//white
			_rgb[4] = new RGB(128,0,0);//Maroon
			_rgb[5] = new RGB(255,0,0);//Red
			_rgb[6] = new RGB(128,0,128);//Purple
			_rgb[7] = new RGB(255,0,255);//Fuchsia
			_rgb[8] = new RGB(0,128,0);//Green
			_rgb[9] = new RGB(0,255,0);//Lime
			_rgb[10] = new RGB(128,128,0);//Olive
			_rgb[11] = new RGB(255,255,0);//Yellow
			_rgb[12] = new RGB(0,0,128);//Navy
			_rgb[13] = new RGB(0,0,255);//Blue
			_rgb[14] = new RGB(0,128,128);//Teal
			_rgb[15] = new RGB(0,255,255);//Aqua
		}
		RGB[] rgbColor = new RGB[1];
		System.arraycopy(_rgb, color, rgbColor, 0, 1);
		return new PaletteData(rgbColor);
	}
}

Back to the top