BrightSide Workbench Full Report + Source Code
Svg.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.zkoss.svg;
19 
20 import java.io.Writer;
21 import java.util.HashMap;
22 import java.util.Map;
23 import org.apache.commons.text.StringEscapeUtils;
24 import org.turro.string.Strings;
25 import org.turro.html.HTMLGenerator;
26 import org.turro.util.IdGenerator;
27 import org.zkoss.zk.ui.Executions;
28 import org.zkoss.zul.Div;
29 
34 public class Svg extends Div {
35 
36  private HTMLGenerator htmlGenerator = new HTMLGenerator();
37  private String forceStyle = null, forceTransform= null, clickElement = null;
38  private String id;
39 
40  public void startComposing(String onUserEvent, String width, String height, double scale) {
41  id = IdGenerator.generate();
42  startTag("div",
43  new String[] {
44  "id='" + id + "'",
45  "xmlns:n='http://www.zkoss.org/2005/zk/native'",
46  onUserEvent != null ? "onUser='" + onUserEvent + "'" : ""
47  });
48  startTag("n:svg",
49  new String[] {
50  "width='" + width + "'",
51  "height='" + height + "'",
52  "version='1.1'",
53  "xmlns='http://www.w3.org/2000/svg'",
54  "xmlns:z='zk' style='overflow:auto'"
55  });
56  startTag("n:g", new String[] {
57  "transform='scale(" + scale + ")'"
58  });
59  }
60 
61  public void example() {
62  doTag("n:circle",
63  new String[] {
64  "style='fill:${each};cursor:pointer'",
65  "z:forEach='${bgnds}'",
66  "cx='${50+rads[forEachStatus.index]}'",
67  "cy='${20+rads[forEachStatus.index]}'",
68  "r='${rads[forEachStatus.index]}'",
69  "onclick=\"zAu.send(new zk.Event(zk.Widget.$('$" + id + "'), 'onUser', '${each}'));\""
70  });
71  }
72 
73  public void generateContent(Map args) {
74  htmlGenerator.endAllTags();
75  Executions.createComponentsDirectly(htmlGenerator.toString(), "zul", this, args);
76  }
77 
78  public void clear() {
79  htmlGenerator = new HTMLGenerator();
80  }
81 
82  public void circle(int x, int y, int r) {
83  circle(x, y, r, 1.0);
84  }
85 
86  public void circle(int x, int y, int r, double opacity) {
87  circle(x, y, r, null, null, null, opacity);
88  }
89 
90  public void circle(int x, int y, int r, String stroke, String strokeWidth, String fill, double opacity) {
91  doTag("n:circle", new String[] {
92  "cx='" + x + "'",
93  "cy='" + y + "'",
94  "r='" + r + "'",
95  (stroke == null ? null : "stroke='" + stroke + "'"),
96  (strokeWidth == null ? null : "stroke-width='" + strokeWidth + "'"),
97  (fill == null ? null : "fill='" + fill + "'"),
98  "opacity='" + opacity + "'",
99  forceStyle != null ? "style='" + forceStyle + "'" : null,
100  forceTransform != null ? "transform='" + forceTransform + "'" : null,
101  clickElement != null ? "onclick=\"" + clickElement + "\"" : null
102  });
103  forceStyle = null;
104  forceTransform = null;
105  clickElement = null;
106  }
107 
108  public void line(int x1, int y1, int x2, int y2, String stroke, String strokeWidth) {
109  line(x1, y1, x2, y2, stroke, strokeWidth, 1);
110  }
111 
112  public void line(int x1, int y1, int x2, int y2, String stroke, String strokeWidth, double opacity) {
113  doTag("n:line", new String[] {
114  "x1='" + x1 + "'",
115  "y1='" + y1 + "'",
116  "x2='" + x2 + "'",
117  "y2='" + y2 + "'",
118  (stroke == null ? null : "stroke='" + stroke + "'"),
119  (strokeWidth == null ? null : "stroke-width='" + strokeWidth + "'"),
120  "opacity='" + opacity + "'",
121  forceStyle != null ? "style='" + forceStyle + "'" : "",
122  forceTransform != null ? "transform='" + forceTransform + "'" : "",
123  clickElement != null ? "onclick=\"" + clickElement + "\"" : ""
124  });
125  forceStyle = null;
126  forceTransform = null;
127  clickElement = null;
128  }
129 
130  public void rectangle(int x, int y, int width, int height, String fill) {
131  rectangle(x, y, width, height, fill, 1);
132  }
133 
134  public void rectangle(int x, int y, int width, int height, String fill, double opacity) {
135  rectangle(x, y, width, height, fill, opacity, null);
136  }
137 
138  public void rectangle(int x, int y, int width, int height, String fill, double opacity, String tooltip) {
139  startTag("n:rect", new String[] {
140  "x='" + x + "'",
141  "y='" + y + "'",
142  "width='" + width + "'",
143  "height='" + height + "'",
144  "fill='" + fill + "'",
145  "opacity='" + opacity + "'",
146  forceStyle != null ? "style='" + forceStyle + "'" : "",
147  forceTransform != null ? "transform='" + forceTransform + "'" : "",
148  clickElement != null ? "onclick=\"" + clickElement + "\"" : ""
149  });
150  if(!Strings.isBlank(tooltip)) {
151  startTag("n:title").write(StringEscapeUtils.escapeXml11(tooltip)).endTag();
152  }
153  endTag();
154  forceStyle = null;
155  forceTransform = null;
156  clickElement = null;
157  }
158 
159  public void roundRectangle(int x, int y, int width, int height, String fill) {
160  roundRectangle(x, y, width, height, fill, 1);
161  }
162 
163  public void roundRectangle(int x, int y, int width, int height, String fill, double opacity) {
164  roundRectangle(x, y, 5, 5, width, height, fill, opacity);
165  }
166 
167  public void roundRectangle(int x, int y, int width, int height, String fill, double opacity, String tooltip) {
168  roundRectangle(x, y, 5, 5, width, height, fill, opacity, tooltip);
169  }
170 
171  public void roundRectangle(int x, int y, int rx, int ry, int width, int height, String fill, double opacity) {
172  roundRectangle(x, y, rx, ry, width, height, fill, opacity, null);
173  }
174 
175  public void roundRectangle(int x, int y, int rx, int ry, int width, int height, String fill, double opacity, String tooltip) {
176  startTag("n:rect", new String[] {
177  "x='" + x + "'",
178  "y='" + y + "'",
179  "rx='" + rx + "'",
180  "ry='" + ry + "'",
181  "width='" + width + "'",
182  "height='" + height + "'",
183  "fill='" + fill + "'",
184  "opacity='" + opacity + "'",
185  forceStyle != null ? "style='" + forceStyle + "'" : "",
186  forceTransform != null ? "transform='" + forceTransform + "'" : "",
187  clickElement != null ? "onclick=\"" + clickElement + "\"" : ""
188  });
189  if(!Strings.isBlank(tooltip)) {
190  startTag("n:title").write(StringEscapeUtils.escapeXml11(tooltip)).endTag();
191  }
192  endTag();
193  forceStyle = null;
194  forceTransform = null;
195  clickElement = null;
196  }
197 
198  public void text(String text, int x, int y) {
199  text(text, x, y, null);
200  }
201 
202  public void text(String text, int x, int y, String tooltip) {
203  startTag("n:text", new String[] {
204  "x='" + x + "'",
205  "y='" + y + "'",
206  forceStyle != null ? "style='" + forceStyle + "'" : "",
207  forceTransform != null ? "transform='" + forceTransform + "'" : "",
208  clickElement != null ? "onclick=\"" + clickElement + "\"" : ""
209  });
210  if(!Strings.isBlank(tooltip)) {
211  startTag("n:title").write(StringEscapeUtils.escapeXml11(tooltip)).endTag();
212  }
213  write(StringEscapeUtils.escapeXml11(text)).endTag();
214  forceStyle = null;
215  forceTransform = null;
216  clickElement = null;
217  }
218 
219  public void span(int x, int y, int width, int height, String fill, double opacity) {
220  rectangle(x, y, width, height / 2, fill, opacity);
221  rectangle(x, y, height / 2, height, fill, opacity);
222  rectangle(x + width - (height / 2), y, height / 2, height, fill, opacity);
223  }
224 
225  public void roundSpan(int x, int y, int width, int height, String fill, double opacity) {
226  roundRectangle(x, y, width, height / 2, fill, opacity);
227  roundRectangle(x, y, height / 2, height, fill, opacity);
228  roundRectangle(x + width - (height / 2), y, height / 2, height, fill, opacity);
229  }
230 
231  public void shadowedSpan(int x, int y, int width, int height, String fill, double opacity) {
232  clearValues();
233  rectangle(x + 2, y + 2, width, height / 2, "gray", 0.2);
234  restoreValues();
235  rectangle(x, y, width, height / 2, fill, opacity);
236  clearValues();
237  rectangle(x + 2, y + 2, height / 2, height, "gray", 0.2);
238  restoreValues();
239  rectangle(x, y, height / 2, height, fill, opacity);
240  clearValues();
241  rectangle(x + width - (height / 2) + 2, y + 2, height / 2, height, "gray", 0.2);
242  restoreValues();
243  rectangle(x + width - (height / 2), y, height / 2, height, fill, opacity);
244  }
245 
246  public void shadowedRoundSpan(int x, int y, int width, int height, String fill, double opacity) {
247  clearValues();
248  roundRectangle(x + 2, y + 2, width, height / 2, "gray", 0.2);
249  restoreValues();
250  roundRectangle(x, y, width, height / 2, fill, opacity);
251  clearValues();
252  roundRectangle(x + 2, y + 2, height / 2, height, "gray", 0.2);
253  restoreValues();
254  roundRectangle(x, y, height / 2, height, fill, opacity);
255  clearValues();
256  roundRectangle(x + width - (height / 2) + 2, y + 2, height / 2, height, "gray", 0.2);
257  restoreValues();
258  roundRectangle(x + width - (height / 2), y, height / 2, height, fill, opacity);
259  }
260 
261  public void shadowedLine(int x1, int y1, int x2, int y2, String stroke, String strokeWidth, double opacity) {
262  clearValues();
263  line(x1 + 2, y1 + 2, x2 + 2, y2 + 2, stroke, strokeWidth, 0.2);
264  restoreValues();
265  line(x1, y1, x2, y2, stroke, strokeWidth, opacity);
266  }
267 
268  public void shadowedRectangle(int x, int y, int width, int height, String fill, double opacity) {
269  clearValues();
270  rectangle(x + 2, y + 2, width, height, "gray", 0.2);
271  restoreValues();
272  rectangle(x, y, width, height, fill, opacity);
273  }
274 
275  public void shadowedRoundRectangle(int x, int y, int width, int height, String fill, double opacity, String tooltip) {
276  clearValues();
277  roundRectangle(x + 2, y + 2, width, height, "gray", 0.2);
278  restoreValues();
279  roundRectangle(x, y, width, height, fill, opacity, tooltip);
280  }
281 
282  public void setForceStyle(String forceStyle) {
283  this.forceStyle = forceStyle;
284  }
285 
286  public void setForceTransform(String forceTransform) {
287  this.forceTransform = forceTransform;
288  }
289 
290  public void setClickElementData(String clickElementData) {
291  this.clickElement = "zAu.send(new zk.Event(zk.Widget.$('$" + id + "'), 'onUser', '" + clickElementData + "'));";
292  }
293 
295  return htmlGenerator.writeSeparator();
296  }
297 
299  return htmlGenerator.writeNewLine();
300  }
301 
302  public HTMLGenerator writeHorizontalStrut(int pixels) {
303  return htmlGenerator.writeHorizontalStrut(pixels);
304  }
305 
306  public HTMLGenerator write(String value) {
307  return htmlGenerator.write(value);
308  }
309 
310  public HTMLGenerator startTag(String tag, String[] attributes) {
311  return htmlGenerator.startTag(tag, attributes);
312  }
313 
314  public HTMLGenerator startTag(String tag, String attributes) {
315  return htmlGenerator.startTag(tag, attributes);
316  }
317 
318  public HTMLGenerator startTag(String tag) {
319  return htmlGenerator.startTag(tag);
320  }
321 
322  public HTMLGenerator startTableRow(String[] attributes) {
323  return htmlGenerator.startTableRow(attributes);
324  }
325 
326  public HTMLGenerator startTableRow(String attributes) {
327  return htmlGenerator.startTableRow(attributes);
328  }
329 
330  public HTMLGenerator startTableCol(String[] attributes) {
331  return htmlGenerator.startTableCol(attributes);
332  }
333 
334  public HTMLGenerator startTableCol(String attributes) {
335  return htmlGenerator.startTableCol(attributes);
336  }
337 
338  public HTMLGenerator startTable(String[] attributes) {
339  return htmlGenerator.startTable(attributes);
340  }
341 
342  public HTMLGenerator startTable(String attributes) {
343  return htmlGenerator.startTable(attributes);
344  }
345 
347  return htmlGenerator.startJavaScript();
348  }
349 
350  public HTMLGenerator startExtAnchor(String url, String hint) {
351  return htmlGenerator.startExtAnchor(url, hint);
352  }
353 
355  return htmlGenerator.startCSS();
356  }
357 
358  public HTMLGenerator startAnchor(String url, String hint, String cssClass, String onClick) {
359  return htmlGenerator.startAnchor(url, hint, cssClass, onClick);
360  }
361 
362  public HTMLGenerator startAnchor(String url, String hint, String cssClass) {
363  return htmlGenerator.startAnchor(url, hint, cssClass);
364  }
365 
366  public HTMLGenerator startAnchor(String url, String hint) {
367  return htmlGenerator.startAnchor(url, hint);
368  }
369 
370  public void setOut(Writer out) {
371  htmlGenerator.setOut(out);
372  }
373 
374  public HTMLGenerator endTag(String tag) {
375  return htmlGenerator.endTag(tag);
376  }
377 
379  return htmlGenerator.endTag();
380  }
381 
383  return htmlGenerator.endTableRow();
384  }
385 
387  return htmlGenerator.endTableCol();
388  }
389 
391  return htmlGenerator.endTable();
392  }
393 
395  return htmlGenerator.endJavaScript();
396  }
397 
399  return htmlGenerator.endCSS();
400  }
401 
402  public HTMLGenerator endBeforeTag(String tag) {
403  return htmlGenerator.endBeforeTag(tag);
404  }
405 
407  return htmlGenerator.endAllTags();
408  }
409 
410  public HTMLGenerator doTag(String tag, String[] attributes) {
411  return htmlGenerator.doTag(tag, attributes);
412  }
413 
414  public HTMLGenerator doTag(String tag, String attributes) {
415  return htmlGenerator.doTag(tag, attributes);
416  }
417 
418  public HTMLGenerator doTag(String tag) {
419  return htmlGenerator.doTag(tag);
420  }
421 
422  public boolean checkTag(String tag) {
423  return htmlGenerator.checkTag(tag);
424  }
425 
426  public HTMLGenerator addJavaScriptLink(String jsFile) {
427  return htmlGenerator.addJavaScriptLink(jsFile);
428  }
429 
430  public HTMLGenerator addCSSLink(String cssFile) {
431  return htmlGenerator.addCSSLink(cssFile);
432  }
433 
434  private final HashMap<String, String> saved = new HashMap<>();
435 
436  private void clearValues() {
437  saved.put("forceStyle", forceStyle);
438  saved.put("forceTransform", forceTransform);
439  saved.put("clickElement", clickElement);
440  forceStyle = null;
441  clickElement = null;
442  }
443 
444  private void restoreValues() {
445  forceStyle = saved.get("forceStyle");
446  forceTransform = saved.get("forceTransform");
447  clickElement = saved.get("clickElement");
448  saved.clear();
449  }
450 
451 }
HTMLGenerator write(String value)
HTMLGenerator doTag(String tag)
HTMLGenerator writeHorizontalStrut(int pixels)
HTMLGenerator startTag(String tag)
HTMLGenerator startTableRow(String attributes)
boolean checkTag(String tag)
HTMLGenerator addJavaScriptLink(String jsFile)
HTMLGenerator endBeforeTag(String tag)
HTMLGenerator startExtAnchor(String url, String hint)
HTMLGenerator startAnchor(String url, String hint)
HTMLGenerator startTableCol(String attributes)
HTMLGenerator addCSSLink(String cssFile)
HTMLGenerator startTable(String attributes)
void text(String text, int x, int y, String tooltip)
Definition: Svg.java:202
HTMLGenerator endJavaScript()
Definition: Svg.java:394
HTMLGenerator startTableRow(String[] attributes)
Definition: Svg.java:322
HTMLGenerator startAnchor(String url, String hint, String cssClass, String onClick)
Definition: Svg.java:358
void startComposing(String onUserEvent, String width, String height, double scale)
Definition: Svg.java:40
HTMLGenerator writeSeparator()
Definition: Svg.java:294
HTMLGenerator endCSS()
Definition: Svg.java:398
HTMLGenerator endTag(String tag)
Definition: Svg.java:374
void rectangle(int x, int y, int width, int height, String fill, double opacity)
Definition: Svg.java:134
void roundSpan(int x, int y, int width, int height, String fill, double opacity)
Definition: Svg.java:225
HTMLGenerator endAllTags()
Definition: Svg.java:406
HTMLGenerator writeNewLine()
Definition: Svg.java:298
HTMLGenerator startExtAnchor(String url, String hint)
Definition: Svg.java:350
void roundRectangle(int x, int y, int width, int height, String fill, double opacity, String tooltip)
Definition: Svg.java:167
HTMLGenerator startJavaScript()
Definition: Svg.java:346
HTMLGenerator addCSSLink(String cssFile)
Definition: Svg.java:430
HTMLGenerator write(String value)
Definition: Svg.java:306
void span(int x, int y, int width, int height, String fill, double opacity)
Definition: Svg.java:219
HTMLGenerator endTableCol()
Definition: Svg.java:386
HTMLGenerator startCSS()
Definition: Svg.java:354
HTMLGenerator endTable()
Definition: Svg.java:390
void circle(int x, int y, int r, double opacity)
Definition: Svg.java:86
void roundRectangle(int x, int y, int rx, int ry, int width, int height, String fill, double opacity)
Definition: Svg.java:171
HTMLGenerator writeHorizontalStrut(int pixels)
Definition: Svg.java:302
void shadowedRoundSpan(int x, int y, int width, int height, String fill, double opacity)
Definition: Svg.java:246
void shadowedRoundRectangle(int x, int y, int width, int height, String fill, double opacity, String tooltip)
Definition: Svg.java:275
void roundRectangle(int x, int y, int width, int height, String fill, double opacity)
Definition: Svg.java:163
void rectangle(int x, int y, int width, int height, String fill, double opacity, String tooltip)
Definition: Svg.java:138
void circle(int x, int y, int r)
Definition: Svg.java:82
HTMLGenerator endTableRow()
Definition: Svg.java:382
void setForceStyle(String forceStyle)
Definition: Svg.java:282
void setOut(Writer out)
Definition: Svg.java:370
HTMLGenerator startAnchor(String url, String hint)
Definition: Svg.java:366
HTMLGenerator endTag()
Definition: Svg.java:378
void roundRectangle(int x, int y, int rx, int ry, int width, int height, String fill, double opacity, String tooltip)
Definition: Svg.java:175
void roundRectangle(int x, int y, int width, int height, String fill)
Definition: Svg.java:159
HTMLGenerator startTag(String tag, String[] attributes)
Definition: Svg.java:310
void shadowedSpan(int x, int y, int width, int height, String fill, double opacity)
Definition: Svg.java:231
boolean checkTag(String tag)
Definition: Svg.java:422
void setClickElementData(String clickElementData)
Definition: Svg.java:290
void line(int x1, int y1, int x2, int y2, String stroke, String strokeWidth)
Definition: Svg.java:108
HTMLGenerator startTag(String tag, String attributes)
Definition: Svg.java:314
HTMLGenerator startTableRow(String attributes)
Definition: Svg.java:326
HTMLGenerator startTableCol(String attributes)
Definition: Svg.java:334
void setForceTransform(String forceTransform)
Definition: Svg.java:286
void text(String text, int x, int y)
Definition: Svg.java:198
HTMLGenerator doTag(String tag, String attributes)
Definition: Svg.java:414
HTMLGenerator startTag(String tag)
Definition: Svg.java:318
HTMLGenerator startTableCol(String[] attributes)
Definition: Svg.java:330
void shadowedRectangle(int x, int y, int width, int height, String fill, double opacity)
Definition: Svg.java:268
HTMLGenerator doTag(String tag, String[] attributes)
Definition: Svg.java:410
HTMLGenerator startTable(String attributes)
Definition: Svg.java:342
HTMLGenerator startTable(String[] attributes)
Definition: Svg.java:338
void line(int x1, int y1, int x2, int y2, String stroke, String strokeWidth, double opacity)
Definition: Svg.java:112
void rectangle(int x, int y, int width, int height, String fill)
Definition: Svg.java:130
void circle(int x, int y, int r, String stroke, String strokeWidth, String fill, double opacity)
Definition: Svg.java:90
HTMLGenerator startAnchor(String url, String hint, String cssClass)
Definition: Svg.java:362
void generateContent(Map args)
Definition: Svg.java:73
HTMLGenerator addJavaScriptLink(String jsFile)
Definition: Svg.java:426
void shadowedLine(int x1, int y1, int x2, int y2, String stroke, String strokeWidth, double opacity)
Definition: Svg.java:261
HTMLGenerator endBeforeTag(String tag)
Definition: Svg.java:402
HTMLGenerator doTag(String tag)
Definition: Svg.java:418