001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020:
021: package com.salmonllc.html;
022:
023: import java.io.PrintWriter;
024: import java.util.Hashtable;
025: import java.util.StringTokenizer;
026:
027: import com.salmonllc.html.events.ValueChangedEvent;
028: import com.salmonllc.properties.Props;
029: import com.salmonllc.util.MessageLog;
030:
031: /**
032: * Displays a box that allows the user to select a color
033: */
034: public class HtmlColorChooser extends HtmlFormComponent {
035:
036: private int _noRows = 3;
037: private String _font;
038: private String _fontStartTag, _fontEndTag;
039: private String _theme;
040:
041: /**
042: * Constructor for HtmlColorChooser.
043: * @param name
044: * @param p
045: */
046: public HtmlColorChooser(String name, HtmlPage p) {
047: super (name, p);
048: }
049:
050: /**
051: * Constructor for HtmlColorChooser.
052: * @param name
053: * @param theme
054: * @param p
055: */
056: public HtmlColorChooser(String name, String theme, HtmlPage p) {
057: super (name, theme, p);
058: }
059:
060: /**
061: * @see com.salmonllc.html.HtmlComponent#generateHTML(PrintWriter, int)
062: */
063: public void generateHTML(PrintWriter p, int rowNo) throws Exception {
064: if (!_visible)
065: return;
066: boolean checked = false;
067: _value = getValue(rowNo, true);
068: StringBuffer sb = new StringBuffer();
069: sb.append("<table border=\"0\">");
070: int colsPerRow = COLORS.length / _noRows;
071: for (int i = 0; i < _noRows; i++) {
072: int rowStart = i * (colsPerRow);
073: int rowEnd = rowStart + colsPerRow;
074: sb.append("<tr height=\"10\">");
075: for (int j = rowStart; j < rowEnd; j++) {
076: sb.append("<td width=\"10\" bgcolor=\"");
077: sb.append(COLORS[j]);
078: sb.append("\"></td>");
079: }
080: sb.append("</tr><tr>");
081: for (int j = rowStart; j < rowEnd; j++) {
082: sb.append("<td><input type=\"radio\" name=\"");
083: sb.append(getFullName());
084: sb.append("\" value=\"");
085: sb.append(COLORS[j]);
086: sb.append("\"");
087: if (COLORS[j].equals(_value)) {
088: sb.append(" CHECKED");
089: checked = true;
090: }
091: sb.append("></td>");
092: }
093: sb.append("</tr>");
094: }
095: sb.append("<tr><td colspan=\"");
096: sb.append(colsPerRow);
097: sb.append("\">");
098:
099: sb.append("<table border=\"0\"><tr><td>");
100: sb.append(_fontStartTag);
101: sb.append("Custom Color:");
102: sb.append(_fontEndTag);
103: sb.append("</td>");
104: sb.append("<td><input type=\"text\" name=\"");
105: sb.append(getFullName() + "custom");
106: sb.append("\" value=\"");
107: if (_value != null && !checked)
108: sb.append(_value);
109: sb.append("\"");
110: sb.append(" onchange=\"");
111: sb.append(getFullName());
112: sb.append("deselect();\"");
113: sb.append("></td>");
114: sb.append("<script language=\"javascript\">");
115: sb.append("function ");
116: sb.append(getFullName());
117: String form = getFormString();
118: sb.append("deselect() {");
119: sb.append("ele=");
120: sb.append(form);
121: sb.append("elements;");
122: sb.append("for (i=0; i < ele.length; i++)");
123: sb.append(" if (ele[i].name == '");
124: sb.append(getFullName());
125: sb.append("') ");
126: sb.append(" ele[i].checked=false;");
127: sb.append("}");
128: sb.append("</script>");
129: if (!checked && _value != null) {
130: sb.append("<td height=\"10\" width=\"10\" bgcolor=\"");
131: sb.append(_value);
132: sb.append("\"></td>");
133: }
134:
135: sb.append("</tr></table></td></tr></table>");
136: p.println(sb.toString());
137: }
138:
139: public boolean processParms(Hashtable parms, int rowNo)
140: throws Exception {
141: if (!getEnabled())
142: return false;
143: Object oldValue = _value;
144:
145: String name = getFullName();
146: if (rowNo > -1) {
147: name += "_" + rowNo;
148: if (_dsBuff != null)
149: oldValue = _dsBuff.getAny(rowNo, _dsColNo);
150: } else {
151: if (_dsBuff != null)
152: oldValue = _dsBuff.getAny(_dsColNo);
153: }
154:
155: String val[] = (String[]) parms.get(name);
156:
157: if (val == null) {
158: val = (String[]) parms.get(name + "custom");
159: if (val == null)
160: _value = null;
161: else
162: _value = val[0];
163: } else
164: _value = val[0];
165:
166: if (!valuesEqual(oldValue, _value) && _value != null) {
167: String s = null;
168: if (oldValue != null)
169: s = oldValue.toString();
170: ValueChangedEvent e = new ValueChangedEvent(getPage(),
171: this , getName(), getFullName(), s,
172: translateColorValue(_value), rowNo, _dsColNo,
173: _dsBuff);
174: addEvent(e);
175: }
176:
177: return false;
178: }
179:
180: /**
181: * This method will load the font start and end tags from the page properties object.See the Constants at the top of the class for valid values to pass to this method.
182: */
183: public void setFont(String font) {
184: _font = font;
185: setTheme(_theme);
186: }
187:
188: /**
189: * This method sets the end font tag for the component.
190: */
191: public void setFontEndTag(String value) {
192: _fontEndTag = value;
193: }
194:
195: /**
196: * This method sets the start font tag for the component.
197: */
198: public void setFontStartTag(String value) {
199: _fontStartTag = value;
200: }
201:
202: /**
203: * This method sets the property theme for the component.
204: * @param theme The theme to use.
205: */
206: public void setTheme(String theme) {
207: Props props = getPage().getPageProperties();
208:
209: if (_font != null) {
210: _fontStartTag = props.getThemeProperty(theme, _font
211: + Props.TAG_START);
212: _fontEndTag = props.getThemeProperty(theme, _font
213: + Props.TAG_END);
214: } else {
215: _fontStartTag = props.getThemeProperty(theme,
216: HtmlText.FONT_DEFAULT + Props.TAG_START);
217: _fontEndTag = props.getThemeProperty(theme,
218: HtmlText.FONT_DEFAULT + Props.TAG_END);
219: }
220:
221: _theme = theme;
222: }
223:
224: /**
225: * This method returns the property theme for the component.
226: * @return
227: */
228: public String getTheme() {
229: return _theme;
230: }
231:
232: /**
233: * Returns the value in the component in the form red,green,blue (all decimal);
234: */
235: public String getValueAsRGB() {
236: String val = getValue();
237: if (val == null)
238: return null;
239: String colVal = (String) COLOR_HASH.get(val.toLowerCase());
240: if (colVal != null)
241: val = colVal;
242: if (val.startsWith("#"))
243: val = val.substring(1);
244: if (val.length() != 6)
245: return null;
246: String red = val.substring(0, 2);
247: String green = val.substring(2, 4);
248: String blue = val.substring(4, 6);
249:
250: try {
251: val = "";
252: val += Integer.parseInt(red, 16) + ",";
253: val += Integer.parseInt(green, 16) + ",";
254: val += Integer.parseInt(blue, 16);
255: } catch (Exception e) {
256: return null;
257: }
258: return val;
259: }
260:
261: /**
262: * gets the value as an HTML String
263: */
264: public String getValueAsHtml() {
265: String val = getValue();
266: if (val == null)
267: return val;
268: if (COLOR_HASH.containsKey(val.toLowerCase()))
269: return val;
270: if (val.startsWith("#"))
271: val = val.substring(1);
272: if (val.length() != 6)
273: return null;
274: String red = val.substring(0, 2);
275: String green = val.substring(2, 4);
276: String blue = val.substring(4, 6);
277: try {
278: Integer.parseInt(red, 16);
279: Integer.parseInt(green, 16);
280: Integer.parseInt(blue, 16);
281: return "#" + val;
282: } catch (Exception e) {
283: return null;
284: }
285:
286: }
287:
288: /**
289: * Takes a value in the form red,green, blue and converts it into HTML style values
290: */
291: public void setRGBValue(String value) {
292: try {
293: StringTokenizer tok = new StringTokenizer(value, ",");
294: String red = Integer.toHexString(Integer.parseInt(tok
295: .nextToken().trim()));
296: if (red.length() == 1)
297: red = "0" + red;
298: String green = Integer.toHexString(Integer.parseInt(tok
299: .nextToken().trim()));
300: if (green.length() == 1)
301: green = "0" + green;
302: String blue = Integer.toHexString(Integer.parseInt(tok
303: .nextToken().trim()));
304: if (blue.length() == 1)
305: blue = "0" + blue;
306: setValue(translateColorValue((red + green + blue)
307: .toLowerCase()));
308: } catch (Exception ex) {
309: MessageLog.writeErrorMessage("setRGBValue()", ex, this );
310: }
311: }
312:
313: /**
314: * Takes the color from a Font tag and sets it to the default for this component
315: */
316: public void setFontValue(String font) {
317: if (font == null) {
318: setValue(null);
319: return;
320: }
321: String val = HtmlFontChooser.getTagAttribute(font, "color");
322: if (val == null) {
323: setValue(null);
324: return;
325: }
326:
327: if (val.startsWith("#"))
328: val = val.substring(1);
329:
330: setValue(translateColorValue(val.toLowerCase()));
331: }
332:
333: /**
334: * Takes the color from a CSS style and sets it to the default for this component
335: */
336: public void setStyleValue(String style) {
337: if (style == null) {
338: setValue(null);
339: return;
340: }
341: String val = HtmlFontChooser.getStyleAttribute(style, "color");
342: if (val == null) {
343: setValue(null);
344: return;
345: }
346:
347: if (val.startsWith("#"))
348: val = val.substring(1);
349:
350: setValue(translateColorValue(val.toLowerCase()));
351: }
352:
353: /**
354: * Sets the value from an HMTL color string
355: */
356: public void setColorValue(String color) {
357: if (color == null) {
358: setValue(null);
359: return;
360: }
361:
362: if (color.startsWith("#"))
363: color = color.substring(1);
364:
365: setValue(translateColorValue(color.toLowerCase()));
366: }
367:
368: private String translateColorValue(String val) {
369: String colVal = (String) COLOR_HASH.get(val);
370: if (colVal != null) {
371: for (int i = 0; i < COLORS.length; i++)
372: if (colVal.equals(COLORS[i])) {
373: val = COLORS[i];
374: break;
375: }
376: }
377: return val;
378: }
379:
380: private static final String COLORS[] = { "ffcfce", "ff6666",
381: "ff0000", "cc3333", "990000", "660033", "ffffce", "ffff9c",
382: "ffff00", "cccc66", "999933", "666633", "ceffff", "9ccfff",
383: "66cccc", "3399cc", "0000ff", "003399", "ffccff", "ff9aff",
384: "cc66cc", "cc33cc", "993399", "840084", "ffcf9c", "ff9933",
385: "ff6600", "cc6600", "996633", "663300", "ceff9c", "9cff63",
386: "33ff33", "00cc00", "009900", "006633", "cecfff", "9c9aff",
387: "9966ff", "6633cc", "663399", "330066", "ffffff", "cccccc",
388: "999999", "666666", "333333", "000000" };
389: private static final String COLOR_NAMES[] = { "Aliceblue",
390: "F0F8FF", "Antiquewhite", "FAEBD7", "Aqua", "00FFFF",
391: "Aquamarine", "7FFFD4", "Azure", "F0FFFF", "Beige",
392: "F5F5DC", "Bisque", "FFE4C4", "Black", "000000",
393: "Blanchedalmond", "FFEBCD", "Blue", "0000FF", "Blueviolet",
394: "8A2BE2", "Brown", "A52A2A", "Burlywood", "DEB887",
395: "Cadetblue", "5F9EA0", "Chartreuse", "7FFF00", "Chocolate",
396: "D2691E", "Coral", "FF7F50", "Cornflowerblue", "6495ED",
397: "Cornsilk", "FFF8DC", "Crimson", "DC143C", "Cyan",
398: "00FFFF", "Darkblue", "00008B", "Darkcyan", "008B8B",
399: "Darkgoldenrod", "B8860B", "Darkgray", "A9A9A9",
400: "Darkgreen", "006400", "Darkkhaki", "BDB76B",
401: "Darkmagenta", "8B008B", "Darkolivegreen", "556B2F",
402: "Darkorange", "FF8C00", "Darkorchid", "9932CC", "Darkred",
403: "8B0000", "Darksalmon", "E9967A", "Darkseagreen", "8FBC8F",
404: "Darkslateblue", "483D8B", "Darkslategray", "2F4F4F",
405: "Darkturquoise", "00CED1", "Darkviolet", "9400D3",
406: "Deeppink", "FF1493", "Deepskyblue", "00BFFF", "Dimgray",
407: "696969", "Dodgerblue", "1E90FF", "Firebrick", "B22222",
408: "Floralwhite", "FFFAF0", "Forestgreen", "228B22",
409: "Fuchsia", "FF00FF", "Gainsboro", "DCDCDC", "Ghostwhite",
410: "F8F8FF", "Gold", "FFD700", "Goldenrod", "DAA520", "Gray",
411: "808080", "Green", "008000", "Greenyellow", "ADFF2F",
412: "Honeydew", "F0FFF0", "Hotpink", "FF69B4", "Indianred",
413: "CD5C5C", "Indigo", "4B0082", "Ivory", "FFFFF0", "Khaki",
414: "F0E68C", "Lavender", "E6E6FA", "Lavenderblush", "FFF0F5",
415: "Lawngreen", "7CFC00", "Lemonchiffon", "FFFACD",
416: "Lightblue", "ADD8E6", "Lightcoral", "F08080", "Lightcyan",
417: "E0FFFF", "Lightgoldenrodyellow", "FAFAD2", "Lightgreen",
418: "90EE90", "Lightgrey", "D3D3D3", "Lightpink", "FFB6C1",
419: "Lightsalmon", "FFA07A", "Lightseagreen", "20B2AA",
420: "Lightskyblue", "87CEFA", "Lightslategray", "778899",
421: "Lightsteelblue", "B0C4DE", "Lightyellow", "FFFFE0",
422: "Lime", "00FF00", "Limegreen", "32CD32", "Linen", "FAF0E6",
423: "Magenta", "FF00FF", "Maroon", "800000",
424: "Mediumauqamarine", "66CDAA", "Mediumblue", "0000CD",
425: "Mediumorchid", "BA55D3", "Mediumpurple", "9370D8",
426: "Mediumseagreen", "3CB371", "Mediumslateblue", "7B68EE",
427: "Mediumspringgreen", "00FA9A", "Mediumturquoise", "48D1CC",
428: "Mediumvioletred", "C71585", "Midnightblue", "191970",
429: "Mintcream", "F5FFFA", "Mistyrose", "FFE4E1", "Moccasin",
430: "FFE4B5", "Navajowhite", "FFDEAD", "Navy", "000080",
431: "Oldlace", "FDF5E6", "Olive", "808000", "Olivedrab",
432: "688E23", "Orange", "FFA500", "Orangered", "FF4500",
433: "Orchid", "DA70D6", "Palegoldenrod", "EEE8AA", "Palegreen",
434: "98FB98", "Paleturquoise", "AFEEEE", "Palevioletred",
435: "D87093", "Papayawhip", "FFEFD5", "Peachpuff", "FFDAB9",
436: "Peru", "CD853F", "Pink", "FFC0CB", "Plum", "DDA0DD",
437: "Powderblue", "B0E0E6", "Purple", "800080", "Red",
438: "FF0000", "Rosybrown", "BC8F8F", "Royalblue", "4169E1",
439: "Saddlebrown", "8B4513", "Salmon", "FA8072", "Sandybrown",
440: "F4A460", "Seagreen", "2E8B57", "Seashell", "FFF5EE",
441: "Sienna", "A0522D", "Silver", "C0C0C0", "Skyblue",
442: "87CEEB", "Slateblue", "6A5ACD", "Slategray", "708090",
443: "Snow", "FFFAFA", "Springgreen", "00FF7F", "Steelblue",
444: "4682B4", "Tan", "D2B48C", "Teal", "008080", "Thistle",
445: "D8BFD8", "Tomato", "FF6347", "Turquoise", "40E0D0",
446: "Violet", "EE82EE", "Wheat", "F5DEB3", "White", "FFFFFF",
447: "Whitesmoke", "F5F5F5", "Yellow", "FFFF00", "YellowGreen",
448: "9ACD32" };
449:
450: private static Hashtable COLOR_HASH = new Hashtable();
451: static {
452: for (int i = 0; i < COLOR_NAMES.length; i += 2)
453: COLOR_HASH.put(COLOR_NAMES[i].toLowerCase(),
454: COLOR_NAMES[i + 1].toLowerCase());
455: }
456: }
|