Search in sources :

Example 1 with CellStyleProxy

use of org.eclipse.nebula.widgets.nattable.style.CellStyleProxy in project nebula.widgets.nattable by eclipse.

the class BlendedBackgroundPainter method blendBackgroundColour.

/**
 * Returns a background colour for the specified cell. If multiple colours
 * have been registered, they are all blended together.
 *
 * @param cell
 *            the
 *            {@link org.eclipse.nebula.widgets.nattable.layer.cell.LayerCell}
 *            to get a background colour for.
 * @param configRegistry
 *            an
 *            {@link org.eclipse.nebula.widgets.nattable.config.IConfigRegistry}
 *            .
 * @param baseColor
 *            Colours are not blended with this colour.
 * @return A blended background colour.
 */
public static Color blendBackgroundColour(final ILayerCell cell, final IConfigRegistry configRegistry, final RGB baseColor) {
    // Get all of the background colours registered for the cell in normal
    // mode.
    final List<Color> colours = CellStyleUtil.getAllBackgroundColors(cell, configRegistry, DisplayMode.NORMAL);
    // to the blending mix.
    if (cell.getDisplayMode().equals(DisplayMode.SELECT)) {
        final IStyle cellStyle = new CellStyleProxy(configRegistry, DisplayMode.SELECT, cell.getConfigLabels().getLabels());
        colours.add(cellStyle.getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR));
    }
    if (colours.size() == 0) {
        return null;
    } else if (colours.size() == 1) {
        return colours.get(0);
    } else {
        RGB rgb = colours.get(0).getRGB();
        for (int i = 1; i < colours.size(); i++) {
            // Don't blend with the grid background colour.
            if (rgb.equals(baseColor)) {
                rgb = colours.get(i).getRGB();
            } else if (!colours.get(i).getRGB().equals(baseColor)) {
                rgb = GUIHelper.blend(rgb, colours.get(i).getRGB());
            }
        }
        return GUIHelper.getColor(rgb);
    }
}
Also used : IStyle(org.eclipse.nebula.widgets.nattable.style.IStyle) Color(org.eclipse.swt.graphics.Color) RGB(org.eclipse.swt.graphics.RGB) CellStyleProxy(org.eclipse.nebula.widgets.nattable.style.CellStyleProxy)

Example 2 with CellStyleProxy

use of org.eclipse.nebula.widgets.nattable.style.CellStyleProxy in project nebula.widgets.nattable by eclipse.

the class NatTableCSSHelper method getNatTableStyle.

/**
 * Retrieves the style attribute for the given display mode and config
 * labels out of the NatTable configuration. Uses the NatTable internal
 * inheritance model to always retrieve a style configuration attribute if
 * there is one configured at any level.
 *
 * @param natTable
 *            The NatTable whose {@link ConfigRegistry} should be checked
 *            for the style configuration.
 * @param styleConfig
 *            The style {@link ConfigAttribute} that is requested.
 * @param displayMode
 *            The {@link DisplayMode} for which the configuration is
 *            requested.
 * @param configLabels
 *            The config labels for which the configuration is requested.
 * @return The style attribute for the given display mode and config labels
 *         out of the NatTable configuration.
 */
public static <T> T getNatTableStyle(NatTable natTable, ConfigAttribute<T> styleConfig, String displayMode, String... configLabels) {
    IConfigRegistry configRegistry = natTable.getConfigRegistry();
    IStyle style = new CellStyleProxy(configRegistry, displayMode, Arrays.asList(configLabels));
    return style.getAttributeValue(styleConfig);
}
Also used : IStyle(org.eclipse.nebula.widgets.nattable.style.IStyle) IConfigRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry) CellStyleProxy(org.eclipse.nebula.widgets.nattable.style.CellStyleProxy)

Example 3 with CellStyleProxy

use of org.eclipse.nebula.widgets.nattable.style.CellStyleProxy in project nebula.widgets.nattable by eclipse.

the class PoiExcelExporter method exportCell.

