aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java
blob: 65eefec5592caf164c3460c2ad702f10db066151 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;

import static org.apache.poi.util.Units.EMU_PER_PIXEL;

import java.math.BigInteger;

import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.model.CommentsTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;

import schemasMicrosoftComVml.CTShape;

public class XSSFComment implements Comment {
	
	private final CTComment _comment;
	private final CommentsTable _comments;
    private final CTShape _vmlShape;

    /**
     * cached reference to the string with the comment text
     */
    private XSSFRichTextString _str;

    /**
	 * Creates a new XSSFComment, associated with a given
	 *  low level comment object.
	 */
	public XSSFComment(CommentsTable comments, CTComment comment, CTShape vmlShape) {
		_comment = comment;
		_comments = comments;
        _vmlShape = vmlShape;

        // we potentially need to adjust the column/row information in the shape
        // the same way as we do in setRow()/setColumn()
        if(vmlShape != null && vmlShape.sizeOfClientDataArray() > 0) {
            CellReference ref = new CellReference(comment.getRef());
            vmlShape.getClientDataArray(0).setRowArray(0, 
                    new BigInteger(String.valueOf(ref.getRow())));
    
            vmlShape.getClientDataArray(0).setColumnArray(0, 
                    new BigInteger(String.valueOf(ref.getCol())));
            
            // There is a very odd xmlbeans bug when changing the row
            //  arrays which can lead to corrupt pointer
            // This call seems to fix them again... See bug #50795
            vmlShape.getClientDataList().toString();
        }
	}

    /**
     *
     * @return Name of the original comment author. Default value is blank.
     */
    public String getAuthor() {
		return _comments.getAuthor((int) _comment.getAuthorId());
	}

    /**
     * Name of the original comment author. Default value is blank.
     *
     * @param author the name of the original author of the comment
     */
    public void setAuthor(String author) {
        _comment.setAuthorId(
                _comments.findAuthor(author)
        );
    }

    /**
     * @return the 0-based column of the cell that the comment is associated with.
     */
	public int getColumn() {
		return new CellReference(_comment.getRef()).getCol();
	}

    /**
     * @return the 0-based row index of the cell that the comment is associated with.
     */
	public int getRow() {
		return new CellReference(_comment.getRef()).getRow();
	}

    /**
     * @return whether the comment is visible
     */
    public boolean isVisible() {
        boolean visible = false;
        if(_vmlShape != null){
            String style = _vmlShape.getStyle();
            visible = style != null && style.indexOf("visibility:visible") != -1;
        }
		return visible;
	}

    /**
     * @param visible whether the comment is visible
     */
    public void setVisible(boolean visible) {
        if(_vmlShape != null){
            String style;
            if(visible) style = "position:absolute;visibility:visible";
            else style = "position:absolute;visibility:hidden";
            _vmlShape.setStyle(style);
        }
    }

    /**
     * Set the column of the cell that contains the comment
     *
     * @param col the 0-based column of the cell that contains the comment
     */
    public void setColumn(int col) {
        String oldRef = _comment.getRef();
        
        CellReference ref = new CellReference(getRow(), col);
        _comment.setRef(ref.formatAsString());
        _comments.referenceUpdated(oldRef, _comment);
        
        if(_vmlShape != null) {
           _vmlShape.getClientDataArray(0).setColumnArray(
                 new BigInteger[] { new BigInteger(String.valueOf(col)) }
           );
           
           // There is a very odd xmlbeans bug when changing the column
           //  arrays which can lead to corrupt pointer
           // This call seems to fix them again... See bug #50795
           _vmlShape.getClientDataList().toString();
        }
	}

    /**
     * Set the row of the cell that contains the comment
     *
     * @param row the 0-based row of the cell that contains the comment
     */
	public void setRow(int row) {
	   String oldRef = _comment.getRef();
	   
		String newRef =
			(new CellReference(row, getColumn())).formatAsString();
		_comment.setRef(newRef);
      _comments.referenceUpdated(oldRef, _comment);
      
        if(_vmlShape != null) {
        	_vmlShape.getClientDataArray(0).setRowArray(0, 
        			new BigInteger(String.valueOf(row)));
        	
            // There is a very odd xmlbeans bug when changing the row
            //  arrays which can lead to corrupt pointer
            // This call seems to fix them again... See bug #50795
            _vmlShape.getClientDataList().toString();
        }
    }
    
    /**
     * @return the rich text string of the comment
     */
	public XSSFRichTextString getString() {
		if(_str == null) {
            CTRst rst = _comment.getText();
            if(rst != null) _str = new XSSFRichTextString(_comment.getText());
        }
        return _str;
	}

    /**
     * Sets the rich text string used by this comment.
     *
     * @param string  the XSSFRichTextString used by this object.
     */
	public void setString(RichTextString string) {
        if(!(string instanceof XSSFRichTextString)){
            throw new IllegalArgumentException("Only XSSFRichTextString argument is supported");
        }
        _str = (XSSFRichTextString)string;
        _comment.setText(_str.getCTRst());
	}
	
	public void setString(String string) {
		setString(new XSSFRichTextString(string));
	}

    @Override
    public ClientAnchor getClientAnchor() {
        String position = _vmlShape.getClientDataArray(0).getAnchorArray(0);
        int[] pos = new int[8];
        int i = 0;
        for (String s : position.split(",")) {
            pos[i++] = Integer.parseInt(s.trim());
        }
        XSSFClientAnchor ca = new XSSFClientAnchor(pos[1]*EMU_PER_PIXEL, pos[3]*EMU_PER_PIXEL, pos[5]*EMU_PER_PIXEL, pos[7]*EMU_PER_PIXEL, pos[0], pos[2], pos[4], pos[6]);
        return ca;
    }

    /**
     * @return the xml bean holding this comment's properties
     */
    protected CTComment getCTComment(){
        return _comment;
    }

    protected CTShape getCTShape(){
        return _vmlShape;
    }
}