View | Details | Raw Unified | Return to bug 10581
Collapse All | Expand All

(-)SVTableCellRenderer.java (-107 / +178 lines)
Line 70 Link Here
70
import java.text.*;
Lines 71-76 Link Here
71
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
72
import org.apache.poi.hssf.usermodel.*;
72
import org.apache.poi.hssf.usermodel.HSSFSheet;
73
import org.apache.poi.hssf.usermodel.HSSFRow;
74
import org.apache.poi.hssf.usermodel.HSSFCell;
75
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
76
import org.apache.poi.hssf.usermodel.HSSFFont;
77
--
Line 78 Link Here
78
import org.apache.poi.hssf.util.HSSFColor.WHITE;
74
79
--
Line 90 Link Here
86
  private static final Color black = getAWTColor(new HSSFColor.BLACK());
87
  private static final Color white = getAWTColor(new HSSFColor.WHITE());
88
Line 91 Link Here
90
    protected SVBorder cellBorder = new SVBorder();
91
92
Line 94 Link Here
96
    /** This class holds the references to the predefined cell formats.
97
     */
98
    private class CellFormatter {
99
      private Format[] textFormatter;
100
101
      private DecimalFormat generalNumberFormat = new DecimalFormat("0");
102
103
      public CellFormatter() {
104
        textFormatter = new Format[0x31];
105
106
        textFormatter[0x01] = new DecimalFormat("0");
107
        textFormatter[0x02] = new DecimalFormat("0.00");
108
        textFormatter[0x03] = new DecimalFormat("#,##0");
109
        textFormatter[0x04] = new DecimalFormat("#,##0.00");
110
        textFormatter[0x05] = new DecimalFormat("$#,##0;$#,##0");
111
        textFormatter[0x06] = new DecimalFormat("$#,##0;$#,##0");
112
        textFormatter[0x07] = new DecimalFormat("$#,##0.00;$#,##0.00");
113
        textFormatter[0x08] = new DecimalFormat("$#,##0.00;$#,##0.00");
114
        textFormatter[0x09] = new DecimalFormat("0%");
115
        textFormatter[0x0A] = new DecimalFormat("0.00%");
116
        textFormatter[0x0B] = new DecimalFormat("0.00E0");
117
//??        textFormatter[0x0C] = new DecimalFormat("# ?/?");
118
//??        textFormatter[0x0D] = new DecimalFormat("# ??/??");
119
        textFormatter[0x0E] = new SimpleDateFormat("M/d/yy");
120
        textFormatter[0x0F] = new SimpleDateFormat("d-MMM-yy");
121
        textFormatter[0x10] = new SimpleDateFormat("d-MMM");
122
        textFormatter[0x11] = new SimpleDateFormat("MMM-yy");
123
        textFormatter[0x12] = new SimpleDateFormat("h:mm a");
124
        textFormatter[0x13] = new SimpleDateFormat("h:mm:ss a");
125
        textFormatter[0x14] = new SimpleDateFormat("h:mm");
126
        textFormatter[0x15] = new SimpleDateFormat("h:mm:ss");
127
        textFormatter[0x16] = new SimpleDateFormat("M/d/yy h:mm");
128
        // 0x17 - 0x24 reserved for international and undocumented 0x25, "(#,##0_);(#,##0)"
129
        //start at 0x26
130
        //jmh need to do colour
131
        //"(#,##0_);[Red](#,##0)"
132
        textFormatter[0x26] = new DecimalFormat("#,##0;#,##0");
133
        //jmh need to do colour
134
        //(#,##0.00_);(#,##0.00)
135
        textFormatter[0x27] = new DecimalFormat("#,##0.00;#,##0.00");
136
        textFormatter[0x28] = new DecimalFormat("#,##0.00;#,##0.00");
137
//??        textFormatter[0x29] = new DecimalFormat("_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
138
//??        textFormatter[0x2A] = new DecimalFormat("_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
139
//??        textFormatter[0x2B] = new DecimalFormat("_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
140
//??        textFormatter[0x2C] = new DecimalFormat("_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
141
        textFormatter[0x2D] = new SimpleDateFormat("mm:ss");
142
//??        textFormatter[0x2E] = new SimpleDateFormat("[h]:mm:ss");
143
        textFormatter[0x2F] = new SimpleDateFormat("mm:ss.0");
144
        textFormatter[0x30] = new DecimalFormat("##0.0E0");
145
      }
146
147
      public String format(short index, Object value) {
148
        if (index == 0)
149
          return value.toString();
150
        if (textFormatter[index] == null)
151
          throw new RuntimeException("Sorry. I cant handle the format code :"+Integer.toHexString(index));
152
        return textFormatter[index].format(value);
153
      }
154
155
      public String format(short index, double value) {
156
        if (index == 0)
157
          return generalNumberFormat.format(value);
158
        if (textFormatter[index] == null)
159
          throw new RuntimeException("Sorry. I cant handle the format code :"+Integer.toHexString(index));
160
        if (textFormatter[index] instanceof DecimalFormat) {
161
          return ((DecimalFormat)textFormatter[index]).format(value);
162
        }
163
        else throw new RuntimeException("Sorry. I cant handle a non decimal formatter for a decimal value :"+Integer.toHexString(index));
164
      }
165
166
      public boolean useRedColor(short index, double value) {
167
        return (((index == 0x06)||(index == 0x08)||(index == 0x26) || (index == 0x27)) && (value < 0));
168
      }
169
    }
