Search in sources :

Example 1 with SelectionListener

use of com.vaadin.event.SelectionEvent.SelectionListener in project VaadinUtils by rlsutton1.

the class ReportParameterTable method init.

protected void init(final String caption, final Class<T> tableClass, final SingularAttribute<T, String> displayField) {
    JpaBaseDao.getGenericDao(tableClass).flushCache();
    container = createContainer(tableClass, displayField);
    UI ui = UI.getCurrent();
    if (ui != null) {
        Runnable runner = new Runnable() {

            @Override
            public void run() {
                try (AutoCloseable closer = EntityManagerProvider.setThreadLocalEntityManagerTryWithResources()) {
                    ReportParameterTable.this.displayField = displayField;
                    layout.setSizeFull();
                    ReportParameterTable.this.caption = caption;
                    TextField searchText = new TextField();
                    searchText.setInputPrompt("Search");
                    searchText.setWidth("100%");
                    searchText.setImmediate(true);
                    searchText.setHeight("20");
                    searchText.addTextChangeListener(new TextChangeListener() {

                        private static final long serialVersionUID = 1315710313315289836L;

                        @Override
                        public void textChange(TextChangeEvent event) {
                            String value = event.getText();
                            removeAllContainerFilters();
                            if (value.length() > 0) {
                                container.addContainerFilter(new SimpleStringFilter(displayField.getName(), value, true, false));
                            }
                        }
                    });
                    grid = new Grid();
                    grid.setImmediate(true);
                    grid.setSizeFull();
                    // table.setHeight("150");
                    grid.setContainerDataSource(container);
                    // build lower case id's map, so we get the case of the
                    // id right
                    Map<String, String> idCaseMap = new HashMap<>();
                    for (Object id : container.getSortableContainerPropertyIds()) {
                        idCaseMap.put(((String) id).toLowerCase(), (String) id);
                    }
                    new GridHeadingPropertySet.Builder<T>().createColumn(caption, idCaseMap.get(displayField.getName().toLowerCase())).setLockedState(true).build().applyToGrid(grid);
                    if (idCaseMap.containsKey(displayField.getName().toLowerCase())) {
                        List<SortOrder> orders = new LinkedList<>();
                        orders.add(new SortOrder(idCaseMap.get(displayField.getName().toLowerCase()), SortDirection.ASCENDING));
                        grid.setSortOrder(orders);
                    }
                    final Label selectionCount = new Label("0 selected");
                    // removed for concertina
                    // layout.addComponent(new Label(caption));
                    layout.addComponent(searchText);
                    layout.addComponent(grid);
                    HorizontalLayout selectionLayout = new HorizontalLayout();
                    selectionLayout.setHeight("30");
                    selectionLayout.setWidth("100%");
                    selectionLayout.addComponent(selectionCount);
                    selectionLayout.setComponentAlignment(selectionCount, Alignment.MIDDLE_RIGHT);
                    layout.addComponent(selectionLayout);
                    grid.addSelectionListener(new SelectionListener() {

                        /**
                         */
                        private static final long serialVersionUID = 1L;

                        @Override
                        public void select(SelectionEvent event) {
                            validate();
                            selectionCount.setValue("" + event.getSelected().size() + " selected");
                        }
                    });
                    layout.setExpandRatio(grid, 1);
                    // layout.setComponentAlignment(selectAll,
                    // Alignment.BOTTOM_RIGHT);
                    loadContainerItems(container, tableClass);
                } catch (Exception e) {
                    ErrorWindow.showErrorWindow(e);
                }
            }
        };
        UI.getCurrent().accessSynchronously(runner);
    } else {
        logger.warn("No vaadin session available, not setting up UI");
    }
}
Also used : HashMap(java.util.HashMap) Grid(com.vaadin.ui.Grid) Label(com.vaadin.ui.Label) SortOrder(com.vaadin.data.sort.SortOrder) LinkedList(java.util.LinkedList) HorizontalLayout(com.vaadin.ui.HorizontalLayout) UI(com.vaadin.ui.UI) TextChangeEvent(com.vaadin.event.FieldEvents.TextChangeEvent) SelectionEvent(com.vaadin.event.SelectionEvent) TextField(com.vaadin.ui.TextField) TextChangeListener(com.vaadin.event.FieldEvents.TextChangeListener) SimpleStringFilter(com.vaadin.data.util.filter.SimpleStringFilter) SelectionListener(com.vaadin.event.SelectionEvent.SelectionListener)

Example 2 with SelectionListener

use of com.vaadin.event.SelectionEvent.SelectionListener in project VaadinUtils by rlsutton1.

the class ReportParameterTable method addSelectionListener.

@Override
public void addSelectionListener(final ValueChangeListener listener) {
    UI ui = UI.getCurrent();
    if (ui != null) {
        Runnable runner = new Runnable() {

            @Override
            public void run() {
                grid.addSelectionListener(new SelectionListener() {

                    /**
                     */
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void select(SelectionEvent event) {
                        listener.valueChange(new ValueChangeEvent() {

                            /**
                             */
                            private static final long serialVersionUID = 1L;

                            @Override
                            public Property<Collection<Long>> getProperty() {
                                return new Property<Collection<Long>>() {

                                    /**
                                     */
                                    private static final long serialVersionUID = 1L;

                                    @Override
                                    public Collection<Long> getValue() {
                                        return getSelectedIds();
                                    }

                                    @Override
                                    public void setValue(Collection<Long> newValue) throws com.vaadin.data.Property.ReadOnlyException {
                                    }

                                    @Override
                                    public Class<? extends Collection<Long>> getType() {
                                        return null;
                                    }

                                    @Override
                                    public boolean isReadOnly() {
                                        return false;
                                    }

                                    @Override
                                    public void setReadOnly(boolean newStatus) {
                                    }
                                };
                            }
                        });
                    }
                });
            }
        };
        UI.getCurrent().accessSynchronously(runner);
    } else {
        logger.warn("No vaadin session available, not setting up UI");
    }
}
Also used : ValueChangeEvent(com.vaadin.data.Property.ValueChangeEvent) UI(com.vaadin.ui.UI) SelectionEvent(com.vaadin.event.SelectionEvent) Collection(java.util.Collection) Property(com.vaadin.data.Property) SelectionListener(com.vaadin.event.SelectionEvent.SelectionListener)

Aggregations

SelectionEvent (com.vaadin.event.SelectionEvent)2 SelectionListener (com.vaadin.event.SelectionEvent.SelectionListener)2 UI (com.vaadin.ui.UI)2 Property (com.vaadin.data.Property)1 ValueChangeEvent (com.vaadin.data.Property.ValueChangeEvent)1 SortOrder (com.vaadin.data.sort.SortOrder)1 SimpleStringFilter (com.vaadin.data.util.filter.SimpleStringFilter)1 TextChangeEvent (com.vaadin.event.FieldEvents.TextChangeEvent)1 TextChangeListener (com.vaadin.event.FieldEvents.TextChangeListener)1 Grid (com.vaadin.ui.Grid)1 HorizontalLayout (com.vaadin.ui.HorizontalLayout)1 Label (com.vaadin.ui.Label)1 TextField (com.vaadin.ui.TextField)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1