KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > drawing > Chart


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.drawing;
21
22 import java.io.IOException JavaDoc;
23 import common.Assert;
24 import common.Logger;
25 import jxl.WorkbookSettings;
26 import jxl.biff.ByteData;
27 import jxl.biff.IntegerHelper;
28 import jxl.biff.IndexMapping;
29 import jxl.biff.Type;
30 import jxl.read.biff.File;
31
32 /**
33  * Contains the various biff records used to insert a chart into a
34  * worksheet
35  */

36 public class Chart implements ByteData, EscherStream
37 {
38   /**
39    * The logger
40    */

41   private static final Logger logger = Logger.getLogger(Chart.class);
42
43   /**
44    * The MsoDrawingRecord associated with the chart
45    */

46   private MsoDrawingRecord msoDrawingRecord;
47
48   /**
49    * The ObjRecord associated with the chart
50    */

51   private ObjRecord objRecord;
52
53   /**
54    * The start pos of the chart bof stream in the data file
55    */

56   private int startpos;
57
58   /**
59    * The start pos of the chart bof stream in the data file
60    */

61   private int endpos;
62
63   /**
64    * A handle to the Excel file
65    */

66   private File file;
67
68   /**
69    * The drawing data
70    */

71   private DrawingData drawingData;
72
73   /**
74    * The drawing number
75    */

76   private int drawingNumber;
77
78   /**
79    * The chart byte data
80    */

81   private byte[] data;
82
83   /**
84    * Flag which indicates that the byte data has been initialized
85    */

86   private boolean initialized;
87
88   /**
89    * The workbook settings
90    */

91   private WorkbookSettings workbookSettings;
92
93   /**
94    * Constructor
95    *
96    * @param mso a <code>MsoDrawingRecord</code> value
97    * @param obj an <code>ObjRecord</code> value
98    * @param sp an <code>int</code> value
99    * @param ep an <code>int</code> value
100    * @param f a <code>File</code> value
101    * @param ws the workbook settings
102    */

103   public Chart(MsoDrawingRecord mso, ObjRecord obj,
104                DrawingData dd,
105                int sp, int ep, File f, WorkbookSettings ws)
106   {
107     msoDrawingRecord = mso;
108     objRecord = obj;
109     startpos = sp;
110     endpos = ep;
111     file = f;
112     workbookSettings = ws;
113
114     // msoDrawingRecord is null if the entire sheet consists of just the
115
// chart. In this case, as there is only one drawing on the page,
116
// it isn't necessary to add to the drawing data record anyway
117
if (msoDrawingRecord != null)
118     {
119       drawingData = dd;
120       drawingData.addData(msoDrawingRecord.getRecord().getData());
121       drawingNumber = drawingData.getNumDrawings() - 1;
122     }
123
124     initialized = false;
125
126     // Note: mso and obj values can be null if we are creating a chart
127
// which takes up an entire worksheet. Check that both are null or both
128
// not null though
129
Assert.verify((mso != null && obj != null) ||
130                   (mso == null && obj == null));
131   }
132
133   /**
134    * Gets the entire binary record for the chart as a chunk of binary data
135    *
136    * @return the bytes
137    */

138   public byte[] getBytes()
139   {
140     if (!initialized)
141     {
142       initialize();
143     }
144
145     return data;
146   }
147
148   /**
149    * Implementation of the EscherStream method
150    *
151    * @return the data
152    */

153   public byte[] getData()
154   {
155     return msoDrawingRecord.getRecord().getData();
156   }
157
158   /**
159    * Initializes the charts byte data
160    */

161   private void initialize()
162   {
163     data = file.read(startpos, endpos - startpos);
164     initialized = true;
165   }
166
167   /**
168    * Rationalizes the sheet's xf index mapping
169    * @param xfMapping the index mapping for XFRecords
170    * @param fontMapping the index mapping for fonts
171    * @param formatMapping the index mapping for formats
172    */

173   public void rationalize(IndexMapping xfMapping,
174                           IndexMapping fontMapping,
175                           IndexMapping formatMapping)
176   {
177     if (!initialized)
178     {
179       initialize();
180     }
181
182     // Read through the array, looking for the data types
183
// This is a total hack bodge for now - it will eventually need to be
184
// integrated properly
185
int pos = 0;
186     int code = 0;
187     int length = 0;
188     Type type = null;
189     while (pos < data.length)
190     {
191       code = IntegerHelper.getInt(data[pos], data[pos+1]);
192       length = IntegerHelper.getInt(data[pos+2], data[pos+3]);
193
194       type = Type.getType(code);
195
196       if (type == Type.FONTX)
197       {
198         int fontind = IntegerHelper.getInt(data[pos+4], data[pos+5]);
199         IntegerHelper.getTwoBytes(fontMapping.getNewIndex(fontind),
200                                   data, pos+4);
201       }
202       else if (type == Type.FBI)
203       {
204         int fontind = IntegerHelper.getInt(data[pos+12], data[pos+13]);
205         IntegerHelper.getTwoBytes(fontMapping.getNewIndex(fontind),
206                                   data, pos+12);
207       }
208       else if (type == Type.IFMT)
209       {
210         int formind = IntegerHelper.getInt(data[pos+4], data[pos+5]);
211         IntegerHelper.getTwoBytes(formatMapping.getNewIndex(formind),
212                                   data, pos+4);
213       }
214       else if (type == Type.ALRUNS)
215       {
216         int numRuns = IntegerHelper.getInt(data[pos+4], data[pos+5]);
217         int fontPos = pos+6;
218         for (int i = 0 ; i < numRuns; i++)
219         {
220           int fontind = IntegerHelper.getInt(data[fontPos+2], data[fontPos+3]);
221           IntegerHelper.getTwoBytes(fontMapping.getNewIndex(fontind),
222                                     data, fontPos+2);
223           fontPos += 4;
224         }
225       }
226
227       pos += length+4;
228     }
229   }
230
231   /**
232    * Gets the SpContainer containing the charts drawing information
233    *
234    * @return the spContainer
235    */

236   EscherContainer getSpContainer()
237   {
238     EscherContainer spContainer = drawingData.getSpContainer(drawingNumber);
239
240     return spContainer;
241   }
242
243   /**
244    * Accessor for the mso drawing record
245    *
246    * @return the drawing record
247    */

248   MsoDrawingRecord getMsoDrawingRecord()
249   {
250     return msoDrawingRecord;
251   }
252
253   /**
254    * Accessor for the obj record
255    *
256    * @return the obj record
257    */

258   ObjRecord getObjRecord()
259   {
260     return objRecord;
261   }
262 }
263
264
265
266
267
268
Popular Tags