OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.openoffice.spreadsheet;

import static org.openconcerto.utils.TimeUtils.SECONDS_PER_HOUR;
import static org.openconcerto.utils.TimeUtils.SECONDS_PER_MINUTE;

import org.openconcerto.openoffice.LengthUnit;
import org.openconcerto.openoffice.Log;
import org.openconcerto.openoffice.ODDocument;
import org.openconcerto.openoffice.ODFrame;
import org.openconcerto.openoffice.ODPackage;
import org.openconcerto.openoffice.ODValueType;
import org.openconcerto.openoffice.StyleDesc;
import org.openconcerto.openoffice.XMLVersion;
import org.openconcerto.openoffice.spreadsheet.BytesProducer.ByteArrayProducer;
import org.openconcerto.openoffice.spreadsheet.BytesProducer.ImageProducer;
import org.openconcerto.openoffice.spreadsheet.CellStyle.StyleTableCellProperties;
import org.openconcerto.openoffice.style.data.BooleanStyle;
import org.openconcerto.openoffice.style.data.DataStyle;
import org.openconcerto.utils.FileUtils;
import org.openconcerto.utils.TimeUtils;
import org.openconcerto.utils.TimeUtils.DurationNullsChanger;
import org.openconcerto.utils.Tuple2;
import org.openconcerto.utils.Tuple3;
import org.openconcerto.utils.cache.LRUMap;
import org.openconcerto.utils.cc.CachedTransformer;

import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;

import javax.xml.datatype.Duration;

import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.Text;

/**
 * A cell whose value can be changed.
 * 
 * @author Sylvain
 * @param <D> type of document
 */
public class MutableCell<D extends ODDocument> extends Cell<D> {

    static private final CachedTransformer<Locale, DateFormat, RuntimeException> TextPDateFormat = new CachedTransformer<>(new LRUMap<>(20), (l) -> DateFormat.getDateInstance(DateFormat.DEFAULT, l),
            false);
    static private final CachedTransformer<Locale, DateFormat, RuntimeException> TextPTimeFormat = new CachedTransformer<>(new LRUMap<>(20), (l) -> DateFormat.getTimeInstance(DateFormat.DEFAULT, l),
            false);
    static private final CachedTransformer<Locale, NumberFormat, RuntimeException> TextPMinuteSecondFormat = new CachedTransformer<>(new LRUMap<>(20),
            (l) -> new DecimalFormat("00.###", DecimalFormatSymbols.getInstance(l)), false);
    static private final char TEXTP_SEP = ':';

    // HH:mm:ss.SSS
    static String textPDuration(final long hours, final int minutes, final BigDecimal secsAndNanos, final Locale locale) {
        final StringBuilder res = new StringBuilder(16);
        res.append(hours);
        res.append(TEXTP_SEP);
        res.append(TextPMinuteSecondFormat.get(locale).format(minutes));
        res.append(TEXTP_SEP);
        res.append(TextPMinuteSecondFormat.get(locale).format(secsAndNanos));
        return res.toString();
    }