170
171
    private final CellFormatter cellFormatter = new CellFormatter();
172
Lines 105-109 Link Here
105
	if (isSelected) {
106
	   setForeground(table.getSelectionForeground());
107
	   setBackground(table.getSelectionBackground());
108
	}
109
Line 115 Link Here
115
Lines 131-132 Link Here
131
          
204
132
          HSSFColor clr = null;
133
--
Lines 134-217 Link Here
134
            clr = (HSSFColor)colors.get(new Integer(s.getFillForegroundColor()));
206
            setBackground(getAWTColor(s.getFillForegroundColor(), white));
135
          }
207
          } else setBackground(white);
136
          if (clr == null) clr = new HSSFColor.WHITE();
208
137
209
          setForeground(getAWTColor(f.getColor(), black));
138
          short[] rgb = clr.getTriplet();
210
139
          Color awtcolor = new Color(rgb[0],rgb[1],rgb[2]);
211
          cellBorder.setBorder(getAWTColor(s.getTopBorderColor(), black),
140
212
                               getAWTColor(s.getRightBorderColor(), black),
141
          setBackground(awtcolor);
213
                               getAWTColor(s.getBottomBorderColor(), black),
142
214
                               getAWTColor(s.getLeftBorderColor(), black),
143
          clr = (HSSFColor)colors.get(new Integer(f.getColor()));
215
                               s.getBorderTop(), s.getBorderRight(),
144
          if (clr == null) clr = new HSSFColor.BLACK();
216
                               s.getBorderBottom(), s.getBorderLeft(),
145
          rgb = clr.getTriplet();
217
                               hasFocus);
146
          awtcolor = new Color(rgb[0],rgb[1],rgb[2]);
218
            setBorder(cellBorder);
147
          setForeground(awtcolor);
219
            isBorderSet=true;
148
220
149
/*          if (s.getBorderBottom() != HSSFCellStyle.BORDER_NONE ||
221
            //Set the value that is rendered for the cell
150
              s.getBorderTop()    != HSSFCellStyle.BORDER_NONE ||
222
            switch (c.getCellType()) {
151
              s.getBorderLeft()   != HSSFCellStyle.BORDER_NONE ||
223
              case HSSFCell.CELL_TYPE_BLANK:
152
              s.getBorderRight()  != HSSFCellStyle.BORDER_NONE) {
224
                setValue("");
153
*/
225
              break;
154
              int borderTop = s.getBorderTop();
226
              case HSSFCell.CELL_TYPE_BOOLEAN:
155
              int borderRight = s.getBorderRight();
227
                if (c.getBooleanCellValue()) {
156
              int borderBottom = s.getBorderBottom();
228
                  setValue("true");
157
              int borderLeft = s.getBorderLeft();
229
                } else {
158
              
230
                  setValue("false");
159
              //OUCH! This could causing rendering performance problems.
231
                }
160
              //Need to somehow create once and store
232
              break;
161
              SVBorder border = new SVBorder(Color.black, Color.black,
233
              case HSSFCell.CELL_TYPE_FORMULA:
162
                                           Color.black, Color.black,
234
              case HSSFCell.CELL_TYPE_NUMERIC:
163
                                           borderTop, borderRight,
235
                short format = s.getDataFormat();
164
                                           borderBottom, borderLeft,
236
                double numericValue = c.getNumericCellValue();
165
                                           s.getBorderTop() != HSSFCellStyle.BORDER_NONE,
237
                if (cellFormatter.useRedColor(format, numericValue))
166
                                           s.getBorderRight() != HSSFCellStyle.BORDER_NONE,
238
                  setForeground(Color.red);
167
                                           s.getBorderBottom() != HSSFCellStyle.BORDER_NONE,
239
                else setForeground(null);
168
                                           s.getBorderLeft() != HSSFCellStyle.BORDER_NONE);
