Example usage for com.vaadin.ui Grid Grid

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

Introduction

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

Prototype

public Grid() 

Source Link

Document

Creates a new grid without support for creating columns based on property names.

Usage

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

License:Open Source License

@SuppressWarnings("unchecked")
public static Grid putResultsInGrid(final ResultSet rs, List<Integer> pkcolumns, int maxResultSize,
        final boolean showRowNumbers, String... excludeValues) throws SQLException {

    final Grid grid = new Grid();
    grid.setImmediate(true);//  w ww .j ava 2s  .co  m
    grid.setSelectionMode(SelectionMode.MULTI);
    grid.setColumnReorderingAllowed(true);
    grid.setData(new HashMap<Object, List<Object>>());

    final ResultSetMetaData meta = rs.getMetaData();
    int columnCount = meta.getColumnCount();
    grid.addColumn("#", Integer.class).setHeaderCaption("#").setHidable(true);
    Set<String> columnNames = new HashSet<String>();
    Set<Integer> skipColumnIndexes = new HashSet<Integer>();
    int[] types = new int[columnCount];
    for (int i = 1; i <= columnCount; i++) {
        String realColumnName = meta.getColumnName(i);
        String columnName = realColumnName;
        if (!Arrays.asList(excludeValues).contains(columnName)) {

            int index = 1;
            while (columnNames.contains(columnName)) {
                columnName = realColumnName + "_" + index++;
            }
            columnNames.add(columnName);

            Class<?> typeClass = Object.class;
            int type = meta.getColumnType(i);
            types[i - 1] = type;
            switch (type) {
            case Types.FLOAT:
            case Types.DOUBLE:
            case Types.NUMERIC:
            case Types.REAL:
            case Types.DECIMAL:
                typeClass = BigDecimal.class;
                break;
            case Types.TINYINT:
            case Types.SMALLINT:
            case Types.BIGINT:
            case Types.INTEGER:
                typeClass = Long.class;
                break;
            case Types.VARCHAR:
            case Types.CHAR:
            case Types.NVARCHAR:
            case Types.NCHAR:
            case Types.CLOB:
                typeClass = String.class;
            default:
                break;
            }
            Column column = grid.addColumn(columnName, typeClass).setHeaderCaption(columnName).setHidable(true);
            if (typeClass.equals(Long.class)) {
                column.setConverter(new StringToLongConverter() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String convertToPresentation(Long value, Class<? extends String> targetType,
                            Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException {
                        if (value == null) {
                            return NULL_TEXT;
                        } else {
                            return value.toString();
                        }
                    }
                });
            } else if (typeClass.equals(BigDecimal.class)) {
                column.setConverter(new StringToBigDecimalConverter() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String convertToPresentation(BigDecimal value, Class<? extends String> targetType,
                            Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException {
                        if (value == null) {
                            return NULL_TEXT;
                        } else {
                            return value.toString();
                        }
                    }
                });
            } else {
                column.setConverter(new Converter<String, Object>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public Object convertToModel(String value, Class<? extends Object> targetType,
                            Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException {
                        return null;
                    }

                    @Override
                    public String convertToPresentation(Object value, Class<? extends String> targetType,
                            Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException {
                        if (value == null) {
                            return NULL_TEXT;
                        } else {
                            return value.toString();
                        }
                    }

                    @Override
                    public Class<Object> getModelType() {
                        return Object.class;
                    }

                    @Override
                    public Class<String> getPresentationType() {
                        return String.class;
                    }

                });
            }
        } else {
            skipColumnIndexes.add(i - 1);
        }

    }
    int rowNumber = 1;
    while (rs.next() && rowNumber <= maxResultSize) {
        Object[] row = new Object[columnNames.size() + 1];
        row[0] = new Integer(rowNumber);
        int rowIndex = 1;
        for (int i = 0; i < columnCount; i++) {
            if (!skipColumnIndexes.contains(i)) {
                Object o = getObject(rs, i + 1);
                int type = types[i];
                switch (type) {
                case Types.FLOAT:
                case Types.DOUBLE:
                case Types.REAL:
                case Types.NUMERIC:
                case Types.DECIMAL:
                    if (o != null && !(o instanceof BigDecimal)) {
                        o = new BigDecimal(castToNumber(o.toString()));
                    }
                    break;
                case Types.TINYINT:
                case Types.SMALLINT:
                case Types.BIGINT:
                case Types.INTEGER:
                    if (o != null && !(o instanceof Long)) {
                        o = new Long(castToNumber(o.toString()));
                    }
                    break;
                default:
                    break;
                }
                List<Object> primaryKeys = new ArrayList<Object>();
                for (Integer pkcolumn : pkcolumns) {
                    primaryKeys.add(getObject(rs, pkcolumn + 1));
                }
                ((HashMap<Object, List<Object>>) grid.getData()).put(o, primaryKeys);
                row[rowIndex] = o;
                rowIndex++;
            }
        }
        grid.addRow(row);
        rowNumber++;
    }

    if (rowNumber < 100) {
        grid.getColumn("#").setWidth(75);
    } else if (rowNumber < 1000) {
        grid.getColumn("#").setWidth(95);
    } else {
        grid.getColumn("#").setWidth(115);
    }

    if (!showRowNumbers) {
        grid.getColumn("#").setHidden(true);
    } else {
        grid.setFrozenColumnCount(1);
    }

    return grid;
}

From source file:org.jumpmind.vaadin.ui.sqlexplorer.SqlHistoryDialog.java

License:Open Source License

public SqlHistoryDialog(ISettingsProvider settingsProvider, QueryPanel queryPanel) {
    super("Sql History");
    this.settingsProvider = settingsProvider;
    this.queryPanel = queryPanel;

    VerticalLayout mainLayout = new VerticalLayout();
    mainLayout.setSizeFull();//from   w w w  .  j a v a 2  s. c o  m
    mainLayout.setMargin(true);
    mainLayout.setSpacing(true);
    addComponent(mainLayout, 1);

    final Set<SqlHistory> sqlHistories = new TreeSet<SqlHistory>(settingsProvider.get().getSqlHistory());

    table = new Grid();
    table.setImmediate(true);

    table.addColumn("sqlStatement", String.class).setHeaderCaption("SQL")
            .setConverter(new AbbreviatorConverter(50));

    table.addColumn("lastExecuteTime", Date.class).setHeaderCaption("Time").setWidth(150).setMaximumWidth(200)
            .setRenderer(new DateRenderer("%1$tk:%1$tM:%1$tS:%1$tL"));

    table.addColumn("lastExecuteDuration", Long.class).setHeaderCaption("Duration").setWidth(120)
            .setConverter(new DurationConverter());

    table.addColumn("executeCount", Long.class).setHeaderCaption("Count").setWidth(120);
    table.setEditorEnabled(false);
    table.setSelectionMode(SelectionMode.MULTI);
    table.setRowDescriptionGenerator(new RowDescriptionGenerator() {

        private static final long serialVersionUID = 1L;

        @Override
        public String getDescription(RowReference row) {
            return (String) row.getItemId();
        }
    });
    final BeanContainer<String, SqlHistory> container = new BeanContainer<String, SqlHistory>(SqlHistory.class);
    container.setBeanIdProperty("sqlStatement");

    HeaderRow filteringHeader = table.appendHeaderRow();
    HeaderCell logTextFilterCell = filteringHeader.getCell("sqlStatement");
    TextField filterField = new TextField();
    filterField.setInputPrompt("Filter");
    filterField.addStyleName(ValoTheme.TEXTFIELD_TINY);
    filterField.setWidth("100%");

    // Update filter When the filter input is changed
    filterField.addTextChangeListener(new TextChangeListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void textChange(TextChangeEvent event) {
            // Can't modify filters so need to replace
            container.removeContainerFilters("sqlStatement");

            // (Re)create the filter if necessary
            if (!event.getText().isEmpty()) {
                container.addContainerFilter(
                        new SimpleStringFilter("sqlStatement", event.getText(), true, false));
            }

        }
    });
    logTextFilterCell.setComponent(filterField);

    table.setContainerDataSource(container);

    table.addItemClickListener(new ItemClickEvent.ItemClickListener() {

        private static final long serialVersionUID = 1L;

        public void itemClick(ItemClickEvent event) {
            Object object = event.getPropertyId();
            if (object != null && !object.toString().equals("")) {
                if (event.isDoubleClick()) {
                    table.select(event.getItemId());
                    select();
                } else {
                    Object row = event.getItemId();
                    if (!table.getSelectedRows().contains(row)) {
                        table.select(row);
                    } else {
                        table.deselect(row);
                    }
                }
            }
        }
    });

    table.setSizeFull();

    mainLayout.addComponent(table);
    mainLayout.setExpandRatio(table, 1);

    container.addAll(sqlHistories);

    Button cancelButton = new Button("Cancel");
    cancelButton.addClickListener(new Button.ClickListener() {
        private static final long serialVersionUID = 1L;

        public void buttonClick(ClickEvent event) {
            close();
        }
    });

    Button applyButton = CommonUiUtils.createPrimaryButton("Select");
    applyButton.setClickShortcut(KeyCode.ENTER);
    applyButton.addClickListener(new Button.ClickListener() {
        private static final long serialVersionUID = 1L;

        public void buttonClick(ClickEvent event) {
            select();
        }
    });

    addComponent(buildButtonFooter(cancelButton, applyButton));

}

