You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SVTableCellRenderer.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.view;
  16. import javax.swing.*;
  17. import javax.swing.table.TableCellRenderer;
  18. import javax.swing.border.*;
  19. import java.awt.Component;
  20. import java.awt.Color;
  21. import java.awt.Rectangle;
  22. import java.io.Serializable;
  23. import java.text.*;
  24. import org.apache.poi.hssf.usermodel.*;
  25. import org.apache.poi.ss.usermodel.BorderStyle;
  26. /**
  27. * Sheet Viewer Table Cell Render -- not commented via javadoc as it
  28. * nearly completely consists of overridden methods.
  29. *
  30. * @author Andrew C. Oliver
  31. */
  32. public class SVTableCellRenderer extends JLabel
  33. implements TableCellRenderer, Serializable
  34. {
  35. protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  36. protected SVBorder cellBorder = new SVBorder();
  37. private HSSFWorkbook wb = null;
  38. /** This class holds the references to the predefined cell formats.
  39. */
  40. private class CellFormatter {
  41. private Format[] textFormatter;
  42. private DecimalFormat generalNumberFormat = new DecimalFormat("0");
  43. public CellFormatter() {
  44. textFormatter = new Format[0x31];
  45. textFormatter[0x01] = new DecimalFormat("0");
  46. textFormatter[0x02] = new DecimalFormat("0.00");
  47. textFormatter[0x03] = new DecimalFormat("#,##0");
  48. textFormatter[0x04] = new DecimalFormat("#,##0.00");
  49. textFormatter[0x05] = new DecimalFormat("$#,##0;$#,##0");
  50. textFormatter[0x06] = new DecimalFormat("$#,##0;$#,##0");
  51. textFormatter[0x07] = new DecimalFormat("$#,##0.00;$#,##0.00");
  52. textFormatter[0x08] = new DecimalFormat("$#,##0.00;$#,##0.00");
  53. textFormatter[0x09] = new DecimalFormat("0%");
  54. textFormatter[0x0A] = new DecimalFormat("0.00%");
  55. textFormatter[0x0B] = new DecimalFormat("0.00E0");
  56. textFormatter[0x0C] = new SVFractionalFormat("# ?/?");
  57. textFormatter[0x0D] = new SVFractionalFormat("# ??/??");
  58. textFormatter[0x0E] = new SimpleDateFormat("M/d/yy");
  59. textFormatter[0x0F] = new SimpleDateFormat("d-MMM-yy");
  60. textFormatter[0x10] = new SimpleDateFormat("d-MMM");
  61. textFormatter[0x11] = new SimpleDateFormat("MMM-yy");
  62. textFormatter[0x12] = new SimpleDateFormat("h:mm a");
  63. textFormatter[0x13] = new SimpleDateFormat("h:mm:ss a");
  64. textFormatter[0x14] = new SimpleDateFormat("h:mm");
  65. textFormatter[0x15] = new SimpleDateFormat("h:mm:ss");
  66. textFormatter[0x16] = new SimpleDateFormat("M/d/yy h:mm");
  67. // 0x17 - 0x24 reserved for international and undocumented 0x25, "(#,##0_);(#,##0)"
  68. //start at 0x26
  69. //jmh need to do colour
  70. //"(#,##0_);[Red](#,##0)"
  71. textFormatter[0x26] = new DecimalFormat("#,##0;#,##0");
  72. //jmh need to do colour
  73. //(#,##0.00_);(#,##0.00)
  74. textFormatter[0x27] = new DecimalFormat("#,##0.00;#,##0.00");
  75. textFormatter[0x28] = new DecimalFormat("#,##0.00;#,##0.00");
  76. //?? textFormatter[0x29] = new DecimalFormat("_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
  77. //?? textFormatter[0x2A] = new DecimalFormat("_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
  78. //?? textFormatter[0x2B] = new DecimalFormat("_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
  79. //?? textFormatter[0x2C] = new DecimalFormat("_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
  80. textFormatter[0x2D] = new SimpleDateFormat("mm:ss");
  81. //?? textFormatter[0x2E] = new SimpleDateFormat("[h]:mm:ss");
  82. textFormatter[0x2F] = new SimpleDateFormat("mm:ss.0");
  83. textFormatter[0x30] = new DecimalFormat("##0.0E0");
  84. }
  85. public String format(short index, Object value) {
  86. if (index == 0)
  87. return value.toString();
  88. if (textFormatter[index] == null)
  89. throw new RuntimeException("Sorry. I cant handle the format code :"+Integer.toHexString(index));
  90. return textFormatter[index].format(value);
  91. }
  92. public String format(short index, double value) {
  93. if ( index <= 0 )
  94. return generalNumberFormat.format(value);
  95. if (textFormatter[index] == null)
  96. throw new RuntimeException("Sorry. I cant handle the format code :"+Integer.toHexString(index));
  97. if (textFormatter[index] instanceof DecimalFormat) {
  98. return ((DecimalFormat)textFormatter[index]).format(value);
  99. }
  100. if (textFormatter[index] instanceof SVFractionalFormat) {
  101. return ((SVFractionalFormat)textFormatter[index]).format(value);
  102. }
  103. throw new RuntimeException("Sorry. I cant handle a non decimal formatter for a decimal value :"+Integer.toHexString(index));
  104. }
  105. public boolean useRedColor(short index, double value) {
  106. return (((index == 0x06)||(index == 0x08)||(index == 0x26) || (index == 0x27)) && (value < 0));
  107. }
  108. }
  109. private final CellFormatter cellFormatter = new CellFormatter();
  110. public SVTableCellRenderer(HSSFWorkbook wb) {
  111. super();
  112. setOpaque(true);
  113. setBorder(noFocusBorder);
  114. this.wb = wb;
  115. }
  116. public Component getTableCellRendererComponent(JTable table, Object value,
  117. boolean isSelected, boolean hasFocus, int row, int column) {
  118. boolean isBorderSet = false;
  119. //If the JTables default cell renderer has been setup correctly the
  120. //value will be the HSSFCell that we are trying to render
  121. HSSFCell c = (HSSFCell)value;
  122. if (c != null) {
  123. HSSFCellStyle s = c.getCellStyle();
  124. HSSFFont f = wb.getFontAt(s.getFontIndex());
  125. setFont(SVTableUtils.makeFont(f));
  126. if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
  127. setBackground(SVTableUtils.getAWTColor(s.getFillForegroundColor(), SVTableUtils.white));
  128. } else setBackground(SVTableUtils.white);
  129. setForeground(SVTableUtils.getAWTColor(f.getColor(), SVTableUtils.black));
  130. cellBorder.setBorder(SVTableUtils.getAWTColor(s.getTopBorderColor(), SVTableUtils.black),
  131. SVTableUtils.getAWTColor(s.getRightBorderColor(), SVTableUtils.black),
  132. SVTableUtils.getAWTColor(s.getBottomBorderColor(), SVTableUtils.black),
  133. SVTableUtils.getAWTColor(s.getLeftBorderColor(), SVTableUtils.black),
  134. s.getBorderTop(), s.getBorderRight(),
  135. s.getBorderBottom(), s.getBorderLeft(),
  136. hasFocus);
  137. setBorder(cellBorder);
  138. isBorderSet=true;
  139. //Set the value that is rendered for the cell
  140. switch (c.getCellType()) {
  141. case HSSFCell.CELL_TYPE_BLANK:
  142. setValue("");
  143. break;
  144. case HSSFCell.CELL_TYPE_BOOLEAN:
  145. if (c.getBooleanCellValue()) {
  146. setValue("true");
  147. } else {
  148. setValue("false");
  149. }
  150. break;
  151. case HSSFCell.CELL_TYPE_NUMERIC:
  152. short format = s.getDataFormat();
  153. double numericValue = c.getNumericCellValue();
  154. if (cellFormatter.useRedColor(format, numericValue))
  155. setForeground(Color.red);
  156. else setForeground(null);
  157. setValue(cellFormatter.format(format, c.getNumericCellValue()));
  158. break;
  159. case HSSFCell.CELL_TYPE_STRING:
  160. setValue(c.getRichStringCellValue().getString());
  161. break;
  162. case HSSFCell.CELL_TYPE_FORMULA:
  163. default:
  164. setValue("?");
  165. }
  166. //Set the text alignment of the cell
  167. switch (s.getAlignment()) {
  168. case HSSFCellStyle.ALIGN_LEFT:
  169. case HSSFCellStyle.ALIGN_JUSTIFY:
  170. case HSSFCellStyle.ALIGN_FILL:
  171. setHorizontalAlignment(SwingConstants.LEFT);
  172. break;
  173. case HSSFCellStyle.ALIGN_CENTER:
  174. case HSSFCellStyle.ALIGN_CENTER_SELECTION:
  175. setHorizontalAlignment(SwingConstants.CENTER);
  176. break;
  177. case HSSFCellStyle.ALIGN_GENERAL:
  178. case HSSFCellStyle.ALIGN_RIGHT:
  179. setHorizontalAlignment(SwingConstants.RIGHT);
  180. break;
  181. default:
  182. setHorizontalAlignment(SwingConstants.LEFT);
  183. break;
  184. }
  185. } else {
  186. setValue("");
  187. setBackground(SVTableUtils.white);
  188. }
  189. if (hasFocus) {
  190. if (!isBorderSet) {
  191. //This is the border to paint when there is no border
  192. //and the cell has focus
  193. cellBorder.setBorder(SVTableUtils.black,
  194. SVTableUtils.black,
  195. SVTableUtils.black,
  196. SVTableUtils.black,
  197. BorderStyle.NONE,
  198. BorderStyle.NONE,
  199. BorderStyle.NONE,
  200. BorderStyle.NONE,
  201. isSelected);
  202. setBorder(cellBorder);
  203. }
  204. if (table.isCellEditable(row, column)) {
  205. setForeground( UIManager.getColor("Table.focusCellForeground") );
  206. setBackground( UIManager.getColor("Table.focusCellBackground") );
  207. }
  208. } else if (!isBorderSet) {
  209. setBorder(noFocusBorder);
  210. }
  211. // ---- begin optimization to avoid painting background ----
  212. Color back = getBackground();
  213. boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque();
  214. setOpaque(!colorMatch);
  215. // ---- end optimization to aviod painting background ----
  216. return this;
  217. }
  218. public void validate() {}
  219. public void revalidate() {}
  220. public void repaint(long tm, int x, int y, int width, int height) {}
  221. public void repaint(Rectangle r) { }
  222. protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  223. // Strings get interned...
  224. if (propertyName=="text") {
  225. super.firePropertyChange(propertyName, oldValue, newValue);
  226. }
  227. }
  228. public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
  229. /**
  230. * Sets the string to either the value or "" if the value is null.
  231. *
  232. */
  233. protected void setValue(Object value) {
  234. setText((value == null) ? "" : value.toString());
  235. }
  236. }