Example usage for com.vaadin.ui Grid getColumns

List of usage examples for com.vaadin.ui Grid getColumns

Introduction

In this page you can find the example usage for com.vaadin.ui Grid getColumns.

Prototype

public List<Column<T, ?>> getColumns() 

Source Link

Document

Gets an unmodifiable collection of all columns currently in this Grid .

Usage

From source file:com.ocs.dynamo.ui.composite.table.export.TableExportActionHandler.java

License:Apache License

/**
 * Export a grid - this is achieved by wrapping the data source from the grid in a table
 * /* w  w w .j  a va  2 s  . co m*/
 * @param grid
 */
public void exportFromGrid(Grid grid) {
    Table table = new Table();

    table.setContainerDataSource(grid.getContainerDataSource());

    // copy header captions from grid to table
    for (Column c : grid.getColumns()) {
        Object oid = c.getPropertyId();

        String caption = "";
        for (int i = 0; i < grid.getHeaderRowCount(); i++) {
            HeaderRow r = grid.getHeaderRow(i);

            if (caption.length() > 0) {
                caption += " ";
            }

            try {
                caption += r.getCell(oid).getText();
            } catch (Exception ex) {
                // if it is not text, then it is HTML (very ugly, but seems
                // to
                // be the only way)
                caption += r.getCell(oid).getHtml();
            }
        }
        // replace HTML line breaks by space
        if (caption != null) {
            caption = caption.replaceAll("<\\w+/>", " ");
        }

        table.setColumnHeader(c.getPropertyId(), caption);
    }
    handleAction(actionExport, table, null);
}

From source file:com.rex.components.valo.Tables.java

License:Apache License

