KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.poi.hssf.record.MergeCellsRecord.MergedRegion;
21
22 /**
23  * Represents a from/to row/col square. This is a object primitive
24  * that can be used to represent row,col - row,col just as one would use String
25  * to represent a string of characters. Its really only useful for HSSF though.
26  *
27  * @author Andrew C. Oliver acoliver at apache dot org
28  */

29
30 public class Region
31     implements Comparable JavaDoc
32 {
33     private int rowFrom;
34     private short colFrom;
35     private int rowTo;
36     private short colTo;
37
38     /**
39      * Creates a new instance of Region (0,0 - 0,0)
40      */

41
42     public Region()
43     {
44     }
45
46     public Region(int rowFrom, short colFrom, int rowTo, short colTo)
47     {
48         this.rowFrom = rowFrom;
49         this.rowTo = rowTo;
50         this.colFrom = colFrom;
51         this.colTo = colTo;
52     }
53
54     /**
55      * special constructor (I know this is bad but it is so wrong that its right
56      * okay) that makes a region from a mergedcells's region subrecord.
57      */

58
59     public Region(MergedRegion region)
60     {
61         this(region.row_from, region.col_from, region.row_to, region.col_to);
62     }
63
64     /**
65      * get the upper left hand corner column number
66      *
67      * @return column number for the upper left hand corner
68      */

69
70     public short getColumnFrom()
71     {
72         return colFrom;
73     }
74
75     /**
76      * get the upper left hand corner row number
77      *
78      * @return row number for the upper left hand corner
79      */

80
81     public int getRowFrom()
82     {
83         return rowFrom;
84     }
85
86     /**
87      * get the lower right hand corner column number
88      *
89      * @return column number for the lower right hand corner
90      */

91
92     public short getColumnTo()
93     {
94         return colTo;
95     }
96
97     /**
98      * get the lower right hand corner row number
99      *
100      * @return row number for the lower right hand corner
101      */

102
103     public int getRowTo()
104     {
105         return rowTo;
106     }
107
108     /**
109      * set the upper left hand corner column number
110      *
111      * @param colFrom column number for the upper left hand corner
112      */

113
114     public void setColumnFrom(short colFrom)
115     {
116         this.colFrom = colFrom;
117     }
118
119     /**
120      * set the upper left hand corner row number
121      *
122      * @param rowFrom row number for the upper left hand corner
123      */

124
125     public void setRowFrom(int rowFrom)
126     {
127         this.rowFrom = rowFrom;
128     }
129
130     /**
131      * set the lower right hand corner column number
132      *
133      * @param colTo column number for the lower right hand corner
134      */

135
136     public void setColumnTo(short colTo)
137     {
138         this.colTo = colTo;
139     }
140
141     /**
142      * get the lower right hand corner row number
143      *
144      * @param rowTo row number for the lower right hand corner
145      */

146
147     public void setRowTo(int rowTo)
148     {
149         this.rowTo = rowTo;
150     }
151
152     /**
153      * Answers: "is the row/column inside this range?"
154      *
155      * @return <code>true</code> if the cell is in the range and
156      * <code>false</code> if it is not
157      */

158
159     public boolean contains(int row, short col)
160     {
161         if ((this.rowFrom <= row) && (this.rowTo >= row)
162                 && (this.colFrom <= col) && (this.colTo >= col))
163         {
164
165 // System.out.println("Region ("+rowFrom+","+colFrom+","+rowTo+","+
166
// colTo+") does contain "+row+","+col);
167
return true;
168         }
169         return false;
170     }
171
172     public boolean equals(Region r)
173     {
174         return (compareTo(r) == 0);
175     }
176
177     /**
178      * Compares that the given region is the same less than or greater than this
179      * region. If any regional coordiant passed in is less than this regions
180      * coordinants then a positive integer is returned. Otherwise a negative
181      * integer is returned.
182      *
183      * @param r region
184      * @see #compareTo(Object)
185      */

186
187     public int compareTo(Region r)
188     {
189         if ((this.getRowFrom() == r.getRowFrom())
190                 && (this.getColumnFrom() == r.getColumnFrom())
191                 && (this.getRowTo() == r.getRowTo())
192                 && (this.getColumnTo() == r.getColumnTo()))
193         {
194             return 0;
195         }
196         if ((this.getRowFrom() < r.getRowFrom())
197                 || (this.getColumnFrom() < r.getColumnFrom())
198                 || (this.getRowTo() < r.getRowTo())
199                 || (this.getColumnTo() < r.getColumnTo()))
200         {
201             return 1;
202         }
203         return -1;
204     }
205
206     public int compareTo(Object JavaDoc o)
207     {
208         return compareTo(( Region ) o);
209     }
210
211     /**
212      * @return the area contained by this region (number of cells)
213      */

214
215     public int getArea()
216     {
217         return ((1 + (getRowTo() - getRowFrom()))
218                 * (1 + (getColumnTo() - getColumnFrom())));
219     }
220 }
221
Popular Tags