001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.contrib.view;
019:
020: import java.util.Hashtable;
021:
022: import javax.swing.*;
023: import javax.swing.table.TableCellRenderer;
024: import javax.swing.border.*;
025:
026: import java.awt.Component;
027: import java.awt.Color;
028: import java.awt.Rectangle;
029: import java.awt.Font;
030:
031: import java.io.Serializable;
032: import java.text.*;
033:
034: import org.apache.poi.hssf.usermodel.*;
035: import org.apache.poi.hssf.util.HSSFColor;
036:
037: /**
038: * Sheet Viewer Table Cell Render -- not commented via javadoc as it
039: * nearly completely consists of overridden methods.
040: *
041: * @author Andrew C. Oliver
042: */
043: public class SVTableCellRenderer extends JLabel implements
044: TableCellRenderer, Serializable {
045: protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
046: protected SVBorder cellBorder = new SVBorder();
047:
048: private HSSFWorkbook wb = null;
049:
050: /** This class holds the references to the predefined cell formats.
051: */
052: private class CellFormatter {
053: private Format[] textFormatter;
054:
055: private DecimalFormat generalNumberFormat = new DecimalFormat(
056: "0");
057:
058: public CellFormatter() {
059: textFormatter = new Format[0x31];
060:
061: textFormatter[0x01] = new DecimalFormat("0");
062: textFormatter[0x02] = new DecimalFormat("0.00");
063: textFormatter[0x03] = new DecimalFormat("#,##0");
064: textFormatter[0x04] = new DecimalFormat("#,##0.00");
065: textFormatter[0x05] = new DecimalFormat("$#,##0;$#,##0");
066: textFormatter[0x06] = new DecimalFormat("$#,##0;$#,##0");
067: textFormatter[0x07] = new DecimalFormat(
068: "$#,##0.00;$#,##0.00");
069: textFormatter[0x08] = new DecimalFormat(
070: "$#,##0.00;$#,##0.00");
071: textFormatter[0x09] = new DecimalFormat("0%");
072: textFormatter[0x0A] = new DecimalFormat("0.00%");
073: textFormatter[0x0B] = new DecimalFormat("0.00E0");
074: textFormatter[0x0C] = new SVFractionalFormat("# ?/?");
075: textFormatter[0x0D] = new SVFractionalFormat("# ??/??");
076: textFormatter[0x0E] = new SimpleDateFormat("M/d/yy");
077: textFormatter[0x0F] = new SimpleDateFormat("d-MMM-yy");
078: textFormatter[0x10] = new SimpleDateFormat("d-MMM");
079: textFormatter[0x11] = new SimpleDateFormat("MMM-yy");
080: textFormatter[0x12] = new SimpleDateFormat("h:mm a");
081: textFormatter[0x13] = new SimpleDateFormat("h:mm:ss a");
082: textFormatter[0x14] = new SimpleDateFormat("h:mm");
083: textFormatter[0x15] = new SimpleDateFormat("h:mm:ss");
084: textFormatter[0x16] = new SimpleDateFormat("M/d/yy h:mm");
085: // 0x17 - 0x24 reserved for international and undocumented 0x25, "(#,##0_);(#,##0)"
086: //start at 0x26
087: //jmh need to do colour
088: //"(#,##0_);[Red](#,##0)"
089: textFormatter[0x26] = new DecimalFormat("#,##0;#,##0");
090: //jmh need to do colour
091: //(#,##0.00_);(#,##0.00)
092: textFormatter[0x27] = new DecimalFormat("#,##0.00;#,##0.00");
093: textFormatter[0x28] = new DecimalFormat("#,##0.00;#,##0.00");
094: //?? textFormatter[0x29] = new DecimalFormat("_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
095: //?? textFormatter[0x2A] = new DecimalFormat("_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
096: //?? textFormatter[0x2B] = new DecimalFormat("_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
097: //?? textFormatter[0x2C] = new DecimalFormat("_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
098: textFormatter[0x2D] = new SimpleDateFormat("mm:ss");
099: //?? textFormatter[0x2E] = new SimpleDateFormat("[h]:mm:ss");
100: textFormatter[0x2F] = new SimpleDateFormat("mm:ss.0");
101: textFormatter[0x30] = new DecimalFormat("##0.0E0");
102: }
103:
104: public String format(short index, Object value) {
105: if (index == 0)
106: return value.toString();
107: if (textFormatter[index] == null)
108: throw new RuntimeException(
109: "Sorry. I cant handle the format code :"
110: + Integer.toHexString(index));
111: return textFormatter[index].format(value);
112: }
113:
114: public String format(short index, double value) {
115: if (index <= 0)
116: return generalNumberFormat.format(value);
117: if (textFormatter[index] == null)
118: throw new RuntimeException(
119: "Sorry. I cant handle the format code :"
120: + Integer.toHexString(index));
121: if (textFormatter[index] instanceof DecimalFormat) {
122: return ((DecimalFormat) textFormatter[index])
123: .format(value);
124: }
125: if (textFormatter[index] instanceof SVFractionalFormat) {
126: return ((SVFractionalFormat) textFormatter[index])
127: .format(value);
128: }
129: throw new RuntimeException(
130: "Sorry. I cant handle a non decimal formatter for a decimal value :"
131: + Integer.toHexString(index));
132: }
133:
134: public boolean useRedColor(short index, double value) {
135: return (((index == 0x06) || (index == 0x08)
136: || (index == 0x26) || (index == 0x27)) && (value < 0));
137: }
138: }
139:
140: private final CellFormatter cellFormatter = new CellFormatter();
141:
142: public SVTableCellRenderer(HSSFWorkbook wb) {
143: super ();
144: setOpaque(true);
145: setBorder(noFocusBorder);
146: this .wb = wb;
147: }
148:
149: public Component getTableCellRendererComponent(JTable table,
150: Object value, boolean isSelected, boolean hasFocus,
151: int row, int column) {
152: boolean isBorderSet = false;
153:
154: //If the JTables default cell renderer has been setup correctly the
155: //value will be the HSSFCell that we are trying to render
156: HSSFCell c = (HSSFCell) value;
157:
158: if (c != null) {
159: HSSFCellStyle s = c.getCellStyle();
160: HSSFFont f = wb.getFontAt(s.getFontIndex());
161: setFont(SVTableUtils.makeFont(f));
162:
163: if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
164: setBackground(SVTableUtils.getAWTColor(s
165: .getFillForegroundColor(), SVTableUtils.white));
166: } else
167: setBackground(SVTableUtils.white);
168:
169: setForeground(SVTableUtils.getAWTColor(f.getColor(),
170: SVTableUtils.black));
171:
172: cellBorder.setBorder(SVTableUtils.getAWTColor(s
173: .getTopBorderColor(), SVTableUtils.black),
174: SVTableUtils.getAWTColor(s.getRightBorderColor(),
175: SVTableUtils.black), SVTableUtils
176: .getAWTColor(s.getBottomBorderColor(),
177: SVTableUtils.black), SVTableUtils
178: .getAWTColor(s.getLeftBorderColor(),
179: SVTableUtils.black), s
180: .getBorderTop(), s.getBorderRight(), s
181: .getBorderBottom(), s.getBorderLeft(),
182: hasFocus);
183: setBorder(cellBorder);
184: isBorderSet = true;
185:
186: //Set the value that is rendered for the cell
187: switch (c.getCellType()) {
188: case HSSFCell.CELL_TYPE_BLANK:
189: setValue("");
190: break;
191: case HSSFCell.CELL_TYPE_BOOLEAN:
192: if (c.getBooleanCellValue()) {
193: setValue("true");
194: } else {
195: setValue("false");
196: }
197: break;
198: case HSSFCell.CELL_TYPE_NUMERIC:
199: short format = s.getDataFormat();
200: double numericValue = c.getNumericCellValue();
201: if (cellFormatter.useRedColor(format, numericValue))
202: setForeground(Color.red);
203: else
204: setForeground(null);
205: setValue(cellFormatter.format(format, c
206: .getNumericCellValue()));
207: break;
208: case HSSFCell.CELL_TYPE_STRING:
209: setValue(c.getRichStringCellValue().getString());
210: break;
211: case HSSFCell.CELL_TYPE_FORMULA:
212: default:
213: setValue("?");
214: }
215: //Set the text alignment of the cell
216: switch (s.getAlignment()) {
217: case HSSFCellStyle.ALIGN_LEFT:
218: case HSSFCellStyle.ALIGN_JUSTIFY:
219: case HSSFCellStyle.ALIGN_FILL:
220: setHorizontalAlignment(SwingConstants.LEFT);
221: break;
222: case HSSFCellStyle.ALIGN_CENTER:
223: case HSSFCellStyle.ALIGN_CENTER_SELECTION:
224: setHorizontalAlignment(SwingConstants.CENTER);
225: break;
226: case HSSFCellStyle.ALIGN_GENERAL:
227: case HSSFCellStyle.ALIGN_RIGHT:
228: setHorizontalAlignment(SwingConstants.RIGHT);
229: break;
230: default:
231: setHorizontalAlignment(SwingConstants.LEFT);
232: break;
233: }
234: } else {
235: setValue("");
236: setBackground(SVTableUtils.white);
237: }
238:
239: if (hasFocus) {
240: if (!isBorderSet) {
241: //This is the border to paint when there is no border
242: //and the cell has focus
243: cellBorder.setBorder(SVTableUtils.black,
244: SVTableUtils.black, SVTableUtils.black,
245: SVTableUtils.black, HSSFCellStyle.BORDER_NONE,
246: HSSFCellStyle.BORDER_NONE,
247: HSSFCellStyle.BORDER_NONE,
248: HSSFCellStyle.BORDER_NONE, isSelected);
249: setBorder(cellBorder);
250: }
251: if (table.isCellEditable(row, column)) {
252: setForeground(UIManager
253: .getColor("Table.focusCellForeground"));
254: setBackground(UIManager
255: .getColor("Table.focusCellBackground"));
256: }
257: } else if (!isBorderSet) {
258: setBorder(noFocusBorder);
259: }
260:
261: // ---- begin optimization to avoid painting background ----
262: Color back = getBackground();
263: boolean colorMatch = (back != null)
264: && (back.equals(table.getBackground()))
265: && table.isOpaque();
266: setOpaque(!colorMatch);
267: // ---- end optimization to aviod painting background ----
268: return this ;
269: }
270:
271: public void validate() {
272: }
273:
274: public void revalidate() {
275: }
276:
277: public void repaint(long tm, int x, int y, int width, int height) {
278: }
279:
280: public void repaint(Rectangle r) {
281: }
282:
283: protected void firePropertyChange(String propertyName,
284: Object oldValue, Object newValue) {
285: // Strings get interned...
286: if (propertyName == "text") {
287: super .firePropertyChange(propertyName, oldValue, newValue);
288: }
289: }
290:
291: public void firePropertyChange(String propertyName,
292: boolean oldValue, boolean newValue) {
293: }
294:
295: /**
296: * Sets the string to either the value or "" if the value is null.
297: *
298: */
299: protected void setValue(Object value) {
300: setText((value == null) ? "" : value.toString());
301: }
302: }
|