OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
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;
15
 
180 ilm 16
import org.openconcerto.openoffice.spreadsheet.BytesProducer;
73 ilm 17
import org.openconcerto.openoffice.text.TextNode;
18
 
17 ilm 19
import java.math.BigDecimal;
20
 
21
import org.jdom.Element;
22
import org.jdom.Namespace;
23
 
24
/**
25
 * Represents a draw:frame, see 9.3 Frames.
26
 *
27
 * @author Sylvain
28
 * @param <D> type of table parent
29
 */
30
public class ODFrame<D extends ODDocument> extends ImmutableDocStyledNode<GraphicStyle, D> {
31
 
180 ilm 32
    static public Element createEmpty(XMLVersion ns, final Number w, final Number h, final LengthUnit unit) {
33
        return createEmpty(ns, BigDecimal.ZERO, BigDecimal.ZERO, w, h, unit);
34
    }
35
 
36
    static public Element createEmpty(XMLVersion ns, final Number x, final Number y, final Number w, final Number h, final LengthUnit unit) {
37
        final Namespace svgNS = ns.getNS("svg");
38
        final Element res = new Element("frame", ns.getNS("draw"));
39
        res.setAttribute("x", unit.format(x), svgNS);
40
        res.setAttribute("y", unit.format(y), svgNS);
41
        res.setAttribute("width", unit.format(w), svgNS);
42
        res.setAttribute("height", unit.format(h), svgNS);
43
        return res;
44
    }
45
 
17 ilm 46
    /**
47
     * Parse SVG and OD length.
48
     *
49
     * @param l the string to parse, eg "1.53cm".
50
     * @param to the unit, eg {@link LengthUnit#MM}.
51
     * @return the length, eg 15.3.
52
     */
53
    public static final float parseLength(final String l, final LengthUnit to) {
20 ilm 54
        return LengthUnit.parseLength(l, to).floatValue();
17 ilm 55
    }
56
 
57
    // BigDecimal are exact and they can be null (eg optional attribute)
58
    private final BigDecimal width, height;
59
 
60
    public ODFrame(D parent, Element frame) {
61
        super(parent, frame, GraphicStyle.class);
20 ilm 62
        this.width = LengthUnit.parseLength(this.getSVGAttr("width"), getUnit());
63
        this.height = LengthUnit.parseLength(this.getSVGAttr("height"), getUnit());
17 ilm 64
    }
65
 
73 ilm 66
    private final Element getTextBox() {
67
        final Element res;
68
        if (this.getODDocument().getVersion() == XMLVersion.OOo)
69
            res = this.getElement();
70
        else
71
            res = this.getElement().getChild("text-box", this.getElement().getNamespace("draw"));
72
        assert res.getName().equals("text-box");
73
        return res;
74
    }
75
 
76
    public final String getCharacterContent(final boolean ooMode) {
77
        return TextNode.getChildrenCharacterContent(this.getTextBox(), getODDocument().getFormatVersion(), ooMode);
78
    }
79
 
17 ilm 80
    public final BigDecimal getWidth() {
81
        return this.getWidth(this.getUnit());
82
    }
83
 
84
    public final BigDecimal getWidth(final LengthUnit in) {
85
        return this.getUnit().convertTo(this.width, in);
86
    }
87
 
88
    public final BigDecimal getHeight() {
89
        return this.getHeight(this.getUnit());
90
    }
91
 
92
    public final BigDecimal getHeight(final LengthUnit in) {
93
        return this.getUnit().convertTo(this.height, in);
94
    }
95
 
96
    private Namespace getSVG() {
97
        return getElement().getNamespace("svg");
98
    }
99
 
100
    public String getSVGAttr(String name) {
101
        return this.getElement().getAttributeValue(name, getSVG());
102
    }
103
 
104
    public void setSVGAttr(String name, String val) {
105
        this.getElement().setAttribute(name, val, this.getSVG());
106
    }
107
 
108
    /**
109
     * This set the svg:name attribute to val mm.
110
     *
111
     * @param name the name of the attribute, eg "x".
112
     * @param val the value of the attribute in {@link #getUnit()}, eg 15.3.
113
     */
114
    public void setSVGAttr(String name, Number val) {
80 ilm 115
        this.setSVGAttr(name, this.getUnit().format(val));
17 ilm 116
    }
117
 
118
    public final double getRatio() {
119
        return this.getWidth().doubleValue() / this.getHeight().doubleValue();
120
    }
121
 
122
    public final BigDecimal getX() {
20 ilm 123
        return LengthUnit.parseLength(this.getSVGAttr("x"), getUnit());
17 ilm 124
    }
125
 
126
    public final BigDecimal getY() {
20 ilm 127
        return LengthUnit.parseLength(this.getSVGAttr("y"), getUnit());
17 ilm 128
    }
129
 
130
    /**
131
     * The unit that all length methods use.
132
     *
133
     * @return the unit used, eg "mm".
134
     */
135
    public final LengthUnit getUnit() {
136
        return LengthUnit.MM;
137
    }
180 ilm 138
 
139
    public final void addImage(final String name, final BytesProducer data) {
140
        final Element imageElem = new Element("image", getElement().getNamespace("draw"));
141
        this.getElement().addContent(imageElem);
142
        this.putImage(imageElem, name, data);
143
    }
144
 
145
    private final void putImage(final Element imageElem, final String name, final BytesProducer data) {
146
        final String imgPath = "Pictures/" + name + (data.getFormat() != null ? "." + data.getFormat() : "");
147
        imageElem.setAttribute("href", imgPath, this.getODDocument().getVersion().getNS("xlink"));
148
        this.getODDocument().getPackage().putFile(imgPath, data.getBytes(this));
149
    }
150
 
151
    public final void setImage(final String name, final BytesProducer data, final boolean allowAdd) {
152
        final Element imageElem = this.getElement().getChild("image", getElement().getNamespace("draw"));
153
        if (imageElem != null) {
154
            final String oldPath = imageElem.getAttributeValue("href", this.getODDocument().getVersion().getNS("xlink"));
155
            this.getODDocument().getPackage().putFile(oldPath, null);
156
            imageElem.removeChild("binary-data", this.getODDocument().getVersion().getOFFICE());
157
 
158
            if (data == null) {
159
                imageElem.detach();
160
            } else {
161
                this.putImage(imageElem, name, data);
162
            }
163
        } else if (data != null) {
164
            if (allowAdd)
165
                this.addImage(name, data);
166
            else
167
                throw new IllegalStateException("No image in " + this);
168
        }
169
    }
17 ilm 170
}