View | Details | Raw Unified | Return to bug 58365
Collapse All | Expand All

(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (-1 / +2 lines)
Lines 38-43 Link Here
38
import org.apache.poi.ss.usermodel.Hyperlink;
38
import org.apache.poi.ss.usermodel.Hyperlink;
39
import org.apache.poi.ss.usermodel.RichTextString;
39
import org.apache.poi.ss.usermodel.RichTextString;
40
import org.apache.poi.ss.usermodel.Row;
40
import org.apache.poi.ss.usermodel.Row;
41
import org.apache.poi.ss.util.CellAddress;
41
import org.apache.poi.ss.util.CellRangeAddress;
42
import org.apache.poi.ss.util.CellRangeAddress;
42
import org.apache.poi.ss.util.CellReference;
43
import org.apache.poi.ss.util.CellReference;
43
import org.apache.poi.util.Internal;
44
import org.apache.poi.util.Internal;
Lines 944-950 Link Here
944
        if(comment != null){
945
        if(comment != null){
945
            String ref = getReference();
946
            String ref = getReference();
946
            XSSFSheet sh = getSheet();
947
            XSSFSheet sh = getSheet();
947
            sh.getCommentsTable(false).removeComment(ref);
948
            sh.getCommentsTable(false).removeComment(new CellAddress(ref));
948
            sh.getVMLDrawing(false).removeCommentShape(getRowIndex(), getColumnIndex());
949
            sh.getVMLDrawing(false).removeCommentShape(getRowIndex(), getColumnIndex());
949
        }
950
        }
950
    }
951
    }
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (-4 / +10 lines)
Lines 52-57 Link Here
52
import org.apache.poi.ss.usermodel.Cell;
52
import org.apache.poi.ss.usermodel.Cell;
53
import org.apache.poi.ss.usermodel.CellRange;
53
import org.apache.poi.ss.usermodel.CellRange;
54
import org.apache.poi.ss.usermodel.CellStyle;
54
import org.apache.poi.ss.usermodel.CellStyle;
55
import org.apache.poi.ss.usermodel.Comment;
55
import org.apache.poi.ss.usermodel.DataValidation;
56
import org.apache.poi.ss.usermodel.DataValidation;
56
import org.apache.poi.ss.usermodel.DataValidationHelper;
57
import org.apache.poi.ss.usermodel.DataValidationHelper;
57
import org.apache.poi.ss.usermodel.Footer;
58
import org.apache.poi.ss.usermodel.Footer;
Lines 60-65 Link Here
60
import org.apache.poi.ss.usermodel.Row;
61
import org.apache.poi.ss.usermodel.Row;
61
import org.apache.poi.ss.usermodel.Sheet;
62
import org.apache.poi.ss.usermodel.Sheet;
62
import org.apache.poi.ss.util.AreaReference;
63
import org.apache.poi.ss.util.AreaReference;
64
import org.apache.poi.ss.util.CellAddress;
63
import org.apache.poi.ss.util.CellRangeAddress;
65
import org.apache.poi.ss.util.CellRangeAddress;
64
import org.apache.poi.ss.util.CellRangeAddressList;
66
import org.apache.poi.ss.util.CellRangeAddressList;
65
import org.apache.poi.ss.util.CellReference;
67
import org.apache.poi.ss.util.CellReference;
Lines 656-662 Link Here
656
            return null;
658
            return null;
657
        }
659
        }
658
660
659
        String ref = new CellReference(row, column).formatAsString();
661
        CellAddress ref = new CellAddress(row, column);
660
        CTComment ctComment = sheetComments.getCTComment(ref);
662
        CTComment ctComment = sheetComments.getCTComment(ref);
661
        if(ctComment == null) return null;
663
        if(ctComment == null) return null;
662
664
Lines 2613-2624 Link Here
2613
                    CTCommentList lst = sheetComments.getCTComments().getCommentList();
2615
                    CTCommentList lst = sheetComments.getCTComments().getCommentList();
2614
                    for (CTComment comment : lst.getCommentArray()) {
2616
                    for (CTComment comment : lst.getCommentArray()) {
2615
                    	String strRef = comment.getRef();
2617
                    	String strRef = comment.getRef();
2616
                    	CellReference ref = new CellReference(strRef);
2618
                    	CellAddress ref = new CellAddress(strRef);
2617
2619
2618
                    	// is this comment part of the current row?
2620
                    	// is this comment part of the current row?
2619
                    	if(ref.getRow() == rownum) {
2621
                    	if(ref.getRow() == rownum) {
2620
                            sheetComments.removeComment(strRef);
2622
                            sheetComments.removeComment(ref);
2621
                            vml.removeCommentShape(ref.getRow(), ref.getCol());
2623
                            vml.removeCommentShape(ref.getRow(), ref.getColumn());
2622
                    	}
2624
                    	}
2623
                    }
2625
                    }