    static String textPDuration(final java.time.Duration d, final Locale locale) {
        // -1H is treated as 23:00 in LO
        if (d.isNegative())
            throw new UnsupportedOperationException("Negative duration");

        final long seconds = d.getSeconds();
        // from Duration.toString()
        final long hours = seconds / SECONDS_PER_HOUR;
        final int minutes = (int) ((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
        final int secs = (int) (seconds % SECONDS_PER_MINUTE);
        final BigDecimal secsAndNanos = BigDecimal.valueOf(secs).add(BigDecimal.valueOf(d.getNano()).movePointLeft(9));

        return textPDuration(hours, minutes, secsAndNanos, locale);
    }

    static private boolean LO_MODE = true;
    // no date part, all time part to zero
    static private final DurationNullsChanger TIME_NULLS = new TimeUtils.DurationNullsBuilder(TimeUtils.EmptyFieldPolicy.SET_TO_ZERO).setToNull(TimeUtils.getDateFields()).build();

    public static void setTimeValueMode(boolean loMode) {
        LO_MODE = loMode;
    }

    /**
     * Whether {@link #setValue(Object)} formats {@link Duration} using the standard way or using
     * the LibreOffice way. LibreOffice does not support years, months and days in cells ; even set
     * to 0 (it does in user meta fields). The initial value is <code>true</code>.
     * 
     * @return <code>true</code> if durations should be formatted the LibO way.
     */
    public static boolean getTimeValueMode() {
        return LO_MODE;
    }

    MutableCell(Row<D> parent, Element elem, StyleDesc<CellStyle> styleDesc) {
        super(parent, elem, styleDesc);
    }

    // ask our column to our row so we don't have to update anything when columns are removed/added
    public final int getX() {
        return this.getRow().getX(this);
    }

    public final int getY() {
        return this.getRow().getY();
    }

    public final Point getPoint() {
        return new Point(getX(), getY());
    }

    final void setRowsSpanned(final int rowsSpanned) {
        if (rowsSpanned <= 1)
            this.getElement().removeAttribute("number-rows-spanned", getNS().getTABLE());
        else
            this.getElement().setAttribute("number-rows-spanned", String.valueOf(rowsSpanned), getNS().getTABLE());
    }

    // *** setValue

    private void setValueAttributes(ODValueType type, Object val) {
        final Namespace valueNS = getValueNS();
        final Attribute valueTypeAttr = this.getElement().getAttribute("value-type", valueNS);
        // e.g. DATE
        final ODValueType currentType = valueTypeAttr == null ? null : ODValueType.get(valueTypeAttr.getValue());

        if (type == null) {
            if (valueTypeAttr != null) {
                valueTypeAttr.detach();
            }
        } else {
            if (!type.equals(currentType)) {
                if (valueTypeAttr != null) {
                    valueTypeAttr.setValue(type.getName());
                } else {
                    // create an instance of Attribute to avoid a getAttribute() in the simpler
                    // setAttribute()
                    this.getElement().setAttribute(new Attribute("value-type", type.getName(), valueNS));
                }
            }
        }

        // remove old value attribute (assume Element is valid, otherwise we would need to remove
        // all possible value attributes)
        if (currentType != null && (!currentType.equals(type) || type == ODValueType.STRING)) {
            // e.g. @date-value
            this.getElement().removeAttribute(currentType.getValueAttribute(), valueNS);
        }
        // Like LO, do not generate string-value
        if (type != null && type != ODValueType.STRING) {
            // LO cells don't support the full syntax of a duration (user meta fields do)
            // instead it support only values without nYnMnD (as does java.time.Duration)
            if (type == ODValueType.TIME && getTimeValueMode() && !(val instanceof java.time.Duration)) {
                final Duration d = val instanceof Duration ? (Duration) val : TimeUtils.timePartToDuration((Calendar) val);
                val = TIME_NULLS.apply(getODDocument().getEpoch().normalizeToHours(d));
            }
            this.getElement().setAttribute(type.getValueAttribute(), type.format(val), valueNS);
        }
    }

    private void setTextP(String value) {
        if (value == null) {
            this.getElement().removeContent();
        } else {
            new Lines(this.getODDocument(), value).setText(getElement(), getTextValueMode());
        }
    }

    private void setValue(ODValueType type, Object value, String textP) {
        // as in LO, setting a cell value clears the formula
        this.setFormula(null);
        this.setValueAttributes(type, value);
        this.setTextP(textP);
    }

    public void clearValue() {
        this.setValue(null, null, null);
    }

    public void setValue(Object obj) {
        this.setValue(obj, true);
    }

    public void setValue(Object obj, final boolean allowTypeChange) throws UnsupportedOperationException {
        final ODValueType type;
        final ODValueType currentType = getValueType();
        // try to keep current type, since for example a Number can work with FLOAT, PERCENTAGE
        // and CURRENCY
        if (currentType != null && currentType.canFormat(obj.getClass())) {
            type = currentType;
        } else {
            final ODValueType tmp = ODValueType.forObject(obj);
            // allow any Object
            if (allowTypeChange && tmp == null) {
                type = ODValueType.STRING;
                obj = String.valueOf(obj);
            } else {
                type = tmp;
            }
        }
        if (type == null) {
            throw new IllegalArgumentException("Couldn't infer type of " + obj);
        }
        this.setValue(obj, type, allowTypeChange, true);
    }

    /**
     * Change the value of this cell.
     * 
     * @param obj the new cell value.
     * @param vt the value type.
     * @param allowTypeChange if <code>true</code> <code>obj</code> and <code>vt</code> might be
     *        changed to allow the data style to format, e.g. from Boolean.FALSE to 0.
     * @param lenient <code>false</code> to throw an exception if we can't format according to the
     *        ODF, <code>true</code> to try best-effort.
     * @throws UnsupportedOperationException if <code>obj</code> couldn't be formatted.
     */
    public void setValue(Object obj, ODValueType vt, final boolean allowTypeChange, final boolean lenient) throws UnsupportedOperationException {
        final String text;
        final Tuple3<String, ODValueType, Object> formatted = format(obj, vt, !allowTypeChange, lenient);
        vt = formatted.get1();
        obj = formatted.get2();

        if (formatted.get0() != null) {
            text = formatted.get0();
        } else {
            // either there were no format, formatting failed or wasn't attempted because the data
            // style cannot format vt

            if (vt == ODValueType.FLOAT) {
                text = ODPackage.formatNumber((Number) obj, getDataLocale(), getDefaultStyle());
            } else if (vt == ODValueType.PERCENTAGE) {
                text = ODPackage.formatPercent((Number) obj, getDataLocale(), getDefaultStyle());
            } else if (vt == ODValueType.CURRENCY) {
                text = ODPackage.formatCurrency((Number) obj, getDataLocale(), getDefaultStyle());
            } else if (vt == ODValueType.DATE) {
                final Date d;
                if (obj instanceof Calendar) {
                    d = ((Calendar) obj).getTime();
                } else {
                    d = (Date) obj;
                }
                text = TextPDateFormat.get(getDataLocale()).format(d);
            } else if (vt == ODValueType.TIME) {
                if (obj instanceof Duration) {
                    final Duration normalized = getODDocument().getEpoch().normalizeToHours((Duration) obj);
                    text = textPDuration(normalized.getHours(), normalized.getMinutes(), TimeUtils.getSeconds(normalized), getDataLocale());
                } else if (obj instanceof java.time.Duration) {
                    text = textPDuration((java.time.Duration) obj, getDataLocale());
                } else {
                    text = TextPTimeFormat.get(getDataLocale()).format(((Calendar) obj).getTime());
                }
            } else if (vt == ODValueType.BOOLEAN) {
                text = BooleanStyle.toString((Boolean) obj, getDataLocale(), lenient);
            } else if (vt == ODValueType.STRING) {
                text = obj.toString();
            } else {
                throw new IllegalStateException(vt + " unknown");
            }
        }
        this.setValue(vt, obj, text);
    }

    /**
     * The locale of the data style. NOTE this doesn't evaluate the map elements with the current
     * cell value.
     * 
     * @return the locale of the data style, or if none, the ODPackage locale.
     */
    public final Locale getDataLocale() {
        return this.getDataLocale(false);
    }

    public final Locale getDataLocale(final boolean local) {
        Locale res = null;
        final CellStyle s = getStyle();
        if (s != null) {
            final DataStyle ds = s.getDataStyle();
            if (ds != null)
                res = ds.getLocale(local);
        }
        if (local || res != null)
            return res;
        return getODDocument().getPackage().getLocale();
    }

    /**
     * Set the locale for the data style. This is different from the locale of the
     * {@link CellStyle#getTextProperties() text properties}. Like LibreOffice, this set the
     * attributes of the main {@link DataStyle}, and all {@link #getDataStyle() mapped} ones.
     * 
     * @param locale the new locale, <code>null</code> to remove attributes.
     * @throws IllegalStateException if there's no {@link DataStyle}.
     */
    public final void setDataLocale(final Locale locale) throws IllegalStateException {
        final CellStyle s = getStyle();
        if (s != null) {
            final DataStyle ds = s.getDataStyle();
            if (ds != null) {
                ds.setLocale(locale);
                // LO does this, and this avoids the need for mapped styles to have a reference to
                // their parent.
                for (final Element mapElem : ds.getMapChildren()) {
                    s.getDataStyle(mapElem.getAttribute("apply-style-name", mapElem.getNamespace())).setLocale(locale);
                }
                return;
            }
        }
        throw new IllegalStateException("No data style for " + this);
    }

    // return null String if no data style exists, or if one exists but we couldn't use it
    private Tuple3<String, ODValueType, Object> format(Object obj, ODValueType valueType, boolean onlyCast, boolean lenient) {
        String res = null;
        try {
            final Tuple3<DataStyle, ODValueType, Object> ds = getDataStyleAndValue(obj, valueType, onlyCast);
            if (ds != null) {
                obj = ds.get2();
                valueType = ds.get1();
                // act like OO, that is if we set a String to a Date cell, change the value and
                // value-type but leave the data-style untouched
                if (ds.get0().canFormat(obj.getClass()))
                    res = ds.get0().format(obj, getDefaultStyle(), lenient);
            }
        } catch (UnsupportedOperationException e) {
            if (lenient)
                Log.get().log(Level.WARNING, "Couldn't format", e);
            else
                throw e;
        }
        return Tuple3.create(res, valueType, obj);
    }

    public final DataStyle getDataStyle() {
        final Tuple3<DataStyle, ODValueType, Object> s = this.getDataStyleAndValue(this.getValue(), this.getValueType(), true);
        return s != null ? s.get0() : null;
    }

    private final Tuple3<DataStyle, ODValueType, Object> getDataStyleAndValue(Object obj, ODValueType valueType, boolean onlyCast) {
        final CellStyle s = this.getStyle();
        return s != null ? s.getDataStyle(obj, valueType, onlyCast) : null;
    }

    protected final CellStyle getDefaultStyle() {
        return this.getRow().getSheet().getDefaultCellStyle();
    }

    public void replaceBy(String oldValue, String newValue) {
        replaceContentBy(this.getElement(), oldValue, newValue);
    }

    private void replaceContentBy(Element l, String oldValue, String newValue) {
        final List<?> content = l.getContent();
        for (int i = 0; i < content.size(); i++) {
            final Object obj = content.get(i);
            if (obj instanceof Text) {
                // System.err.println(" Text --> " + obj.toString());
                final Text t = (Text) obj;
                t.setText(t.getText().replaceAll(oldValue, newValue));
            } else if (obj instanceof Element) {
                replaceContentBy((Element) obj, oldValue, newValue);
            }
        }
    }

    /**
     * Set the raw value of the formula attribute.
     * 
     * @param formula the raw value, e.g. "of:sum(A1:A2)".
     */
    public final void setRawFormula(final String formula) {
        // from 19.642 table:formula of OpenDocument-v1.2 : Whenever the initial text of a formula
        // has the appearance of an NCName followed by ":", an OpenDocument producer shall provide a
        // valid namespace prefix in order to eliminate any ambiguity.
        final String nsPrefix = formula == null ? null : getFormulaNSPrefix(formula).get0();
        if (nsPrefix != null && this.getElement().getNamespace(nsPrefix) == null) {
            throw new IllegalArgumentException("Unknown namespace prefix : " + nsPrefix);
        }
        this.setFormulaNoCheck(formula);
    }

    private final void setFormulaNoCheck(final String formula) {
        if (formula == null)
            this.getElement().removeAttribute("formula", getTABLE());
        else
            this.getElement().setAttribute("formula", formula, getTABLE());
    }

    public final void setFormulaAndNamespace(final Tuple2<Namespace, String> formula) {
        this.setFormulaAndNamespace(formula.get0(), formula.get1());
    }

    public final void setFormula(final String formula) {
        this.setFormulaAndNamespace(null, formula);
    }

    public final void setFormulaAndNamespace(final Namespace ns, final String formula) {
        if (formula == null) {
            this.setFormulaNoCheck(formula);
        } else if (getODDocument().getVersion() == XMLVersion.OOo) {
            if (ns != null)
                throw new IllegalArgumentException("Namespaces not supported by this version : " + ns);
            this.setFormulaNoCheck(formula);
        } else if (ns == null) {
            final String nsPrefix = getFormulaNSPrefix(formula).get0();
            if (nsPrefix != null) {
                // eliminate ambiguity
                final Namespace defaultNS = getDefaultFormulaNS();
                // prevent infinite recursion
                if (defaultNS == null)
                    throw new IllegalStateException("Cannot resolve ambiguity, formula appears to begin with " + nsPrefix + " and no default namespace found");
                this.setFormulaAndNamespace(defaultNS, formula);
            } else {
                this.setFormulaNoCheck(formula);
            }
        } else {
            final Namespace existingNS = this.getElement().getNamespace(ns.getPrefix());
            if (existingNS == null)
                this.getElement().getDocument().getRootElement().addNamespaceDeclaration(ns);
            else if (!existingNS.equals(ns))
                throw new IllegalStateException("Namespace conflict : " + existingNS + " != " + ns);
            this.setFormulaNoCheck(ns.getPrefix() + ':' + formula);
        }
    }

    public final void unmerge() {
        // from 8.1.3 Table Cell : table-cell are like covered-table-cell with some extra
        // optional attributes so it's safe to rename covered cells into normal ones
        final int x = this.getX();
        final int y = this.getY();
        final int columnsSpanned = getColumnsSpanned();
        final int rowsSpanned = getRowsSpanned();
        for (int i = 0; i < columnsSpanned; i++) {
            for (int j = 0; j < rowsSpanned; j++) {
                // don't mind if we change us at 0,0 we're already a table-cell
                this.getRow().getSheet().getImmutableCellAt(x + i, y + j).getElement().setName("table-cell");
            }
        }
        this.getElement().removeAttribute("number-columns-spanned", getNS().getTABLE());
        this.getElement().removeAttribute("number-rows-spanned", getNS().getTABLE());
    }

    /**
     * Merge this cell and the following ones. If this cell already spanned multiple columns/rows
     * this method un-merge any additional cells.
     * 
     * @param columnsSpanned number of columns to merge.
     * @param rowsSpanned number of rows to merge.
     */
    public final void merge(final int columnsSpanned, final int rowsSpanned) {
        final int currentCols = this.getColumnsSpanned();
        final int currentRows = this.getRowsSpanned();

        // nothing to do
        if (columnsSpanned == currentCols && rowsSpanned == currentRows)
            return;

        final int x = this.getX();
        final int y = this.getY();

        // check for problems before any modifications
        for (int i = 0; i < columnsSpanned; i++) {
            for (int j = 0; j < rowsSpanned; j++) {
                final boolean coveredByThis = i < currentCols && j < currentRows;
                if (!coveredByThis) {
                    final int x2 = x + i;
                    final int y2 = y + j;
                    final Cell<D> immutableCell = this.getRow().getSheet().getImmutableCellAt(x2, y2);
                    // check for overlapping range from inside
                    if (immutableCell.coversOtherCells())
                        throw new IllegalArgumentException("Cell at " + x2 + "," + y2 + " is a merged cell.");
                    // and outside
                    if (immutableCell.getElement().getName().equals("covered-table-cell"))
                        throw new IllegalArgumentException("Cell at " + x2 + "," + y2 + " is already covered.");
                }
            }
        }

        final boolean shrinks = columnsSpanned < currentCols || rowsSpanned < currentRows;
        if (shrinks)
            this.unmerge();

        // from 8.1.3 Table Cell : table-cell are like covered-table-cell with some extra
        // optional attributes so it's safe to rename
        for (int i = 0; i < columnsSpanned; i++) {
            for (int j = 0; j < rowsSpanned; j++) {
                final boolean coveredByThis = i < currentCols && j < currentRows;
                // don't cover this,
                // if we grow the current covered cells are invalid so don't try to access them
                if ((i != 0 || j != 0) && (shrinks || !coveredByThis))
                    // MutableCell is needed to break repeated
                    this.getRow().getSheet().getCellAt(x + i, y + j).getElement().setName("covered-table-cell");
            }
        }
        this.getElement().setAttribute("number-columns-spanned", columnsSpanned + "", getNS().getTABLE());
        this.getElement().setAttribute("number-rows-spanned", rowsSpanned + "", getNS().getTABLE());
    }

    @Override
    public final String getStyleName() {
        return this.getRow().getSheet().getStyleNameAt(this.getX(), this.getY());
    }

    public final StyleTableCellProperties getTableCellProperties() {
        return this.getRow().getSheet().getTableCellPropertiesAt(this.getX(), this.getY());
    }

    public final ODFrame<D> addFrame(final Number x, final Number y, final Number w, final Number h, final LengthUnit unit) {
        final Element elem = ODFrame.createEmpty(getNS(), x, y, w, h, unit);
        this.getElement().addContent(elem);
        return new ODFrame<>(getODDocument(), elem);
    }

    public void setImage(final File pic) throws IOException {
        this.setImage(pic, false);
    }

    public void setImage(final File pic, boolean keepRatio) throws IOException {
        this.setImage(pic.getName(), new ByteArrayProducer(FileUtils.readBytes(pic), keepRatio));
    }

    public void setImage(final String name, final Image img) throws IOException {
        this.setImage(name, img == null ? null : new ImageProducer(img, true));
    }

    private void setImage(final String name, final BytesProducer data) {
        final Namespace draw = this.getNS().getNS("draw");
        final Element frame = this.getElement().getChild("frame", draw);

        if (frame != null) {
            new ODFrame<>(getODDocument(), frame).setImage(name, data, false);
        } else if (data != null)
            throw new IllegalStateException("this cell doesn't contain a frame: " + this);
    }

    public final void setBackgroundColor(final Color color) {
        this.getPrivateStyle().getTableCellProperties(this).setBackgroundColor(color);
    }
}