17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.openoffice.spreadsheet;
|
|
|
15 |
|
20 |
ilm |
16 |
import org.openconcerto.openoffice.Log;
|
17 |
ilm |
17 |
import org.openconcerto.openoffice.ODDocument;
|
|
|
18 |
import org.openconcerto.openoffice.ODFrame;
|
|
|
19 |
import org.openconcerto.openoffice.ODValueType;
|
25 |
ilm |
20 |
import org.openconcerto.openoffice.StyleDesc;
|
73 |
ilm |
21 |
import org.openconcerto.openoffice.XMLVersion;
|
17 |
ilm |
22 |
import org.openconcerto.openoffice.spreadsheet.BytesProducer.ByteArrayProducer;
|
|
|
23 |
import org.openconcerto.openoffice.spreadsheet.BytesProducer.ImageProducer;
|
73 |
ilm |
24 |
import org.openconcerto.openoffice.spreadsheet.CellStyle.StyleTableCellProperties;
|
61 |
ilm |
25 |
import org.openconcerto.openoffice.style.data.BooleanStyle;
|
20 |
ilm |
26 |
import org.openconcerto.openoffice.style.data.DataStyle;
|
17 |
ilm |
27 |
import org.openconcerto.utils.FileUtils;
|
73 |
ilm |
28 |
import org.openconcerto.utils.TimeUtils;
|
80 |
ilm |
29 |
import org.openconcerto.utils.TimeUtils.DurationNullsChanger;
|
73 |
ilm |
30 |
import org.openconcerto.utils.Tuple2;
|
25 |
ilm |
31 |
import org.openconcerto.utils.Tuple3;
|
17 |
ilm |
32 |
|
|
|
33 |
import java.awt.Color;
|
|
|
34 |
import java.awt.Image;
|
21 |
ilm |
35 |
import java.awt.Point;
|
17 |
ilm |
36 |
import java.io.File;
|
|
|
37 |
import java.io.IOException;
|
|
|
38 |
import java.text.DateFormat;
|
|
|
39 |
import java.text.DecimalFormat;
|
|
|
40 |
import java.text.NumberFormat;
|
20 |
ilm |
41 |
import java.util.Calendar;
|
17 |
ilm |
42 |
import java.util.Date;
|
|
|
43 |
import java.util.List;
|
61 |
ilm |
44 |
import java.util.Locale;
|
65 |
ilm |
45 |
import java.util.logging.Level;
|
17 |
ilm |
46 |
|
25 |
ilm |
47 |
import javax.xml.datatype.Duration;
|
|
|
48 |
|
17 |
ilm |
49 |
import org.jdom.Attribute;
|
|
|
50 |
import org.jdom.Element;
|
|
|
51 |
import org.jdom.Namespace;
|
|
|
52 |
import org.jdom.Text;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* A cell whose value can be changed.
|
|
|
56 |
*
|
|
|
57 |
* @author Sylvain
|
|
|
58 |
* @param <D> type of document
|
|
|
59 |
*/
|
|
|
60 |
public class MutableCell<D extends ODDocument> extends Cell<D> {
|
|
|
61 |
|
20 |
ilm |
62 |
static private final DateFormat TextPDateFormat = DateFormat.getDateInstance();
|
|
|
63 |
static private final DateFormat TextPTimeFormat = DateFormat.getTimeInstance();
|
25 |
ilm |
64 |
static private final NumberFormat TextPMinuteSecondFormat = new DecimalFormat("00.###");
|
17 |
ilm |
65 |
|
80 |
ilm |
66 |
static private boolean LO_MODE = true;
|
|
|
67 |
// no date part, all time part to zero
|
|
|
68 |
static private final DurationNullsChanger TIME_NULLS = new TimeUtils.DurationNullsBuilder(TimeUtils.EmptyFieldPolicy.SET_TO_ZERO).setToNull(TimeUtils.getDateFields()).build();
|
|
|
69 |
|
|
|
70 |
public static void setTimeValueMode(boolean loMode) {
|
|
|
71 |
LO_MODE = loMode;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Whether {@link #setValue(Object)} formats {@link Duration} using the standard way or using
|
|
|
76 |
* the LibreOffice way. LibreOffice does not support years, months and days in cells ; even set
|
|
|
77 |
* to 0 (it does in user meta fields). The initial value is <code>true</code>.
|
|
|
78 |
*
|
|
|
79 |
* @return <code>true</code> if durations should be formatted the LibO way.
|
|
|
80 |
*/
|
|
|
81 |
public static boolean getTimeValueMode() {
|
|
|
82 |
return LO_MODE;
|
|
|
83 |
}
|
|
|
84 |
|
25 |
ilm |
85 |
MutableCell(Row<D> parent, Element elem, StyleDesc<CellStyle> styleDesc) {
|
|
|
86 |
super(parent, elem, styleDesc);
|
17 |
ilm |
87 |
}
|
|
|
88 |
|
|
|
89 |
// ask our column to our row so we don't have to update anything when columns are removed/added
|
|
|
90 |
public final int getX() {
|
|
|
91 |
return this.getRow().getX(this);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
public final int getY() {
|
|
|
95 |
return this.getRow().getY();
|
|
|
96 |
}
|
|
|
97 |
|
21 |
ilm |
98 |
public final Point getPoint() {
|
|
|
99 |
return new Point(getX(), getY());
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
final void setRowsSpanned(final int rowsSpanned) {
|
|
|
103 |
if (rowsSpanned <= 1)
|
|
|
104 |
this.getElement().removeAttribute("number-rows-spanned", getNS().getTABLE());
|
|
|
105 |
else
|
|
|
106 |
this.getElement().setAttribute("number-rows-spanned", String.valueOf(rowsSpanned), getNS().getTABLE());
|
|
|
107 |
}
|
|
|
108 |
|
17 |
ilm |
109 |
// *** setValue
|
|
|
110 |
|
|
|
111 |
private void setValueAttributes(ODValueType type, Object val) {
|
73 |
ilm |
112 |
final Namespace valueNS = getValueNS();
|
|
|
113 |
final Attribute valueTypeAttr = this.getElement().getAttribute("value-type", valueNS);
|
67 |
ilm |
114 |
// e.g. DATE
|
|
|
115 |
final ODValueType currentType = valueTypeAttr == null ? null : ODValueType.get(valueTypeAttr.getValue());
|
|
|
116 |
|
17 |
ilm |
117 |
if (type == null) {
|
|
|
118 |
if (valueTypeAttr != null) {
|
|
|
119 |
valueTypeAttr.detach();
|
|
|
120 |
}
|
|
|
121 |
} else {
|
73 |
ilm |
122 |
if (!type.equals(currentType)) {
|
|
|
123 |
if (valueTypeAttr != null) {
|
|
|
124 |
valueTypeAttr.setValue(type.getName());
|
|
|
125 |
} else {
|
|
|
126 |
// create an instance of Attribute to avoid a getAttribute() in the simpler
|
|
|
127 |
// setAttribute()
|
|
|
128 |
this.getElement().setAttribute(new Attribute("value-type", type.getName(), valueNS));
|
|
|
129 |
}
|
|
|
130 |
}
|
17 |
ilm |
131 |
}
|
73 |
ilm |
132 |
|
|
|
133 |
// remove old value attribute (assume Element is valid, otherwise we would need to remove
|
|
|
134 |
// all possible value attributes)
|
|
|
135 |
if (currentType != null && (!currentType.equals(type) || type == ODValueType.STRING)) {
|
|
|
136 |
// e.g. @date-value
|
|
|
137 |
this.getElement().removeAttribute(currentType.getValueAttribute(), valueNS);
|
|
|
138 |
}
|
|
|
139 |
// Like LO, do not generate string-value
|
|
|
140 |
if (type != null && type != ODValueType.STRING) {
|
80 |
ilm |
141 |
// LO cells don't support the full syntax of a duration (user meta fields do)
|
|
|
142 |
// instead it support only values without nYnMnD
|
|
|
143 |
if (type == ODValueType.TIME && getTimeValueMode()) {
|
|
|
144 |
final Duration d = val instanceof Duration ? (Duration) val : TimeUtils.timePartToDuration((Calendar) val);
|
|
|
145 |
val = TIME_NULLS.apply(getODDocument().getEpoch().normalizeToHours(d));
|
|
|
146 |
}
|
73 |
ilm |
147 |
this.getElement().setAttribute(type.getValueAttribute(), type.format(val), valueNS);
|
|
|
148 |
}
|
17 |
ilm |
149 |
}
|
|
|
150 |
|
|
|
151 |
private void setTextP(String value) {
|
83 |
ilm |
152 |
if (value == null) {
|
17 |
ilm |
153 |
this.getElement().removeContent();
|
83 |
ilm |
154 |
} else {
|
|
|
155 |
new Lines(this.getODDocument(), value).setText(getElement(), getTextValueMode());
|
17 |
ilm |
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
private void setValue(ODValueType type, Object value, String textP) {
|
132 |
ilm |
160 |
// as in LO, setting a cell value clears the formula
|
|
|
161 |
this.setFormula(null);
|
17 |
ilm |
162 |
this.setValueAttributes(type, value);
|
|
|
163 |
this.setTextP(textP);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public void clearValue() {
|
|
|
167 |
this.setValue(null, null, null);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
public void setValue(Object obj) {
|
25 |
ilm |
171 |
this.setValue(obj, true);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
public void setValue(Object obj, final boolean allowTypeChange) throws UnsupportedOperationException {
|
20 |
ilm |
175 |
final ODValueType type;
|
|
|
176 |
final ODValueType currentType = getValueType();
|
|
|
177 |
// try to keep current type, since for example a Number can work with FLOAT, PERCENTAGE
|
|
|
178 |
// and CURRENCY
|
|
|
179 |
if (currentType != null && currentType.canFormat(obj.getClass())) {
|
|
|
180 |
type = currentType;
|
|
|
181 |
} else {
|
57 |
ilm |
182 |
final ODValueType tmp = ODValueType.forObject(obj);
|
|
|
183 |
// allow any Object
|
|
|
184 |
if (allowTypeChange && tmp == null) {
|
|
|
185 |
type = ODValueType.STRING;
|
|
|
186 |
obj = String.valueOf(obj);
|
|
|
187 |
} else {
|
|
|
188 |
type = tmp;
|
|
|
189 |
}
|
25 |
ilm |
190 |
}
|
|
|
191 |
if (type == null) {
|
20 |
ilm |
192 |
throw new IllegalArgumentException("Couldn't infer type of " + obj);
|
|
|
193 |
}
|
25 |
ilm |
194 |
this.setValue(obj, type, allowTypeChange, true);
|
20 |
ilm |
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Change the value of this cell.
|
|
|
199 |
*
|
|
|
200 |
* @param obj the new cell value.
|
|
|
201 |
* @param vt the value type.
|
25 |
ilm |
202 |
* @param allowTypeChange if <code>true</code> <code>obj</code> and <code>vt</code> might be
|
|
|
203 |
* changed to allow the data style to format, e.g. from Boolean.FALSE to 0.
|
20 |
ilm |
204 |
* @param lenient <code>false</code> to throw an exception if we can't format according to the
|
|
|
205 |
* ODF, <code>true</code> to try best-effort.
|
|
|
206 |
* @throws UnsupportedOperationException if <code>obj</code> couldn't be formatted.
|
|
|
207 |
*/
|
25 |
ilm |
208 |
public void setValue(Object obj, ODValueType vt, final boolean allowTypeChange, final boolean lenient) throws UnsupportedOperationException {
|
20 |
ilm |
209 |
final String text;
|
25 |
ilm |
210 |
final Tuple3<String, ODValueType, Object> formatted = format(obj, vt, !allowTypeChange, lenient);
|
|
|
211 |
vt = formatted.get1();
|
|
|
212 |
obj = formatted.get2();
|
20 |
ilm |
213 |
|
25 |
ilm |
214 |
if (formatted.get0() != null) {
|
|
|
215 |
text = formatted.get0();
|
20 |
ilm |
216 |
} else {
|
|
|
217 |
// either there were no format or formatting failed
|
|
|
218 |
if (vt == ODValueType.FLOAT) {
|
174 |
ilm |
219 |
text = getODDocument().getPackage().formatNumber((Number) obj, getDefaultStyle());
|
20 |
ilm |
220 |
} else if (vt == ODValueType.PERCENTAGE) {
|
174 |
ilm |
221 |
text = getODDocument().getPackage().formatPercent((Number) obj, getDefaultStyle());
|
20 |
ilm |
222 |
} else if (vt == ODValueType.CURRENCY) {
|
174 |
ilm |
223 |
text = getODDocument().getPackage().formatCurrency((Number) obj, getDefaultStyle());
|
20 |
ilm |
224 |
} else if (vt == ODValueType.DATE) {
|
25 |
ilm |
225 |
final Date d;
|
|
|
226 |
if (obj instanceof Calendar) {
|
|
|
227 |
d = ((Calendar) obj).getTime();
|
|
|
228 |
} else {
|
|
|
229 |
d = (Date) obj;
|
|
|
230 |
}
|
|
|
231 |
text = TextPDateFormat.format(d);
|
20 |
ilm |
232 |
} else if (vt == ODValueType.TIME) {
|
25 |
ilm |
233 |
if (obj instanceof Duration) {
|
|
|
234 |
final Duration normalized = getODDocument().getEpoch().normalizeToHours((Duration) obj);
|
73 |
ilm |
235 |
text = "" + normalized.getHours() + ':' + TextPMinuteSecondFormat.format(normalized.getMinutes()) + ':' + TextPMinuteSecondFormat.format(TimeUtils.getSeconds(normalized));
|
25 |
ilm |
236 |
} else {
|
|
|
237 |
text = TextPTimeFormat.format(((Calendar) obj).getTime());
|
|
|
238 |
}
|
20 |
ilm |
239 |
} else if (vt == ODValueType.BOOLEAN) {
|
174 |
ilm |
240 |
Locale l = null;
|
61 |
ilm |
241 |
final CellStyle s = getStyle();
|
|
|
242 |
if (s != null) {
|
|
|
243 |
final DataStyle ds = s.getDataStyle();
|
|
|
244 |
if (ds != null)
|
174 |
ilm |
245 |
l = ds.getLocale();
|
61 |
ilm |
246 |
}
|
174 |
ilm |
247 |
if (l == null)
|
|
|
248 |
l = getODDocument().getPackage().getLocale();
|
61 |
ilm |
249 |
text = BooleanStyle.toString((Boolean) obj, l, lenient);
|
20 |
ilm |
250 |
} else if (vt == ODValueType.STRING) {
|
|
|
251 |
text = obj.toString();
|
19 |
ilm |
252 |
} else {
|
20 |
ilm |
253 |
throw new IllegalStateException(vt + " unknown");
|
19 |
ilm |
254 |
}
|
20 |
ilm |
255 |
}
|
|
|
256 |
this.setValue(vt, obj, text);
|
17 |
ilm |
257 |
}
|
|
|
258 |
|
25 |
ilm |
259 |
// return null String if no data style exists, or if one exists but we couldn't use it
|
|
|
260 |
private Tuple3<String, ODValueType, Object> format(Object obj, ODValueType valueType, boolean onlyCast, boolean lenient) {
|
|
|
261 |
String res = null;
|
20 |
ilm |
262 |
try {
|
25 |
ilm |
263 |
final Tuple3<DataStyle, ODValueType, Object> ds = getDataStyleAndValue(obj, valueType, onlyCast);
|
|
|
264 |
if (ds != null) {
|
|
|
265 |
obj = ds.get2();
|
|
|
266 |
valueType = ds.get1();
|
|
|
267 |
// act like OO, that is if we set a String to a Date cell, change the value and
|
|
|
268 |
// value-type but leave the data-style untouched
|
|
|
269 |
if (ds.get0().canFormat(obj.getClass()))
|
|
|
270 |
res = ds.get0().format(obj, getDefaultStyle(), lenient);
|
|
|
271 |
}
|
20 |
ilm |
272 |
} catch (UnsupportedOperationException e) {
|
|
|
273 |
if (lenient)
|
65 |
ilm |
274 |
Log.get().log(Level.WARNING, "Couldn't format", e);
|
20 |
ilm |
275 |
else
|
|
|
276 |
throw e;
|
|
|
277 |
}
|
25 |
ilm |
278 |
return Tuple3.create(res, valueType, obj);
|
20 |
ilm |
279 |
}
|
|
|
280 |
|
|
|
281 |
public final DataStyle getDataStyle() {
|
25 |
ilm |
282 |
final Tuple3<DataStyle, ODValueType, Object> s = this.getDataStyleAndValue(this.getValue(), this.getValueType(), true);
|
|
|
283 |
return s != null ? s.get0() : null;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
private final Tuple3<DataStyle, ODValueType, Object> getDataStyleAndValue(Object obj, ODValueType valueType, boolean onlyCast) {
|
20 |
ilm |
287 |
final CellStyle s = this.getStyle();
|
73 |
ilm |
288 |
return s != null ? s.getDataStyle(obj, valueType, onlyCast) : null;
|
20 |
ilm |
289 |
}
|
|
|
290 |
|
|
|
291 |
protected final CellStyle getDefaultStyle() {
|
|
|
292 |
return this.getRow().getSheet().getDefaultCellStyle();
|
|
|
293 |
}
|
|
|
294 |
|
17 |
ilm |
295 |
public void replaceBy(String oldValue, String newValue) {
|
|
|
296 |
replaceContentBy(this.getElement(), oldValue, newValue);
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
private void replaceContentBy(Element l, String oldValue, String newValue) {
|
20 |
ilm |
300 |
final List<?> content = l.getContent();
|
17 |
ilm |
301 |
for (int i = 0; i < content.size(); i++) {
|
|
|
302 |
final Object obj = content.get(i);
|
|
|
303 |
if (obj instanceof Text) {
|
|
|
304 |
// System.err.println(" Text --> " + obj.toString());
|
|
|
305 |
final Text t = (Text) obj;
|
|
|
306 |
t.setText(t.getText().replaceAll(oldValue, newValue));
|
|
|
307 |
} else if (obj instanceof Element) {
|
|
|
308 |
replaceContentBy((Element) obj, oldValue, newValue);
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
73 |
ilm |
313 |
/**
|
|
|
314 |
* Set the raw value of the formula attribute.
|
|
|
315 |
*
|
|
|
316 |
* @param formula the raw value, e.g. "of:sum(A1:A2)".
|
|
|
317 |
*/
|
|
|
318 |
public final void setRawFormula(final String formula) {
|
|
|
319 |
// from 19.642 table:formula of OpenDocument-v1.2 : Whenever the initial text of a formula
|
|
|
320 |
// has the appearance of an NCName followed by ":", an OpenDocument producer shall provide a
|
|
|
321 |
// valid namespace prefix in order to eliminate any ambiguity.
|
132 |
ilm |
322 |
final String nsPrefix = formula == null ? null : getFormulaNSPrefix(formula).get0();
|
73 |
ilm |
323 |
if (nsPrefix != null && this.getElement().getNamespace(nsPrefix) == null) {
|
|
|
324 |
throw new IllegalArgumentException("Unknown namespace prefix : " + nsPrefix);
|
|
|
325 |
}
|
|
|
326 |
this.setFormulaNoCheck(formula);
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
private final void setFormulaNoCheck(final String formula) {
|
132 |
ilm |
330 |
if (formula == null)
|
|
|
331 |
this.getElement().removeAttribute("formula", getTABLE());
|
|
|
332 |
else
|
|
|
333 |
this.getElement().setAttribute("formula", formula, getTABLE());
|
73 |
ilm |
334 |
}
|
|
|
335 |
|
|
|
336 |
public final void setFormulaAndNamespace(final Tuple2<Namespace, String> formula) {
|
|
|
337 |
this.setFormulaAndNamespace(formula.get0(), formula.get1());
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
public final void setFormula(final String formula) {
|
|
|
341 |
this.setFormulaAndNamespace(null, formula);
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
public final void setFormulaAndNamespace(final Namespace ns, final String formula) {
|
132 |
ilm |
345 |
if (formula == null) {
|
|
|
346 |
this.setFormulaNoCheck(formula);
|
|
|
347 |
} else if (getODDocument().getVersion() == XMLVersion.OOo) {
|
73 |
ilm |
348 |
if (ns != null)
|
|
|
349 |
throw new IllegalArgumentException("Namespaces not supported by this version : " + ns);
|
|
|
350 |
this.setFormulaNoCheck(formula);
|
|
|
351 |
} else if (ns == null) {
|
|
|
352 |
final String nsPrefix = getFormulaNSPrefix(formula).get0();
|
|
|
353 |
if (nsPrefix != null) {
|
|
|
354 |
// eliminate ambiguity
|
|
|
355 |
final Namespace defaultNS = getDefaultFormulaNS();
|
|
|
356 |
// prevent infinite recursion
|
|
|
357 |
if (defaultNS == null)
|
|
|
358 |
throw new IllegalStateException("Cannot resolve ambiguity, formula appears to begin with " + nsPrefix + " and no default namespace found");
|
|
|
359 |
this.setFormulaAndNamespace(defaultNS, formula);
|
|
|
360 |
} else {
|
|
|
361 |
this.setFormulaNoCheck(formula);
|
|
|
362 |
}
|
|
|
363 |
} else {
|
|
|
364 |
final Namespace existingNS = this.getElement().getNamespace(ns.getPrefix());
|
|
|
365 |
if (existingNS == null)
|
|
|
366 |
this.getElement().getDocument().getRootElement().addNamespaceDeclaration(ns);
|
|
|
367 |
else if (!existingNS.equals(ns))
|
|
|
368 |
throw new IllegalStateException("Namespace conflict : " + existingNS + " != " + ns);
|
|
|
369 |
this.setFormulaNoCheck(ns.getPrefix() + ':' + formula);
|
|
|
370 |
}
|
|
|
371 |
}
|
|
|
372 |
|
17 |
ilm |
373 |
public final void unmerge() {
|
|
|
374 |
// from 8.1.3 Table Cell : table-cell are like covered-table-cell with some extra
|
|
|
375 |
// optional attributes so it's safe to rename covered cells into normal ones
|
|
|
376 |
final int x = this.getX();
|
|
|
377 |
final int y = this.getY();
|
|
|
378 |
final int columnsSpanned = getColumnsSpanned();
|
|
|
379 |
final int rowsSpanned = getRowsSpanned();
|
|
|
380 |
for (int i = 0; i < columnsSpanned; i++) {
|
|
|
381 |
for (int j = 0; j < rowsSpanned; j++) {
|
|
|
382 |
// don't mind if we change us at 0,0 we're already a table-cell
|
|
|
383 |
this.getRow().getSheet().getImmutableCellAt(x + i, y + j).getElement().setName("table-cell");
|
|
|
384 |
}
|
|
|
385 |
}
|
|
|
386 |
this.getElement().removeAttribute("number-columns-spanned", getNS().getTABLE());
|
|
|
387 |
this.getElement().removeAttribute("number-rows-spanned", getNS().getTABLE());
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
/**
|
|
|
391 |
* Merge this cell and the following ones. If this cell already spanned multiple columns/rows
|
|
|
392 |
* this method un-merge any additional cells.
|
|
|
393 |
*
|
|
|
394 |
* @param columnsSpanned number of columns to merge.
|
|
|
395 |
* @param rowsSpanned number of rows to merge.
|
|
|
396 |
*/
|
|
|
397 |
public final void merge(final int columnsSpanned, final int rowsSpanned) {
|
|
|
398 |
final int currentCols = this.getColumnsSpanned();
|
|
|
399 |
final int currentRows = this.getRowsSpanned();
|
|
|
400 |
|
|
|
401 |
// nothing to do
|
|
|
402 |
if (columnsSpanned == currentCols && rowsSpanned == currentRows)
|
|
|
403 |
return;
|
|
|
404 |
|
|
|
405 |
final int x = this.getX();
|
|
|
406 |
final int y = this.getY();
|
|
|
407 |
|
|
|
408 |
// check for problems before any modifications
|
|
|
409 |
for (int i = 0; i < columnsSpanned; i++) {
|
|
|
410 |
for (int j = 0; j < rowsSpanned; j++) {
|
|
|
411 |
final boolean coveredByThis = i < currentCols && j < currentRows;
|
|
|
412 |
if (!coveredByThis) {
|
|
|
413 |
final int x2 = x + i;
|
|
|
414 |
final int y2 = y + j;
|
|
|
415 |
final Cell<D> immutableCell = this.getRow().getSheet().getImmutableCellAt(x2, y2);
|
|
|
416 |
// check for overlapping range from inside
|
|
|
417 |
if (immutableCell.coversOtherCells())
|
|
|
418 |
throw new IllegalArgumentException("Cell at " + x2 + "," + y2 + " is a merged cell.");
|
|
|
419 |
// and outside
|
|
|
420 |
if (immutableCell.getElement().getName().equals("covered-table-cell"))
|
|
|
421 |
throw new IllegalArgumentException("Cell at " + x2 + "," + y2 + " is already covered.");
|
|
|
422 |
}
|
|
|
423 |
}
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
final boolean shrinks = columnsSpanned < currentCols || rowsSpanned < currentRows;
|
|
|
427 |
if (shrinks)
|
|
|
428 |
this.unmerge();
|
|
|
429 |
|
|
|
430 |
// from 8.1.3 Table Cell : table-cell are like covered-table-cell with some extra
|
|
|
431 |
// optional attributes so it's safe to rename
|
|
|
432 |
for (int i = 0; i < columnsSpanned; i++) {
|
|
|
433 |
for (int j = 0; j < rowsSpanned; j++) {
|
|
|
434 |
final boolean coveredByThis = i < currentCols && j < currentRows;
|
|
|
435 |
// don't cover this,
|
|
|
436 |
// if we grow the current covered cells are invalid so don't try to access them
|
|
|
437 |
if ((i != 0 || j != 0) && (shrinks || !coveredByThis))
|
|
|
438 |
// MutableCell is needed to break repeated
|
|
|
439 |
this.getRow().getSheet().getCellAt(x + i, y + j).getElement().setName("covered-table-cell");
|
|
|
440 |
}
|
|
|
441 |
}
|
|
|
442 |
this.getElement().setAttribute("number-columns-spanned", columnsSpanned + "", getNS().getTABLE());
|
|
|
443 |
this.getElement().setAttribute("number-rows-spanned", rowsSpanned + "", getNS().getTABLE());
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
@Override
|
|
|
447 |
public final String getStyleName() {
|
|
|
448 |
return this.getRow().getSheet().getStyleNameAt(this.getX(), this.getY());
|
|
|
449 |
}
|
|
|
450 |
|
73 |
ilm |
451 |
public final StyleTableCellProperties getTableCellProperties() {
|
|
|
452 |
return this.getRow().getSheet().getTableCellPropertiesAt(this.getX(), this.getY());
|
|
|
453 |
}
|
|
|
454 |
|
17 |
ilm |
455 |
public void setImage(final File pic) throws IOException {
|
|
|
456 |
this.setImage(pic, false);
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
public void setImage(final File pic, boolean keepRatio) throws IOException {
|
|
|
460 |
this.setImage(pic.getName(), new ByteArrayProducer(FileUtils.readBytes(pic), keepRatio));
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
public void setImage(final String name, final Image img) throws IOException {
|
|
|
464 |
this.setImage(name, img == null ? null : new ImageProducer(img, true));
|
|
|
465 |
}
|
|
|
466 |
|
|
|
467 |
private void setImage(final String name, final BytesProducer data) {
|
|
|
468 |
final Namespace draw = this.getNS().getNS("draw");
|
|
|
469 |
final Element frame = this.getElement().getChild("frame", draw);
|
|
|
470 |
final Element imageElem = frame == null ? null : frame.getChild("image", draw);
|
|
|
471 |
|
|
|
472 |
if (imageElem != null) {
|
|
|
473 |
final Attribute refAttr = imageElem.getAttribute("href", this.getNS().getNS("xlink"));
|
|
|
474 |
this.getODDocument().getPackage().putFile(refAttr.getValue(), null);
|
|
|
475 |
|
|
|
476 |
if (data == null)
|
|
|
477 |
frame.detach();
|
|
|
478 |
else {
|
|
|
479 |
refAttr.setValue("Pictures/" + name + (data.getFormat() != null ? "." + data.getFormat() : ""));
|
|
|
480 |
this.getODDocument().getPackage().putFile(refAttr.getValue(), data.getBytes(new ODFrame<D>(getODDocument(), frame)));
|
|
|
481 |
}
|
|
|
482 |
} else if (data != null)
|
|
|
483 |
throw new IllegalStateException("this cell doesn't contain an image: " + this);
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
public final void setBackgroundColor(final Color color) {
|
73 |
ilm |
487 |
this.getPrivateStyle().getTableCellProperties(this).setBackgroundColor(color);
|
17 |
ilm |
488 |
}
|
|
|
489 |
}
|