2624
                }
2626
                }
Lines 3965-3968 Link Here
3965
        }
3967
        }
3966
        return col.getOutlineLevel();
3968
        return col.getOutlineLevel();
3967
    }
3969
    }
3970
3971
    public Map<CellAddress, Comment> getCellComments(){
3972
        return this.sheetComments.getCellComments();
3973
    }
3968
}
3974
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java (-7 / +7 lines)
Lines 23-28 Link Here
23
import org.apache.poi.ss.usermodel.ClientAnchor;
23
import org.apache.poi.ss.usermodel.ClientAnchor;
24
import org.apache.poi.ss.usermodel.Comment;
24
import org.apache.poi.ss.usermodel.Comment;
25
import org.apache.poi.ss.usermodel.RichTextString;
25
import org.apache.poi.ss.usermodel.RichTextString;
26
import org.apache.poi.ss.util.CellAddress;
26
import org.apache.poi.ss.util.CellReference;
27
import org.apache.poi.ss.util.CellReference;
27
import org.apache.poi.xssf.model.CommentsTable;
28
import org.apache.poi.xssf.model.CommentsTable;
28
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
29
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
Lines 130-139 Link Here
130
     * @param col the 0-based column of the cell that contains the comment
131
     * @param col the 0-based column of the cell that contains the comment
131
     */
132
     */