@Override
public void exportCell(OutputStream outputStream, Object exportDisplayValue, ILayerCell cell, IConfigRegistry configRegistry) throws IOException {
    int columnPosition = cell.getColumnPosition();
    int rowPosition = cell.getRowPosition();
    if (columnPosition != cell.getOriginColumnPosition() || rowPosition != cell.getOriginRowPosition()) {
        return;
    }
    if (this.applyColumnWidths && cell.getColumnSpan() == 1) {
        this.xlSheet.setColumnWidth(columnPosition, getPoiColumnWidth(cell.getBounds().width) + getPoiColumnWidth(5));
    }
    if (this.applyRowHeights && cell.getRowSpan() == 1) {
        this.xlRow.setHeight(getPoiRowHeight(cell.getBounds().height));
    }
    // check if cell was already created because of spanning
    Cell xlCell = this.xlRow.getCell(columnPosition);
    if (xlCell == null) {
        xlCell = this.xlRow.createCell(columnPosition);
    }
    CellStyleProxy cellStyle = new CellStyleProxy(configRegistry, DisplayMode.NORMAL, cell.getConfigLabels().getLabels());
    Color fg = cellStyle.getAttributeValue(CellStyleAttributes.FOREGROUND_COLOR);
    Color bg = cellStyle.getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR);
    org.eclipse.swt.graphics.Font font = cellStyle.getAttributeValue(CellStyleAttributes.FONT);
    FontData fontData = font.getFontData()[0];
    String dataFormat = null;
    if (exportDisplayValue instanceof Calendar || exportDisplayValue instanceof Date) {
        dataFormat = getDataFormatString(cell, configRegistry);
    }
    int hAlign = HorizontalAlignmentEnum.getSWTStyle(cellStyle);
    int vAlign = VerticalAlignmentEnum.getSWTStyle(cellStyle);
    ICellPainter cellPainter = configRegistry.getConfigAttribute(CellConfigAttributes.CELL_PAINTER, DisplayMode.NORMAL, cell.getConfigLabels().getLabels());
    boolean vertical = this.applyVerticalTextConfiguration ? isVertical(cellPainter) : false;
    boolean wrap = this.applyTextWrapping ? wrapText(cellPainter) : false;
    CellStyle xlCellStyle = getExcelCellStyle(fg, bg, fontData, dataFormat, hAlign, vAlign, vertical, wrap, this.applyCellBorders);
    xlCell.setCellStyle(xlCellStyle);
    int columnSpan = cell.getColumnSpan();
    int rowSpan = cell.getRowSpan();
    if (columnSpan > 1 || rowSpan > 1) {
        int lastRow = rowPosition + rowSpan - 1;
        int lastColumn = columnPosition + columnSpan - 1;
        this.xlSheet.addMergedRegion(new CellRangeAddress(rowPosition, lastRow, columnPosition, lastColumn));
        // apply style to all cells that get merged
        Row tempRow = null;
        Cell tempCell = null;
        for (int i = rowPosition; i <= lastRow; i++) {
            tempRow = this.xlSheet.getRow(i);
            if (tempRow == null) {
                tempRow = this.xlSheet.createRow(i);
            }
            for (int j = columnPosition; j <= lastColumn; j++) {
                tempCell = tempRow.getCell(j);
                if (tempCell == null) {
                    tempCell = tempRow.createCell(j);
                }
                tempCell.setCellStyle(xlCellStyle);
            }
        }
    }
    if (exportDisplayValue == null)
        // $NON-NLS-1$
        exportDisplayValue = "";
    if (exportDisplayValue instanceof Boolean) {
        xlCell.setCellValue((Boolean) exportDisplayValue);
    } else if (exportDisplayValue instanceof Calendar) {
        xlCell.setCellValue((Calendar) exportDisplayValue);
    } else if (exportDisplayValue instanceof Date) {
        xlCell.setCellValue((Date) exportDisplayValue);
    } else if (exportDisplayValue instanceof Number) {
        xlCell.setCellValue(((Number) exportDisplayValue).doubleValue());
    } else if (exportDisplayValue instanceof InputStream) {
        exportImage((InputStream) exportDisplayValue, xlCell);
    } else if (this.formulaParser != null) {
        // formula export is enabled, so we perform checks on the cell
        // values
        String cellValue = exportDisplayValue.toString();
        if (this.formulaParser.isFunction(cellValue)) {
            String functionString = this.formulaParser.getFunctionOnly(cellValue);
            // POI expects the formula parameters to be separated by ,
            // instead of ;
            // also localized decimal separators need to be modified for
            // export
            functionString = functionString.replace(',', '.');
            functionString = functionString.replace(';', ',');
            xlCell.setCellFormula(functionString);
        } else if (this.formulaParser.isNumber(cellValue)) {
            try {
                xlCell.setCellValue(this.nf.parse(cellValue).doubleValue());
            } catch (ParseException e) {
                // $NON-NLS-1$
                throw new IOException("Error on parsing number value: " + cellValue, e);
            }
        } else {
            xlCell.setCellValue(exportDisplayValue.toString());
        }
    } else {
        xlCell.setCellValue(exportDisplayValue.toString());
    }
}
Also used : InputStream(java.io.InputStream) Color(org.eclipse.swt.graphics.Color) FontData(org.eclipse.swt.graphics.FontData) Calendar(java.util.Calendar) IOException(java.io.IOException) Date(java.util.Date) ICellPainter(org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter) CellStyle(org.apache.poi.ss.usermodel.CellStyle) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Row(org.apache.poi.ss.usermodel.Row) ParseException(java.text.ParseException) Cell(org.apache.poi.ss.usermodel.Cell) ILayerCell(org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell) CellStyleProxy(org.eclipse.nebula.widgets.nattable.style.CellStyleProxy)

