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:
030: /**
031: * Displays a box that allows the user to select the face, style and weight for a font
032: */
033: public class HtmlFontChooser extends HtmlFormComponent {
034:
035: String _fontList[] = { "Arial", "Courier", "Helvetica",
036: "TimesRoman", "Verdana" };
037: String _pointSizeList[][] = {
038: { "8", "9", "10", "11", "12", "13", "14", "15", "16", "18",
039: "20", "24", "36" },
040: { "8pt", "9pt", "10pt", "11pt", "12pt", "13pt", "14pt",
041: "15pt", "16pt", "18pt", "20pt", "24pt", "36pt" } };
042: String _relativeSizeList[][] = {
043: { "-3", "-2", "-1", "0", "+1", "+2", "+3", "+4" },
044: { "Smallest", "Very Small", "Small", "Normal", "Large",
045: "Very Large", "Extremely Large", "Largest" } };
046:
047: boolean _usePointSize = true;
048:
049: private String _font;
050: private String _fontStartTag, _fontEndTag;
051: private String _theme;
052:
053: /**
054: * Constructor for HtmlColorChooser.
055: * @param name
056: * @param p
057: */
058: public HtmlFontChooser(String name, HtmlPage p) {
059: super (name, p);
060: }
061:
062: /**
063: * @see com.salmonllc.html.HtmlComponent#generateHTML(PrintWriter, int)
064: */
065: public void generateHTML(PrintWriter p, int rowNo) throws Exception {
066: if (!_visible)
067: return;
068:
069: _value = getValue(rowNo, true);
070:
071: String face = null;
072: String style = null;
073: String size = null;
074:
075: if (_value != null) {
076: StringTokenizer tok = new StringTokenizer(_value, ",");
077: face = tok.nextToken();
078: style = tok.nextToken();
079: size = tok.nextToken().toLowerCase();
080: }
081:
082: StringBuffer sb = new StringBuffer();
083:
084: //Font Face
085: sb.append("<table border=\"0\"><tr><td>");
086: sb.append(_fontStartTag);
087: sb.append("Font Face:");
088: sb.append(_fontEndTag);
089: sb.append("</td><td>");
090: sb.append("<select name=\"");
091: sb.append(getFullName());
092: sb.append("face\">");
093: sb.append("<option value=\"\"");
094: if (face == null)
095: sb.append(" selected></option>");
096: else
097: sb.append("></option>");
098:
099: for (int i = 0; i < _fontList.length; i++) {
100: sb.append("<option value=\"");
101: sb.append(_fontList[i]);
102: sb.append("\"");
103: if (face != null && face.equalsIgnoreCase(_fontList[i]))
104: sb.append(" selected ");
105: sb.append(">");
106: sb.append(_fontList[i]);
107: sb.append("</option>");
108:
109: }
110: sb.append("</select>");
111:
112: //Font Size
113: sb.append("<td>");
114: sb.append(_fontStartTag);
115: sb.append("Size:");
116: sb.append(_fontEndTag);
117: sb.append("</td><td>");
118: sb.append("<select name=\"");
119: sb.append(getFullName());
120: sb.append("size\">");
121: String[][] list = _relativeSizeList;
122: if (_usePointSize)
123: list = _pointSizeList;
124: else {
125: if (!size.startsWith("+") && !size.startsWith("-"))
126: size = "+" + size;
127: }
128: for (int i = 0; i < list[0].length; i++) {
129: sb.append("<option value=\"");
130: sb.append(list[0][i]);
131: sb.append("\"");
132: if (size != null
133: && (size.equals(list[0][i]) || size
134: .equals(list[1][i])))
135: sb.append(" selected ");
136: sb.append(">");
137: sb.append(list[1][i]);
138: sb.append("</option>");
139:
140: }
141: sb.append("</select>");
142:
143: //bold/italic
144: sb.append("<td>");
145: sb.append(_fontStartTag);
146: sb.append("Bold:");
147: sb.append(_fontEndTag);
148: sb.append("</td><td>");
149: sb.append("<input type=\"checkbox\" name=\"");
150: sb.append(getFullName());
151: sb.append("bold\"");
152: if (style != null && style.indexOf("BOLD") > -1)
153: sb.append(" checked");
154: sb.append(">");
155:
156: sb.append("<td>");
157: sb.append(_fontStartTag);
158: sb.append("Italic:");
159: sb.append(_fontEndTag);
160: sb.append("</td><td>");
161: sb.append("<input type=\"checkbox\" name=\"");
162: sb.append(getFullName());
163: sb.append("italic\"");
164: if (style != null && style.indexOf("ITALIC") > -1)
165: sb.append(" checked");
166: sb.append(">");
167: sb.append("</td></tr></table>");
168: p.println(sb.toString());
169: }
170:
171: public boolean processParms(Hashtable parms, int rowNo)
172: throws Exception {
173: if (!getEnabled())
174: return false;
175: Object oldValue = _value;
176:
177: String name = getFullName();
178: if (rowNo > -1) {
179: name += "_" + rowNo;
180: if (_dsBuff != null)
181: oldValue = _dsBuff.getAny(rowNo, _dsColNo);
182: } else {
183: if (_dsBuff != null)
184: oldValue = _dsBuff.getAny(_dsColNo);
185: }
186:
187: String font[] = (String[]) parms.get(name + "face");
188: String size[] = (String[]) parms.get(name + "size");
189: String bold[] = (String[]) parms.get(name + "bold");
190: String italic[] = (String[]) parms.get(name + "italic");
191:
192: if (font == null || font[0].equals(""))
193: _value = null;
194: else {
195: _value = font[0] + ",";
196: if (bold == null && italic == null)
197: _value += "PLAIN,";
198: else if (bold != null && italic == null)
199: _value += "BOLD,";
200: else if (bold == null && italic != null)
201: _value += "ITALIC,";
202: else
203: _value += "BOLD|ITALIC,";
204:
205: if (size == null)
206: _value += _usePointSize ? "10" : "0";
207: else
208: _value += size[0];
209: }
210: if (!valuesEqual(oldValue, _value)) {
211: String s = null;
212: if (oldValue != null)
213: s = oldValue.toString();
214: ValueChangedEvent e = new ValueChangedEvent(getPage(),
215: this , getName(), getFullName(), s, _value, rowNo,
216: _dsColNo, _dsBuff);
217: addEvent(e);
218: }
219:
220: return false;
221: }
222:
223: /**
224: * This method returns the property theme for the component.
225: * @return
226: */
227: public String getTheme() {
228: return _theme;
229: }
230:
231: /**
232: * 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.
233: */
234: public void setFont(String font) {
235: _font = font;
236: setTheme(_theme);
237: }
238:
239: /**
240: * This method sets the end font tag for the component.
241: */
242: public void setFontEndTag(String value) {
243: _fontEndTag = value;
244: }
245:
246: /**
247: * This method sets the start font tag for the component.
248: */
249: public void setFontStartTag(String value) {
250: _fontStartTag = value;
251: }
252:
253: /**
254: * This method sets the property theme for the component.
255: * @param theme The theme to use.
256: */
257: public void setTheme(String theme) {
258: Props props = getPage().getPageProperties();
259:
260: if (_font != null) {
261: _fontStartTag = props.getThemeProperty(theme, _font
262: + Props.TAG_START);
263: _fontEndTag = props.getThemeProperty(theme, _font
264: + Props.TAG_END);
265: } else {
266: _fontStartTag = props.getThemeProperty(theme,
267: HtmlText.FONT_DEFAULT + Props.TAG_START);
268: _fontEndTag = props.getThemeProperty(theme,
269: HtmlText.FONT_DEFAULT + Props.TAG_END);
270: }
271:
272: _theme = theme;
273: }
274:
275: /**
276: * Returns the usePointSize flag. True will mean the size is in points, false means it will be relative.
277: * @return boolean
278: */
279: public boolean isUsePointSize() {
280: return _usePointSize;
281: }
282:
283: /**
284: * Sets the usePointSize flag. True will mean the size is in points, false means it will be relative.
285: * @param usePointSize The usePointSize to set
286: */
287: public void setUsePointSize(boolean usePointSize) {
288: _usePointSize = usePointSize;
289: }
290:
291: public String getValueAsStyle(String color) {
292: String value = getValue();
293: String face = null;
294: String style = null;
295: String size = null;
296:
297: if (value != null) {
298: StringTokenizer tok = new StringTokenizer(_value, ",");
299: face = tok.nextToken();
300: if (face.equals(""))
301: return null;
302: style = tok.nextToken();
303: size = tok.nextToken();
304: StringBuffer ret = new StringBuffer();
305: ret.append("font-family: ");
306: ret.append(face);
307: ret.append("; font-size: ");
308: ret.append(size.toString());
309: ret.append(";text-decoration: none;");
310: if (style.indexOf("BOLD") > -1)
311: ret.append("font-weight:bold;");
312: if (style.indexOf("ITALIC") > -1)
313: ret.append("font-style:italic;>");
314: if (color != null) {
315: ret.append("color:");
316: ret.append(color);
317: ret.append(";");
318: }
319: return ret.toString();
320: }
321: return null;
322: }
323:
324: /**
325: * Returns the value in the component as the start of a generated font tag
326: */
327: public String getValueAsFontStartTag(String color) {
328: String value = getValue();
329:
330: String face = null;
331: String style = null;
332: String size = null;
333:
334: if (value != null) {
335: StringTokenizer tok = new StringTokenizer(value, ",");
336: face = tok.nextToken();
337: if (face.equals(""))
338: return null;
339: style = tok.nextToken();
340: size = tok.nextToken();
341: if (size.endsWith("PT") || size.endsWith("pt"))
342: size = size.substring(0, size.length() - 2);
343: StringBuffer ret = new StringBuffer();
344: ret.append("<FONT face=\"");
345: ret.append(face);
346: ret.append("\"");
347: if (_usePointSize) {
348: ret.append(" STYLE=\"FONT-SIZE:");
349: ret.append(size);
350: ret.append("pt;\"");
351: } else {
352: ret.append("\" SIZE=\"");
353: ret.append(size);
354: ret.append("\"");
355: }
356: if (color != null) {
357: ret.append(" COLOR=\"");
358: ret.append(color);
359: ret.append("\"");
360: }
361: ret.append(">");
362: if (style.indexOf("BOLD") > -1)
363: ret.append("<b>");
364: if (style.indexOf("ITALIC") > -1)
365: ret.append("<i>");
366: return ret.toString();
367:
368: }
369: return null;
370:
371: }
372:
373: /**
374: * Returns the value in the component as the start of a generated font tag
375: */
376: public String getValueAsFontEndTag() {
377: String value = getValue();
378: String style = null;
379: if (value != null) {
380: StringTokenizer tok = new StringTokenizer(value, ",");
381: tok.nextToken();
382: style = tok.nextToken();
383: StringBuffer ret = new StringBuffer();
384: if (style.indexOf("ITALIC") > -1)
385: ret.append("</i>");
386: if (style.indexOf("BOLD") > -1)
387: ret.append("</b>");
388: ret.append("</FONT>");
389: return ret.toString();
390:
391: }
392: return null;
393:
394: }
395:
396: /**
397: * Sets the value of this component from the attributes in a FONT tag
398: */
399: public void setFontValue(String fontTag) {
400: if (fontTag == null) {
401: setValue(null);
402: return;
403: }
404: String face = getTagAttribute(fontTag, "face");
405: String size = getTagAttribute(fontTag, "size");
406: if (size == null) {
407: size = getTagAttribute(fontTag, "style").toUpperCase();
408: int pos = size.indexOf("FONT-SIZE:");
409: if (pos > -1) {
410: int pos2 = size.indexOf(";", pos);
411: if (pos2 > -1) {
412: size = size.substring(pos + 10, pos2);
413: _usePointSize = true;
414: }
415: }
416: } else
417: _usePointSize = false;
418:
419: fontTag = fontTag.toLowerCase();
420: boolean italic = (fontTag.indexOf("<i>") > -1);
421: boolean bold = (fontTag.indexOf("<b>") > -1);
422: String style = "PLAIN";
423:
424: if (bold && !italic)
425: style += "BOLD";
426: else if (!bold && italic)
427: style += "ITALIC";
428: else if (bold && italic)
429: style += "BOLD|ITALIC";
430:
431: String value = face + "," + style + "," + size;
432: setValue(value);
433:
434: }
435:
436: /**
437: * Sets the value of this component from the attributes in a CSS Style
438: */
439: public void setStyleValue(String style) {
440: if (style == null) {
441: setValue(null);
442: return;
443: }
444: String face = getStyleAttribute(style, "font-family");
445: String size = getStyleAttribute(style, "font-size");
446: if (size != null && size.endsWith("px"))
447: size = size.substring(0, size.length() - 2);
448: String weight = getStyleAttribute(style, "font-weight");
449: String fontStyle = getStyleAttribute(style, "font-style");
450: _usePointSize = true;
451:
452: boolean bold = (weight != null && weight
453: .equalsIgnoreCase("bold"));
454: boolean italic = (fontStyle != null && style
455: .equalsIgnoreCase("italic"));
456:
457: String s = "PLAIN";
458: if (bold && !italic)
459: s += "BOLD";
460: else if (!bold && italic)
461: s += "ITALIC";
462: else if (bold && italic)
463: s += "BOLD|ITALIC";
464:
465: String value = face + "," + s + "," + size;
466: setValue(value);
467:
468: }
469:
470: /**
471: * Gets an attribute from a style entry
472: */
473: public static String getStyleAttribute(String style,
474: String attribute) {
475: if (style == null)
476: return null;
477:
478: String test1 = style.toUpperCase();
479: String test2 = attribute.toUpperCase();
480: int pos1 = test1.indexOf(test2 + ":");
481: if (pos1 == -1)
482: return null;
483: int pos2 = test1.indexOf(";", pos1);
484: if (pos2 == -1)
485: return null;
486: return style.substring(pos1 + attribute.length() + 1, pos2)
487: .trim();
488: }
489:
490: /**
491: * Specify a font and and attribute and this method will return the value
492: */
493: public static String getTagAttribute(String tag, String attribute) {
494: Token tok = new Token();
495: boolean next = false;
496: while (nextToken(tag, tok)) {
497: String val = tok.value.toString();
498: if (next)
499: return val;
500: if (val.equalsIgnoreCase(attribute))
501: next = true;
502: }
503: return null;
504: }
505:
506: private static boolean nextToken(String st, Token tok) {
507: tok.value.setLength(0);
508: boolean quoteMode = false;
509: if (tok.index >= st.length())
510: return false;
511: while ((tok.index < st.length())) {
512: char c = st.charAt(tok.index);
513: if (quoteMode) {
514: tok.index++;
515: if (c == '"')
516: return true;
517: else
518: tok.value.append(c);
519: } else if (c == '"') {
520: quoteMode = true;
521: tok.index++;
522: } else {
523: if (c == '<')
524: tok.index++;
525: else if (c == ' ' || c == '=' || c == '>') {
526: if (tok.value.length() != 0)
527: return true;
528: tok.index++;
529: } else {
530: tok.value.append(c);
531: tok.index++;
532: }
533: }
534: }
535: return false;
536: }
537:
538: private static class Token {
539: StringBuffer value = new StringBuffer();
540: int index;
541: }
542: }
|