1
24 package net.sf.jasperreports.engine;
25
26 import java.awt.font.TextAttribute;
27 import java.text.AttributedCharacterIterator.Attribute;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import net.sf.jasperreports.engine.fonts.FontUtil;
33 import net.sf.jasperreports.engine.type.ModeEnum;
34 import net.sf.jasperreports.engine.util.JRDataUtils;
35 import net.sf.jasperreports.engine.util.JRStyledTextParser;
36 import net.sf.jasperreports.engine.util.JRStyledTextUtil;
37
38
45 public abstract class JRStyledTextAttributeSelector
46 {
47 protected final JasperReportsContext jasperReportsContext;
48
49
52 protected JRStyledTextAttributeSelector(JasperReportsContext jasperReportsContext)
53 {
54 this.jasperReportsContext = jasperReportsContext;
55 }
56
57
60 private static Locale getLocale()
61 {
62 return JRStyledTextParser.getLocale();
63 }
64
65
68 public static Locale getTextLocale(JRPrintText printText)
69 {
70 String localeCode = printText.getLocaleCode();
71 if (localeCode == null)
72 {
73 return getLocale();
74 }
75 return JRDataUtils.getLocale(localeCode);
76 }
77
78
85 public abstract Map<Attribute,Object> getStyledTextAttributes(JRPrintText printText);
86
87
88
92 public static JRStyledTextAttributeSelector getAllSelector(JasperReportsContext jasperReportsContext)
93 {
94 return new AllSelector(jasperReportsContext);
95 }
96
97
98
102 private static class AllSelector extends JRStyledTextAttributeSelector
103 {
104 public AllSelector(JasperReportsContext jasperReportsContext)
105 {
106 super(jasperReportsContext);
107 }
108
109 @Override
110 public Map<Attribute,Object> getStyledTextAttributes(JRPrintText printText)
111 {
112 Map<Attribute,Object> attributes = new HashMap<Attribute,Object>();
113
114 FontUtil.getInstance(jasperReportsContext).getAttributesWithoutAwtFont(attributes, printText);
115 attributes.put(TextAttribute.FOREGROUND, printText.getForecolor());
116 if (printText.getModeValue() == ModeEnum.OPAQUE)
117 {
118 attributes.put(TextAttribute.BACKGROUND, printText.getBackcolor());
119 }
120 return attributes;
121 }
122 }
123
124
125
129 public static JRStyledTextAttributeSelector getNoBackcolorSelector(JasperReportsContext jasperReportsContext)
130 {
131 return new NoBackcolorSelector(jasperReportsContext);
132 }
133
134
135
139 private static class NoBackcolorSelector extends JRStyledTextAttributeSelector
140 {
141 public NoBackcolorSelector(JasperReportsContext jasperReportsContext)
142 {
143 super(jasperReportsContext);
144 }
145
146 @Override
147 public Map<Attribute,Object> getStyledTextAttributes(JRPrintText printText)
148 {
149 Map<Attribute,Object> attributes = new HashMap<Attribute,Object>();
150
151 FontUtil.getInstance(jasperReportsContext).getAttributesWithoutAwtFont(attributes, printText);
152 attributes.put(TextAttribute.FOREGROUND, printText.getForecolor());
153 return attributes;
154 }
155 }
156
157
158
161 public static JRStyledTextAttributeSelector getNoneSelector(JasperReportsContext jasperReportsContext)
162 {
163 return new NoneSelector(jasperReportsContext);
164 }
165
166
167
170 private static class NoneSelector extends JRStyledTextAttributeSelector
171 {
172 public NoneSelector(JasperReportsContext jasperReportsContext)
173 {
174 super(jasperReportsContext);
175 }
176
177 @Override
178 public Map<Attribute,Object> getStyledTextAttributes(JRPrintText printText)
179 {
180 return null;
181 }
182 }
183
184 }
185
186