List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle getFontIndex
@Override @Deprecated public short getFontIndex()
From source file:poi.hssf.view.SVTableCellEditor.java
License:Apache License
/** * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object * * @return The tableCellEditorComponent value *///from w w w. ja va 2s . c o m public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { System.out.println("GetTableCellEditorComponent"); HSSFCell cell = (HSSFCell) value; if (cell != null) { HSSFCellStyle style = cell.getCellStyle(); HSSFFont f = wb.getFontAt(style.getFontIndex()); boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL; boolean isitalics = f.getItalic(); int fontstyle = Font.PLAIN; if (isbold) fontstyle = Font.BOLD; if (isitalics) fontstyle = fontstyle | Font.ITALIC; int fontheight = f.getFontHeightInPoints(); if (fontheight == 9) fontheight = 10; //fix for stupid ol Windows Font font = new Font(f.getFontName(), fontstyle, fontheight); editor.setFont(font); if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) { editor.setBackground(getAWTColor(style.getFillForegroundColor(), white)); } else editor.setBackground(white); editor.setForeground(getAWTColor(f.getColor(), black)); //Set the value that is rendered for the cell switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_BLANK: editor.setText(""); break; case HSSFCell.CELL_TYPE_BOOLEAN: if (cell.getBooleanCellValue()) { editor.setText("true"); } else { editor.setText("false"); } break; case HSSFCell.CELL_TYPE_NUMERIC: editor.setText(Double.toString(cell.getNumericCellValue())); break; case HSSFCell.CELL_TYPE_STRING: editor.setText(cell.getRichStringCellValue().getString()); break; case HSSFCell.CELL_TYPE_FORMULA: default: editor.setText("?"); } switch (style.getAlignment()) { case HSSFCellStyle.ALIGN_LEFT: case HSSFCellStyle.ALIGN_JUSTIFY: case HSSFCellStyle.ALIGN_FILL: editor.setHorizontalAlignment(SwingConstants.LEFT); break; case HSSFCellStyle.ALIGN_CENTER: case HSSFCellStyle.ALIGN_CENTER_SELECTION: editor.setHorizontalAlignment(SwingConstants.CENTER); break; case HSSFCellStyle.ALIGN_GENERAL: case HSSFCellStyle.ALIGN_RIGHT: editor.setHorizontalAlignment(SwingConstants.RIGHT); break; default: editor.setHorizontalAlignment(SwingConstants.LEFT); break; } } return editor; }
From source file:poi.hssf.view.SVTableCellRenderer.java
License:Apache License
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { boolean isBorderSet = false; //If the JTables default cell renderer has been setup correctly the //value will be the HSSFCell that we are trying to render HSSFCell c = (HSSFCell) value;/*from www . j a v a 2s .c om*/ if (c != null) { HSSFCellStyle s = c.getCellStyle(); HSSFFont f = wb.getFontAt(s.getFontIndex()); setFont(SVTableUtils.makeFont(f)); if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) { setBackground(SVTableUtils.getAWTColor(s.getFillForegroundColor(), SVTableUtils.white)); } else setBackground(SVTableUtils.white); setForeground(SVTableUtils.getAWTColor(f.getColor(), SVTableUtils.black)); cellBorder.setBorder(SVTableUtils.getAWTColor(s.getTopBorderColor(), SVTableUtils.black), SVTableUtils.getAWTColor(s.getRightBorderColor(), SVTableUtils.black), SVTableUtils.getAWTColor(s.getBottomBorderColor(), SVTableUtils.black), SVTableUtils.getAWTColor(s.getLeftBorderColor(), SVTableUtils.black), s.getBorderTop(), s.getBorderRight(), s.getBorderBottom(), s.getBorderLeft(), hasFocus); setBorder(cellBorder); isBorderSet = true; //Set the value that is rendered for the cell switch (c.getCellType()) { case HSSFCell.CELL_TYPE_BLANK: setValue(""); break; case HSSFCell.CELL_TYPE_BOOLEAN: if (c.getBooleanCellValue()) { setValue("true"); } else { setValue("false"); } break; case HSSFCell.CELL_TYPE_NUMERIC: short format = s.getDataFormat(); double numericValue = c.getNumericCellValue(); if (cellFormatter.useRedColor(format, numericValue)) setForeground(Color.red); else setForeground(null); setValue(cellFormatter.format(format, c.getNumericCellValue())); break; case HSSFCell.CELL_TYPE_STRING: setValue(c.getRichStringCellValue().getString()); break; case HSSFCell.CELL_TYPE_FORMULA: default: setValue("?"); } //Set the text alignment of the cell switch (s.getAlignment()) { case HSSFCellStyle.ALIGN_LEFT: case HSSFCellStyle.ALIGN_JUSTIFY: case HSSFCellStyle.ALIGN_FILL: setHorizontalAlignment(SwingConstants.LEFT); break; case HSSFCellStyle.ALIGN_CENTER: case HSSFCellStyle.ALIGN_CENTER_SELECTION: setHorizontalAlignment(SwingConstants.CENTER); break; case HSSFCellStyle.ALIGN_GENERAL: case HSSFCellStyle.ALIGN_RIGHT: setHorizontalAlignment(SwingConstants.RIGHT); break; default: setHorizontalAlignment(SwingConstants.LEFT); break; } } else { setValue(""); setBackground(SVTableUtils.white); } if (hasFocus) { if (!isBorderSet) { //This is the border to paint when there is no border //and the cell has focus cellBorder.setBorder(SVTableUtils.black, SVTableUtils.black, SVTableUtils.black, SVTableUtils.black, HSSFCellStyle.BORDER_NONE, HSSFCellStyle.BORDER_NONE, HSSFCellStyle.BORDER_NONE, HSSFCellStyle.BORDER_NONE, isSelected); setBorder(cellBorder); } if (table.isCellEditable(row, column)) { setForeground(UIManager.getColor("Table.focusCellForeground")); setBackground(UIManager.getColor("Table.focusCellBackground")); } } else if (!isBorderSet) { setBorder(noFocusBorder); } // ---- begin optimization to avoid painting background ---- Color back = getBackground(); boolean colorMatch = (back != null) && (back.equals(table.getBackground())) && table.isOpaque(); setOpaque(!colorMatch); // ---- end optimization to aviod painting background ---- return this; }