From source file:org.openthinclient.web.pkgmngr.ui.InstallationPlanSummaryDialog.java

/**
 * Creates a table with datasource of IndexedContainer
 * @return the Grid for InstallationSummary
 *//*from   ww  w.j a  va  2  s. co m*/
private Grid<InstallationSummary> createTable(GridTypes type) {

    Grid<InstallationSummary> summary = new Grid<>();
    summary.setDataProvider(DataProvider.ofCollection(Collections.EMPTY_LIST));
    summary.setSelectionMode(Grid.SelectionMode.NONE);
    summary.addColumn(InstallationSummary::getPackageName)
            .setCaption(mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_NAME));
    summary.addColumn(InstallationSummary::getPackageVersion)
            .setCaption(mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_VERSION));
    if (type == GridTypes.INSTALL_UNINSTALL && !packageManagerOperation.hasPackagesToUninstall()) { // license column
        summary.addComponentColumn(is -> {
            if (is.getLicense() != null) {
                Button button = new Button(
                        mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_LICENSE_SHOW));
                button.addClickListener(click -> {
                    // licence already clicked, re-set button caption
                    licenceButtons.stream().filter(b -> !b.equals(button)).forEach(b -> {
                        if (b.getCaption().equals(
                                mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_LICENSE_HIDE))) {
                            b.setCaption(
                                    mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_LICENSE_SHOW));
                        }
                    });
                    // display licence
                    if (button.getCaption()
                            .equals(mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_LICENSE_SHOW))) {
                        licenceTextArea.setVisible(true);
                        licenceTextArea.setValue(is.getLicense());
                    } else {
                        licenceTextArea.setVisible(false);
                    }
                    button.setCaption(licenceTextArea.isVisible()
                            ? mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_LICENSE_HIDE)
                            : mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_LICENSE_SHOW));
                });
                button.addStyleName("package_install_summary_display_license_button");
                licenceButtons.add(button);
                return button;
            } else {
                return null;
            }
        }).setCaption(mc.getMessage(ConsoleWebMessages.UI_PACKAGEMANAGER_PACKAGE_LICENSE));
    }

    summary.addStyleName(ValoTheme.TABLE_BORDERLESS);
    summary.addStyleName(ValoTheme.TABLE_NO_HEADER);
    summary.addStyleName(ValoTheme.TABLE_NO_VERTICAL_LINES);
    summary.addStyleName(ValoTheme.TABLE_NO_HORIZONTAL_LINES);
    summary.setHeightMode(HeightMode.ROW);

    return summary;
}

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

