java/sql-vyuka/src/java/cz/frantovo/sql/vyuka/dto/Tabulka.java
author František Kučera <franta-hg@frantovo.cz>
Fri May 29 00:13:11 2009 +0200 (2009-05-29)
changeset 12 1b10a6565e8c
child 13 96e711f3ef48
permissions -rw-r--r--
Propojení javascriptu a servletu.
     1 package cz.frantovo.sql.vyuka.dto;
     2 
     3 import cz.frantovo.sql.vyuka.Html;
     4 import java.util.ArrayList;
     5 import java.util.Collection;
     6 
     7 /**
     8  * Tabulka, která je výsledkem SQL dotazu.
     9  * @author fiki
    10  */
    11 public class Tabulka implements HtmlObjekt {
    12 
    13     private String[] zahlavi;
    14     private Collection<Object[]> hodnoty = new ArrayList<Object[]>();
    15 
    16     public String getHtml() {
    17 
    18         if (getZahlavi() == null || getHodnoty() == null || getZahlavi().length < 1) {
    19             return "<p>Chybná tabulka</p>";
    20         } else {
    21 
    22             StringBuffer html = new StringBuffer();
    23 
    24             html.append("<table>");
    25 
    26 
    27             html.append("<thead title=\"Chceš setřídit výsledek podle nějakého sloupce? Co takhle ORDER BY sloupec.\">");
    28             html.append("<tr>");
    29             for (String z : getZahlavi()) {
    30                 html.append("<td>" + Html.escapuj(z) + "</td>");
    31             }
    32             html.append("</tr>");
    33             html.append("</thead>");
    34 
    35 
    36             html.append("<tbody>");
    37             for (Object[] hh : getHodnoty()) {
    38                 for (Object h : hh) {
    39                     html.append(formatujRadek(h));
    40                 }
    41             }
    42             html.append("</tbody>");
    43 
    44             html.append("</table>");
    45 
    46 
    47             return html.toString();
    48         }
    49     }
    50 
    51     private String formatujRadek(Object o) {
    52         if (o instanceof Integer) {
    53             return "<td class=\"cislo\">" + Html.escapuj(String.valueOf(o)) + "</td>";
    54         } else {
    55             return "<td>" + Html.escapuj(String.valueOf(o)) + "</td>";
    56         }
    57     }
    58 
    59     public String[] getZahlavi() {
    60         return zahlavi;
    61     }
    62 
    63     public void setZahlavi(String[] zahlavi) {
    64         this.zahlavi = zahlavi;
    65     }
    66 
    67     public Collection<Object[]> getHodnoty() {
    68         return hodnoty;
    69     }
    70 
    71     public void setHodnoty(Collection<Object[]> hodnoty) {
    72         this.hodnoty = hodnoty;
    73     }
    74 }