KickJava   Java API By Example, From Geeks To Geeks.

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


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 common.Logger;
23
24 import jxl.biff.XFRecord;
25 import jxl.write.WritableFont;
26 import jxl.write.WritableCellFormat;
27 import jxl.write.WritableWorkbook;
28 import jxl.write.NumberFormats;
29 import jxl.write.DateFormat;
30 import jxl.write.DateFormats;
31
32 /**
33  * A structure containing the styles used by this workbook. This is used
34  * to enforce thread safety by tying the default styles to a workbook
35  * instance rather than by initializing them statically
36  */

37 class Styles
38 {
39   /**
40    * The logger
41    */

42   private static Logger logger = Logger.getLogger(Styles.class);
43
44   /**
45    * The default font for Cell formats
46    */

47   private WritableFont arial10pt;
48
49   /**
50    * The font used for hyperlinks
51    */

52   private WritableFont hyperlinkFont;
53
54   /**
55    * The default style for cells
56    */

57   private WritableCellFormat normalStyle;
58
59   /**
60    * The style used for hyperlinks
61    */

62   private WritableCellFormat hyperlinkStyle;
63
64   /**
65    * A cell format used to hide the cell contents
66    */

67   private WritableCellFormat hiddenStyle;
68
69   /**
70    * A cell format used for the default date format
71    */

72   private WritableCellFormat defaultDateFormat;
73
74   /**
75    * Constructor
76    */

77   public Styles()
78   {
79     arial10pt = null;
80     hyperlinkFont = null;
81     normalStyle = null;
82     hyperlinkStyle = null;
83     hiddenStyle = null;
84   }
85
86   private synchronized void initNormalStyle()
87   {
88     normalStyle = new WritableCellFormat(getArial10Pt(),
89                                          NumberFormats.DEFAULT);
90     normalStyle.setFont(getArial10Pt());
91   }
92
93   public WritableCellFormat getNormalStyle()
94   {
95     if (normalStyle == null)
96     {
97       initNormalStyle();
98     }
99
100     return normalStyle;
101   }
102
103   private synchronized void initHiddenStyle()
104   {
105     hiddenStyle = new WritableCellFormat
106       (getArial10Pt(), new DateFormat(";;;"));
107   }
108
109   public WritableCellFormat getHiddenStyle()
110   {
111     if (hiddenStyle == null)
112     {
113       initHiddenStyle();
114     }
115
116     return hiddenStyle;
117   }
118
119   private synchronized void initHyperlinkStyle()
120   {
121     hyperlinkStyle = new WritableCellFormat(getHyperlinkFont(),
122                                             NumberFormats.DEFAULT);
123   }
124
125   public WritableCellFormat getHyperlinkStyle()
126   {
127     if (hyperlinkStyle == null)
128     {
129       initHyperlinkStyle();
130     }
131
132     return hyperlinkStyle;
133   }
134
135   private synchronized void initArial10Pt()
136   {
137     arial10pt = new WritableFont(WritableWorkbook.ARIAL_10_PT);
138   }
139
140   public WritableFont getArial10Pt()
141   {
142     if (arial10pt == null)
143     {
144       initArial10Pt();
145     }
146
147     return arial10pt;
148   }
149
150   private synchronized void initHyperlinkFont()
151   {
152     hyperlinkFont = new WritableFont(WritableWorkbook.HYPERLINK_FONT);
153   }
154
155   public WritableFont getHyperlinkFont()
156   {
157     if (hyperlinkFont == null)
158     {
159       initHyperlinkFont();
160     }
161
162     return hyperlinkFont;
163   }
164
165   private synchronized void initDefaultDateFormat()
166   {
167     defaultDateFormat = new WritableCellFormat(DateFormats.DEFAULT);
168   }
169
170   public WritableCellFormat getDefaultDateFormat()
171   {
172     if (defaultDateFormat == null)
173     {
174       initDefaultDateFormat();
175     }
176
177     return defaultDateFormat;
178   }
179
180   /**
181    * Gets the thread safe version of the cell format passed in. If the
182    * format is already thread safe (ie. it doesn't use a statically initialized
183    * format or font) then the same object is simply returned
184    * This object is already tied to a workbook instance, so no synchronisation
185    * is necesasry
186    *
187    * @param wf a format to verify
188    * @return the thread safe format
189    */

190   public XFRecord getFormat(XFRecord wf)
191   {
192     XFRecord format = wf;
193
194     // Check to see if the format is one of the shared Workbook defaults. If
195
// so, then get hold of the Workbook's specific instance
196
if (format == WritableWorkbook.NORMAL_STYLE)
197     {
198       format = getNormalStyle();
199     }
200     else if (format == WritableWorkbook.HYPERLINK_STYLE)
201     {
202       format = getHyperlinkStyle();
203     }
204     else if (format == WritableWorkbook.HIDDEN_STYLE)
205     {
206       format = getHiddenStyle();
207     }
208     else if (format == DateRecord.defaultDateFormat)
209     {
210       format = getDefaultDateFormat();
211     }
212
213     // Do the same with the statically shared fonts
214
if (format.getFont() == WritableWorkbook.ARIAL_10_PT)
215     {
216       format.setFont(getArial10Pt());
217     }
218     else if (format.getFont() == WritableWorkbook.HYPERLINK_FONT)
219     {
220       format.setFont(getHyperlinkFont());
221     }
222
223     return format;
224   }
225 }
226
Popular Tags