19 package org.turro.newsletter;
21 import com.steadystate.css.parser.CSSOMParser;
22 import com.steadystate.css.parser.SACParserCSS3;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.HashMap;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.string.Strings;
30 import org.jsoup.Jsoup;
31 import org.jsoup.nodes.Document;
32 import org.jsoup.nodes.Element;
33 import org.jsoup.select.Elements;
34 import org.turro.command.Command;
35 import org.turro.command.Context;
36 import org.turro.elephant.context.Application;
37 import org.turro.elephant.context.ElephantContext;
38 import org.turro.i18n.I_;
39 import org.turro.zkoss.dialog.SelectionDialog;
40 import org.turro.zkoss.text.RepositoryContent;
41 import org.turro.zkoss.text.RepositoryWalker;
42 import org.w3c.css.sac.InputSource;
43 import org.w3c.dom.css.CSSStyleDeclaration;
44 import org.zkoss.zk.ui.Executions;
45 import org.zkoss.zk.ui.IdSpace;
46 import org.zkoss.zk.ui.event.Event;
47 import org.zkoss.zk.ui.select.Selectors;
48 import org.zkoss.zk.ui.select.annotation.Listen;
49 import org.zkoss.zk.ui.select.annotation.Wire;
50 import org.zkoss.zkex.zul.Colorbox;
51 import org.zkoss.zul.Checkbox;
52 import org.zkoss.zul.Div;
53 import org.zkoss.zul.Html;
54 import org.zkoss.zul.Textbox;
62 private final String entityPath;
64 @Wire(
"#width")
private Textbox width;
65 @Wire(
"#label")
private Textbox label;
66 @Wire(
"#color")
private Colorbox color;
67 @Wire(
"#bgColor")
private Colorbox bgColor;
68 @Wire(
"#fontSize")
private Textbox fontSize;
69 @Wire(
"#bold")
private Checkbox bold;
70 @Wire(
"#italic")
private Checkbox italic;
71 @Wire(
"#underline")
private Checkbox underline;
72 @Wire(
"#banner")
private Html html;
74 @Listen(
"onChange = *; onSelect = *; onCheck = *")
76 html.setContent(bannerString());
79 @Listen(
"onClick = #selImage")
82 repositories.
selectFolder(
"/_internal/files" + entityPath);
88 public Object execute(Context context) {
94 html.setContent(bannerString());
100 @Listen(
"onClick = #delImage")
103 html.setContent(bannerString());
107 HashMap args =
new HashMap();
109 args.put(
"i_",
I_.
api());
110 args.put(
"i",
I_.
map());
112 Executions.createComponents(
"/WEB-INF/_zul/bs/comps/editor/bannerComposer.zul",
this, args);
113 Selectors.wireComponents(
this,
this,
false);
114 Selectors.wireEventListeners(
this,
this);
115 this.entityPath = entityPath;
116 parseValues(htmlStr);
117 html.setContent(bannerString());
121 return bannerString();
126 private String imgSrc;
128 private String bannerString() {
129 if(Strings.isBlank(label.getValue()))
return "";
131 StringBuilder sb =
new StringBuilder();
132 sb.append(
"<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width:100%;\">")
133 .append(
"<tr valign=\"middle\" style=\"vertical-align:middle;\">");
134 if(!Strings.isBlank(imgSrc)) {
136 if(!Strings.isBlank(width.getValue())) {
137 sb.append(
"width=\"")
138 .append(width.getValue())
141 sb.append(
"style=\"");
142 if(!Strings.isBlank(bgColor.getValue())) {
143 if(underline.isChecked()) {
144 sb.append(
"border-bottom-style:solid;border-bottom-width:3px;border-bottom-color:")
145 .append(bgColor.getValue())
148 sb.append(
"background-color:")
149 .append(bgColor.getValue())
153 if(!Strings.isBlank(width.getValue())) {
155 .append(width.getValue())
158 sb.append(
"\"><img border=\"0\" style=\"");
159 if(!Strings.isBlank(width.getValue())) {
161 .append(width.getValue())
164 sb.append(
"\" src=\"")
166 .append(
"\"/></td>");
168 if(!Strings.isBlank(label.getValue())) {
169 sb.append(
"<td style=\"padding-left:10px;");
170 if(!Strings.isBlank(fontSize.getValue())) {
171 sb.append(
"font-size:")
172 .append(fontSize.getValue())
175 if(bold.isChecked()) {
176 sb.append(
"font-weight:bold;");
178 sb.append(
"font-weight:normal;");
180 if(italic.isChecked()) {
181 sb.append(
"font-style:italic;");
183 sb.append(
"font-style:normal;");
185 if(!Strings.isBlank(color.getValue())) {
187 .append(color.getValue())
190 if(!Strings.isBlank(bgColor.getValue())) {
191 if(underline.isChecked()) {
192 sb.append(
"border-bottom-style:solid;border-bottom-width:3px;border-bottom-color:")
193 .append(bgColor.getValue())
196 sb.append(
"background-color:")
197 .append(bgColor.getValue())
202 .append(label.getValue())
205 sb.append(
"</tr></table>")
206 .append(divider(10));
208 return sb.toString();
211 private String divider(
int height) {
212 return "<div width=\"100%\" style=\"width:100%;height:" + height +
"px;\"> </div>";
215 private void parseValues(String html) {
216 if(Strings.isBlank(html))
return;
217 Document document = Jsoup.parse(html);
218 Elements images = document.getElementsByTag(
"img");
219 if(!images.isEmpty()) {
220 Element image = images.first();
221 CSSStyleDeclaration style = parseStyle(image);
223 width.setValue(style.getPropertyValue(
"width"));
225 imgSrc = image.attr(
"src");
227 Elements tds = document.getElementsByTag(
"td");
229 Element td = tds.last();
230 CSSStyleDeclaration style = parseStyle(td);
232 fontSize.setValue(style.getPropertyValue(
"font-size"));
233 bold.setChecked(
"bold".equals(style.getPropertyValue(
"font-weight")));
234 italic.setChecked(
"italic".equals(style.getPropertyValue(
"font-style")));
235 if(!Strings.isBlank(style.getPropertyValue(
"color"))) {
236 color.setValue(convert(style.getPropertyValue(
"color")));
238 if(
"solid".equals(style.getPropertyValue(
"border-bottom-style"))) {
239 if(!Strings.isBlank(style.getPropertyValue(
"border-bottom-color"))) {
240 bgColor.setValue(convert(style.getPropertyValue(
"border-bottom-color")));
242 underline.setChecked(
true);
244 if(!Strings.isBlank(style.getPropertyValue(
"background-color"))) {
245 bgColor.setValue(convert(style.getPropertyValue(
"background-color")));
247 underline.setChecked(
false);
250 label.setValue(td.html());
254 private CSSStyleDeclaration parseStyle(Element element) {
255 String style = element.attr(
"style");
256 if(!Strings.isBlank(style)) {
258 InputSource source =
new InputSource(
new StringReader(style));
259 CSSOMParser parser =
new CSSOMParser(
new SACParserCSS3());
260 return parser.parseStyleDeclaration(source);
261 }
catch (IOException ex) {
262 Logger.getLogger(
BannerComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
268 private String convert(String propertyValue) {
269 if(propertyValue.startsWith(
"rgb")) {
270 String colors[] = propertyValue.substring(4, propertyValue.length() - 1).split(
"\\s*,\\s*");
271 propertyValue = String.format(
"#%02x%02x%02x",
272 Integer.parseInt(colors[0]), Integer.parseInt(colors[1]), Integer.parseInt(colors[2]));
274 return propertyValue;
static Map getStringMap()
static String getRelativePath(String path)
static String getRootWebPath()
static I18nByKey byKeyMap()
static String get(String msg)
static I18nApiWrapper api()
static I18nMapWrapper map()
final void onChange(Event event)
BannerComposer(String htmlStr, String entityPath)
static void showComponent(Page page, String title, Component component, String width, String height, final Command command)
static final String IMAGE_FILTER
void setFilter(String filter)
void selectFolder(String folder)
void setNoTreeRepository(boolean noTreeRepository)
void setReadOnlyRepository(boolean readOnlyRepository)