source: osm/applications/editors/josm/plugins/opendata/includes/org/jopendocument/dom/spreadsheet/Table.java@ 28000

Last change on this file since 28000 was 28000, checked in by donvip, 12 years ago

Import new "opendata" JOSM plugin

File size: 4.7 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of the GNU
7 * General Public License Version 3 only ("GPL").
8 * You may not use this file except in compliance with the License.
9 * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
10 * See the License for the specific language governing permissions and limitations under the License.
11 *
12 * When distributing the software, include this License Header Notice in each file.
13 *
14 */
15
16package org.jopendocument.dom.spreadsheet;
17
18import java.util.ArrayList;
19import java.util.List;
20import java.util.ListIterator;
21
22import org.jdom.Attribute;
23import org.jdom.Element;
24import org.jopendocument.dom.ODDocument;
25import org.jopendocument.util.Tuple2;
26
27/**
28 * A single sheet in a spreadsheet.
29 *
30 * @author Sylvain
31 * @param <D> type of table parent
32 */
33public class Table<D extends ODDocument> extends TableCalcNode<TableStyle, D> {
34
35 static final String getName(final Element elem) {
36 return elem.getAttributeValue("name", elem.getNamespace("table"));
37 }
38
39 // ATTN Row have their index as attribute
40 private final List<Row<D>> rows;
41 private final List<Column<D>> cols;
42
43 public Table(D parent, Element local) {
44 super(parent, local, TableStyle.class);
45
46 this.rows = new ArrayList<Row<D>>();
47 this.cols = new ArrayList<Column<D>>();
48
49 this.readColumns();
50 this.readRows();
51 }
52
53 private void readColumns() {
54 this.read(true);
55 }
56
57 private final void readRows() {
58 this.read(false);
59 }
60
61 private final void read(final boolean col) {
62 final Tuple2<List<Element>, Integer> r = flatten(col);
63 (col ? this.cols : this.rows).clear();
64 for (final Element clone : r.get0()) {
65 if (col)
66 this.addCol(clone);
67 else
68 this.addRow(clone);
69 }
70 }
71
72 private final void addCol(Element clone) {
73 this.cols.add(new Column<D>(this, clone));
74 }
75
76 private Tuple2<List<Element>, Integer> flatten(boolean col) {
77 final List<Element> res = new ArrayList<Element>();
78 final Element header = this.getElement().getChild("table-header-" + getName(col) + "s", getTABLE());
79 if (header != null)
80 res.addAll(flatten(header, col));
81 final int headerCount = res.size();
82
83 res.addAll(flatten(getElement(), col));
84
85 return Tuple2.create(res, headerCount);
86 }
87
88 @SuppressWarnings("unchecked")
89 private List<Element> flatten(final Element elem, boolean col) {
90 final String childName = getName(col);
91 final List<Element> children = elem.getChildren("table-" + childName, getTABLE());
92 // not final, since iter.add() does not work consistently, and
93 // thus we must recreate an iterator each time
94 ListIterator<Element> iter = children.listIterator();
95 while (iter.hasNext()) {
96 final Element row = iter.next();
97 final Attribute repeatedAttr = row.getAttribute("number-" + childName + "s-repeated", getTABLE());
98 if (repeatedAttr != null) {
99 row.removeAttribute(repeatedAttr);
100 final int index = iter.previousIndex();
101 int repeated = Integer.parseInt(repeatedAttr.getValue());
102 if (repeated > 60000) {
103 repeated = 10;
104 }
105 // -1 : we keep the original row
106 for (int i = 0; i < repeated - 1; i++) {
107 final Element clone = (Element) row.clone();
108 // cannot use iter.add() since on JDOM 1.1 if row is the last table-column
109 // before table-row the clone is added at the very end
110 children.add(index, clone);
111 }
112 // restart after the added rows
113 iter = children.listIterator(index + repeated);
114 }
115 }
116
117 return children;
118 }
119
120 public void detach() {
121 this.getElement().detach();
122 }
123
124 private final String getName(boolean col) {
125 return col ? "column" : "row";
126 }
127
128
129 private synchronized void addRow(Element child) {
130 this.rows.add(new Row<D>(this, child, this.rows.size()));
131 }
132
133
134 // *** get count
135
136 public final Column<D> getColumn(int i) { // NO_UCD
137 return this.cols.get(i);
138 }
139
140 public final int getRowCount() { // NO_UCD
141 return this.rows.size();
142 }
143
144 public final int getColumnCount() { // NO_UCD
145 return this.cols.size();
146 }
147}
Note: See TracBrowser for help on using the repository browser.