Example 4 with CellStyleProxy

use of org.eclipse.nebula.widgets.nattable.style.CellStyleProxy in project nebula.widgets.nattable by eclipse.

the class CellStyleProxyTest method proxyShouldRetreiveConfigAttributeUsingTheDisplayModeOrdering.

@Test
public void proxyShouldRetreiveConfigAttributeUsingTheDisplayModeOrdering() throws Exception {
    ConfigRegistry configRegistry = new ConfigRegistry();
    Style testCellStyle1 = new Style();
    testCellStyle1.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.RIGHT);
    Style testCellStyle2 = new Style();
    testCellStyle2.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.CENTER);
    testCellStyle2.setAttributeValue(CellStyleAttributes.VERTICAL_ALIGNMENT, VerticalAlignmentEnum.MIDDLE);
    // NORMAL mode has an horizontal align attribute registered
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, testCellStyle1, DisplayMode.NORMAL, TEST_CONFIG_LABEL1);
    // SELECT mode has a 'default' horizontal align attribute registered
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, testCellStyle2, DisplayMode.SELECT);
    // The 'default' from SELECT gets picked up
    StyleProxy cellStyleProxy = new CellStyleProxy(configRegistry, DisplayMode.SELECT, Arrays.asList(TEST_CONFIG_LABEL1));
    HorizontalAlignmentEnum alignmentFromProxy = cellStyleProxy.getAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT);
    Assert.assertEquals(HorizontalAlignmentEnum.CENTER, alignmentFromProxy);
}
Also used : ConfigRegistry(org.eclipse.nebula.widgets.nattable.config.ConfigRegistry) HorizontalAlignmentEnum(org.eclipse.nebula.widgets.nattable.style.HorizontalAlignmentEnum) Style(org.eclipse.nebula.widgets.nattable.style.Style) StyleProxy(org.eclipse.nebula.widgets.nattable.style.StyleProxy) CellStyleProxy(org.eclipse.nebula.widgets.nattable.style.CellStyleProxy) CellStyleProxy(org.eclipse.nebula.widgets.nattable.style.CellStyleProxy) Test(org.junit.Test)

Example 5 with CellStyleProxy

use of org.eclipse.nebula.widgets.nattable.style.CellStyleProxy in project nebula.widgets.nattable by eclipse.

the class AbstractCellEditor method activateCell.

@Override
public final Control activateCell(Composite parent, Object originalCanonicalValue, EditModeEnum editMode, ICellEditHandler editHandler, ILayerCell cell, IConfigRegistry configRegistry) {
    this.closed = false;
    this.parent = parent;
    this.editHandler = editHandler;
    this.editMode = editMode;
    this.layerCell = cell;
    this.configRegistry = configRegistry;
    this.labelStack = cell.getConfigLabels();
    final List<String> configLabels = this.labelStack.getLabels();
    this.displayConverter = configRegistry.getConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, DisplayMode.EDIT, configLabels);
    this.cellStyle = new CellStyleProxy(configRegistry, DisplayMode.EDIT, configLabels);
    this.dataValidator = configRegistry.getConfigAttribute(EditConfigAttributes.DATA_VALIDATOR, DisplayMode.EDIT, configLabels);
    this.conversionEditErrorHandler = EditConfigHelper.getEditErrorHandler(configRegistry, EditConfigAttributes.CONVERSION_ERROR_HANDLER, configLabels);
    this.validationEditErrorHandler = EditConfigHelper.getEditErrorHandler(configRegistry, EditConfigAttributes.VALIDATION_ERROR_HANDLER, configLabels);
    return activateCell(parent, originalCanonicalValue);
}
Also used : CellStyleProxy(org.eclipse.nebula.widgets.nattable.style.CellStyleProxy)

Aggregations

CellStyleProxy (org.eclipse.nebula.widgets.nattable.style.CellStyleProxy)6 Color (org.eclipse.swt.graphics.Color)3 IStyle (org.eclipse.nebula.widgets.nattable.style.IStyle)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellStyle (org.apache.poi.ss.usermodel.CellStyle)1 Row (org.apache.poi.ss.usermodel.Row)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 ConfigRegistry (org.eclipse.nebula.widgets.nattable.config.ConfigRegistry)1 IConfigRegistry (org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)1 ILayerCell (org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell)1 ICellPainter (org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter)1 HorizontalAlignmentEnum (org.eclipse.nebula.widgets.nattable.style.HorizontalAlignmentEnum)1 Style (org.eclipse.nebula.widgets.nattable.style.Style)1 StyleProxy (org.eclipse.nebula.widgets.nattable.style.StyleProxy)1 Font (org.eclipse.swt.graphics.Font)1