KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > SheetRangeImpl


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.Range;
23 import jxl.Cell;
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     return sheet.getCell(column1, row1);
100   }
101
102   /**
103    * Gets the cell at the bottom right of this range
104    *
105    * @return the cell at the bottom right
106    */

107   public Cell getBottomRight()
108   {
109     return sheet.getCell(column2, row2);
110   }
111
112   /**
113    * Not supported. Returns -1, indicating that it refers to the current
114    * sheet
115    *
116    * @return -1
117    */

118   public int getFirstSheetIndex()
119   {
120     return -1;
121   }
122
123   /**
124    * Not supported. Returns -1, indicating that it refers to the current
125    * sheet
126    *
127    * @return -1
128    */

129   public int getLastSheetIndex()
130   {
131     return -1;
132   }
133
134   /**
135    * Sees whether there are any intersections between this range and the
136    * range passed in. This method is used internally by the WritableSheet to
137    * verify the integrity of merged cells, hyperlinks etc. Ranges are
138    * only ever compared for the same sheet
139    *
140    * @param range the range to compare against
141    * @return TRUE if the ranges intersect, FALSE otherwise
142    */

143   public boolean intersects(SheetRangeImpl range)
144   {
145     if (range == this)
146     {
147       return true;
148     }
149
150     if (row2 < range.row1 ||
151         row1 > range.row2 ||
152         column2 < range.column1 ||
153         column1 > range.column2)
154     {
155       return false;
156     }
157
158     return true;
159   }
160
161   /**
162    * To string method - primarily used during debugging
163    *
164    * @return the string version of this object
165    */

166   public String JavaDoc toString()
167   {
168     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
169     CellReferenceHelper.getCellReference(column1, row1, sb);
170     sb.append('-');
171     CellReferenceHelper.getCellReference(column2, row2, sb);
172     return sb.toString();
173   }
174
175   /**
176    * A row has been inserted, so adjust the range objects accordingly
177    *
178    * @param r the row which has been inserted
179    */

180   public void insertRow(int r)
181   {
182     if (r > row2)
183     {
184       return;
185     }
186
187     if (r <= row1)
188     {
189       row1++;
190     }
191
192     if (r <= row2)
193     {
194       row2++;
195     }
196   }
197
198   /**
199    * A column has been inserted, so adjust the range objects accordingly
200    *
201    * @param c the column which has been inserted
202    */

203   public void insertColumn(int c)
204   {
205     if (c > column2)
206     {
207       return;
208     }
209
210     if (c <= column1)
211     {
212       column1++;
213     }
214
215     if (c <= column2)
216     {
217       column2++;
218     }
219   }
220
221   /**
222    * A row has been removed, so adjust the range objects accordingly
223    *
224    * @param r the row which has been inserted
225    */

226   public void removeRow(int r)
227   {
228     if (r > row2)
229     {
230       return;
231     }
232
233     if (r < row1)
234     {
235       row1--;
236     }
237
238     if (r < row2)
239     {
240       row2--;
241     }
242   }
243
244   /**
245    * A column has been removed, so adjust the range objects accordingly
246    *
247    * @param c the column which has been removed
248    */

249   public void removeColumn(int c)
250   {
251     if (c > column2)
252     {
253       return;
254     }
255
256     if (c < column1)
257     {
258       column1--;
259     }
260
261     if (c < column2)
262     {
263       column2--;
264     }
265   }
266
267   /**
268    * Standard hash code method
269    *
270    * @return the hash code
271    */

272   public int hashCode()
273   {
274     return 0xffff ^ row1 ^ row2 ^ column1 ^ column2;
275   }
276
277   /**
278    * Standard equals method
279    *
280    * @param o the object to compare
281    * @return TRUE if the two objects are the same, FALSE otherwise
282    */

283   public boolean equals(Object JavaDoc o)
284   {
285     if (o == this)
286     {
287       return true;
288     }
289
290     if (!(o instanceof SheetRangeImpl))
291     {
292       return false;
293     }
294
295     SheetRangeImpl compare = (SheetRangeImpl) o;
296
297     return (column1 == compare.column1 &&
298             column2 == compare.column2 &&
299             row1 == compare.row1 &&
300             row2 == compare.row2);
301   }
302
303 }
304
305
306
307
308
309
310
311
312
313
314
Popular Tags