KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > biff > ColumnInfoRecord


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.write.biff;
21
22
23 import jxl.biff.WritableRecordData;
24 import jxl.biff.XFRecord;
25 import jxl.biff.IntegerHelper;
26 import jxl.biff.Type;
27 import jxl.biff.FormattingRecords;
28 import jxl.biff.IndexMapping;
29
30 /**
31  * Describes the column formatting for a particular column
32  */

33 class ColumnInfoRecord extends WritableRecordData
34 {
35   /**
36    * The binary data
37    */

38   private byte[] data;
39   /**
40    * The column number which this format applies to
41    */

42   private int column;
43   /**
44    * The style for the column
45    */

46   private XFRecord style;
47   /**
48    * The index for the style of this column
49    */

50   private int xfIndex;
51
52   /**
53    * The width of the column in 1/256 of a character
54    */

55   private int width;
56
57   /**
58    * Flag to indicate the hidden status of this column
59    */

60   private boolean hidden;
61
62   /**
63    * Constructor used when setting column information from the user
64    * API
65    *
66    * @param w the width of the column in characters
67    * @param col the column to format
68    * @param xf the style for the column
69    */

70   public ColumnInfoRecord(int col, int w, XFRecord xf)
71   {
72     super(Type.COLINFO);
73
74     column = col;
75     width = w;
76     style = xf;
77     xfIndex = style.getXFIndex();
78     hidden = false;
79   }
80
81   /**
82    * Constructor used when copying an existing spreadsheet
83    *
84    * @param col the column number
85    * @param cir the column info record read in
86    * @param fr the format records
87    */

88   public ColumnInfoRecord(jxl.read.biff.ColumnInfoRecord cir,
89                           int col,
90                           FormattingRecords fr)
91   {
92     super(Type.COLINFO);
93
94     column = col;
95     width = cir.getWidth();
96     xfIndex = cir.getXFIndex();
97     style = fr.getXFRecord(xfIndex);
98   }
99
100   /**
101    * Gets the column this format applies to
102    *
103    * @return the column which is formatted
104    */

105   public int getColumn()
106   {
107     return column;
108   }
109
110   /**
111    * Increments the column. Called when inserting a new column into
112    * the sheet
113    */

114   public void incrementColumn()
115   {
116     column++;
117   }
118
119   /**
120    * Decrements the column. Called when removing a column from
121    * the sheet
122    */

123   public void decrementColumn()
124   {
125     column--;
126   }
127
128   /**
129    * Accessor for the width
130    *
131    * @return the width
132    */

133   int getWidth()
134   {
135     return width;
136   }
137
138   /**
139    * Gets the binary data to be written to the output file
140    *
141    * @return the data to write to file
142    */

143   public byte[] getData()
144   {
145     data = new byte[0x0c];
146
147     IntegerHelper.getTwoBytes(column, data, 0);
148     IntegerHelper.getTwoBytes(column, data, 2);
149     IntegerHelper.getTwoBytes(width, data, 4);
150     IntegerHelper.getTwoBytes(xfIndex, data, 6);
151
152     // int options = 0x2;
153
int options = 0x6;
154     if (hidden)
155     {
156       options |= 0x1;
157     }
158     IntegerHelper.getTwoBytes(options, data, 8);
159     // IntegerHelper.getTwoBytes(2, data, 10);
160

161     return data;
162   }
163
164   /**
165    * Gets the cell format associated with this column info record
166    *
167    * @retun the cell format for this column
168    */

169   public XFRecord getCellFormat()
170   {
171     return style;
172   }
173
174   /**
175    * Rationalizes the sheets xf index mapping
176    * @param xfmapping the index mapping
177    */

178   void rationalize(IndexMapping xfmapping)
179   {
180     xfIndex = xfmapping.getNewIndex(xfIndex);
181   }
182
183   /**
184    * Sets this column to be hidden (or otherwise)
185    *
186    * @param h TRUE if the column is to be hidden, FALSE otherwise
187    */

188   void setHidden(boolean h)
189   {
190     hidden = h;
191   }
192
193   /**
194    * Accessor for the hidden flag
195    *
196    * @return TRUE if this column is hidden, FALSE otherwise
197    */

198   boolean getHidden()
199   {
200     return hidden;
201   }
202
203   /**
204    * Standard equals method
205    *
206    * @return TRUE if these objects are equal, FALSE otherwise
207    */

208   public boolean equals(Object JavaDoc o)
209   {
210     if (o == this)
211     {
212       return true;
213     }
214
215     if (!(o instanceof ColumnInfoRecord))
216     {
217       return false;
218     }
219
220     ColumnInfoRecord cir = (ColumnInfoRecord) o;
221
222     int col2 = cir.column;
223
224     if (column != cir.column ||
225         xfIndex != cir.xfIndex ||
226         width != cir.width ||
227         hidden != cir.hidden)
228     {
229       return false;
230     }
231
232     if ((style == null && cir.style != null) ||
233         (style != null && cir.style == null))
234     {
235       return false;
236     }
237     
238     return style.equals(cir.style);
239   }
240
241   /**
242    * Standard hashCode method
243    *
244    * @return the hashCode
245    */

246   public int hashCode()
247   {
248     int hashValue = 137;
249     int oddPrimeNumber = 79;
250     
251     hashValue = hashValue * oddPrimeNumber + column;
252     hashValue = hashValue * oddPrimeNumber + xfIndex;
253     hashValue = hashValue * oddPrimeNumber + width;
254     hashValue = hashValue * oddPrimeNumber + (hidden ? 1:0);
255
256     if (style != null)
257     {
258       hashValue ^= style.hashCode();
259     }
260
261     return hashValue;
262   }
263 }
264
Popular Tags