1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-vyuka/src/java/cz/frantovo/sql/vyuka/dto/Tabulka.java Fri May 29 00:13:11 2009 +0200
1.3 @@ -0,0 +1,74 @@
1.4 +package cz.frantovo.sql.vyuka.dto;
1.5 +
1.6 +import cz.frantovo.sql.vyuka.Html;
1.7 +import java.util.ArrayList;
1.8 +import java.util.Collection;
1.9 +
1.10 +/**
1.11 + * Tabulka, která je výsledkem SQL dotazu.
1.12 + * @author fiki
1.13 + */
1.14 +public class Tabulka implements HtmlObjekt {
1.15 +
1.16 + private String[] zahlavi;
1.17 + private Collection<Object[]> hodnoty = new ArrayList<Object[]>();
1.18 +
1.19 + public String getHtml() {
1.20 +
1.21 + if (getZahlavi() == null || getHodnoty() == null || getZahlavi().length < 1) {
1.22 + return "<p>Chybná tabulka</p>";
1.23 + } else {
1.24 +
1.25 + StringBuffer html = new StringBuffer();
1.26 +
1.27 + html.append("<table>");
1.28 +
1.29 +
1.30 + html.append("<thead title=\"Chceš setřídit výsledek podle nějakého sloupce? Co takhle ORDER BY sloupec.\">");
1.31 + html.append("<tr>");
1.32 + for (String z : getZahlavi()) {
1.33 + html.append("<td>" + Html.escapuj(z) + "</td>");
1.34 + }
1.35 + html.append("</tr>");
1.36 + html.append("</thead>");
1.37 +
1.38 +
1.39 + html.append("<tbody>");
1.40 + for (Object[] hh : getHodnoty()) {
1.41 + for (Object h : hh) {
1.42 + html.append(formatujRadek(h));
1.43 + }
1.44 + }
1.45 + html.append("</tbody>");
1.46 +
1.47 + html.append("</table>");
1.48 +
1.49 +
1.50 + return html.toString();
1.51 + }
1.52 + }
1.53 +
1.54 + private String formatujRadek(Object o) {
1.55 + if (o instanceof Integer) {
1.56 + return "<td class=\"cislo\">" + Html.escapuj(String.valueOf(o)) + "</td>";
1.57 + } else {
1.58 + return "<td>" + Html.escapuj(String.valueOf(o)) + "</td>";
1.59 + }
1.60 + }
1.61 +
1.62 + public String[] getZahlavi() {
1.63 + return zahlavi;
1.64 + }
1.65 +
1.66 + public void setZahlavi(String[] zahlavi) {
1.67 + this.zahlavi = zahlavi;
1.68 + }
1.69 +
1.70 + public Collection<Object[]> getHodnoty() {
1.71 + return hodnoty;
1.72 + }
1.73 +
1.74 + public void setHodnoty(Collection<Object[]> hodnoty) {
1.75 + this.hodnoty = hodnoty;
1.76 + }
1.77 +}