1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.swt;
17
18 import org.apache.commons.jelly.JellyTagException;
19 import org.apache.commons.jelly.TagSupport;
20 import org.apache.commons.jelly.XMLOutput;
21 import org.apache.commons.jelly.tags.swt.converters.ColorConverter;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.widgets.Widget;
27
28 /***
29 * Class to create a {@link Font} instance within Jelly SWT.
30 *
31 * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
32 * @author Written with much help thanks to the ImageTag class
33 * @version CVS $Id: FontTag.java 155420 2005-02-26 13:06:03Z dirkv $
34 */
35 public class FontTag extends TagSupport {
36
37 /*** The Log to which logging calls will be made. */
38 private static final Log log = LogFactory.getLog(FontTag.class);
39
40 /*** Font type */
41 private String type;
42
43 /*** Font size */
44 private int size;
45
46 /*** Font style */
47 private String style;
48
49 /*** Font variable name */
50 private String var;
51
52 /***
53 * Set the type of this {@link Font}
54 *
55 * @param type {@link Font} type name
56 */
57 public void setType(final String type) {
58 this.type = type;
59 }
60
61 /***
62 * Obtain the {@link Font} type name
63 *
64 * @return the {@link Font} type name
65 */
66 public String getType() {
67 return this.type;
68 }
69
70 /***
71 * Set the size of this {@link Font}
72 *
73 * @param size {@link Font} size
74 */
75 public void setSize(final int size) {
76 this.size = size;
77 }
78
79 /***
80 * Obtain the {@link Font} size
81 *
82 * @return the {@link Font} size
83 */
84 public int getSize() {
85 return this.size;
86 }
87
88 /***
89 * Set the style of this {@link Font} (eg. bold, normal, italics)
90 *
91 * @param style the style of this {@link Font}
92 */
93 public void setStyle(final String style) {
94 this.style = style;
95 }
96
97 /***
98 * Obtain the style of this {@link Font}
99 *
100 * @return the style of this {@link Font}
101 */
102 public String getStyle() {
103 return this.style;
104 }
105
106 /***
107 * Sets the variable name
108 *
109 * @param var the variable name of this {@link Font} instance
110 */
111 public void setVar(final String var) {
112 this.var = var;
113 }
114
115 /***
116 * Obtain the variable name.
117 *
118 * @return the variable name of this {@link Font} instance
119 */
120 public String getVar() {
121 return this.var;
122 }
123
124 /***
125 * @return the parent widget which will deliver us a {@link Device} reference
126 */
127 public Widget getParentWidget() {
128 final WidgetTag tag = (WidgetTag) findAncestorWithClass(WidgetTag.class);
129 if (tag != null) {
130 return tag.getWidget();
131 }
132 return null;
133 }
134
135
136
137
138 /***
139 * Creates a {@link Font} instance as defined by the type, size and style
140 * attributes, and stores this {@link Font} instance in the Context so that
141 * it can be referenced in the Jelly script.
142 *
143 * @param output {@link XMLOutput} reference
144 * @throws JellyTagException if an error occurs
145 * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
146 */
147 public void doTag(final XMLOutput output) throws JellyTagException {
148
149 invokeBody(output);
150
151 final Widget parent = getParentWidget();
152
153 if (parent == null) {
154 throw new JellyTagException(
155 "This tag must be nested within a Widget or a Window"
156 );
157 }
158
159 if (var == null) {
160 throw new JellyTagException("This tag requires a context variable name");
161 }
162
163 if (type == null) {
164 throw new JellyTagException("This tag requires a font type name");
165 }
166
167 if (size <= 0) {
168 throw new JellyTagException("This tag requires a font size greater than 0");
169 }
170
171 if (style == null) {
172 if (log.isDebugEnabled()) {
173 log.debug("No style set on font " + type + ", defaulting to normal");
174 }
175 }
176
177 final Font font =
178 new Font(
179 parent.getDisplay(),
180 type,
181 size,
182 style == null ? SWT.NORMAL : SwtHelper.parseStyle(SWT.class, style)
183 );
184
185
186 context.setVariable(var, font);
187 }
188 }