Search in sources :

Example 1 with Table

use of com.haulmont.cuba.gui.components.Table in project cuba by cuba-platform.

the class GroupTableLoader method loadColumns.

@Override
protected List<Table.Column> loadColumns(final Table component, Element columnsElement, CollectionDatasource ds) {
    List<Table.Column> columns = new ArrayList<>();
    Element groupElement = columnsElement.element("group");
    if (groupElement != null) {
        columns.addAll(super.loadColumns(component, groupElement, ds));
        final List<Object> groupProperties = new ArrayList<>(columns.size());
        for (Table.Column column : columns) {
            if (column.isCollapsed()) {
                String msg = String.format("Can't group by collapsed column: %s", column.getId());
                throw new GuiDevelopmentException(msg, context.getFullFrameId());
            }
            if (column.isGroupAllowed()) {
                groupProperties.add(column.getId());
            }
        }
        context.addPostInitTask((context1, window) -> ((GroupTable) component).groupBy(groupProperties.toArray()));
    }
    columns.addAll(super.loadColumns(component, columnsElement, ds));
    return columns;
}
Also used : GroupTable(com.haulmont.cuba.gui.components.GroupTable) Table(com.haulmont.cuba.gui.components.Table) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 2 with Table

use of com.haulmont.cuba.gui.components.Table in project cuba by cuba-platform.

the class LinkColumnHelper method initColumn.

public static void initColumn(Table table, final String propertyName, final Handler handler) {
    final ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.NAME);
    table.addGeneratedColumn(propertyName, new Table.ColumnGenerator() {

        @Override
        public Component generateCell(final Entity entity) {
            // //process properties like building.house.room
            String[] props = propertyName.split("\\.");
            Instance nestedEntity = entity;
            for (int i = 0; i < props.length - 1; i++) {
                nestedEntity = nestedEntity.getValue(props[i]);
                if (nestedEntity == null) {
                    break;
                }
            }
            final Object value = (nestedEntity == null) ? null : nestedEntity.getValue(props[props.length - 1]);
            if (value != null) {
                Button button = componentsFactory.createComponent(Button.class);
                button.setStyleName("link");
                button.setAction(new AbstractAction("open") {

                    @Override
                    public void actionPerform(Component component) {
                        handler.onClick(entity);
                    }

                    @Override
                    public String getCaption() {
                        String str;
                        Datatype datatype = Datatypes.get(value.getClass());
                        if (datatype != null) {
                            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
                            str = datatype.format(value, sessionSource.getLocale());
                        } else {
                            str = value.toString();
                        }
                        return str;
                    }
                });
                button.setStyleName("link");
                return button;
            }
            return null;
        }
    });
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Table(com.haulmont.cuba.gui.components.Table) Instance(com.haulmont.chile.core.model.Instance) Datatype(com.haulmont.chile.core.datatypes.Datatype) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Button(com.haulmont.cuba.gui.components.Button) Component(com.haulmont.cuba.gui.components.Component) AbstractAction(com.haulmont.cuba.gui.components.AbstractAction)

Example 3 with Table

use of com.haulmont.cuba.gui.components.Table in project cuba by cuba-platform.

the class WebAbstractTable method initComponent.

