001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.util;
019:
020: import org.apache.poi.hssf.record.MergeCellsRecord.MergedRegion;
021:
022: /**
023: * Represents a from/to row/col square. This is a object primitive
024: * that can be used to represent row,col - row,col just as one would use String
025: * to represent a string of characters. Its really only useful for HSSF though.
026: *
027: * @author Andrew C. Oliver acoliver at apache dot org
028: */
029:
030: public class Region implements Comparable {
031: private int rowFrom;
032: private short colFrom;
033: private int rowTo;
034: private short colTo;
035:
036: /**
037: * Creates a new instance of Region (0,0 - 0,0)
038: */
039:
040: public Region() {
041: }
042:
043: public Region(int rowFrom, short colFrom, int rowTo, short colTo) {
044: this .rowFrom = rowFrom;
045: this .rowTo = rowTo;
046: this .colFrom = colFrom;
047: this .colTo = colTo;
048: }
049:
050: /**
051: * special constructor (I know this is bad but it is so wrong that its right
052: * okay) that makes a region from a mergedcells's region subrecord.
053: */
054:
055: public Region(MergedRegion region) {
056: this (region.row_from, region.col_from, region.row_to,
057: region.col_to);
058: }
059:
060: /**
061: * get the upper left hand corner column number
062: *
063: * @return column number for the upper left hand corner
064: */
065:
066: public short getColumnFrom() {
067: return colFrom;
068: }
069:
070: /**
071: * get the upper left hand corner row number
072: *
073: * @return row number for the upper left hand corner
074: */
075:
076: public int getRowFrom() {
077: return rowFrom;
078: }
079:
080: /**
081: * get the lower right hand corner column number
082: *
083: * @return column number for the lower right hand corner
084: */
085:
086: public short getColumnTo() {
087: return colTo;
088: }
089:
090: /**
091: * get the lower right hand corner row number
092: *
093: * @return row number for the lower right hand corner
094: */
095:
096: public int getRowTo() {
097: return rowTo;
098: }
099:
100: /**
101: * set the upper left hand corner column number
102: *
103: * @param colFrom column number for the upper left hand corner
104: */
105:
106: public void setColumnFrom(short colFrom) {
107: this .colFrom = colFrom;
108: }
109:
110: /**
111: * set the upper left hand corner row number
112: *
113: * @param rowFrom row number for the upper left hand corner
114: */
115:
116: public void setRowFrom(int rowFrom) {
117: this .rowFrom = rowFrom;
118: }
119:
120: /**
121: * set the lower right hand corner column number
122: *
123: * @param colTo column number for the lower right hand corner
124: */
125:
126: public void setColumnTo(short colTo) {
127: this .colTo = colTo;
128: }
129:
130: /**
131: * get the lower right hand corner row number
132: *
133: * @param rowTo row number for the lower right hand corner
134: */
135:
136: public void setRowTo(int rowTo) {
137: this .rowTo = rowTo;
138: }
139:
140: /**
141: * Answers: "is the row/column inside this range?"
142: *
143: * @return <code>true</code> if the cell is in the range and
144: * <code>false</code> if it is not
145: */
146:
147: public boolean contains(int row, short col) {
148: if ((this .rowFrom <= row) && (this .rowTo >= row)
149: && (this .colFrom <= col) && (this .colTo >= col)) {
150:
151: // System.out.println("Region ("+rowFrom+","+colFrom+","+rowTo+","+
152: // colTo+") does contain "+row+","+col);
153: return true;
154: }
155: return false;
156: }
157:
158: public boolean equals(Region r) {
159: return (compareTo(r) == 0);
160: }
161:
162: /**
163: * Compares that the given region is the same less than or greater than this
164: * region. If any regional coordiant passed in is less than this regions
165: * coordinants then a positive integer is returned. Otherwise a negative
166: * integer is returned.
167: *
168: * @param r region
169: * @see #compareTo(Object)
170: */
171:
172: public int compareTo(Region r) {
173: if ((this .getRowFrom() == r.getRowFrom())
174: && (this .getColumnFrom() == r.getColumnFrom())
175: && (this .getRowTo() == r.getRowTo())
176: && (this .getColumnTo() == r.getColumnTo())) {
177: return 0;
178: }
179: if ((this .getRowFrom() < r.getRowFrom())
180: || (this .getColumnFrom() < r.getColumnFrom())
181: || (this .getRowTo() < r.getRowTo())
182: || (this .getColumnTo() < r.getColumnTo())) {
183: return 1;
184: }
185: return -1;
186: }
187:
188: public int compareTo(Object o) {
189: return compareTo((Region) o);
190: }
191:
192: /**
193: * @return the area contained by this region (number of cells)
194: */
195:
196: public int getArea() {
197: return ((1 + (getRowTo() - getRowFrom())) * (1 + (getColumnTo() - getColumnFrom())));
198: }
199: }
|