KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > contrib > view > SVTableCellRenderer


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.contrib.view;
20
21 import java.util.Hashtable JavaDoc;
22
23 import javax.swing.*;
24 import javax.swing.table.TableCellRenderer JavaDoc;
25 import javax.swing.border.*;
26
27 import java.awt.Component JavaDoc;
28 import java.awt.Color JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.Font JavaDoc;
31
32 import java.io.Serializable JavaDoc;
33 import java.text.*;
34
35 import org.apache.poi.hssf.usermodel.*;
36 import org.apache.poi.hssf.util.HSSFColor;
37
38
39
40 /**
41  * Sheet Viewer Table Cell Render -- not commented via javadoc as it
42  * nearly completely consists of overridden methods.
43  *
44  * @author Andrew C. Oliver
45  */

46 public class SVTableCellRenderer extends JLabel
47     implements TableCellRenderer JavaDoc, Serializable JavaDoc
48 {
49     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
50     protected SVBorder cellBorder = new SVBorder();
51
52
53     private HSSFWorkbook wb = null;
54
55     /** This class holds the references to the predefined cell formats.
56      */

57     private class CellFormatter {
58       private Format[] textFormatter;
59
60       private DecimalFormat generalNumberFormat = new DecimalFormat("0");
61
62       public CellFormatter() {
63         textFormatter = new Format[0x31];
64
65         textFormatter[0x01] = new DecimalFormat("0");
66         textFormatter[0x02] = new DecimalFormat("0.00");
67         textFormatter[0x03] = new DecimalFormat("#,##0");
68         textFormatter[0x04] = new DecimalFormat("#,##0.00");
69         textFormatter[0x05] = new DecimalFormat("$#,##0;$#,##0");
70         textFormatter[0x06] = new DecimalFormat("$#,##0;$#,##0");
71         textFormatter[0x07] = new DecimalFormat("$#,##0.00;$#,##0.00");
72         textFormatter[0x08] = new DecimalFormat("$#,##0.00;$#,##0.00");
73         textFormatter[0x09] = new DecimalFormat("0%");
74         textFormatter[0x0A] = new DecimalFormat("0.00%");
75         textFormatter[0x0B] = new DecimalFormat("0.00E0");
76         textFormatter[0x0C] = new SVFractionalFormat("# ?/?");
77         textFormatter[0x0D] = new SVFractionalFormat("# ??/??");
78         textFormatter[0x0E] = new SimpleDateFormat("M/d/yy");
79         textFormatter[0x0F] = new SimpleDateFormat("d-MMM-yy");
80         textFormatter[0x10] = new SimpleDateFormat("d-MMM");
81         textFormatter[0x11] = new SimpleDateFormat("MMM-yy");
82         textFormatter[0x12] = new SimpleDateFormat("h:mm a");
83         textFormatter[0x13] = new SimpleDateFormat("h:mm:ss a");
84         textFormatter[0x14] = new SimpleDateFormat("h:mm");
85         textFormatter[0x15] = new SimpleDateFormat("h:mm:ss");
86         textFormatter[0x16] = new SimpleDateFormat("M/d/yy h:mm");
87         // 0x17 - 0x24 reserved for international and undocumented 0x25, "(#,##0_);(#,##0)"
88
//start at 0x26
89
//jmh need to do colour
90
//"(#,##0_);[Red](#,##0)"
91
textFormatter[0x26] = new DecimalFormat("#,##0;#,##0");
92         //jmh need to do colour
93
//(#,##0.00_);(#,##0.00)
94
textFormatter[0x27] = new DecimalFormat("#,##0.00;#,##0.00");
95         textFormatter[0x28] = new DecimalFormat("#,##0.00;#,##0.00");
96 //?? textFormatter[0x29] = new DecimalFormat("_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
97
//?? textFormatter[0x2A] = new DecimalFormat("_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
98
//?? textFormatter[0x2B] = new DecimalFormat("_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
99
//?? textFormatter[0x2C] = new DecimalFormat("_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
100
textFormatter[0x2D] = new SimpleDateFormat("mm:ss");
101 //?? textFormatter[0x2E] = new SimpleDateFormat("[h]:mm:ss");
102
textFormatter[0x2F] = new SimpleDateFormat("mm:ss.0");
103         textFormatter[0x30] = new DecimalFormat("##0.0E0");
104       }
105
106       public String JavaDoc format(short index, Object JavaDoc value) {
107         if (index == 0)
108           return value.toString();
109         if (textFormatter[index] == null)
110           throw new RuntimeException JavaDoc("Sorry. I cant handle the format code :"+Integer.toHexString(index));
111         return textFormatter[index].format(value);
112       }
113
114       public String JavaDoc format(short index, double value) {
115         if (index == 0)
116           return generalNumberFormat.format(value);
117         if (textFormatter[index] == null)
118           throw new RuntimeException JavaDoc("Sorry. I cant handle the format code :"+Integer.toHexString(index));
119         if (textFormatter[index] instanceof DecimalFormat) {
120           return ((DecimalFormat)textFormatter[index]).format(value);
121         }
122         if (textFormatter[index] instanceof SVFractionalFormat) {
123           return ((SVFractionalFormat)textFormatter[index]).format(value);
124         }
125         throw new RuntimeException JavaDoc("Sorry. I cant handle a non decimal formatter for a decimal value :"+Integer.toHexString(index));
126       }
127
128       public boolean useRedColor(short index, double value) {
129         return (((index == 0x06)||(index == 0x08)||(index == 0x26) || (index == 0x27)) && (value < 0));
130       }
131     }
132
133     private final CellFormatter cellFormatter = new CellFormatter();
134
135     public SVTableCellRenderer(HSSFWorkbook wb) {
136     super();
137     setOpaque(true);
138         setBorder(noFocusBorder);
139         this.wb = wb;
140     }
141
142     public Component JavaDoc getTableCellRendererComponent(JTable table, Object JavaDoc value,
143                           boolean isSelected, boolean hasFocus, int row, int column) {
144     boolean isBorderSet = false;
145
146         //If the JTables default cell renderer has been setup correctly the
147
//value will be the HSSFCell that we are trying to render
148
HSSFCell c = (HSSFCell)value;
149
150         if (c != null) {
151           HSSFCellStyle s = c.getCellStyle();
152           HSSFFont f = wb.getFontAt(s.getFontIndex());
153           setFont(SVTableUtils.makeFont(f));
154
155           if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
156             setBackground(SVTableUtils.getAWTColor(s.getFillForegroundColor(), SVTableUtils.white));
157           } else setBackground(SVTableUtils.white);
158
159           setForeground(SVTableUtils.getAWTColor(f.getColor(), SVTableUtils.black));
160
161           cellBorder.setBorder(SVTableUtils.getAWTColor(s.getTopBorderColor(), SVTableUtils.black),
162                                SVTableUtils.getAWTColor(s.getRightBorderColor(), SVTableUtils.black),
163                                SVTableUtils.getAWTColor(s.getBottomBorderColor(), SVTableUtils.black),
164                                SVTableUtils.getAWTColor(s.getLeftBorderColor(), SVTableUtils.black),
165                                s.getBorderTop(), s.getBorderRight(),
166                                s.getBorderBottom(), s.getBorderLeft(),
167                                hasFocus);
168             setBorder(cellBorder);
169             isBorderSet=true;
170
171             //Set the value that is rendered for the cell
172
switch (c.getCellType()) {
173               case HSSFCell.CELL_TYPE_BLANK:
174                 setValue("");
175               break;
176               case HSSFCell.CELL_TYPE_BOOLEAN:
177                 if (c.getBooleanCellValue()) {
178                   setValue("true");
179                 } else {
180                   setValue("false");
181                 }
182               break;
183               case HSSFCell.CELL_TYPE_NUMERIC:
184                 short format = s.getDataFormat();
185                 double numericValue = c.getNumericCellValue();
186                 if (cellFormatter.useRedColor(format, numericValue))
187                   setForeground(Color.red);
188                 else setForeground(null);
189                 setValue(cellFormatter.format(format, c.getNumericCellValue()));
190               break;
191               case HSSFCell.CELL_TYPE_STRING:
192                 setValue(c.getStringCellValue());
193               break;
194               case HSSFCell.CELL_TYPE_FORMULA:
195               default:
196                 setValue("?");
197             }
198             //Set the text alignment of the cell
199
switch (s.getAlignment()) {
200               case HSSFCellStyle.ALIGN_LEFT:
201               case HSSFCellStyle.ALIGN_JUSTIFY:
202               case HSSFCellStyle.ALIGN_FILL:
203                 setHorizontalAlignment(SwingConstants.LEFT);
204                 break;
205               case HSSFCellStyle.ALIGN_CENTER:
206               case HSSFCellStyle.ALIGN_CENTER_SELECTION:
207                 setHorizontalAlignment(SwingConstants.CENTER);
208                 break;
209               case HSSFCellStyle.ALIGN_GENERAL:
210               case HSSFCellStyle.ALIGN_RIGHT:
211                 setHorizontalAlignment(SwingConstants.RIGHT);
212                 break;
213               default:
214                 setHorizontalAlignment(SwingConstants.LEFT);
215                 break;
216             }
217         } else {
218           setValue("");
219           setBackground(SVTableUtils.white);
220         }
221
222
223     if (hasFocus) {
224             if (!isBorderSet) {
225               //This is the border to paint when there is no border
226
//and the cell has focus
227
cellBorder.setBorder(SVTableUtils.black,
228                                    SVTableUtils.black,
229                                    SVTableUtils.black,
230                                    SVTableUtils.black,
231                                    HSSFCellStyle.BORDER_NONE,
232                                    HSSFCellStyle.BORDER_NONE,
233                                    HSSFCellStyle.BORDER_NONE,
234                                    HSSFCellStyle.BORDER_NONE,
235                                    isSelected);
236               setBorder(cellBorder);
237             }
238         if (table.isCellEditable(row, column)) {
239             setForeground( UIManager.getColor("Table.focusCellForeground") );
240             setBackground( UIManager.getColor("Table.focusCellBackground") );
241         }
242     } else if (!isBorderSet) {
243         setBorder(noFocusBorder);
244     }
245
246     // ---- begin optimization to avoid painting background ----
247
Color JavaDoc back = getBackground();
248     boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque();
249         setOpaque(!colorMatch);
250     // ---- end optimization to aviod painting background ----
251
return this;
252     }
253
254     public void validate() {}
255
256     public void revalidate() {}
257
258     public void repaint(long tm, int x, int y, int width, int height) {}
259
260     public void repaint(Rectangle JavaDoc r) { }
261
262     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
263     // Strings get interned...
264
if (propertyName=="text") {
265         super.firePropertyChange(propertyName, oldValue, newValue);
266     }
267     }
268
269     public void firePropertyChange(String JavaDoc propertyName, boolean oldValue, boolean newValue) { }
270
271     /**
272      * Sets the string to either the value or "" if the value is null.
273      *
274      */

275     protected void setValue(Object JavaDoc value) {
276     setText((value == null) ? "" : value.toString());
277     }
278 }
279
Popular Tags