001: package com.javelin.swinglets.plaf.html;
002:
003: import java.io.*;
004: import java.awt.*;
005:
006: import com.javelin.swinglets.*;
007:
008: public class HTMLUtility //These need to be put into HTMLComponentUI
009: {
010: /**
011: * Write the SFrame name out in a hidden field.
012: */
013: public synchronized static void setFrameName(PrintWriter out,
014: SComponent component) {
015: if (component.getTopLevelAncestor() != null) {
016: out.print("<INPUT TYPE=\"HIDDEN\"");
017: out.print(" NAME=\"");
018: out.print(SConstants.SOURCE_CONTAINER);
019: out.print("\" VALUE=\"");
020: out.print(component.getTopLevelAncestor().getName());
021: out.println("\" >");
022: }
023: }
024:
025: /**
026: * Write the SComponent name out in a hidden field.
027: */
028: public static void setComponentName(PrintWriter out,
029: SComponent component) {
030: if (component != null) {
031: out.print("<INPUT TYPE=\"HIDDEN\"");
032: out.print(" NAME=\"");
033: out.print(SConstants.SOURCE_COMPONENT);
034: out.print("\" VALUE=\"");
035: out.print(component.getName());
036: out.println("\" >");
037: }
038: }
039:
040: /**
041: * Write the target SComponent out as a hidden field.
042: */
043: public static void setTargetName(PrintWriter out,
044: SComponent component) {
045: if (component != null) {
046: out.print("<INPUT TYPE=\"HIDDEN\"");
047: out.print(" NAME=\"");
048: out.print(SConstants.TARGET_COMPONENT);
049: out.print("\" VALUE=\"");
050: out.print(component.getName());
051: out.println("\" >");
052: }
053: }
054:
055: public static void setTag(PrintWriter out, String tagname,
056: int tagvalue) {
057: out.print(" ");
058: out.print(tagname);
059: out.print("=\"");
060: out.print(tagvalue);
061: out.print("\"");
062: }
063:
064: public static void setTag(PrintWriter out, String tagname,
065: String tagvalue) {
066: if (tagvalue == null)
067: return;
068:
069: out.print(" ");
070: out.print(tagname);
071: out.print("=\"");
072: out.print(tagvalue);
073: out.print("\"");
074: }
075:
076: public static String makeTag(String tagname, int tagvalue) {
077: StringBuffer buf = new StringBuffer(30);
078:
079: buf.append(" ");
080: buf.append(tagname);
081: buf.append("=\"");
082: buf.append(tagvalue);
083: buf.append("\"");
084:
085: return buf.toString();
086: }
087:
088: public static String makeTag(String tagname, String tagvalue) {
089: StringBuffer buf = new StringBuffer(30);
090:
091: buf.append(" ");
092: buf.append(tagname);
093: buf.append("=\"");
094: buf.append(tagvalue);
095: buf.append("\"");
096:
097: return buf.toString();
098: }
099:
100: public static void setName(PrintWriter out, SComponent c) {
101: if (c.getName() != null) {
102: setTag(out, "NAME", c.getName());
103: }
104: }
105:
106: public static void setValue(PrintWriter out, SAbstractButton c) {
107: if (c.getText() != null) {
108: setTag(out, "VALUE", c.getText());
109: }
110: }
111:
112: public static void setValue(PrintWriter out, STextComponent c) {
113: if (c.getText() != null) {
114: setTag(out, "VALUE", c.getText());
115: }
116: }
117:
118: public static void setSrc(PrintWriter out, SIcon c) {
119: if (c.getUrl() != null) {
120: setTag(out, "SRC", c.getUrl());
121: }
122: }
123:
124: public static void setTabIndex(PrintWriter out, SComponent c) {
125: if (c.getTabIndex() > 0 && c.isEnabled()) {
126: out.print(" tabIndex=");
127: out.print(c.getTabIndex());
128: }
129: }
130:
131: // Even HTML can do tooltips with title=''. Of course NS 4.5 doesn't support it, 'cos its shit.
132: // Its an HTML 4 thing.
133: public static void setMouseOverStatusText(PrintWriter out,
134: String text) {
135: if (text != null) {
136: out.print(" title=\"");
137: out.print(text);
138: out.print("\"");
139: }
140: }
141:
142: public static void updateHorizontalAlignment(PrintWriter out,
143: SComponent component, int defaultAlignment) {
144: if (component != null) {
145: updateHorizontalAlignment(out, component
146: .getHorizontalAlignment(), defaultAlignment);
147: } else {
148: updateHorizontalAlignment(out, defaultAlignment,
149: defaultAlignment);
150: }
151: }
152:
153: public static void updateHorizontalAlignment(PrintWriter out,
154: int horizontalAlignment, SComponent component) {
155: if (component != null) {
156: updateHorizontalAlignment(out, horizontalAlignment,
157: component.getHorizontalAlignment());
158: } else {
159: updateHorizontalAlignment(out, horizontalAlignment,
160: horizontalAlignment);
161: }
162: }
163:
164: public static void updateHorizontalAlignment(PrintWriter out,
165: int horizontalAlignment, int defaultAlignment) {
166: if (horizontalAlignment == SConstants.LEFT) {
167: out.print(" ALIGN=LEFT");
168: } else if (horizontalAlignment == SConstants.CENTER) {
169: out.print(" ALIGN=CENTER");
170: } else if (horizontalAlignment == SConstants.RIGHT) {
171: out.print(" ALIGN=RIGHT");
172: } else if (defaultAlignment == SConstants.LEFT) {
173: out.print(" ALIGN=LEFT");
174: } else if (defaultAlignment == SConstants.CENTER) {
175: out.print(" ALIGN=CENTER");
176: } else if (defaultAlignment == SConstants.RIGHT) {
177: out.print(" ALIGN=RIGHT");
178: }
179: }
180:
181: public static void updateVerticalAlignment(PrintWriter out,
182: SComponent component, int defaultAlignment) {
183: if (component != null) {
184: updateVerticalAlignment(out, component
185: .getVerticalAlignment(), defaultAlignment);
186: } else {
187: updateVerticalAlignment(out, defaultAlignment,
188: defaultAlignment);
189: }
190: }
191:
192: public static void updateVerticalAlignment(PrintWriter out,
193: int verticalAlignment, SComponent component) {
194: if (component != null) {
195: updateVerticalAlignment(out, verticalAlignment, component
196: .getVerticalAlignment());
197: } else {
198: updateVerticalAlignment(out, verticalAlignment,
199: verticalAlignment);
200: }
201: }
202:
203: public static void updateVerticalAlignment(PrintWriter out,
204: int verticalAlignment, int defaultAlignment) {
205: if (verticalAlignment == SConstants.TOP) {
206: out.print(" VALIGN=TOP");
207: } else if (verticalAlignment == SConstants.MIDDLE) {
208: out.print(" VALIGN=MIDDLE");
209: } else if (verticalAlignment == SConstants.BOTTOM) {
210: out.print(" VALIGN=BOTTOM");
211: } else if (defaultAlignment == SConstants.TOP) {
212: out.print(" VALIGN=TOP");
213: } else if (defaultAlignment == SConstants.MIDDLE) {
214: out.print(" VALIGN=MIDDLE");
215: } else if (defaultAlignment == SConstants.BOTTOM) {
216: out.print(" VALIGN=BOTTOM");
217: }
218: }
219:
220: public static void updateText(PrintWriter out, String text,
221: SFont font, SColor foreground, int horizontalAlignment) {
222: if (text == null)
223: return;
224:
225: if (font != null) {
226: if (font.isStrike())
227: out.print("<STRIKE>");
228: if (font.isUnderlined())
229: out.print("<U>");
230: if (font.isHeading1())
231: out.print("<H1>");
232: if (font.isMonoSpaced())
233: out.print("<TT>");
234: if (font.isBig())
235: out.print("<BIG>");
236: if (font.isSmall())
237: out.print("<SMALL>");
238: if (font.isBold())
239: out.print("<B>");
240: if (font.isItalic())
241: out.print("<I>");
242: }
243:
244: if (font != null || foreground != null) {
245: out.print("<FONT");
246: }
247:
248: if (font != null) {
249: if (font.getName() != null) {
250: out.print(" FACE =\"" + font.getName() + "\"");
251: }
252:
253: //Write the font size
254: if (font.getSize() != 12) {
255: String size = SUtilities.getSize(font);
256: if (size != null) {
257: out.print(" SIZE=" + size);
258: }
259: }
260: }
261:
262: //Write the foreground color
263: if (foreground != null) {
264: out.print(" COLOR=\"#" + SColor.toHexString(foreground)
265: + "\"");
266: }
267:
268: if (font != null || foreground != null) {
269: //Close font
270: out.print(" >");
271: }
272:
273: //Write the text alignment
274: if (horizontalAlignment == SConstants.CENTER) {
275: out.print("<CENTER>");
276: } else if (horizontalAlignment == SConstants.RIGHT) {
277: out.print("<DIV ALIGN=RIGHT>");
278: }
279:
280: if (text != null) {
281: out.print(text);
282: }
283:
284: if (horizontalAlignment == SConstants.CENTER) {
285: out.print("</CENTER>");
286: } else if (horizontalAlignment == SConstants.RIGHT) {
287: out.print("</DIV>");
288: }
289:
290: if (font != null || foreground != null) {
291: out.print("</FONT>");
292: }
293:
294: if (font != null) {
295: if (font.isBold())
296: out.print("</B>");
297: if (font.isItalic())
298: out.print("</I>");
299: if (font.isMonoSpaced())
300: out.print("</TT>");
301: if (font.isBig())
302: out.print("</BIG>");
303: if (font.isSmall())
304: out.print("</SMALL>");
305: if (font.isHeading1())
306: out.print("</H1>");
307: if (font.isUnderlined())
308: out.print("</U>");
309: if (font.isStrike())
310: out.print("</STRIKE>");
311: }
312: }
313:
314: public static String encodeURL(SComponent component, String url) {
315: SwingletManager sm = component.getSwingletManager();
316:
317: if (sm instanceof ServletManager
318: && ((ServletManager) sm).getResponse() != null) {
319: return ((ServletManager) sm).getResponse().encodeUrl(url);
320: }
321:
322: return url;
323: }
324:
325: }
|