static void configure(Table table, Grid grid, boolean footer, boolean sized, boolean expandRatios,
        boolean stripes, boolean verticalLines, boolean horizontalLines, boolean borderless, boolean headers,
        boolean compact, boolean small, boolean rowIndex, boolean rowCaption, boolean rowIcon,
        boolean componentsInRows) {

    table.setSelectable(true);/*from  w  w w  . j  a v a2 s.co  m*/
    table.setMultiSelect(true);
    grid.setSelectionMode(SelectionMode.MULTI);

    table.setSortEnabled(true);
    for (Column c : grid.getColumns()) {
        if (!c.getPropertyId().equals("icon")) {
            c.setSortable(true);
        }
        c.setHidable(true);
    }

    table.setColumnCollapsingAllowed(true);
    table.setColumnReorderingAllowed(true);
    grid.setColumnReorderingAllowed(true);

    table.setPageLength(6);
    grid.setHeightByRows(6);

    table.addActionHandler(ReportEngineUI.getActionHandler());
    table.setDragMode(TableDragMode.MULTIROW);
    table.setDropHandler(new DropHandler() {
        @Override
        public AcceptCriterion getAcceptCriterion() {
            return AcceptAll.get();
        }

        @Override
        public void drop(DragAndDropEvent event) {
            Notification.show(event.getTransferable().toString());
        }
    });
    table.setColumnAlignment(ReportEngineUI.DESCRIPTION_PROPERTY, Align.RIGHT);
    table.setColumnAlignment(ReportEngineUI.INDEX_PROPERTY, Align.CENTER);

    table.removeContainerProperty("textfield");
    table.removeGeneratedColumn("textfield");
    table.removeContainerProperty("button");
    table.removeGeneratedColumn("button");
    table.removeContainerProperty("label");
    table.removeGeneratedColumn("label");
    table.removeContainerProperty("checkbox");
    table.removeGeneratedColumn("checkbox");
    table.removeContainerProperty("datefield");
    table.removeGeneratedColumn("datefield");
    table.removeContainerProperty("combobox");
    table.removeGeneratedColumn("combobox");
    table.removeContainerProperty("optiongroup");
    table.removeGeneratedColumn("optiongroup");
    table.removeContainerProperty("slider");
    table.removeGeneratedColumn("slider");
    table.removeContainerProperty("progress");
    table.removeGeneratedColumn("progress");

    if (componentsInRows) {
        table.addContainerProperty("textfield", TextField.class, null);
        table.addGeneratedColumn("textfield", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                TextField tf = new TextField();
                tf.setInputPrompt("Type here");
                // tf.addStyleName("compact");
                if ((Integer) itemId % 2 == 0) {
                    tf.addStyleName("borderless");
                }
                return tf;
            }
        });

        table.addContainerProperty("datefield", TextField.class, null);
        table.addGeneratedColumn("datefield", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                DateField tf = new DateField();
                tf.addStyleName("compact");
                if ((Integer) itemId % 2 == 0) {
                    tf.addStyleName("borderless");
                }
                return tf;
            }
        });

        table.addContainerProperty("combobox", TextField.class, null);
        table.addGeneratedColumn("combobox", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                ComboBox tf = new ComboBox();
                tf.setInputPrompt("Select");
                tf.addStyleName("compact");
                if ((Integer) itemId % 2 == 0) {
                    tf.addStyleName("borderless");
                }
                return tf;
            }
        });

        table.addContainerProperty("button", Button.class, null);
        table.addGeneratedColumn("button", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                Button b = new Button("Button");
                b.addStyleName("small");
                return b;
            }
        });

        table.addContainerProperty("label", TextField.class, null);
        table.addGeneratedColumn("label", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                Label label = new Label("Label component");
                label.setSizeUndefined();
                label.addStyleName("bold");
                return label;
            }
        });

        table.addContainerProperty("checkbox", TextField.class, null);
        table.addGeneratedColumn("checkbox", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                CheckBox cb = new CheckBox(null, true);
                return cb;
            }
        });

        table.addContainerProperty("optiongroup", TextField.class, null);
        table.addGeneratedColumn("optiongroup", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                OptionGroup op = new OptionGroup();
                op.addItem("Male");
                op.addItem("Female");
                op.addStyleName("horizontal");
                return op;
            }
        });

        table.addContainerProperty("slider", TextField.class, null);
        table.addGeneratedColumn("slider", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                Slider s = new Slider();
                s.setValue(30.0);
                return s;
            }
        });

        table.addContainerProperty("progress", TextField.class, null);
        table.addGeneratedColumn("progress", new ColumnGenerator() {
            @Override
            public Object generateCell(Table source, Object itemId, Object columnId) {
                ProgressBar bar = new ProgressBar();
                bar.setValue(0.7f);
                return bar;
            }
        });
    }
    table.setFooterVisible(footer);
    if (footer) {
        table.setColumnFooter(ReportEngineUI.CAPTION_PROPERTY, "caption");
        table.setColumnFooter(ReportEngineUI.DESCRIPTION_PROPERTY, "description");
        table.setColumnFooter(ReportEngineUI.ICON_PROPERTY, "icon");
        table.setColumnFooter(ReportEngineUI.INDEX_PROPERTY, "index");
    }

    if (sized) {
        table.setWidth("400px");
        grid.setWidth("400px");
        table.setHeight("300px");
        grid.setHeight("300px");
    } else {
        table.setSizeUndefined();
        grid.setSizeUndefined();
    }

    if (componentsInRows) {
        table.setWidth("100%");
    } else {
        table.setWidth(null);
    }

    if (expandRatios) {
        if (!sized) {
            table.setWidth("100%");
        }
    }
    table.setColumnExpandRatio(ReportEngineUI.CAPTION_PROPERTY, expandRatios ? 1.0f : 0);
    table.setColumnExpandRatio(ReportEngineUI.DESCRIPTION_PROPERTY, expandRatios ? 1.0f : 0);

    if (!stripes) {
        table.addStyleName("no-stripes");
    } else {
        table.removeStyleName("no-stripes");
    }

    if (!verticalLines) {
        table.addStyleName("no-vertical-lines");
    } else {
        table.removeStyleName("no-vertical-lines");
    }

    if (!horizontalLines) {
        table.addStyleName("no-horizontal-lines");
    } else {
        table.removeStyleName("no-horizontal-lines");
    }

    if (borderless) {
        table.addStyleName("borderless");
    } else {
        table.removeStyleName("borderless");
    }

    if (!headers) {
        table.addStyleName("no-header");
    } else {
        table.removeStyleName("no-header");
    }

    if (compact) {
        table.addStyleName("compact");
    } else {
        table.removeStyleName("compact");
    }

    if (small) {
        table.addStyleName("small");
    } else {
        table.removeStyleName("small");
    }

    if (!rowIndex && !rowCaption && rowIcon) {
        table.setRowHeaderMode(RowHeaderMode.HIDDEN);
    }

    if (rowIndex) {
        table.setRowHeaderMode(RowHeaderMode.INDEX);
    }

    if (rowCaption) {
        table.setRowHeaderMode(RowHeaderMode.PROPERTY);
        table.setItemCaptionPropertyId(ReportEngineUI.CAPTION_PROPERTY);
    } else {
        table.setItemCaptionPropertyId(null);
    }

    if (rowIcon) {
        table.setRowHeaderMode(RowHeaderMode.ICON_ONLY);
        table.setItemIconPropertyId(ReportEngineUI.ICON_PROPERTY);
    } else {
        table.setItemIconPropertyId(null);
    }
}

