1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/
19
20 package jxl.biff;
21
22 import jxl.Cell;
23 import jxl.Range;
24 import jxl.Sheet;
25
26 /**
27 * Implementation class for the Range interface. This merely
28 * holds the raw range information. This implementation is used
29 * for ranges which are present on the current working sheet, so the
30 * getSheetIndex merely returns -1
31 */
32 public class SheetRangeImpl implements Range
33 {
34 /**
35 * A handle to the sheet containing this range
36 */
37 private Sheet sheet;
38
39 /**
40 * The column number of the cell at the top left of the range
41 */
42 private int column1;
43
44 /**
45 * The row number of the cell at the top left of the range
46 */
47 private int row1;
48
49 /**
50 * The column index of the cell at the bottom right
51 */
52 private int column2;
53
54 /**
55 * The row index of the cell at the bottom right
56 */
57 private int row2;
58
59 /**
60 * Constructor
61 * @param s the sheet containing the range
62 * @param c1 the column number of the top left cell of the range
63 * @param r1 the row number of the top left cell of the range
64 * @param c2 the column number of the bottom right cell of the range
65 * @param r2 the row number of the bottomr right cell of the range
66 */
67 public SheetRangeImpl(Sheet s, int c1, int r1,
68 int c2, int r2)
69 {
70 sheet = s;
71 row1 = r1;
72 row2 = r2;
73 column1 = c1;
74 column2 = c2;
75 }
76
77 /**
78 * A copy constructor used for copying ranges between sheets
79 *
80 * @param c the range to copy from
81 * @param s the writable sheet
82 */
83 public SheetRangeImpl(SheetRangeImpl c, Sheet s)
84 {
85 sheet = s;
86 row1 = c.row1;
87 row2 = c.row2;
88 column1 = c.column1;
89 column2 = c.column2;
90 }
91
92 /**
93 * Gets the cell at the top left of this range
94 *
95 * @return the cell at the top left
96 */
97 public Cell getTopLeft()
98 {
99 // If the print area exceeds the bounds of the sheet, then handle
100 // it here. The sheet implementation will give a NPE
101 if (column1 >= sheet.getColumns() ||
102 row1 >= sheet.getRows())
103 {
104 return new EmptyCell(column1,row1);
105 }
106
107 return sheet.getCell(column1, row1);
108 }
109
110 /**
111 * Gets the cell at the bottom right of this range
112 *
113 * @return the cell at the bottom right
114 */
115 public Cell getBottomRight()
116 {
117 // If the print area exceeds the bounds of the sheet, then handle
118 // it here. The sheet implementation will give a NPE
119 if (column2 >= sheet.getColumns() ||
120 row2 >= sheet.getRows())
121 {
122 return new EmptyCell(column2,row2);
123 }
124
125 return sheet.getCell(column2, row2);
126 }
127
128 /**
129 * Not supported. Returns -1, indicating that it refers to the current
130 * sheet
131 *
132 * @return -1
133 */
134 public int getFirstSheetIndex()
135 {
136 return -1;
137 }
138
139 /**
140 * Not supported. Returns -1, indicating that it refers to the current
141 * sheet
142 *
143 * @return -1
144 */
145 public int getLastSheetIndex()
146 {
147 return -1;
148 }
149
150 /**
151 * Sees whether there are any intersections between this range and the
152 * range passed in. This method is used internally by the WritableSheet to
153 * verify the integrity of merged cells, hyperlinks etc. Ranges are
154 * only ever compared for the same sheet
155 *
156 * @param range the range to compare against
157 * @return TRUE if the ranges intersect, FALSE otherwise
158 */
159 public boolean intersects(SheetRangeImpl range)
160 {
161 if (range == this)
162 {
163 return true;
164 }
165
166 if (row2 < range.row1 ||
167 row1 > range.row2 ||
168 column2 < range.column1 ||
169 column1 > range.column2)
170 {
171 return false;
172 }
173
174 return true;
175 }
176
177 /**
178 * To string method - primarily used during debugging
179 *
180 * @return the string version of this object
181 */
182 public String toString()
183 {
184 StringBuffer sb = new StringBuffer();
185 CellReferenceHelper.getCellReference(column1, row1, sb);
186 sb.append('-');
187 CellReferenceHelper.getCellReference(column2, row2, sb);
188 return sb.toString();
189 }
190
191 /**
192 * A row has been inserted, so adjust the range objects accordingly
193 *
194 * @param r the row which has been inserted
195 */
196 public void insertRow(int r)
197 {
198 if (r > row2)
199 {
200 return;
201 }
202
203 if (r <= row1)
204 {
205 row1++;
206 }
207
208 if (r <= row2)
209 {
210 row2++;
211 }
212 }
213
214 /**
215 * A column has been inserted, so adjust the range objects accordingly
216 *
217 * @param c the column which has been inserted
218 */
219 public void insertColumn(int c)
220 {
221 if (c > column2)
222 {
223 return;
224 }
225
226 if (c <= column1)
227 {
228 column1++;
229 }
230
231 if (c <= column2)
232 {
233 column2++;
234 }
235 }
236
237 /**
238 * A row has been removed, so adjust the range objects accordingly
239 *
240 * @param r the row which has been inserted
241 */
242 public void removeRow(int r)
243 {
244 if (r > row2)
245 {
246 return;
247 }
248
249 if (r < row1)
250 {
251 row1--;
252 }
253
254 if (r < row2)
255 {
256 row2--;
257 }
258 }
259
260 /**
261 * A column has been removed, so adjust the range objects accordingly
262 *
263 * @param c the column which has been removed
264 */
265 public void removeColumn(int c)
266 {
267 if (c > column2)
268 {
269 return;
270 }
271
272 if (c < column1)
273 {
274 column1--;
275 }
276
277 if (c < column2)
278 {
279 column2--;
280 }
281 }
282
283 /**
284 * Standard hash code method
285 *
286 * @return the hash code
287 */
288 public int hashCode()
289 {
290 return 0xffff ^ row1 ^ row2 ^ column1 ^ column2;
291 }
292
293 /**
294 * Standard equals method
295 *
296 * @param o the object to compare
297 * @return TRUE if the two objects are the same, FALSE otherwise
298 */
299 public boolean equals(Object o)
300 {
301 if (o == this)
302 {
303 return true;
304 }
305
306 if (!(o instanceof SheetRangeImpl))
307 {
308 return false;
309 }
310
311 SheetRangeImpl compare = (SheetRangeImpl) o;
312
313 return (column1 == compare.column1 &&
314 column2 == compare.column2 &&
315 row1 == compare.row1 &&
316 row2 == compare.row2);
317 }
318
319 }
320
321
322
323
324
325
326
327
328
329