KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*********************************************************************
2 *
3 * Copyright (C) 2004 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 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import common.Logger;
27 import common.Assert;
28 import jxl.WorkbookSettings;
29
30 /**
31  * Class which encapsulates a data validation (typically in the form of a
32  * dropdown list box
33  */

34 public class DataValidation
35 {
36   /**
37    * The logger
38    */

39   private final static Logger logger = Logger.getLogger(DataValidation.class);
40
41   /**
42    * The data validity list
43    */

44   private DataValidityListRecord validityList;
45
46   /**
47    * The data validity record
48    */

49   private ArrayList JavaDoc validitySettings;
50
51   /**
52    * The current position in the validitySettins array
53    */

54   private int pos;
55
56   /**
57    * Handle to the workbook
58    */

59   private WritableWorkbookImpl workbook;
60
61   /**
62    * Handle to the workbook settings
63    */

64   private WorkbookSettings workbookSettings;
65
66   /**
67    * Constructor
68    *
69    * @param dv the data validations from the read only sheet
70    */

71   DataValidation(jxl.read.biff.DataValidation dv,
72                  WritableWorkbookImpl w,
73                  WorkbookSettings ws)
74   {
75     workbook = w;
76     workbookSettings = ws;
77     validityList = new DataValidityListRecord(dv.getDataValidityList());
78
79     jxl.read.biff.DataValiditySettingsRecord[] settings =
80       dv.getDataValiditySettings();
81
82     validitySettings = new ArrayList JavaDoc(settings.length);
83     for (int i = 0; i < settings.length ; i++)
84     {
85       validitySettings.add(new DataValiditySettingsRecord(settings[i],
86                                                           workbook,
87                                                           workbookSettings));
88     }
89   }
90
91   /**
92    * Constructor
93    *
94    * @param dv the data validations from the writable sheet
95    */

96   DataValidation(DataValidation dv,
97                  WritableWorkbookImpl w,
98                  WorkbookSettings ws)
99
100   {
101     workbook = w;
102     workbookSettings = ws;
103     validityList = new DataValidityListRecord(dv.validityList);
104
105     validitySettings = new ArrayList JavaDoc(dv.validitySettings.size());
106
107     for (Iterator JavaDoc i = dv.validitySettings.iterator(); i.hasNext(); )
108     {
109       DataValiditySettingsRecord dvsr = (DataValiditySettingsRecord) i.next();
110       validitySettings.add
111         (new DataValiditySettingsRecord(dvsr,
112                                         workbook,
113                                         workbookSettings));
114     }
115   }
116
117
118   /**
119    * Writes out the data validation
120    *
121    * @exception IOException
122    * @param outputFile the output file
123    */

124   public void write(File outputFile) throws IOException JavaDoc
125   {
126     if (!validityList.hasDVRecords())
127     {
128       return;
129     }
130
131     outputFile.write(validityList);
132     
133     for (Iterator JavaDoc i = validitySettings.iterator(); i.hasNext() ; )
134     {
135       DataValiditySettingsRecord dv = (DataValiditySettingsRecord) i.next();
136       outputFile.write(dv);
137     }
138   }
139
140   /**
141    * Inserts a row
142    *
143    * @param row the inserted row
144    */

145   public void insertRow(int row)
146   {
147     for (Iterator JavaDoc i = validitySettings.iterator(); i.hasNext() ; )
148     {
149       DataValiditySettingsRecord dv = (DataValiditySettingsRecord) i.next();
150       dv.insertRow(row);
151     }
152   }
153
154   /**
155    * Inserts a row
156    *
157    * @param row the inserted row
158    */

159   public void removeRow(int row)
160   {
161     for (Iterator JavaDoc i = validitySettings.iterator(); i.hasNext() ; )
162     {
163       DataValiditySettingsRecord dv = (DataValiditySettingsRecord) i.next();
164
165       if (dv.getFirstRow() == row && dv.getLastRow() == row)
166       {
167         i.remove();
168         validityList.dvRemoved();
169       }
170       else
171       {
172         dv.removeRow(row);
173       }
174     }
175   }
176
177   /**
178    * Inserts a column
179    *
180    * @param col the inserted column
181    */

182   public void insertColumn(int col)
183   {
184     for (Iterator JavaDoc i = validitySettings.iterator(); i.hasNext() ; )
185     {
186       DataValiditySettingsRecord dv = (DataValiditySettingsRecord) i.next();
187       dv.insertColumn(col);
188     }
189   }
190
191   /**
192    * Removes a column
193    *
194    * @param col the inserted column
195    */

196   public void removeColumn(int col)
197   {
198     for (Iterator JavaDoc i = validitySettings.iterator(); i.hasNext() ; )
199     {
200       DataValiditySettingsRecord dv = (DataValiditySettingsRecord) i.next();
201       
202       if (dv.getFirstColumn() == col && dv.getLastColumn() == col)
203       {
204         i.remove();
205         validityList.dvRemoved();
206       }
207       else
208       {
209         dv.removeColumn(col);
210       }
211     }
212   }
213 }
214
Popular Tags