KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > util > AreaReference


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17
18 package org.apache.poi.hssf.util;
19
20 public class AreaReference {
21
22
23 private CellReference [] cells;
24 private int dim;
25
26     /** Create an area ref from a string representation
27      */

28     public AreaReference(String JavaDoc reference) {
29         String JavaDoc[] refs = seperateAreaRefs(reference);
30         dim = refs.length;
31         cells = new CellReference[dim];
32         for (int i=0;i<dim;i++) {
33             cells[i]=new CellReference(refs[i]);
34         }
35     }
36     //not sure if we need to be flexible here!
37
/** return the dimensions of this area
38      **/

39     public int getDim() {
40         return dim;
41     }
42     /** return the cell references that define this area */
43     public CellReference[] getCells() {
44         return cells;
45     }
46
47     public String JavaDoc toString() {
48         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
49         for (int i=0;i<dim;i++){
50             retval.append(':');
51             retval.append(cells[i].toString());
52         }
53         retval.deleteCharAt(0);
54         return retval.toString();
55     }
56
57     /**
58      * seperates Area refs in two parts and returns them as seperate elements in a
59      * String array
60      */

61     private String JavaDoc[] seperateAreaRefs(String JavaDoc reference) {
62         String JavaDoc[] retval = null;
63
64         int length = reference.length();
65
66         int loc = reference.indexOf(':',0);
67         if(loc == -1){
68            retval = new String JavaDoc[1];
69            retval[0] = reference;
70         }
71         else{
72            retval = new String JavaDoc[2];
73            int sheetStart = reference.indexOf("!");
74
75            retval[0] = reference.substring(0, sheetStart+1) + reference.substring(sheetStart + 1,loc);
76            retval[1] = reference.substring(0, sheetStart+1) + reference.substring(loc+1);
77         }
78         return retval;
79     }
80 }
Popular Tags