protected void initComponent(final T component) {
    component.setMultiSelect(false);
    component.setImmediate(true);
    component.setValidationVisible(false);
    component.setShowBufferedSourceException(false);
    component.setBeforePaintListener(() -> {
        com.vaadin.ui.Table.CellStyleGenerator generator = component.getCellStyleGenerator();
        if (generator instanceof WebAbstractTable.StyleGeneratorAdapter) {
            // noinspection unchecked
            ((StyleGeneratorAdapter) generator).resetExceptionHandledFlag();
        }
    });
    Messages messages = AppBeans.get(Messages.NAME);
    component.setSortAscendingLabel(messages.getMainMessage("tableSort.ascending"));
    component.setSortResetLabel(messages.getMainMessage("tableSort.reset"));
    component.setSortDescendingLabel(messages.getMainMessage("tableSort.descending"));
    setClientCaching(component);
    int defaultRowHeaderWidth = 16;
    ThemeConstants theme = App.getInstance().getThemeConstants();
    if (theme != null) {
        defaultRowHeaderWidth = theme.getInt("cuba.web.Table.defaultRowHeaderWidth");
    }
    // CAUTION: vaadin considers null as row header property id;
    // todo get width from theme
    component.setColumnWidth(null, defaultRowHeaderWidth);
    contextMenuPopup.setParent(component);
    component.setContextMenuPopup(contextMenuPopup);
    shortcutsDelegate.setAllowEnterShortcut(false);
    component.addValueChangeListener(event -> {
        if (datasource == null) {
            return;
        }
        final Set<? extends Entity> selected = getSelected();
        if (selected.isEmpty()) {
            Entity dsItem = datasource.getItemIfValid();
            datasource.setItem(null);
            if (dsItem == null) {
                // in this case item change event will not be generated
                refreshActionsState();
            }
        } else {
            // reset selection and select new item
            if (isMultiSelect()) {
                datasource.setItem(null);
            }
            Entity newItem = selected.iterator().next();
            Entity dsItem = datasource.getItemIfValid();
            datasource.setItem(newItem);
            if (Objects.equals(dsItem, newItem)) {
                // in this case item change event will not be generated
                refreshActionsState();
            }
        }
        LookupSelectionChangeEvent selectionChangeEvent = new LookupSelectionChangeEvent(this);
        getEventRouter().fireEvent(LookupSelectionChangeListener.class, LookupSelectionChangeListener::lookupValueChanged, selectionChangeEvent);
    });
    component.addShortcutListener(new ShortcutListener("tableEnter", com.vaadin.event.ShortcutAction.KeyCode.ENTER, null) {

        @Override
        public void handleAction(Object sender, Object target) {
            if (target == WebAbstractTable.this.component) {
                if (enterPressAction != null) {
                    enterPressAction.actionPerform(WebAbstractTable.this);
                } else {
                    handleClickAction();
                }
            }
        }
    });
    component.addItemClickListener(event -> {
        if (event.isDoubleClick() && event.getItem() != null) {
            handleClickAction();
        }
    });
    component.setSelectable(true);
    component.setTableFieldFactory(createFieldFactory());
    component.setColumnCollapsingAllowed(true);
    component.setColumnReorderingAllowed(true);
    setEditable(false);
    componentComposition = new TableComposition();
    componentComposition.setTable(component);
    componentComposition.setPrimaryStyleName("c-table-composition");
    componentComposition.addComponent(component);
    component.setCellStyleGenerator(createStyleGenerator());
    component.addColumnCollapseListener(this::handleColumnCollapsed);
    // force default sizes
    componentComposition.setHeightUndefined();
    componentComposition.setWidthUndefined();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Table(com.haulmont.cuba.gui.components.Table) CubaEnhancedTable(com.haulmont.cuba.web.toolkit.ui.CubaEnhancedTable) ThemeConstants(com.haulmont.cuba.gui.theme.ThemeConstants) ShortcutListener(com.vaadin.event.ShortcutListener)

Aggregations

Table (com.haulmont.cuba.gui.components.Table)3 Entity (com.haulmont.cuba.core.entity.Entity)2 Datatype (com.haulmont.chile.core.datatypes.Datatype)1 Instance (com.haulmont.chile.core.model.Instance)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)1 AbstractAction (com.haulmont.cuba.gui.components.AbstractAction)1 Button (com.haulmont.cuba.gui.components.Button)1 Component (com.haulmont.cuba.gui.components.Component)1 GroupTable (com.haulmont.cuba.gui.components.GroupTable)1 ThemeConstants (com.haulmont.cuba.gui.theme.ThemeConstants)1 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)1 CubaEnhancedTable (com.haulmont.cuba.web.toolkit.ui.CubaEnhancedTable)1 ShortcutListener (com.vaadin.event.ShortcutListener)1 ArrayList (java.util.ArrayList)1 Element (org.dom4j.Element)1