From source file:de.symeda.sormas.ui.utils.GridExportStreamResource.java

License:Open Source License

public GridExportStreamResource(Grid<?> grid, String tempFilePrefix, String filename,
        String... ignoredPropertyIds) {
    super(new StreamSource() {
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override/*from w w w  .j a  va2  s.  co m*/
        public InputStream getStream() {
            List<String> ignoredPropertyIdsList = Arrays.asList(ignoredPropertyIds);
            List<Column> columns = new ArrayList<>(grid.getColumns());
            columns.removeIf(c -> c.isHidden());
            columns.removeIf(c -> ignoredPropertyIdsList.contains(c.getId()));

            try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
                try (CSVWriter writer = CSVUtils.createCSVWriter(
                        new OutputStreamWriter(byteStream, StandardCharsets.UTF_8.name()),
                        FacadeProvider.getConfigFacade().getCsvSeparator())) {

                    List<String> headerRow = new ArrayList<>();
                    columns.forEach(c -> {
                        headerRow.add(c.getCaption());
                    });
                    writer.writeNext(headerRow.toArray(new String[headerRow.size()]));

                    String[] rowValues = new String[columns.size()];

                    int totalRowCount = grid.getDataProvider().size(new Query());
                    for (int i = 0; i < totalRowCount; i += 100) {
                        grid.getDataProvider().fetch(new Query(i, 100, grid.getSortOrder(), null, null))
                                .forEach(row -> {
                                    for (int c = 0; c < columns.size(); c++) {
                                        Column column = columns.get(c);
                                        Object value = column.getValueProvider().apply(row);
                                        String valueString;
                                        if (value != null) {
                                            if (value instanceof Date) {
                                                valueString = DateHelper.formatLocalDateTime((Date) value);
                                            } else if (value instanceof Boolean) {
                                                if ((Boolean) value == true) {
                                                    valueString = I18nProperties
                                                            .getEnumCaption(YesNoUnknown.YES);
                                                } else
                                                    valueString = I18nProperties
                                                            .getEnumCaption(YesNoUnknown.NO);
                                            } else {
                                                valueString = value.toString();
                                            }
                                        } else {
                                            valueString = "";
                                        }
                                        rowValues[c] = valueString;
                                    }
                                    writer.writeNext(rowValues);
                                });
                        writer.flush();
                    }
                }
                return new BufferedInputStream(new ByteArrayInputStream(byteStream.toByteArray()));
            } catch (IOException e) {
                // TODO This currently requires the user to click the "Export" button again or reload the page as the UI
                // is not automatically updated; this should be changed once Vaadin push is enabled (see #516)
                new Notification(I18nProperties.getString(Strings.headingExportFailed),
                        I18nProperties.getString(Strings.messageExportFailed), Type.ERROR_MESSAGE, false)
                                .show(Page.getCurrent());
                return null;
            }
        }
    }, filename);
    setMIMEType("text/csv");
    setCacheTime(0);
}