License:Apache License

@Override
protected void init(VaadinRequest request) {
    VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);/*from   w w w  .j  a  v  a2  s. co m*/
    setContent(layout);

    Grid grid = new Grid();

    initializeGrid(grid);
    grid.setWidth("700px");
    grid.setHeight("500px");

    applyDemoHacks(grid);

    layout.addComponent(grid);
}

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

License:Apache License

protected I18NTestClass() {
    super();//from   w ww .ja  v  a  2s.co  m
    buttonWithAnnotation = new Button();
    buttonWithoutAnnotation = new Button();
    newButton = new Button();
    label = new Label();
    demoLabel = new Label();
    integer = new Integer(5);

    cnc = new TestCompositeNonComponent();
    ccn = new TestCompositeComponentNested();
    ccs = new TestCompositeComponent();
    ccc = new TestCompositeComponent();
    specificLocale = new Button();
    value = new TextField();
    valueLocale = new TextField();
    grid = new Grid();
}

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

License:Apache License

protected I18NTestClass() {
    super();//from  w  w w  .  java2s . c o  m
    buttonWithAnnotation = new Button();
    buttonWithoutAnnotation = new Button();
    newButton = new Button();
    label = new Label();
    demoLabel = new Label();
    integer = new Integer(5);
    table = new Table();
    setupTableColumns();

    cnc = new TestCompositeNonComponent();
    ccn = new TestCompositeComponentNested();
    ccs = new TestCompositeComponent();
    ccc = new TestCompositeComponent();
    specificLocale = new Button();
    value = new Label();
    valueLocale = new Label();
    grid = new Grid();
}