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

(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (+6 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 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/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/model/CommentsTable.java (-3 / +24 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 117-131 Link Here
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(String cellRef) {
119
        // Create the cache if needed
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<String, CTComment> e: commentRefs.entrySet()){
135
            map.put(CellAddress.fromString(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() {
143
        // Create the cache if needed
120
        if(commentRefs == null) {
144
        if(commentRefs == null) {
121
           commentRefs = new HashMap<String, CTComment>();
145
           commentRefs = new HashMap<String, CTComment>();
122
           for (CTComment comment : comments.getCommentList().getCommentArray()) {
146
           for (CTComment comment : comments.getCommentList().getCommentArray()) {
123
              commentRefs.put(comment.getRef(), comment);
147
              commentRefs.put(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
    /**
(-)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/java/org/apache/poi/ss/util/CellAddress.java (+101 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
    private final int _row;
29
    private final int _col;
30
    
31
    /**
32
     * Create a new CellAddress object.
33
     *
34
     * @param row Row index (first row is 0)
35
     * @param column Column index (first column is 0)
36
     */
37
    public CellAddress(int row, int column) {
38
        super();
39
        this._row = row;
40
        this._col = column;
41
    }
42
    
43
    public int getRow() {
44
        return _row;
45
    }
46
47
    public int getColumn() {
48
        return _col;
49
    }
50
51
    public int compareTo(CellAddress o) {
52
        int r = this._row-o._row;
53
        if (r!=0) return r;
54
55
        r = this._col-o._col;
56
        if (r!=0) return r;
57
58
        return 0;
59
    }
60
61
    public boolean equals(Object o) {
62
        if (this == o) {
63
            return true;
64
        }
65
        if(!(o instanceof CellAddress)) {
66
            return false;
67
        }
68
        
69
        CellAddress cr = (CellAddress) o;
70
        return _row == cr._row
71
                && _col == cr._col
72
        ;
73
    }
74
75
    public int hashCode() {
76
        return this._row + this._col<<16;
77
    }
78
79
    public static CellAddress fromString(String reference) {
80
        int length = reference.length();
81
82
        int loc = 0;
83
        // step over column name chars until first digit for row number.
84
        for (; loc < length; loc++) {
85
            char ch = reference.charAt(loc);
86
            if (Character.isDigit(ch)) {
87
                break;
88
            }
89
        }
90
91
        String sCol = reference.substring(0,loc).toUpperCase(Locale.ROOT);
92
        String sRow = reference.substring(loc);
93
94
        
95
        return new CellAddress(Integer.parseInt(sRow)-1, CellReference.convertColStringToIndex(sCol));
96
    }
97
98
    public String toString() {
99
        return CellReference.convertNumToColString(this._col)+(this._row+1);
100
    }
101
}
(-)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