KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xtpdoc > Table


1 /*
2  * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.xtpdoc;
31
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamWriter;
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 public class Table extends Node implements ContentItem {
39   private static int _count = 0;
40
41   private int _myCount = _count++;
42   private Document _document;
43   protected String JavaDoc _title;
44   protected int _columns = 0;
45   protected ArrayList JavaDoc<TableRow> _rows = new ArrayList JavaDoc<TableRow>();
46
47   public Table(Document document)
48   {
49     _document = document;
50   }
51
52   public void setTitle(String JavaDoc title)
53   {
54     _title = title;
55   }
56
57   public TableRow createTr()
58   {
59     TableRow row = new TableRow(_document);
60     _rows.add(row);
61     return row;
62   }
63
64   public void writeHtml(XMLStreamWriter out)
65     throws XMLStreamException
66   {
67     out.writeStartElement("table");
68
69     for (TableRow row : _rows)
70       row.writeHtml(out);
71
72     out.writeEndElement();
73   }
74
75   protected void writeRows(PrintWriter JavaDoc out)
76     throws IOException JavaDoc
77   {
78     for (TableRow row : _rows)
79       row.writeLaTeX(out);
80   }
81
82   public void writeLaTeX(PrintWriter JavaDoc out)
83     throws IOException JavaDoc
84   {
85     for (TableRow row : _rows)
86       _columns = Math.max(_columns, row.getNumberOfColumns());
87
88     out.println("\\begin{filecontents}{ltx" + _myCount + ".tex}");
89     out.print("\\begin{longtable}");
90
91     out.print("{");
92
93     for (int i = 0; i < _columns; i++)
94       out.print("X");
95
96     out.println("}");
97
98     writeRows(out);
99
100     out.println("\\end{longtable}");
101     out.println("\\end{filecontents}");
102
103
104     out.println("\\begin{center}");
105
106     out.println("\\LTXtable{\\linewidth}{ltx" + _myCount + "}");
107
108     if (_title != null)
109       out.println("\\textbf{" + LaTeXUtil.escapeForLaTeX(_title) + "}");
110
111     out.println("\\end{center}");
112   }
113
114   public void writeLaTeXEnclosed(PrintWriter JavaDoc out)
115     throws IOException JavaDoc
116   {
117     writeLaTeX(out);
118   }
119
120   public void writeLaTeXTop(PrintWriter JavaDoc out)
121     throws IOException JavaDoc
122   {
123     writeLaTeX(out);
124   }
125 }
126
Popular Tags