240
                setValue(cellFormatter.format(format, c.getNumericCellValue()));
169
              setBorder(border);
241
              break;
170
              isBorderSet=true;
242
              case HSSFCell.CELL_TYPE_STRING:
171
243
                setValue(c.getStringCellValue());
172
              //Set the value that is rendered for the cell
244
              break;
173
          switch (c.getCellType()) {
245
              default:
174
            case HSSFCell.CELL_TYPE_BLANK:
246
                setValue("?");
175
              setValue("");
247
            }
176
            break;
248
            //Set the text alignment of the cell
177
            case HSSFCell.CELL_TYPE_BOOLEAN:
249
            switch (s.getAlignment()) {
178
              if (c.getBooleanCellValue()) {
250
              case HSSFCellStyle.ALIGN_LEFT:
179
                setValue("true");
251
              case HSSFCellStyle.ALIGN_JUSTIFY:
180
              } else {
252
              case HSSFCellStyle.ALIGN_FILL:
181
                setValue("false");
253
                setHorizontalAlignment(SwingConstants.LEFT);
182
              }
254
                break;
183
            break;
255
              case HSSFCellStyle.ALIGN_CENTER:
184
            case HSSFCell.CELL_TYPE_FORMULA:
256
              case HSSFCellStyle.ALIGN_CENTER_SELECTION:
185
            case HSSFCell.CELL_TYPE_NUMERIC:
257
                setHorizontalAlignment(SwingConstants.CENTER);
186
              setValue(""+c.getNumericCellValue());
258
                break;
187
            break;
259
              case HSSFCellStyle.ALIGN_GENERAL:
188
            case HSSFCell.CELL_TYPE_STRING:
260
              case HSSFCellStyle.ALIGN_RIGHT:
189
              setValue(c.getStringCellValue());
261
                setHorizontalAlignment(SwingConstants.RIGHT);
190
            break;
262
                break;
191
            default:
263
              default:
192
              setValue("?");
264
                setHorizontalAlignment(SwingConstants.LEFT);
193
          }
265
                break;
194
              //Set the text alignment of the cell
266
            }
195
              switch (s.getAlignment()) {
267
        } else {
196
                case HSSFCellStyle.ALIGN_GENERAL:
268
          setValue("");
197
                case HSSFCellStyle.ALIGN_LEFT:
269
          setBackground(white);
198
                case HSSFCellStyle.ALIGN_JUSTIFY:
270
        }
199
                case HSSFCellStyle.ALIGN_FILL:
200
                  setHorizontalAlignment(SwingConstants.LEFT);
201
                  break;
202
                case HSSFCellStyle.ALIGN_CENTER:
203
                case HSSFCellStyle.ALIGN_CENTER_SELECTION:
204
                  setHorizontalAlignment(SwingConstants.CENTER);
205
                  break;
206
                case HSSFCellStyle.ALIGN_RIGHT:
207
                  setHorizontalAlignment(SwingConstants.RIGHT);
208
                  break;
209
                default:
210
                  setHorizontalAlignment(SwingConstants.LEFT);
211
                  break;
212
              }
213
//          }
214
       } else {
215
           setValue("");
216
          setBackground(Color.white);
217
       }
218
--
Line 222 Link Here
222
	        setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
275
              cellBorder.setBorder(black,
223
--
276
                                   black,
277
                                   black,
278
                                   black,
279
                                   HSSFCellStyle.BORDER_NONE,
280
                                   HSSFCellStyle.BORDER_NONE,
281
                                   HSSFCellStyle.BORDER_NONE,
282
                                   HSSFCellStyle.BORDER_NONE,
283
                                   isSelected);
284
              setBorder(cellBorder);
Line 237 Link Here
237
Line 241 Link Here
241
Line 266 Link Here
326
327
    /** This method retrieves the AWT Color representation from the colour hash table
328
     *
329
     */
330
    private final Color getAWTColor(int index, Color deflt) {
331
      HSSFColor clr = (HSSFColor)colors.get(new Integer(index));
332
      if (clr == null) return deflt;
333
      return getAWTColor(clr);
334
    }
335
336
    private static final Color getAWTColor(HSSFColor clr) {
337
      short[] rgb = clr.getTriplet();
338
      return new Color(rgb[0],rgb[1],rgb[2]);
339
    }
340
341

Return to bug 10581