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 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 80 Rev 180
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.openoffice;
14
 package org.openconcerto.openoffice;
15
 
15
 
-
 
16
import org.openconcerto.openoffice.spreadsheet.BytesProducer;
16
import org.openconcerto.openoffice.text.TextNode;
17
import org.openconcerto.openoffice.text.TextNode;
17
 
18
 
18
import java.math.BigDecimal;
19
import java.math.BigDecimal;
19
 
20
 
20
import org.jdom.Element;
21
import org.jdom.Element;
21
import org.jdom.Namespace;
22
import org.jdom.Namespace;
22
 
23
 
23
/**
24
/**
24
 * Represents a draw:frame, see 9.3 Frames.
25
 * Represents a draw:frame, see 9.3 Frames.
25
 * 
26
 * 
26
 * @author Sylvain
27
 * @author Sylvain
27
 * @param <D> type of table parent
28
 * @param <D> type of table parent
28
 */
29
 */
29
public class ODFrame<D extends ODDocument> extends ImmutableDocStyledNode<GraphicStyle, D> {
30
public class ODFrame<D extends ODDocument> extends ImmutableDocStyledNode<GraphicStyle, D> {
30
 
31
 
-
 
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
 
31
    /**
46
    /**
32
     * Parse SVG and OD length.
47
     * Parse SVG and OD length.
33
     * 
48
     * 
34
     * @param l the string to parse, eg "1.53cm".
49
     * @param l the string to parse, eg "1.53cm".
35
     * @param to the unit, eg {@link LengthUnit#MM}.
50
     * @param to the unit, eg {@link LengthUnit#MM}.
36
     * @return the length, eg 15.3.
51
     * @return the length, eg 15.3.
37
     */
52
     */
38
    public static final float parseLength(final String l, final LengthUnit to) {
53
    public static final float parseLength(final String l, final LengthUnit to) {
39
        return LengthUnit.parseLength(l, to).floatValue();
54
        return LengthUnit.parseLength(l, to).floatValue();
40
    }
55
    }
41
 
56
 
42
    // BigDecimal are exact and they can be null (eg optional attribute)
57
    // BigDecimal are exact and they can be null (eg optional attribute)
43
    private final BigDecimal width, height;
58
    private final BigDecimal width, height;
44
 
59
 
45
    public ODFrame(D parent, Element frame) {
60
    public ODFrame(D parent, Element frame) {
46
        super(parent, frame, GraphicStyle.class);
61
        super(parent, frame, GraphicStyle.class);
47
        this.width = LengthUnit.parseLength(this.getSVGAttr("width"), getUnit());
62
        this.width = LengthUnit.parseLength(this.getSVGAttr("width"), getUnit());
48
        this.height = LengthUnit.parseLength(this.getSVGAttr("height"), getUnit());
63
        this.height = LengthUnit.parseLength(this.getSVGAttr("height"), getUnit());
49
    }
64
    }
50
 
65
 
51
    private final Element getTextBox() {
66
    private final Element getTextBox() {
52
        final Element res;
67
        final Element res;
53
        if (this.getODDocument().getVersion() == XMLVersion.OOo)
68
        if (this.getODDocument().getVersion() == XMLVersion.OOo)
54
            res = this.getElement();
69
            res = this.getElement();
55
        else
70
        else
56
            res = this.getElement().getChild("text-box", this.getElement().getNamespace("draw"));
71
            res = this.getElement().getChild("text-box", this.getElement().getNamespace("draw"));
57
        assert res.getName().equals("text-box");
72
        assert res.getName().equals("text-box");
58
        return res;
73
        return res;
59
    }
74
    }
60
 
75
 
61
    public final String getCharacterContent(final boolean ooMode) {
76
    public final String getCharacterContent(final boolean ooMode) {
62
        return TextNode.getChildrenCharacterContent(this.getTextBox(), getODDocument().getFormatVersion(), ooMode);
77
        return TextNode.getChildrenCharacterContent(this.getTextBox(), getODDocument().getFormatVersion(), ooMode);
63
    }
78
    }
64
 
79
 
65
    public final BigDecimal getWidth() {
80
    public final BigDecimal getWidth() {
66
        return this.getWidth(this.getUnit());
81
        return this.getWidth(this.getUnit());
67
    }
82
    }
68
 
83
 
69
    public final BigDecimal getWidth(final LengthUnit in) {
84
    public final BigDecimal getWidth(final LengthUnit in) {
70
        return this.getUnit().convertTo(this.width, in);
85
        return this.getUnit().convertTo(this.width, in);
71
    }
86
    }
72
 
87
 
73
    public final BigDecimal getHeight() {
88
    public final BigDecimal getHeight() {
74
        return this.getHeight(this.getUnit());
89
        return this.getHeight(this.getUnit());
75
    }
90
    }
76
 
91
 
77
    public final BigDecimal getHeight(final LengthUnit in) {
92
    public final BigDecimal getHeight(final LengthUnit in) {
78
        return this.getUnit().convertTo(this.height, in);
93
        return this.getUnit().convertTo(this.height, in);
79
    }
94
    }
80
 
95
 
81
    private Namespace getSVG() {
96
    private Namespace getSVG() {
82
        return getElement().getNamespace("svg");
97
        return getElement().getNamespace("svg");
83
    }
98
    }
84
 
99
 
85
    public String getSVGAttr(String name) {
100
    public String getSVGAttr(String name) {
86
        return this.getElement().getAttributeValue(name, getSVG());
101
        return this.getElement().getAttributeValue(name, getSVG());
87
    }
102
    }
88
 
103
 
89
    public void setSVGAttr(String name, String val) {
104
    public void setSVGAttr(String name, String val) {
90
        this.getElement().setAttribute(name, val, this.getSVG());
105
        this.getElement().setAttribute(name, val, this.getSVG());
91
    }
106
    }
92
 
107
 
93
    /**
108
    /**
94
     * This set the svg:name attribute to val mm.
109
     * This set the svg:name attribute to val mm.
95
     * 
110
     * 
96
     * @param name the name of the attribute, eg "x".
111
     * @param name the name of the attribute, eg "x".
97
     * @param val the value of the attribute in {@link #getUnit()}, eg 15.3.
112
     * @param val the value of the attribute in {@link #getUnit()}, eg 15.3.
98
     */
113
     */
99
    public void setSVGAttr(String name, Number val) {
114
    public void setSVGAttr(String name, Number val) {
100
        this.setSVGAttr(name, this.getUnit().format(val));
115
        this.setSVGAttr(name, this.getUnit().format(val));
101
    }
116
    }
102
 
117
 
103
    public final double getRatio() {
118
    public final double getRatio() {
104
        return this.getWidth().doubleValue() / this.getHeight().doubleValue();
119
        return this.getWidth().doubleValue() / this.getHeight().doubleValue();
105
    }
120
    }
106
 
121
 
107
    public final BigDecimal getX() {
122
    public final BigDecimal getX() {
108
        return LengthUnit.parseLength(this.getSVGAttr("x"), getUnit());
123
        return LengthUnit.parseLength(this.getSVGAttr("x"), getUnit());
109
    }
124
    }
110
 
125
 
111
    public final BigDecimal getY() {
126
    public final BigDecimal getY() {
112
        return LengthUnit.parseLength(this.getSVGAttr("y"), getUnit());
127
        return LengthUnit.parseLength(this.getSVGAttr("y"), getUnit());
113
    }
128
    }
114
 
129
 
115
    /**
130
    /**
116
     * The unit that all length methods use.
131
     * The unit that all length methods use.
117
     * 
132
     * 
118
     * @return the unit used, eg "mm".
133
     * @return the unit used, eg "mm".
119
     */
134
     */
120
    public final LengthUnit getUnit() {
135
    public final LengthUnit getUnit() {
121
        return LengthUnit.MM;
136
        return LengthUnit.MM;
122
    }
137
    }
-
 
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
    }
123
}
170
}