Example usage for com.vaadin.ui Grid sort

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

Introduction

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

Prototype

public void sort(String columnId, SortDirection direction) 

Source Link

Document

Sort this Grid in a user-specified direction by a column defined by id.

Usage

From source file:net.sourceforge.javydreamercsw.validation.manager.web.dashboard.DashboardProvider.java

License:Apache License

@Override
public Component getContent() {
    try {/*from w  w w  .j av a 2  s  .  c  o  m*/
        BorderLayout bl = new BorderLayout();
        //Add activity stream
        List<Activity> activities = ActivityServer.getActivities();
        BeanItemContainer<Activity> container = new BeanItemContainer<>(Activity.class, activities);
        GeneratedPropertyContainer gpc = new GeneratedPropertyContainer(container);
        gpc.addGeneratedProperty("avatar", new PropertyValueGenerator<Resource>() {

            @Override
            public Resource getValue(Item item, Object itemId, Object propertyId) {
                VmUser user = ((Activity) itemId).getSourceUser();
                Resource image = new ThemeResource("VMSmall.png");
                AvatarProvider ap = Lookup.getDefault().lookup(AvatarProvider.class);
                Resource icon = ap == null ? null : ap.getAvatar(user, 30);
                if (icon != null) {
                    image = icon;
                }
                return image;
            }

            @Override
            public Class<Resource> getType() {
                return Resource.class;
            }
        });
        Grid grid = new Grid(TRANSLATOR.translate("general.activity.stream"), gpc);
        Column at = grid.getColumn("activityType");
        at.setHeaderCaption(TRANSLATOR.translate("activity.type"));
        at.setConverter(new Converter<String, ActivityType>() {
            int type;

            @Override
            public ActivityType convertToModel(String value, Class<? extends ActivityType> targetType,
                    Locale locale) throws Converter.ConversionException {
                return new ActivityTypeJpaController(DataBaseManager.getEntityManagerFactory())
                        .findActivityType(type);
            }

            @Override
            public String convertToPresentation(ActivityType value, Class<? extends String> targetType,
                    Locale locale) throws Converter.ConversionException {
                type = value.getId();
                return TRANSLATOR.translate(value.getTypeName());
            }

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

            @Override
            public Class<String> getPresentationType() {
                return String.class;
            }
        });
        Column type = grid.getColumn("activityType");
        type.setHeaderCaption(TRANSLATOR.translate("general.type"));
        Column desc = grid.getColumn("description");
        desc.setHeaderCaption(TRANSLATOR.translate("general.description"));
        Column user = grid.getColumn("sourceUser");
        user.setHeaderCaption(TRANSLATOR.translate("general.user"));
        user.setConverter(new Converter<String, VmUser>() {
            private int user;

            @Override
            public String convertToPresentation(VmUser value, Class<? extends String> targetType, Locale l)
                    throws Converter.ConversionException {
                try {
                    user = value.getId();
                    return new VMUserServer(user).toString();
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                return "";
            }

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

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

            @Override
            public VmUser convertToModel(String value, Class<? extends VmUser> targetType, Locale locale)
                    throws Converter.ConversionException {
                return new VmUserJpaController(DataBaseManager.getEntityManagerFactory()).findVmUser(user);
            }
        });
        Column avatar = grid.getColumn("avatar");
        avatar.setHeaderCaption("");
        avatar.setRenderer(new ImageRenderer());
        Column time = grid.getColumn("activityTime");
        time.setHeaderCaption(TRANSLATOR.translate("general.time"));
        grid.setColumns("avatar", "sourceUser", "activityType", "description", "activityTime");
        grid.sort("activityTime", SortDirection.DESCENDING);
        bl.addComponent(grid, BorderLayout.Constraint.CENTER);
        bl.setId(getComponentCaption());
        return bl;
    } catch (IllegalArgumentException | IllegalStateException ex) {
        Exceptions.printStackTrace(ex);
    }
    return null;
}

From source file:org.jumpmind.metl.ui.views.design.ManageProjectsPanel.java

License:Open Source License

protected Component buildVersionGrid(Project project) {
    context.getConfigurationService().refresh(project);
    List<ProjectVersion> versions = project.getProjectVersions();
    BeanItemContainer<ProjectVersion> versionGridContainer = new BeanItemContainer<>(ProjectVersion.class);
    Grid versionGrid = new Grid();

    VerticalLayout layout = new VerticalLayout();
    layout.setWidth(100, Unit.PERCENTAGE);
    layout.setMargin(true);//from   ww  w.  j  a  v a 2s . co m
    layout.setSpacing(true);
    HorizontalLayout buttons = new HorizontalLayout();
    buttons.setSpacing(true);
    Button openButton = new Button("Open", (event) -> {
        Collection<Object> selected = versionGrid.getSelectedRows();
        for (Object object : selected) {
            projectNavigator.addProjectVersion(((ProjectVersion) object));
        }
    });
    buttons.addComponent(openButton);
    Button newButton = new Button("New Version", (event) -> newVersion(versionGrid));
    buttons.addComponent(newButton);
    Button editButton = new Button("Edit Version", (event) -> edit(versionGrid));
    buttons.addComponent(editButton);
    Button removeButton = new Button("Remove Version", (event) -> removeVersion(versionGrid));
    buttons.addComponent(removeButton);

    openButton.setEnabled(false);
    newButton.setEnabled(false);
    removeButton.setEnabled(false);
    editButton.setEnabled(false);

    layout.addComponent(buttons);

    versionGrid.setWidth(100, Unit.PERCENTAGE);
    versionGrid.setHeight(3 + (versions.size() * ROW_EM), Unit.EM);
    versionGrid.setEditorEnabled(true);
    versionGrid.setSelectionMode(SelectionMode.MULTI);

    versionGrid.addColumn("versionLabel", String.class).setHeaderCaption("Version").setExpandRatio(2);
    versionGrid.addColumn("description", String.class).setHeaderCaption("Description").setExpandRatio(1);
    versionGrid.addColumn("readOnly", Boolean.class).setHeaderCaption("Read Only").setMaximumWidth(100)
            .setRenderer(new HtmlRenderer(), new StringToBooleanConverter() {
                private static final long serialVersionUID = 1L;

                protected String getTrueString() {
                    return FontAwesome.CHECK.getHtml();
                };

                protected String getFalseString() {
                    return "";
                };
            });
    versionGrid.addColumn("createTime", Date.class).setHeaderCaption("Create Time").setWidth(185)
            .setMaximumWidth(200).setRenderer(new DateRenderer(UiConstants.DATETIME_FORMAT)).setEditable(false);

    versionGrid.setContainerDataSource(versionGridContainer);
    versionGrid.setEditorFieldFactory(new FieldFactory());

    versionGrid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler() {

        private static final long serialVersionUID = 1L;

        @Override
        public void preCommit(CommitEvent commitEvent) throws CommitException {
        }

        @Override
        public void postCommit(CommitEvent commitEvent) throws CommitException {
            ProjectVersion item = (ProjectVersion) versionGrid.getEditedItemId();
            IConfigurationService configurationService = context.getConfigurationService();
            configurationService.save(item);
            projectGrid.markAsDirty();
        }
    });

    versionGrid.addSelectionListener((event) -> {
        int numberSelected = versionGrid.getSelectedRows().size();
        boolean currentlyEditing = projectGrid.getEditedItemId() != null;
        boolean selected = numberSelected > 0 && !currentlyEditing;
        openButton.setEnabled(selected);
        newButton.setEnabled(selected);
        removeButton.setEnabled(selected);
        editButton.setEnabled(selected);
    });
    versionGrid.addItemClickListener(new GridClickListener(versionGrid));
    layout.addComponent(versionGrid);
    layout.setExpandRatio(versionGrid, 1);
    layout.addComponent(new Label(" "));
    versionGridContainer.addAll(versions);
    versionGrid.sort("versionLabel", SortDirection.DESCENDING);

    return layout;
}