132
    public void setColumn(int col) {
133
    public void setColumn(int col) {
133
        String oldRef = _comment.getRef();
134
        CellAddress oldRef = new CellAddress(_comment.getRef());
134
        
135
        
135
        CellReference ref = new CellReference(getRow(), col);
136
        CellAddress ref = new CellAddress(getRow(), col);
136
        _comment.setRef(ref.formatAsString());
137
        _comment.setRef(ref.toString());
137
        _comments.referenceUpdated(oldRef, _comment);
138
        _comments.referenceUpdated(oldRef, _comment);
138
        
139
        
139
        if(_vmlShape != null) {
140
        if(_vmlShape != null) {
Lines 154-165 Link Here
154
     * @param row the 0-based row of the cell that contains the comment
155
     * @param row the 0-based row of the cell that contains the comment
155
     */
156
     */
156
	public void setRow(int row) {
157
	public void setRow(int row) {
157
	   String oldRef = _comment.getRef();
158
	   CellAddress oldRef = new CellAddress(_comment.getRef());
158
	   
159
	   
159
		String newRef =
160
		String newRef = (new CellReference(row, getColumn())).formatAsString();
160
			(new CellReference(row, getColumn())).formatAsString();
161
		_comment.setRef(newRef);
161
		_comment.setRef(newRef);
162
      _comments.referenceUpdated(oldRef, _comment);
162
		_comments.referenceUpdated(oldRef, _comment);
163
      
163
      
164
        if(_vmlShape != null) {
164
        if(_vmlShape != null) {
165
        	_vmlShape.getClientDataArray(0).setRowArray(0, 
165
        	_vmlShape.getClientDataArray(0).setRowArray(0, 
(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java (-5 / +6 lines)
Lines 38-43 Link Here
38
import org.apache.poi.ss.usermodel.Row;
38
import org.apache.poi.ss.usermodel.Row;
39
import org.apache.poi.ss.usermodel.Sheet;
39
import org.apache.poi.ss.usermodel.Sheet;
40
import org.apache.poi.ss.usermodel.Workbook;
40
import org.apache.poi.ss.usermodel.Workbook;
41
import org.apache.poi.ss.util.CellAddress;
41
import org.apache.poi.ss.util.CellReference;
42
import org.apache.poi.ss.util.CellReference;
42
import org.apache.poi.xssf.XSSFITestDataProvider;
43
import org.apache.poi.xssf.XSSFITestDataProvider;
43
import org.apache.poi.xssf.XSSFTestDataSamples;
44
import org.apache.poi.xssf.XSSFTestDataSamples;
Lines 73-79 Link Here
73
        assertEquals(1, sheetComments.getCTComments().getAuthors().sizeOfAuthorArray());
74
        assertEquals(1, sheetComments.getCTComments().getAuthors().sizeOfAuthorArray());
74
        assertEquals(1, sheetComments.getNumberOfAuthors());
75
        assertEquals(1, sheetComments.getNumberOfAuthors());
75
76
76
        CTComment ctComment = sheetComments.newComment("A1");
77
        CTComment ctComment = sheetComments.newComment(CellAddress.A1);
77
        CTShape vmlShape = CTShape.Factory.newInstance();
78
        CTShape vmlShape = CTShape.Factory.newInstance();
78
79
79
        XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
80
        XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
Lines 88-94 Link Here
88
    public void getSetCol() {
89
    public void getSetCol() {
89
        CommentsTable sheetComments = new CommentsTable();
90
        CommentsTable sheetComments = new CommentsTable();
90
        XSSFVMLDrawing vml = new XSSFVMLDrawing();
91
        XSSFVMLDrawing vml = new XSSFVMLDrawing();
91
        CTComment ctComment = sheetComments.newComment("A1");
92
        CTComment ctComment = sheetComments.newComment(CellAddress.A1);
92
        CTShape vmlShape = vml.newCommentShape();
93
        CTShape vmlShape = vml.newCommentShape();
93
94
94
        XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
95
        XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
Lines 107-113 Link Here
107
    public void getSetRow() {
108
    public void getSetRow() {
108
        CommentsTable sheetComments = new CommentsTable();
109
        CommentsTable sheetComments = new CommentsTable();
109
        XSSFVMLDrawing vml = new XSSFVMLDrawing();
110
        XSSFVMLDrawing vml = new XSSFVMLDrawing();
110
        CTComment ctComment = sheetComments.newComment("A1");
111
        CTComment ctComment = sheetComments.newComment(CellAddress.A1);
111
        CTShape vmlShape = vml.newCommentShape();
112
        CTShape vmlShape = vml.newCommentShape();
112
113
113
        XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
114
        XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
Lines 179-185 Link Here
179
    @Test
180
    @Test
180
    public void author() {
181
    public void author() {
181
        CommentsTable sheetComments = new CommentsTable();
182
        CommentsTable sheetComments = new CommentsTable();
182
        CTComment ctComment = sheetComments.newComment("A1");
183
        CTComment ctComment = sheetComments.newComment(CellAddress.A1);
183
184
184
        assertEquals(1, sheetComments.getNumberOfAuthors());
185
        assertEquals(1, sheetComments.getNumberOfAuthors());
185
        XSSFComment comment = new XSSFComment(sheetComments, ctComment, null);
186
        XSSFComment comment = new XSSFComment(sheetComments, ctComment, null);
Lines 243-249 Link Here
243
                vmlShape2.getClientDataArray(0).setAnchorArray(0, position);
244
                vmlShape2.getClientDataArray(0).setAnchorArray(0, position);
244
            }
245
            }
245
            
246
            
246
            String ref = new CellReference(ca.getRow1(), ca.getCol1()).formatAsString();
247
            CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
247
            XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
248
            XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
248
        
249
        
249
            assertEquals(shape1.getAuthor(), shape2.getAuthor());
250
            assertEquals(shape1.getAuthor(), shape2.getAuthor());
(-)src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (+30 lines)
Lines 21-26 Link Here
21
import java.util.ArrayList;
21
import java.util.ArrayList;
22
import java.util.Iterator;
22
import java.util.Iterator;
23
import java.util.List;
23
import java.util.List;
24
import java.util.Map;
24
import java.util.TreeMap;
25
import java.util.TreeMap;
25
26
26
import org.apache.poi.ddf.EscherRecord;
27
import org.apache.poi.ddf.EscherRecord;
Lines 56-64 Link Here
56
import org.apache.poi.ss.usermodel.Cell;
57
import org.apache.poi.ss.usermodel.Cell;
57
import org.apache.poi.ss.usermodel.CellRange;
58
import org.apache.poi.ss.usermodel.CellRange;
58
import org.apache.poi.ss.usermodel.CellStyle;
59
import org.apache.poi.ss.usermodel.CellStyle;
60
import org.apache.poi.ss.usermodel.Comment;
59
import org.apache.poi.ss.usermodel.DataValidation;
61
import org.apache.poi.ss.usermodel.DataValidation;
60
import org.apache.poi.ss.usermodel.DataValidationHelper;
62
import org.apache.poi.ss.usermodel.DataValidationHelper;
61
import org.apache.poi.ss.usermodel.Row;
63
import org.apache.poi.ss.usermodel.Row;
64
import org.apache.poi.ss.util.CellAddress;
62
import org.apache.poi.ss.util.CellRangeAddress;
65
import org.apache.poi.ss.util.CellRangeAddress;
63
import org.apache.poi.ss.util.CellRangeAddressList;
66
import org.apache.poi.ss.util.CellRangeAddressList;
64
import org.apache.poi.ss.util.CellReference;
67
import org.apache.poi.ss.util.CellReference;
Lines 2194-2199 Link Here
2194
        return null;
2197
        return null;
2195
    }
2198
    }
2196
2199
2200
    public Map<CellAddress,Comment> getCellComments() {
2201
        HSSFPatriarch patriarch = getDrawingPatriarch();
2202
        if (null == patriarch) {
2203
            patriarch = createDrawingPatriarch();
2204
        }
2205
        
2206
        
2207
        Map<CellAddress, Comment> locations = new TreeMap<CellAddress, Comment>();
2208
        findCellCommentLocations(patriarch, locations);
2209
        return locations;
2210
    }
2211
    private void findCellCommentLocations(HSSFShapeContainer container, Map<CellAddress, Comment> locations){
2212
        for (Object object : container.getChildren()) {
2213
            HSSFShape shape = (HSSFShape) object;
2214
            if (shape instanceof HSSFShapeGroup) {
2215
                findCellCommentLocations((HSSFShapeGroup) shape, locations);
2216
                continue;
2217
            }
2218
            if (shape instanceof HSSFComment) {
2219
                HSSFComment comment = (HSSFComment) shape;
2220
                if (comment.hasPosition()) {
2221
                    locations.put(new CellAddress(comment.getRow(), comment.getColumn()), comment);
2222
                }
2223
            }
2224
        }
2225
    }
2226
2197
2227
2198
    public CellRangeAddress getRepeatingRows() {
2228
    public CellRangeAddress getRepeatingRows() {
2199
        return getRepeatingRowsOrColums(true);
2229
        return getRepeatingRowsOrColums(true);
(-)src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java (-12 / +12 lines)
Lines 22-27 Link Here
22
22
23
import org.apache.poi.ss.usermodel.BuiltinFormats;
23
import org.apache.poi.ss.usermodel.BuiltinFormats;
24
import org.apache.poi.ss.usermodel.DataFormatter;
24
import org.apache.poi.ss.usermodel.DataFormatter;
25
import org.apache.poi.ss.util.CellAddress;
25
import org.apache.poi.ss.util.CellReference;
26
import org.apache.poi.ss.util.CellReference;
26
import org.apache.poi.util.POILogFactory;
27
import org.apache.poi.util.POILogFactory;
27
import org.apache.poi.util.POILogger;
28
import org.apache.poi.util.POILogger;
Lines 105-111 Link Here
105
   private StringBuffer formula = new StringBuffer();
106
   private StringBuffer formula = new StringBuffer();
106
   private StringBuffer headerFooter = new StringBuffer();
107
   private StringBuffer headerFooter = new StringBuffer();
107
108
108
   private Queue<CellReference> commentCellRefs;
109
   private Queue<CellAddress> commentCellRefs;
109
110
110
   /**
111
   /**
111
    * Accepts objects needed while parsing.
112
    * Accepts objects needed while parsing.
Lines 162-170 Link Here
162
   @SuppressWarnings("deprecation")
163
   @SuppressWarnings("deprecation")
163
   private void init() {
164
   private void init() {
164
       if (commentsTable != null) {
165
       if (commentsTable != null) {
165
           commentCellRefs = new LinkedList<CellReference>();
166
           commentCellRefs = new LinkedList<CellAddress>();
166
           for (CTComment comment : commentsTable.getCTComments().getCommentList().getCommentArray()) {
167
           for (CTComment comment : commentsTable.getCTComments().getCommentList().getCommentArray()) {
167
               commentCellRefs.add(new CellReference(comment.getRef()));
168
               commentCellRefs.add(new CellAddress(comment.getRef()));
168
           }
169
           }
169
       }   
170
       }   
170
   }
171
   }
Lines 362-368 Link Here
362
           
363
           
363
           // Do we have a comment for this cell?
364
           // Do we have a comment for this cell?
364
           checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
365
           checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
365
           XSSFComment comment = commentsTable != null ? commentsTable.findCellComment(cellRef) : null;
366
           XSSFComment comment = commentsTable != null ? commentsTable.findCellComment(new CellAddress(cellRef)) : null;
366
           
367
           
367
           // Output
368
           // Output
368
           output.cell(cellRef, thisStr, comment);
369
           output.cell(cellRef, thisStr, comment);
Lines 443-459 Link Here
443
               }
444
               }
444
           }
445
           }
445
446
446
           CellReference nextCommentCellRef;
447
           CellAddress nextCommentCellRef;
447
           do {
448
           do {
448
               CellReference cellRef = new CellReference(this.cellRef);
449
               CellAddress cellRef = new CellAddress(this.cellRef);
449
               CellReference peekCellRef = commentCellRefs.peek();
450
               CellAddress peekCellRef = commentCellRefs.peek();
450
               if (type == EmptyCellCommentsCheckType.CELL && cellRef.equals(peekCellRef)) {
451
               if (type == EmptyCellCommentsCheckType.CELL && cellRef.equals(peekCellRef)) {
451
                   // remove the comment cell ref from the list if we're about to handle it alongside the cell content
452
                   // remove the comment cell ref from the list if we're about to handle it alongside the cell content
452
                   commentCellRefs.remove();
453
                   commentCellRefs.remove();
453
                   return;
454
                   return;
454
               } else {
455
               } else {
455
                   // fill in any gaps if there are empty cells with comment mixed in with non-empty cells
456
                   // fill in any gaps if there are empty cells with comment mixed in with non-empty cells
456
                   int comparison = cellRefComparator.compare(peekCellRef, cellRef);
457
                   int comparison = peekCellRef.compareTo(cellRef);
457
                   if (comparison > 0 && type == EmptyCellCommentsCheckType.END_OF_ROW && peekCellRef.getRow() <= rowNum) {
458
                   if (comparison > 0 && type == EmptyCellCommentsCheckType.END_OF_ROW && peekCellRef.getRow() <= rowNum) {
458
                       nextCommentCellRef = commentCellRefs.remove();
459
                       nextCommentCellRef = commentCellRefs.remove();
459
                       outputEmptyCellComment(nextCommentCellRef);
460
                       outputEmptyCellComment(nextCommentCellRef);
Lines 472-481 Link Here
472
   /**
473
   /**
473
    * Output an empty-cell comment.
474
    * Output an empty-cell comment.
474
    */
475
    */
475
   private void outputEmptyCellComment(CellReference cellRef) {
476
   private void outputEmptyCellComment(CellAddress cellRef) {
476
       String cellRefString = cellRef.formatAsString();
477
       XSSFComment comment = commentsTable.findCellComment(cellRef);
477
       XSSFComment comment = commentsTable.findCellComment(cellRefString);
478
       output.cell(cellRef.toString(), null, comment);
478
       output.cell(cellRefString, null, comment);
479
   }
479
   }
480
   
480
   
481
   private enum EmptyCellCommentsCheckType {
481
   private enum EmptyCellCommentsCheckType {
(-)src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java (-16 / +38 lines)
Lines 21-30 Link Here
21
import java.io.OutputStream;
21
import java.io.OutputStream;
22
import java.util.HashMap;
22
import java.util.HashMap;
23
import java.util.Map;
23
import java.util.Map;
24
import java.util.Map.Entry;
25
import java.util.TreeMap;
24
26
25
import org.apache.poi.POIXMLDocumentPart;
27
import org.apache.poi.POIXMLDocumentPart;
26
import org.apache.poi.openxml4j.opc.PackagePart;
28
import org.apache.poi.openxml4j.opc.PackagePart;
27
import org.apache.poi.openxml4j.opc.PackageRelationship;
29
import org.apache.poi.openxml4j.opc.PackageRelationship;
30
import org.apache.poi.ss.usermodel.Comment;
31
import org.apache.poi.ss.util.CellAddress;
28
import org.apache.poi.xssf.usermodel.XSSFComment;
32
import org.apache.poi.xssf.usermodel.XSSFComment;
29
import org.apache.xmlbeans.XmlException;
33
import org.apache.xmlbeans.XmlException;
30
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
34
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
Lines 39-45 Link Here
39
     *  to search, so we wrap things with our own
43
     *  to search, so we wrap things with our own
40
     *  map for fast lookup.
44
     *  map for fast lookup.
41
     */
45
     */
42
    private Map<String, CTComment> commentRefs;
46
    private Map<CellAddress, CTComment> commentRefs;
43
47
44
    public CommentsTable() {
48
    public CommentsTable() {
45
        super();
49
        super();
Lines 79-88 Link Here
79
     * Called after the reference is updated, so that
83
     * Called after the reference is updated, so that
80
     *  we can reflect that in our cache
84
     *  we can reflect that in our cache
81
     */
85
     */
82
    public void referenceUpdated(String oldReference, CTComment comment) {
86
    public void referenceUpdated(CellAddress oldReference, CTComment comment) {
83
       if(commentRefs != null) {
87
       if(commentRefs != null) {
84
          commentRefs.remove(oldReference);
88
          commentRefs.remove(oldReference);
85
          commentRefs.put(comment.getRef(), comment);
89
          commentRefs.put(new CellAddress(comment.getRef()), comment);
86
       }
90
       }
87
    }
91
    }
88
92
Lines 109-131 Link Here
109
        return addNewAuthor(author);
113
        return addNewAuthor(author);
110
    }
114
    }
111
115
112
    public XSSFComment findCellComment(String cellRef) {
116
    public XSSFComment findCellComment(CellAddress cellRef) {
113
        CTComment ct = getCTComment(cellRef);
117
        CTComment ct = getCTComment(cellRef);
114
        return ct == null ? null : new XSSFComment(this, ct, null);
118
        return ct == null ? null : new XSSFComment(this, ct, null);
115
    }
119
    }
116
120
117
    @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
121
    @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
118
    public CTComment getCTComment(String cellRef) {
122
    public CTComment getCTComment(CellAddress cellRef) {
123
        // Create the cache if needed
124
        prepareCTCommentCache();
125
126
        // Return the comment, or null if not known
127
        return commentRefs.get(cellRef);
128
    }
129
    
130
    public Map<CellAddress, Comment> getCellComments(){
131
        prepareCTCommentCache();
132
        final TreeMap<CellAddress, Comment> map = new TreeMap<CellAddress, Comment>();
133
        
134
        for(final Entry<CellAddress, CTComment> e: commentRefs.entrySet()){
135
            map.put(e.getKey(), new XSSFComment(this, e.getValue(), null));
136
        }
137
        
138
        return map;
139
    }
140
141
    @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
142
    private void prepareCTCommentCache() {
119
        // Create the cache if needed
143
        // Create the cache if needed
120
        if(commentRefs == null) {
144
        if(commentRefs == null) {
121
           commentRefs = new HashMap<String, CTComment>();
145
           commentRefs = new HashMap<CellAddress, CTComment>();
122
           for (CTComment comment : comments.getCommentList().getCommentArray()) {
146
           for (CTComment comment : comments.getCommentList().getCommentArray()) {
123
              commentRefs.put(comment.getRef(), comment);
147
              commentRefs.put(new CellAddress(comment.getRef()), comment);
124
           }
148
           }
125
        }
149
        }
126
127
        // Return the comment, or null if not known
128
        return commentRefs.get(cellRef);
129
    }
150
    }
130
151
131
    /**
152
    /**
Lines 135-161 Link Here
135
     */
156
     */
136
    @Deprecated
157
    @Deprecated
137
    public CTComment newComment() {
158
    public CTComment newComment() {
138
        return newComment("A1");
159
        return newComment(CellAddress.A1);
139
    }
160
    }
140
    
161
    
141
    public CTComment newComment(String ref) {
162
    public CTComment newComment(CellAddress ref) {
142
        CTComment ct = comments.getCommentList().addNewComment();
163
        CTComment ct = comments.getCommentList().addNewComment();
143
        ct.setRef(ref);
164
        ct.setRef(ref.toString());
144
        ct.setAuthorId(0);
165
        ct.setAuthorId(0);
145
        
166
        
146
        if(commentRefs != null) {
167
        if(commentRefs != null) {
147
           commentRefs.put(ct.getRef(), ct);
168
           commentRefs.put(ref, ct);
148
        }
169
        }
149
        return ct;
170
        return ct;
150
    }
171
    }
151
172
152
    public boolean removeComment(String cellRef) {
173
    public boolean removeComment(CellAddress cellRef) {
174
        final String stringRef = cellRef.toString();
153
        CTCommentList lst = comments.getCommentList();
175
        CTCommentList lst = comments.getCommentList();
154
        if(lst != null) {
176
        if(lst != null) {
155
            CTComment[] commentArray = lst.getCommentArray();
177
            CTComment[] commentArray = lst.getCommentArray();
156
            for (int i = 0; i < commentArray.length; i++) {
178
            for (int i = 0; i < commentArray.length; i++) {
157
                CTComment comment = commentArray[i];
179
                CTComment comment = commentArray[i];
158
                if (cellRef.equals(comment.getRef())) {
180
                if (stringRef.equals(comment.getRef())) {
159
                    lst.removeComment(i);
181
                    lst.removeComment(i);
160
182
161
                    if(commentRefs != null) {
183
                    if(commentRefs != null) {
(-)src/java/org/apache/poi/ss/usermodel/Sheet.java (+9 lines)
Lines 19-26 Link Here
19
19
20
import java.util.Iterator;
20
import java.util.Iterator;
21
import java.util.List;
21
import java.util.List;
22
import java.util.Map;
22
23
23
import org.apache.poi.hssf.util.PaneInformation;
24
import org.apache.poi.hssf.util.PaneInformation;
25
import org.apache.poi.ss.util.CellAddress;
24
import org.apache.poi.ss.util.CellRangeAddress;
26
import org.apache.poi.ss.util.CellRangeAddress;
25
27
26
/**
28
/**
Lines 1067-1070 Link Here
1067
     *  you take it out of them.
1069
     *  you take it out of them.
1068
     */
1070
     */
1069
    int getColumnOutlineLevel(int columnIndex);
1071
    int getColumnOutlineLevel(int columnIndex);
1072
1073
1074
    /**
1075
     * Returns all cell comments of this sheet.
1076
     * @return A map of CellAdress objects and their associated comments.
1077
     */
1078
    Map<CellAddress, Comment> getCellComments();
1070
}
1079
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java (-1 / +2 lines)
Lines 33-38 Link Here
33
import org.apache.poi.openxml4j.opc.TargetMode;
33
import org.apache.poi.openxml4j.opc.TargetMode;
34
import org.apache.poi.ss.usermodel.ClientAnchor;
34
import org.apache.poi.ss.usermodel.ClientAnchor;
35
import org.apache.poi.ss.usermodel.Drawing;
35
import org.apache.poi.ss.usermodel.Drawing;
36
import org.apache.poi.ss.util.CellAddress;
36
import org.apache.poi.ss.util.CellReference;
37
import org.apache.poi.ss.util.CellReference;
37
import org.apache.poi.util.Internal;
38
import org.apache.poi.util.Internal;
38
import org.apache.poi.util.Units;
39
import org.apache.poi.util.Units;
Lines 312-318 Link Here
312
                    ca.getRow2() + ", " + dy2Pixels;
313
                    ca.getRow2() + ", " + dy2Pixels;
313
            vmlShape.getClientDataArray(0).setAnchorArray(0, position);
314
            vmlShape.getClientDataArray(0).setAnchorArray(0, position);
314
        }
315
        }
315
        String ref = new CellReference(ca.getRow1(), ca.getCol1()).formatAsString();
316
        CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
316
317
317
        if(comments.findCellComment(ref) != null) {
318
        if(comments.findCellComment(ref) != null) {
318
            throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
319
            throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
(-)src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java (-21 / +26 lines)
Lines 34-39 Link Here
34
import org.apache.poi.ss.usermodel.Row;
34
import org.apache.poi.ss.usermodel.Row;
35
import org.apache.poi.ss.usermodel.Sheet;
35
import org.apache.poi.ss.usermodel.Sheet;
36
import org.apache.poi.ss.usermodel.Workbook;
36
import org.apache.poi.ss.usermodel.Workbook;
37
import org.apache.poi.ss.util.CellAddress;
37
import org.apache.poi.xssf.XSSFTestDataSamples;
38
import org.apache.poi.xssf.XSSFTestDataSamples;
38
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
39
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
39
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
40
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
Lines 86-94 Link Here
86
		comment1.setText(ctrst1);
87
		comment1.setText(ctrst1);
87
88
88
		// test finding the right comment for a cell
89
		// test finding the right comment for a cell
89
		assertSame(comment0, sheetComments.getCTComment("A1"));
90
		assertSame(comment0, sheetComments.getCTComment(new CellAddress("A1")));
90
		assertSame(comment1, sheetComments.getCTComment("A2"));
91
		assertSame(comment1, sheetComments.getCTComment(new CellAddress("A2")));
91
		assertNull(sheetComments.getCTComment("A3"));
92
		assertNull(sheetComments.getCTComment(new CellAddress("A3")));
92
	}
93
	}
93
94
94
95
Lines 210-242 Link Here
210
211
211
	@Test
212
	@Test
212
	public void removeComment() throws Exception {
213
	public void removeComment() throws Exception {
214
	    final CellAddress adra1 = new CellAddress("A1");
215
        final CellAddress adra2 = new CellAddress("A2");
216
        final CellAddress adra3 = new CellAddress("A3");
217
	    
213
        CommentsTable sheetComments = new CommentsTable();
218
        CommentsTable sheetComments = new CommentsTable();
214
        CTComment a1 = sheetComments.newComment("A1");
219
        CTComment a1 = sheetComments.newComment(adra1);
215
        CTComment a2 = sheetComments.newComment("A2");
220
        CTComment a2 = sheetComments.newComment(adra2);
216
        CTComment a3 = sheetComments.newComment("A3");
221
        CTComment a3 = sheetComments.newComment(adra3);
217
222
218
        assertSame(a1, sheetComments.getCTComment("A1"));
223
        assertSame(a1, sheetComments.getCTComment(adra1));
219
        assertSame(a2, sheetComments.getCTComment("A2"));
224
        assertSame(a2, sheetComments.getCTComment(adra2));
220
        assertSame(a3, sheetComments.getCTComment("A3"));
225
        assertSame(a3, sheetComments.getCTComment(adra3));
221
        assertEquals(3, sheetComments.getNumberOfComments());
226
        assertEquals(3, sheetComments.getNumberOfComments());
222
227
223
        assertTrue(sheetComments.removeComment("A1"));
228
        assertTrue(sheetComments.removeComment(adra1));
224
        assertEquals(2, sheetComments.getNumberOfComments());
229
        assertEquals(2, sheetComments.getNumberOfComments());
225
        assertNull(sheetComments.getCTComment("A1"));
230
        assertNull(sheetComments.getCTComment(adra1));
226
        assertSame(a2, sheetComments.getCTComment("A2"));
231
        assertSame(a2, sheetComments.getCTComment(adra2));
227
        assertSame(a3, sheetComments.getCTComment("A3"));
232
        assertSame(a3, sheetComments.getCTComment(adra3));
228
233
229
        assertTrue(sheetComments.removeComment("A2"));
234
        assertTrue(sheetComments.removeComment(adra2));
230
        assertEquals(1, sheetComments.getNumberOfComments());
235
        assertEquals(1, sheetComments.getNumberOfComments());
231
        assertNull(sheetComments.getCTComment("A1"));
236
        assertNull(sheetComments.getCTComment(adra1));
232
        assertNull(sheetComments.getCTComment("A2"));
237
        assertNull(sheetComments.getCTComment(adra2));
233
        assertSame(a3, sheetComments.getCTComment("A3"));
238
        assertSame(a3, sheetComments.getCTComment(adra3));
234
239
235
        assertTrue(sheetComments.removeComment("A3"));
240
        assertTrue(sheetComments.removeComment(adra3));
236
        assertEquals(0, sheetComments.getNumberOfComments());
241
        assertEquals(0, sheetComments.getNumberOfComments());
237
        assertNull(sheetComments.getCTComment("A1"));
242
        assertNull(sheetComments.getCTComment(adra1));
238
        assertNull(sheetComments.getCTComment("A2"));
243
        assertNull(sheetComments.getCTComment(adra2));
239
        assertNull(sheetComments.getCTComment("A3"));
244
        assertNull(sheetComments.getCTComment(adra3));
240
    }
245
    }
241
246
242
	@Test
247
	@Test
(-)src/java/org/apache/poi/ss/util/CellAddress.java (+105 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.ss.util;
19
20
import java.util.Locale;
21
22
/**
23
 * This class is a container for POI usermodel row=0 column=0 cell references.
24
 * It is barely a container for these two coordinates. The implementation
25
 * of the Comparable interface sorts by "natural" order top left to bottom right.
26
 */
27
public class CellAddress implements Comparable<CellAddress>{
28
    /** A constant for references to the first cell in a sheet. */
29
    public static final CellAddress A1 = new CellAddress(0, 0);
30
    
31
    private final int _row;
32
    private final int _col;
33
    
34
    /**
35
     * Create a new CellAddress object.
36
     *
37
     * @param row Row index (first row is 0)
38
     * @param column Column index (first column is 0)
39
     */
40
    public CellAddress(int row, int column) {
41
        super();
42
        this._row = row;
43
        this._col = column;
44
    }
45
    
46
    public int getRow() {
47
        return _row;
48
    }
49
50
    public int getColumn() {
51
        return _col;
52
    }
53
54
    public int compareTo(CellAddress o) {
55
        int r = this._row-o._row;
56
        if (r!=0) return r;
57
58
        r = this._col-o._col;
59
        if (r!=0) return r;
60
61
        return 0;
62
    }
63
64
    public boolean equals(Object o) {
65
        if (this == o) {
66
            return true;
67
        }
68
        if(!(o instanceof CellAddress)) {
69
            return false;
70
        }
71
        
72
        CellAddress cr = (CellAddress) o;
73
        return _row == cr._row
74
                && _col == cr._col
75
        ;
76
    }
77
78
    public int hashCode() {
79
        return this._row + this._col<<16;
80
    }
81
82
    public CellAddress(String reference) {
83
        int length = reference.length();
84
85
        int loc = 0;
86
        // step over column name chars until first digit for row number.
87
        for (; loc < length; loc++) {
88
            char ch = reference.charAt(loc);
89
            if (Character.isDigit(ch)) {
90
                break;
91
            }
92
        }
93
94
        String sCol = reference.substring(0,loc).toUpperCase(Locale.ROOT);
95
        String sRow = reference.substring(loc);
96
97
        
98
        this._row = Integer.parseInt(sRow)-1;
99
        this._col = CellReference.convertColStringToIndex(sCol);
100
    }
101
102
    public String toString() {
103
        return CellReference.convertNumToColString(this._col)+(this._row+1);
104
    }
105
}
(-)src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (+5 lines)
Lines 41-46 Link Here
41
import org.apache.poi.ss.usermodel.Sheet;
41
import org.apache.poi.ss.usermodel.Sheet;
42
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
42
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
43
import org.apache.poi.ss.usermodel.Workbook;
43
import org.apache.poi.ss.usermodel.Workbook;
44
import org.apache.poi.ss.util.CellAddress;
44
import org.apache.poi.ss.util.CellRangeAddress;
45
import org.apache.poi.ss.util.CellRangeAddress;
45
import org.apache.poi.ss.util.SheetUtil;
46
import org.apache.poi.ss.util.SheetUtil;
46
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
47
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
Lines 1539-1542 Link Here
1539
    public int getColumnOutlineLevel(int columnIndex) {
1540
    public int getColumnOutlineLevel(int columnIndex) {
1540
        return _sh.getColumnOutlineLevel(columnIndex);
1541
        return _sh.getColumnOutlineLevel(columnIndex);
1541
    }
1542
    }
1543
1544
    public Map<CellAddress, Comment> getCellComments() {
1545
        return _sh.getCellComments();
1546
    }
1542
}
1547
}

Return to bug 58365