From source file:org.jumpmind.vaadin.ui.common.CommonUiUtils.java

License:Open Source License

public static String[] getHeaderCaptions(Grid grid) {
    List<String> headers = new ArrayList<String>();
    List<Column> columns = grid.getColumns();
    for (Column column : columns) {
        headers.add(column.getHeaderCaption());
    }//  w  ww.  j av  a 2s  . com
    return headers.toArray(new String[headers.size()]);
}

From source file:org.vaadin.allaboutgrid.AllAboutGridUI.java

License:Apache License

private void hackDateColumnWidth(Grid grid) {
    List<Column> columns = grid.getColumns();
    if (columns.isEmpty()) {
        return;//from  w  w w . ja v  a 2 s .  c  o m
    }

    Column lastCol = columns.get(columns.size() - 1);
    if (!"orderTime".equals(lastCol.getPropertyId())) {
        return;
    }

    lastCol.setWidth(174);
}

From source file:uk.q3c.krail.core.i18n.DefaultI18NProcessor.java

License:Apache License

/**
 * Sets the I18N values for the Grid itself, and also iterates the columns for column ids which are I18NKeys, and translates those as well
 *
 * @param grid             the Grid to process
 * @param annotationValues the annotation values to apply
 * @param annotationInfo   used primarily to identify the Field, and therefore its name
 *//*  ww  w . j a va2s .  com*/

protected void processGrid(Grid grid, AnnotationValues annotationValues, AnnotationInfo annotationInfo) {

    // do the grid itself
    applyAnnotationValues(grid, annotationValues, annotationInfo);

    // now do the column headers
    Locale locale = annotationValues.locale.isPresent() ? annotationValues.locale.get()
            : currentLocale.getLocale();
    final List<Grid.Column> columns = grid.getColumns();
    I18NKeyConverter converter = new I18NKeyConverter();

    for (Grid.Column column : columns) {
        try {
            I18NKey columnKey = converter.convertToModel(column.getId());
            String header = translate.from(columnKey, locale);
            column.setCaption(header);
        } catch (Exception e) {
            log.debug("Column id {} is not an I18NKey", column.getId());
        }
    }
}

From source file:uk.q3c.krail.i18n.DefaultI18NProcessor.java

License:Apache License

/**
 * Sets the I18N values for the Grid itself, and also iterates the columns for column ids which are I18NKeys, and translates those as well
 *
 * @param grid/*from  w ww .  j  a v  a  2  s  .c  om*/
 *         the Grid to process
 * @param annotationValues
 *         the annotation values to apply
 * @param annotationInfo
 *         used primarily to identify the Field, and therefore its name
 */
protected void processGrid(Grid grid, AnnotationValues annotationValues, AnnotationInfo annotationInfo) {

    // do the grid itself
    applyAnnotationValues(grid, annotationValues, annotationInfo);

    // now do the column headers
    Locale locale = annotationValues.locale.isPresent() ? annotationValues.locale.get()
            : currentLocale.getLocale();
    final List<Grid.Column> columns = grid.getColumns();

    for (Grid.Column column : columns) {
        if (column.getPropertyId() instanceof I18NKey) {
            I18NKey columnKey = (I18NKey) column.getPropertyId();
            String header = translate.from(columnKey, locale);
            column.setHeaderCaption(header);
        } else {
            column.setHeaderCaption(column.getPropertyId().toString());
        }
    }
}