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

private void sort(boolean userOriginated) 

Source Link

Usage

From source file:net.sourceforge.javydreamercsw.validation.manager.web.notification.NotificationScreenProvider.java

License:Apache License

@Override
public Component getContent() {
    VerticalLayout vs = new VerticalLayout();
    //On top put a list of notifications
    BeanItemContainer<Notification> container = new BeanItemContainer<>(Notification.class);
    ValidationManagerUI.getInstance().getUser().getNotificationList().forEach(n -> {
        container.addBean(n);//from w  ww  . j ava 2s . c o m
    });
    //        Unable to use VerticalSplitPanel as I hoped.
    //        See: https://github.com/vaadin/framework/issues/9460
    //        VerticalSplitPanel vs = new VerticalSplitPanel();
    //        vs.setSplitPosition(25, Sizeable.Unit.PERCENTAGE);
    TextArea text = new TextArea(TRANSLATOR.translate("general.text"));
    text.setWordwrap(true);
    text.setReadOnly(true);
    text.setSizeFull();
    Grid grid = new Grid(TRANSLATOR.translate("general.notifications"), container);
    grid.setColumns("notificationType", "author", "creationDate", "archieved");
    if (container.size() > 0) {
        grid.setHeightMode(HeightMode.ROW);
        grid.setHeightByRows(container.size() > 5 ? 5 : container.size());
    }
    GridCellFilter filter = new GridCellFilter(grid);
    filter.setBooleanFilter("archieved",
            new GridCellFilter.BooleanRepresentation(VaadinIcons.CHECK, TRANSLATOR.translate("general.yes")),
            new GridCellFilter.BooleanRepresentation(VaadinIcons.CLOSE, TRANSLATOR.translate("general.no")));
    filter.setDateFilter("creationDate",
            new SimpleDateFormat(VMSettingServer.getSetting("date.format").getStringVal()), true);
    grid.sort("creationDate");
    Column nt = grid.getColumn("notificationType");
    nt.setHeaderCaption(TRANSLATOR.translate("notification.type"));
    nt.setConverter(new Converter<String, NotificationType>() {
        @Override
        public NotificationType convertToModel(String value, Class<? extends NotificationType> targetType,
                Locale locale) throws Converter.ConversionException {
            for (NotificationType n : new NotificationTypeJpaController(
                    DataBaseManager.getEntityManagerFactory()).findNotificationTypeEntities()) {
                if (Lookup.getDefault().lookup(InternationalizationProvider.class).translate(n.getTypeName())
                        .equals(value)) {
                    return n;
                }
            }
            return null;
        }

        @Override
        public String convertToPresentation(NotificationType value, Class<? extends String> targetType,
                Locale locale) throws Converter.ConversionException {
            return Lookup.getDefault().lookup(InternationalizationProvider.class)
                    .translate(value.getTypeName());
        }

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

        @Override
        public Class<String> getPresentationType() {
            return String.class;
        }
    });
    Column author = grid.getColumn("author");
    author.setConverter(new UserToStringConverter());
    author.setHeaderCaption(TRANSLATOR.translate("notification.author"));
    Column creation = grid.getColumn("creationDate");
    creation.setHeaderCaption(TRANSLATOR.translate("creation.time"));
    Column archive = grid.getColumn("archieved");
    archive.setHeaderCaption(TRANSLATOR.translate("general.archived"));
    archive.setConverter(new Converter<String, Boolean>() {
        @Override
        public Boolean convertToModel(String value, Class<? extends Boolean> targetType, Locale locale)
                throws Converter.ConversionException {
            return value.equals(TRANSLATOR.translate("general.yes"));
        }

        @Override
        public String convertToPresentation(Boolean value, Class<? extends String> targetType, Locale locale)
                throws Converter.ConversionException {
            return value ? TRANSLATOR.translate("general.yes") : TRANSLATOR.translate("general.no");
        }

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

        @Override
        public Class<String> getPresentationType() {
            return String.class;
        }
    });
    grid.setSelectionMode(SelectionMode.SINGLE);
    grid.setSizeFull();
    ContextMenu menu = new ContextMenu(grid, true);
    menu.addItem(TRANSLATOR.translate("notification.mark.unread"), (MenuItem selectedItem) -> {
        Object selected = ((SingleSelectionModel) grid.getSelectionModel()).getSelectedRow();
        if (selected != null) {
            NotificationServer ns = new NotificationServer((Notification) selected);
            ns.setAcknowledgeDate(null);
            try {
                ns.write2DB();
                ((VMUI) UI.getCurrent()).updateScreen();
                ((VMUI) UI.getCurrent()).showTab(getComponentCaption());
            } catch (VMException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        }
    });
    menu.addItem(TRANSLATOR.translate("notification.archive"), (MenuItem selectedItem) -> {
        Object selected = ((SingleSelectionModel) grid.getSelectionModel()).getSelectedRow();
        if (selected != null) {
            NotificationServer ns = new NotificationServer((Notification) selected);
            ns.setArchieved(true);
            try {
                ns.write2DB();
                ((VMUI) UI.getCurrent()).updateScreen();
                ((VMUI) UI.getCurrent()).showTab(getComponentCaption());
            } catch (VMException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        }
    });
    grid.addSelectionListener(selectionEvent -> {
        // Get selection from the selection model
        Object selected = ((SingleSelectionModel) grid.getSelectionModel()).getSelectedRow();
        if (selected != null) {
            text.setReadOnly(false);
            Notification n = (Notification) selected;
            text.setValue(n.getContent());
            text.setReadOnly(true);
            if (n.getAcknowledgeDate() != null) {
                try {
                    //Mark as read
                    NotificationServer ns = new NotificationServer((Notification) n);
                    ns.setAcknowledgeDate(new Date());
                    ns.write2DB();
                } catch (VMException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    vs.addComponent(grid);
    vs.addComponent(text);
    vs.setSizeFull();
    vs.setId(getComponentCaption());
    return vs;
}