01: /*
02: * Copyright (C) 2005 Jeff Tassin
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package com.jeta.swingbuilder.codegen.builder.properties;
20:
21: import java.awt.Font;
22: import java.lang.reflect.Method;
23:
24: import com.jeta.forms.gui.beans.JETAPropertyDescriptor;
25: import com.jeta.forms.store.properties.FontProperty;
26: import com.jeta.swingbuilder.codegen.builder.BeanWriter;
27: import com.jeta.swingbuilder.codegen.builder.DeclarationManager;
28: import com.jeta.swingbuilder.codegen.builder.MethodExpression;
29: import com.jeta.swingbuilder.codegen.builder.MethodStatement;
30: import com.jeta.swingbuilder.codegen.builder.PropertyWriter;
31: import com.jeta.swingbuilder.codegen.builder.StringExpression;
32:
33: public class FontPropertyWriter implements PropertyWriter {
34:
35: /**
36: * PropertyWriter implementation
37: */
38: public void writeProperty(DeclarationManager declMgr,
39: BeanWriter writer, JETAPropertyDescriptor pd, Object value) {
40: try {
41: Method write = pd.getWriteMethod();
42: if (write != null) {
43: Font font = null;
44: if (value instanceof Font)
45: font = (Font) value;
46: else if (value instanceof FontProperty)
47: font = ((FontProperty) value).getFont();
48:
49: if (font != null && write != null) {
50: declMgr.addImport("java.awt.Font");
51: MethodStatement ms = new MethodStatement(writer
52: .getBeanVariable(), write.getName());
53: ms.addParameter(createFontExpression(font));
54: writer.addStatement(ms);
55: }
56: }
57: } catch (Exception e) {
58: e.printStackTrace();
59: }
60: }
61:
62: public static MethodExpression createFontExpression(Font font) {
63: MethodExpression expr = new MethodExpression("new Font");
64: expr.addParameter(new StringExpression(font.getName()));
65: int style = font.getStyle();
66: if (((style & Font.BOLD) != 0) && ((style & Font.ITALIC) != 0))
67: expr.addParameter("Font.BOLD | Font.ITALIC");
68: else if ((style & Font.BOLD) != 0)
69: expr.addParameter("Font.BOLD");
70: else if ((style & Font.ITALIC) != 0)
71: expr.addParameter("Font.ITALIC");
72: else
73: expr.addParameter("Font.PLAIN");
74:
75: expr.addParameter(String.valueOf(font.getSize()));
76: return expr;
77: }
78:
79: }
|