Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * The Contents of this file are made available subject to the terms of
5 : * either of the following licenses
6 : *
7 : * - GNU Lesser General Public License Version 2.1
8 : * - Sun Industry Standards Source License Version 1.1
9 : *
10 : * Sun Microsystems Inc., October, 2000
11 : *
12 : * GNU Lesser General Public License Version 2.1
13 : * =============================================
14 : * Copyright 2000 by Sun Microsystems, Inc.
15 : * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 : *
17 : * This library is free software; you can redistribute it and/or
18 : * modify it under the terms of the GNU Lesser General Public
19 : * License version 2.1, as published by the Free Software Foundation.
20 : *
21 : * This library is distributed in the hope that it will be useful,
22 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : * Lesser General Public License for more details.
25 : *
26 : * You should have received a copy of the GNU Lesser General Public
27 : * License along with this library; if not, write to the Free Software
28 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 : * MA 02111-1307 USA
30 : *
31 : *
32 : * Sun Industry Standards Source License Version 1.1
33 : * =================================================
34 : * The contents of this file are subject to the Sun Industry Standards
35 : * Source License Version 1.1 (the "License"); You may not use this file
36 : * except in compliance with the License. You may obtain a copy of the
37 : * License at http://www.openoffice.org/license.html.
38 : *
39 : * Software provided under this License is provided on an "AS IS" basis,
40 : * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 : * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 : * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 : * See the License for the specific provisions governing your rights and
44 : * obligations concerning the Software.
45 : *
46 : * The Initial Developer of the Original Code is: IBM Corporation
47 : *
48 : * Copyright: 2008 by IBM Corporation
49 : *
50 : * All Rights Reserved.
51 : *
52 : * Contributor(s): _______________________________________
53 : *
54 : *
55 : ************************************************************************/
56 : /*************************************************************************
57 : * @file
58 : * Table object.
59 : ************************************************************************/
60 : #include "xftable.hxx"
61 : #include "xfrow.hxx"
62 : #include "xfcolstyle.hxx"
63 : #include "xfstylemanager.hxx"
64 : #include <cassert>
65 :
66 1 : XFTable::XFTable()
67 : {
68 1 : m_strName = XFGlobal::GenTableName();
69 1 : m_bSubTable = sal_False;
70 1 : m_pOwnerCell = NULL;
71 1 : }
72 :
73 0 : XFTable::XFTable(const XFTable& other):XFContent(other)
74 : {
75 0 : m_strName = other.m_strName;
76 0 : m_bSubTable = other.m_bSubTable;
77 0 : m_pOwnerCell = NULL;
78 0 : for( int i=1; i<=other.m_aHeaderRows.GetCount(); i++ )
79 : {
80 0 : XFRow *pRow = (XFRow*)other.m_aHeaderRows.GetContent(i+1);
81 0 : if( pRow )
82 : {
83 0 : m_aHeaderRows.Add( new XFRow(*pRow) );
84 : }
85 : }
86 :
87 0 : m_aColumns = other.m_aColumns;
88 0 : m_strDefCellStyle = other.m_strDefCellStyle;
89 0 : m_strDefRowStyle = other.m_strDefRowStyle;
90 0 : m_strDefColStyle = other.m_strDefColStyle;
91 0 : }
92 :
93 0 : XFTable& XFTable::operator =(const XFTable& other)
94 : {
95 0 : m_strName = other.m_strName;
96 0 : m_bSubTable = other.m_bSubTable;
97 0 : m_pOwnerCell = NULL;
98 0 : for( int i=1; i<=other.m_aHeaderRows.GetCount(); i++ )
99 : {
100 0 : XFRow *pRow = (XFRow*)other.m_aHeaderRows.GetContent(i);
101 0 : if( pRow )
102 : {
103 0 : m_aHeaderRows.Add( new XFRow(*pRow) );
104 : }
105 : }
106 :
107 0 : m_aColumns = other.m_aColumns;
108 0 : m_strDefCellStyle = other.m_strDefCellStyle;
109 0 : m_strDefRowStyle = other.m_strDefRowStyle;
110 0 : m_strDefColStyle = other.m_strDefColStyle;
111 0 : return *this;
112 : }
113 :
114 :
115 :
116 3 : XFTable::~XFTable()
117 : {
118 1 : std::map<sal_Int32,XFRow*>::iterator it;
119 6 : for( it=m_aRows.begin(); it!=m_aRows.end(); ++it )
120 : {
121 5 : XFRow *pRow = (*it).second;
122 5 : if( pRow )
123 5 : delete pRow;
124 : }
125 1 : m_aRows.clear();
126 1 : m_aColumns.clear();
127 2 : }
128 :
129 4 : void XFTable::SetColumnStyle(sal_Int32 col, rtl::OUString style)
130 : {
131 4 : m_aColumns[col] = style;
132 4 : }
133 :
134 5 : void XFTable::AddRow(XFRow *pRow)
135 : {
136 : assert(pRow);
137 5 : if( !pRow )
138 5 : return;
139 :
140 5 : int row = pRow->GetRow();
141 :
142 5 : if( row<1 )
143 5 : pRow->SetRow(m_aRows.size()+1);
144 :
145 5 : row = pRow->GetRow();
146 5 : if( m_aRows.find(row) != m_aRows.end() )
147 0 : delete m_aRows[row];
148 :
149 5 : pRow->SetOwnerTable(this);
150 5 : m_aRows[row] = pRow;
151 : }
152 :
153 0 : void XFTable::AddHeaderRow(XFRow *pRow)
154 : {
155 0 : if( !pRow )
156 0 : return;
157 0 : m_aHeaderRows.Add(pRow);
158 : }
159 :
160 0 : rtl::OUString XFTable::GetTableName()
161 : {
162 0 : if( m_bSubTable )
163 : {
164 0 : return m_pOwnerCell->GetCellName();
165 : }
166 : else
167 0 : return m_strName;
168 : }
169 :
170 0 : sal_Int32 XFTable::GetRowCount()
171 : {
172 0 : sal_Int32 rowMax = -1;
173 0 : std::map<sal_Int32,XFRow*>::iterator it;
174 0 : for( it=m_aRows.begin(); it!=m_aRows.end(); ++it )
175 : {
176 0 : if( it->first>rowMax )
177 0 : rowMax = it->first;
178 : }
179 :
180 0 : return rowMax;
181 : }
182 :
183 0 : XFRow* XFTable::GetRow(sal_Int32 row)
184 : {
185 0 : return m_aRows[row];
186 : }
187 :
188 0 : sal_Int32 XFTable::GetColumnCount()
189 : {
190 0 : int colMax = -1;
191 0 : std::map<sal_Int32,rtl::OUString>::iterator it;
192 0 : for( it=m_aColumns.begin(); it!=m_aColumns.end(); ++it )
193 : {
194 0 : if( it->first>colMax )
195 0 : colMax = it->first;
196 : }
197 0 : return colMax;
198 : }
199 :
200 0 : enumXFContent XFTable::GetContentType()
201 : {
202 0 : return enumXFContentTable;
203 : }
204 :
205 1 : void XFTable::ToXml(IXFStream *pStrm)
206 : {
207 1 : IXFAttrList *pAttrList = pStrm->GetAttrList();
208 :
209 1 : pAttrList->Clear();
210 : //sub table shouldn't use table name.
211 1 : if( !m_bSubTable )
212 1 : pAttrList->AddAttribute( A2OUSTR("table:name"), m_strName);
213 :
214 1 : if( !GetStyleName().isEmpty() )
215 1 : pAttrList->AddAttribute( A2OUSTR("table:style-name"), GetStyleName() );
216 :
217 1 : if( m_bSubTable )
218 0 : pStrm->StartElement( A2OUSTR("table:sub-table") );
219 : else
220 1 : pStrm->StartElement( A2OUSTR("table:table") );
221 :
222 : //test code
223 : // sal_Int32 rowMax = GetRowCount();
224 : // sal_Int32 colMax = GetColumnCount();
225 : //output columns:
226 : {
227 1 : int lastCol = 0;
228 1 : std::map<sal_Int32,rtl::OUString>::iterator it;
229 5 : for( it=m_aColumns.begin(); it!=m_aColumns.end(); ++it )
230 : {
231 4 : sal_Int32 col = (*it).first;
232 4 : rtl::OUString style = m_aColumns[col];
233 :
234 : //default col repeated:
235 4 : if( col >lastCol+1 )
236 : {
237 0 : if( col > lastCol + 2 )
238 : {
239 0 : if( !m_strDefColStyle.isEmpty() )
240 : {
241 0 : pAttrList->AddAttribute( A2OUSTR("table:style-name"), m_strDefColStyle );
242 : }
243 0 : pAttrList->AddAttribute( A2OUSTR("table:number-columns-repeated"), Int32ToOUString(col-lastCol-1) );
244 : }
245 0 : pStrm->StartElement( A2OUSTR("table:table-column") );
246 0 : pStrm->EndElement( A2OUSTR("table:table-column") );
247 : }
248 :
249 4 : if( !style.isEmpty() )
250 : {
251 4 : pAttrList->AddAttribute( A2OUSTR("table:style-name"), style );
252 : }
253 4 : pStrm->StartElement( A2OUSTR("table:table-column") );
254 4 : pStrm->EndElement( A2OUSTR("table:table-column") );
255 4 : lastCol = col;
256 4 : }
257 : }
258 :
259 1 : if( m_aHeaderRows.GetCount()>0 )
260 : {
261 0 : pStrm->StartElement( A2OUSTR("table:table-header-rows") );
262 0 : m_aHeaderRows.ToXml(pStrm);
263 0 : pStrm->EndElement( A2OUSTR("table:table-header-rows") );
264 : }
265 : //output rows:
266 : {
267 1 : int lastRow = 0;
268 1 : pAttrList = pStrm->GetAttrList();
269 :
270 1 : std::map<sal_Int32,XFRow* >::iterator it = m_aRows.begin();
271 6 : for( ; it!=m_aRows.end(); ++it )
272 : {
273 5 : int row = (*it).first;
274 5 : XFRow *pRow = (*it).second;
275 :
276 : //null row repeated:
277 5 : if( row>lastRow+1 )
278 : {
279 0 : XFRow *pNullRow = new XFRow();
280 0 : pNullRow->SetStyleName(m_strDefRowStyle);
281 0 : if( row>lastRow+2)
282 0 : pNullRow->SetRepeated(row-lastRow-1);
283 0 : XFCell *pCell = new XFCell();
284 0 : pCell->SetStyleName(m_strDefCellStyle);
285 0 : pNullRow->AddCell(pCell);
286 0 : pNullRow->ToXml(pStrm);
287 : }
288 5 : pRow->ToXml(pStrm);
289 5 : lastRow = row;
290 : }
291 : }
292 :
293 1 : if( m_bSubTable )
294 0 : pStrm->EndElement( A2OUSTR("table:sub-table") );
295 : else
296 1 : pStrm->EndElement( A2OUSTR("table:table") );
297 1 : }
298 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|