Replace lambda creation with method references in WebAbstractDataGrid #1304

This commit is contained in:
Gleb Gorelov 2018-09-27 10:43:44 +04:00
parent 3c1b5ce2fc
commit 47d04d11aa
3 changed files with 112 additions and 805 deletions

View File

@ -17,7 +17,6 @@
package com.haulmont.cuba.web.gui.components;
import com.google.common.collect.ImmutableMap;
import com.haulmont.bali.events.EventRouter;
import com.haulmont.bali.events.Subscription;
import com.haulmont.bali.util.Dom4j;
import com.haulmont.bali.util.Preconditions;
@ -40,7 +39,7 @@ import com.haulmont.cuba.gui.components.sys.ShowInfoAction;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.DsBuilder;
import com.haulmont.cuba.gui.data.impl.*;
import com.haulmont.cuba.gui.data.impl.DatasourceImplementation;
import com.haulmont.cuba.gui.theme.ThemeConstants;
import com.haulmont.cuba.gui.theme.ThemeConstantsManager;
import com.haulmont.cuba.web.App;
@ -84,7 +83,6 @@ import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import javax.annotation.Nullable;
import javax.inject.Inject;
@ -97,8 +95,8 @@ import java.util.stream.Collectors;
import static com.haulmont.bali.util.Preconditions.checkNotNullArgument;
import static com.haulmont.cuba.gui.ComponentsHelper.findActionById;
public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E>, E extends Entity>
extends WebAbstractComponent<T>
public abstract class WebAbstractDataGrid<C extends Grid<E> & CubaEnhancedGrid<E>, E extends Entity>
extends WebAbstractComponent<C>
implements DataGrid<E>, SecuredActionsHolder, LookupComponent.LookupSelectionChangeNotifier,
DataGridSourceEventsDelegate<E>, HasInnerComponents, InitializingBean {
@ -108,7 +106,6 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
private static final Logger log = LoggerFactory.getLogger(WebAbstractDataGrid.class);
/* Beans */
protected ApplicationContext applicationContext;
protected MetadataTools metadataTools;
protected Security security;
protected Messages messages;
@ -153,11 +150,8 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
protected Registration columnCollapsingChangeListenerRegistration;
protected Registration columnResizeListenerRegistration;
protected Registration sortListenerRegistration;
protected Registration contextClickListenerRegistration;
protected com.vaadin.event.selection.SelectionListener<E> selectionListener;
// protected CubaGrid.EditorCloseListener editorCloseListener;
// protected CubaGrid.BeforeEditorOpenListener beforeEditorOpenListener;
// protected CubaGrid.EditorPreCommitListener editorPreCommitListener;
@ -202,7 +196,7 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
return new GridComposition();
}
protected abstract T createComponent();
protected abstract C createComponent();
protected ShortcutsDelegate<ShortcutListener> createShortcutsDelegate() {
return new ShortcutsDelegate<ShortcutListener>() {
@ -248,11 +242,6 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
initContextMenu();
}
@Inject
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Inject
public void setMetadataTools(MetadataTools metadataTools) {
this.metadataTools = metadataTools;
@ -280,19 +269,18 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
}
protected void initComponent(Grid<E> component) {
selectionListener = createSelectionListener();
setSelectionMode(SelectionMode.SINGLE);
component.setColumnReorderingAllowed(true);
component.addItemClickListener(createItemClickListener());
component.addColumnReorderListener(createColumnReorderListener());
component.addSortListener(createSortListener());
component.addItemClickListener(this::onItemClick);
component.addColumnReorderListener(this::onColumnReorder);
component.addSortListener(this::onSort);
component.setSizeUndefined();
component.setHeightMode(HeightMode.UNDEFINED);
component.setStyleGenerator(createRowStyleGenerator());
component.setStyleGenerator(this::getGeneratedRowStyle);
}
protected void initComponentComposition(GridComposition componentComposition) {
@ -304,57 +292,106 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
componentComposition.addShortcutListener(createEnterShortcutListener());
}
protected com.vaadin.event.SortEvent.SortListener<GridSortOrder<E>> createSortListener() {
return (com.vaadin.event.SortEvent.SortListener<GridSortOrder<E>>) event -> {
if (component.getDataProvider() instanceof SortableDataProvider) {
//noinspection unchecked
SortableDataProvider<E> dataProvider = (SortableDataProvider<E>) component.getDataProvider();
protected void onItemClick(Grid.ItemClick<E> e) {
CubaUI ui = (CubaUI) component.getUI();
if (!ui.isAccessibleForUser(component)) {
LoggerFactory.getLogger(WebDataGrid.class)
.debug("Ignore click attempt because DataGrid is inaccessible for user");
return;
}
List<GridSortOrder<E>> sortOrders = event.getSortOrder();
if (sortOrders.isEmpty()) {
dataProvider.resetSortOrder();
} else {
GridSortOrder<E> sortOrder = sortOrders.get(0);
com.vaadin.shared.MouseEventDetails vMouseEventDetails = e.getMouseEventDetails();
if (vMouseEventDetails.isDoubleClick() && e.getItem() != null
&& !WebAbstractDataGrid.this.isEditorEnabled()) {
// note: for now Grid doesn't send double click if editor is enabled,
// but it's better to handle it manually
handleDoubleClickAction();
}
Column<E> column = getColumnByGridColumn(sortOrder.getSorted());
if (column != null) {
MetaPropertyPath propertyPath = column.getPropertyPath();
boolean ascending = com.vaadin.shared.data.sort.SortDirection.ASCENDING
.equals(sortOrder.getDirection());
dataProvider.sort(new Object[]{propertyPath}, new boolean[]{ascending});
}
}
}
};
}
if (hasSubscriptions(ItemClickEvent.class)) {
MouseEventDetails mouseEventDetails = WebWrapperUtils.toMouseEventDetails(vMouseEventDetails);
protected com.vaadin.event.selection.SelectionListener<E> createSelectionListener() {
return e -> {
DataGridSource<E> dataGridSource = getDataGridSource();
if (dataGridSource == null
|| dataGridSource.getState() == BindingState.INACTIVE) {
//noinspection unchecked
E item = e.getItem();
if (item == null) {
// this can happen if user clicked on an item which is removed from the
// datasource, so we don't want to send such event because it's useless
return;
}
Set<E> selected = getSelected();
if (selected.isEmpty()) {
dataGridSource.setSelectedItem(null);
} else {
// reset selection and select new item
if (isMultiSelect()) {
dataGridSource.setSelectedItem(null);
}
Column<E> column = getColumnById(item.getId());
E newItem = selected.iterator().next();
dataGridSource.setSelectedItem(newItem);
ItemClickEvent<E> event = new ItemClickEvent<>(WebAbstractDataGrid.this,
mouseEventDetails, item, item.getId(), column != null ? column.getId() : null);
publish(ItemClickEvent.class, event);
}
}
protected void onColumnReorder(Grid.ColumnReorderEvent e) {
if (e.isUserOriginated()) {
// Grid doesn't know about columns hidden by security permissions,
// so we need to return them back to they previous positions
columnsOrder = restoreColumnsOrder(getColumnsOrderInternal());
ColumnReorderEvent event = new ColumnReorderEvent(WebAbstractDataGrid.this);
publish(ColumnReorderEvent.class, event);
}
}
protected void onSort(com.vaadin.event.SortEvent<GridSortOrder<E>> e) {
if (component.getDataProvider() instanceof SortableDataProvider) {
//noinspection unchecked
SortableDataProvider<E> dataProvider = (SortableDataProvider<E>) component.getDataProvider();
List<GridSortOrder<E>> sortOrders = e.getSortOrder();
if (sortOrders.isEmpty()) {
dataProvider.resetSortOrder();
} else {
GridSortOrder<E> sortOrder = sortOrders.get(0);
Column<E> column = getColumnByGridColumn(sortOrder.getSorted());
if (column != null) {
MetaPropertyPath propertyPath = column.getPropertyPath();
boolean ascending = com.vaadin.shared.data.sort.SortDirection.ASCENDING
.equals(sortOrder.getDirection());
dataProvider.sort(new Object[]{propertyPath}, new boolean[]{ascending});
}
}
}
if (e.isUserOriginated()) {
List<SortOrder> sortOrders = convertToDataGridSortOrder(e.getSortOrder());
SortEvent event = new SortEvent(WebAbstractDataGrid.this, sortOrders);
publish(SortEvent.class, event);
}
}
protected void onSelectionChange(com.vaadin.event.selection.SelectionEvent<E> e) {
DataGridSource<E> dataGridSource = getDataGridSource();
if (dataGridSource == null
|| dataGridSource.getState() == BindingState.INACTIVE) {
return;
}
Set<E> selected = getSelected();
if (selected.isEmpty()) {
dataGridSource.setSelectedItem(null);
} else {
// reset selection and select new item
if (isMultiSelect()) {
dataGridSource.setSelectedItem(null);
}
LookupSelectionChangeEvent selectionChangeEvent = new LookupSelectionChangeEvent(this);
publish(LookupSelectionChangeEvent.class, selectionChangeEvent);
E newItem = selected.iterator().next();
dataGridSource.setSelectedItem(newItem);
}
fireSelectionEvent(e);
};
LookupSelectionChangeEvent selectionChangeEvent = new LookupSelectionChangeEvent(this);
publish(LookupSelectionChangeEvent.class, selectionChangeEvent);
fireSelectionEvent(e);
}
protected void fireSelectionEvent(com.vaadin.event.selection.SelectionEvent<E> e) {
@ -404,56 +441,6 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
});
}
protected com.vaadin.ui.components.grid.ItemClickListener<E> createItemClickListener() {
return e -> {
CubaUI ui = (CubaUI) component.getUI();
if (!ui.isAccessibleForUser(component)) {
LoggerFactory.getLogger(WebDataGrid.class)
.debug("Ignore click attempt because DataGrid is inaccessible for user");
return;
}
com.vaadin.shared.MouseEventDetails vMouseEventDetails = e.getMouseEventDetails();
if (vMouseEventDetails.isDoubleClick() && e.getItem() != null
&& !WebAbstractDataGrid.this.isEditorEnabled()) {
// note: for now Grid doesn't send double click if editor is enabled,
// but it's better to handle it manually
handleDoubleClickAction();
}
if (hasSubscriptions(ItemClickEvent.class)) {
MouseEventDetails mouseEventDetails = WebWrapperUtils.toMouseEventDetails(vMouseEventDetails);
//noinspection unchecked
E item = e.getItem();
if (item == null) {
// this can happen if user clicked on an item which is removed from the
// datasource, so we don't want to send such event because it's useless
return;
}
Column<E> column = getColumnById(item.getId());
ItemClickEvent<E> event = new ItemClickEvent<>(WebAbstractDataGrid.this,
mouseEventDetails, item, item.getId(), column != null ? column.getId() : null);
publish(ItemClickEvent.class, event);
}
};
}
protected com.vaadin.ui.components.grid.ColumnReorderListener createColumnReorderListener() {
return e -> {
if (e.isUserOriginated()) {
// Grid doesn't know about columns hidden by security permissions,
// so we need to return them back to they previous positions
columnsOrder = restoreColumnsOrder(getColumnsOrderInternal());
ColumnReorderEvent event = new ColumnReorderEvent(WebAbstractDataGrid.this);
publish(ColumnReorderEvent.class, event);
}
};
}
@Override
public Collection<com.haulmont.cuba.gui.components.Component> getInnerComponents() {
if (buttonsPanel != null) {
@ -593,10 +580,6 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
return null;
}
protected RowStyleGeneratorAdapter<E> createRowStyleGenerator() {
return new RowStyleGeneratorAdapter<>();
}
@Override
public List<Column<E>> getColumns() {
return Collections.unmodifiableList(columnsOrder);
@ -1515,7 +1498,7 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
// Every time we change selection mode, the new selection model is set,
// so we need to add selection listener again.
component.getSelectionModel().addSelectionListener(selectionListener);
component.getSelectionModel().addSelectionListener(this::onSelectionChange);
}
@Override
@ -2182,7 +2165,7 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
@SuppressWarnings("unchecked")
@Override
public Function<E, String>getRowDescriptionProvider() {
public Function<E, String> getRowDescriptionProvider() {
return (Function<E, String>) rowDescriptionProvider;
}
@ -2196,16 +2179,15 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
this.rowDescriptionProvider = provider;
if (provider != null) {
component.setDescriptionGenerator(createRowDescriptionGenerator(),
component.setDescriptionGenerator(this::getRowDescription,
WebWrapperUtils.toVaadinContentMode(contentMode));
} else {
component.setDescriptionGenerator(null);
}
}
protected DescriptionGenerator<E> createRowDescriptionGenerator() {
return item ->
rowDescriptionProvider.apply(item);
protected String getRowDescription(E item) {
return rowDescriptionProvider.apply(item);
}
@Override
@ -2356,12 +2338,12 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
@Override
public Subscription addSortListener(Consumer<SortEvent> listener) {
if (sortListenerRegistration == null) {
sortListenerRegistration = component.addSortListener(this::onSort);
}
getEventHub().subscribe(SortEvent.class, listener);
return getEventHub().subscribe(SortEvent.class, listener);
}
return () -> removeSortListener(listener);
@Override
public void removeSortListener(Consumer<SortEvent> listener) {
unsubscribe(SortEvent.class, listener);
}
@Override
@ -2438,26 +2420,6 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
unsubscribe(ItemClickEvent.class, (Consumer) listener);
}
protected void onSort(com.vaadin.event.SortEvent<GridSortOrder<E>> e) {
if (e.isUserOriginated()) {
List<SortOrder> sortOrders = convertToDataGridSortOrder(e.getSortOrder());
SortEvent event = new SortEvent(WebAbstractDataGrid.this, sortOrders);
publish(SortEvent.class, event);
}
}
@Override
public void removeSortListener(Consumer<SortEvent> listener) {
unsubscribe(SortEvent.class, listener);
if (!hasSubscriptions(SortEvent.class)
&& sortListenerRegistration != null) {
sortListenerRegistration.remove();
sortListenerRegistration = null;
}
}
@Override
public Subscription addContextClickListener(Consumer<ContextClickEvent> listener) {
if (contextClickListenerRegistration == null) {
@ -2635,14 +2597,12 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
@Override
public void setDetailsGenerator(DetailsGenerator<E> detailsGenerator) {
this.detailsGenerator = detailsGenerator;
component.setDetailsGenerator(detailsGenerator != null ? createDetailsGenerator() : null);
component.setDetailsGenerator(detailsGenerator != null ? this::getRowDetails : null);
}
protected com.vaadin.ui.components.grid.DetailsGenerator<E> createDetailsGenerator() {
return (com.vaadin.ui.components.grid.DetailsGenerator<E>) item -> {
com.haulmont.cuba.gui.components.Component component = detailsGenerator.getDetails(item);
return component != null ? component.unwrapComposition(Component.class) : null;
};
protected Component getRowDetails(E item) {
com.haulmont.cuba.gui.components.Component component = detailsGenerator.getDetails(item);
return component != null ? component.unwrapComposition(Component.class) : null;
}
@Override
@ -2665,13 +2625,6 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
component.setTabIndex(tabIndex);
}
protected class RowStyleGeneratorAdapter<T extends E> implements StyleGenerator<T> {
@Override
public String apply(T item) {
return getGeneratedRowStyle(item);
}
}
@Nullable
protected String getGeneratedRowStyle(E item) {
if (rowStyleProviders == null) {
@ -3752,167 +3705,4 @@ public abstract class WebAbstractDataGrid<T extends Grid<E> & CubaEnhancedGrid<E
}
}
}
@Deprecated
public class CollectionDsListenersWrapper implements
Datasource.ItemChangeListener,
Datasource.ItemPropertyChangeListener,
Datasource.StateChangeListener,
CollectionDatasource.CollectionChangeListener {
protected WeakItemChangeListener weakItemChangeListener;
protected WeakItemPropertyChangeListener weakItemPropertyChangeListener;
protected WeakStateChangeListener weakStateChangeListener;
protected WeakCollectionChangeListener weakCollectionChangeListener;
private EventRouter eventRouter;
/**
* Use EventRouter for listeners instead of fields with listeners List.
*
* @return lazily initialized {@link EventRouter} instance.
* @see EventRouter
*/
protected EventRouter getEventRouter() {
if (eventRouter == null) {
eventRouter = new EventRouter();
}
return eventRouter;
}
@SuppressWarnings("unchecked")
@Override
public void collectionChanged(CollectionDatasource.CollectionChangeEvent e) {
// #PL-2035, reload selection from ds
Set<E> selectedItems = component.getSelectedItems();
if (selectedItems == null) {
selectedItems = Collections.emptySet();
}
//noinspection unchecked
Set<E> newSelection = selectedItems.stream()
.filter(entity -> e.getDs().containsItem(entity.getId()))
.collect(Collectors.toSet());
if (e.getDs().getState() == Datasource.State.VALID && e.getDs().getItem() != null) {
newSelection.add((E) e.getDs().getItem());
}
if (newSelection.isEmpty()) {
setSelected((E) null);
} else {
setSelectedItems(newSelection);
}
for (Action action : getActions()) {
action.refreshState();
}
if (getEventRouter().hasListeners(CollectionDatasource.CollectionChangeListener.class)) {
getEventRouter().fireEvent(CollectionDatasource.CollectionChangeListener.class,
CollectionDatasource.CollectionChangeListener::collectionChanged, e);
}
}
@SuppressWarnings("unchecked")
@Override
public void itemChanged(Datasource.ItemChangeEvent e) {
for (Action action : getActions()) {
action.refreshState();
}
if (getEventRouter().hasListeners(Datasource.ItemChangeListener.class)) {
getEventRouter().fireEvent(Datasource.ItemChangeListener.class,
Datasource.ItemChangeListener::itemChanged, e);
}
}
@SuppressWarnings("unchecked")
@Override
public void itemPropertyChanged(Datasource.ItemPropertyChangeEvent e) {
for (Action action : getActions()) {
action.refreshState();
}
if (getEventRouter().hasListeners(Datasource.ItemPropertyChangeListener.class)) {
getEventRouter().fireEvent(Datasource.ItemPropertyChangeListener.class,
Datasource.ItemPropertyChangeListener::itemPropertyChanged, e);
}
}
@SuppressWarnings("unchecked")
@Override
public void stateChanged(Datasource.StateChangeEvent e) {
for (Action action : getActions()) {
action.refreshState();
}
if (getEventRouter().hasListeners(Datasource.StateChangeListener.class)) {
getEventRouter().fireEvent(Datasource.StateChangeListener.class,
Datasource.StateChangeListener::stateChanged, e);
}
}
public void addCollectionChangeListener(CollectionDatasource.CollectionChangeListener listener) {
getEventRouter().addListener(CollectionDatasource.CollectionChangeListener.class, listener);
}
public void removeCollectionChangeListener(CollectionDatasource.CollectionChangeListener listener) {
getEventRouter().removeListener(CollectionDatasource.CollectionChangeListener.class, listener);
}
public void addItemChangeListener(Datasource.ItemChangeListener listener) {
getEventRouter().addListener(Datasource.ItemChangeListener.class, listener);
}
public void removeItemChangeListener(Datasource.ItemChangeListener listener) {
getEventRouter().removeListener(Datasource.ItemChangeListener.class, listener);
}
public void addItemPropertyChangeListener(Datasource.ItemPropertyChangeListener listener) {
getEventRouter().addListener(Datasource.ItemPropertyChangeListener.class, listener);
}
public void removeItemPropertyChangeListener(Datasource.ItemPropertyChangeListener listener) {
getEventRouter().removeListener(Datasource.ItemPropertyChangeListener.class, listener);
}
public void addStateChangeListener(Datasource.StateChangeListener listener) {
getEventRouter().addListener(Datasource.StateChangeListener.class, listener);
}
public void removeStateChangeListener(Datasource.StateChangeListener listener) {
getEventRouter().removeListener(Datasource.StateChangeListener.class, listener);
}
@SuppressWarnings("unchecked")
public void bind(CollectionDatasource ds) {
weakItemChangeListener = new WeakItemChangeListener(ds, this);
ds.addItemChangeListener(weakItemChangeListener);
weakItemPropertyChangeListener = new WeakItemPropertyChangeListener(ds, this);
ds.addItemPropertyChangeListener(weakItemPropertyChangeListener);
weakStateChangeListener = new WeakStateChangeListener(ds, this);
ds.addStateChangeListener(weakStateChangeListener);
weakCollectionChangeListener = new WeakCollectionChangeListener(ds, this);
ds.addCollectionChangeListener(weakCollectionChangeListener);
}
@SuppressWarnings("unchecked")
public void unbind(CollectionDatasource ds) {
ds.removeItemChangeListener(weakItemChangeListener);
weakItemChangeListener = null;
ds.removeItemPropertyChangeListener(weakItemPropertyChangeListener);
weakItemPropertyChangeListener = null;
ds.removeStateChangeListener(weakStateChangeListener);
weakStateChangeListener = null;
ds.removeCollectionChangeListener(weakCollectionChangeListener);
weakCollectionChangeListener = null;
}
}
}

View File

@ -1,412 +0,0 @@
/*
* Copyright (c) 2008-2016 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.web.gui.data;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils;
import com.haulmont.cuba.core.global.View;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.CollectionDsHelper;
import com.haulmont.cuba.web.gui.components.WebAbstractDataGrid.CollectionDsListenersWrapper;
import com.vaadin.v7.data.Container;
import com.vaadin.v7.data.Item;
import com.vaadin.v7.data.Property;
import com.vaadin.v7.data.util.AbstractContainer;
import com.vaadin.ui.UI;
import java.util.*;
@Deprecated
public class DataGridIndexedCollectionDsWrapper
extends
AbstractContainer
implements
Container.Indexed, Container.PropertySetChangeNotifier, Container.ItemSetChangeNotifier,
UnsubscribableDsWrapper {
protected boolean autoRefresh;
protected boolean ignoreListeners;
protected CollectionDatasource.Indexed datasource;
protected Collection<MetaPropertyPath> properties = new ArrayList<>();
protected CollectionDsListenersWrapper collectionDsListenersWrapper;
protected Datasource.StateChangeListener cdsStateChangeListener;
protected Datasource.ItemPropertyChangeListener cdsItemPropertyChangeListener;
protected CollectionDatasource.CollectionChangeListener cdsCollectionChangeListener;
public DataGridIndexedCollectionDsWrapper(CollectionDatasource datasource, boolean autoRefresh,
CollectionDsListenersWrapper collectionDsListenersWrapper) {
this(datasource, null, autoRefresh, collectionDsListenersWrapper);
}
@SuppressWarnings("unchecked")
public DataGridIndexedCollectionDsWrapper(CollectionDatasource datasource, Collection<MetaPropertyPath> properties,
boolean autoRefresh,
CollectionDsListenersWrapper collectionDsListenersWrapper) {
if (!(datasource instanceof CollectionDatasource.Indexed)) {
throw new IllegalArgumentException("Datasource must implement " +
"com.haulmont.cuba.gui.data.CollectionDatasource.Indexed");
}
this.datasource = (CollectionDatasource.Indexed) datasource;
this.autoRefresh = autoRefresh;
this.collectionDsListenersWrapper = collectionDsListenersWrapper;
final View view = datasource.getView();
final MetaClass metaClass = datasource.getMetaClass();
if (properties == null) {
createProperties(view, metaClass);
} else {
this.properties.addAll(properties);
}
cdsItemPropertyChangeListener = createItemPropertyChangeListener();
cdsStateChangeListener = createStateChangeListener();
cdsCollectionChangeListener = createCollectionChangeListener();
collectionDsListenersWrapper.addItemPropertyChangeListener(cdsItemPropertyChangeListener);
collectionDsListenersWrapper.addStateChangeListener(cdsStateChangeListener);
collectionDsListenersWrapper.addCollectionChangeListener(cdsCollectionChangeListener);
}
protected void createProperties(View view, MetaClass metaClass) {
properties.addAll(CollectionDsHelper.createProperties(view, metaClass));
}
protected CollectionDatasource.CollectionChangeListener createCollectionChangeListener() {
return new ContainerDatasourceCollectionChangeListener();
}
protected Datasource.ItemPropertyChangeListener createItemPropertyChangeListener() {
return new ContainerDatasourceItemPropertyChangeListener();
}
protected Datasource.StateChangeListener createStateChangeListener() {
return new ContainerDatasourceStateChangeListener();
}
@Override
public int indexOfId(Object itemId) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
return datasource.indexOfId(itemId);
}
@Override
public Object getIdByIndex(int index) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
return datasource.getIdByIndex(index);
}
@Override
public List getItemIds(int startIndex, int numberOfItems) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
return datasource.getItemIds(startIndex, numberOfItems);
}
@Override
public Object addItemAt(int index) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public Item addItemAt(int index, Object newItemId) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public Object nextItemId(Object itemId) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
return datasource.nextItemId(itemId);
}
@Override
public Object prevItemId(Object itemId) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
return datasource.prevItemId(itemId);
}
@Override
public Object firstItemId() {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
return datasource.firstItemId();
}
@Override
public Object lastItemId() {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
return datasource.lastItemId();
}
@Override
public boolean isFirstId(Object itemId) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
return datasource.isFirstId(itemId);
}
@Override
public boolean isLastId(Object itemId) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
return datasource.isLastId(itemId);
}
@Override
public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public Item addItemAfter(Object previousItemId, Object newItemId) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public Item getItem(Object itemId) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
final Object item = datasource.getItem(itemId);
return item == null ? null : getItemWrapper(item);
}
protected Map<Object, ItemWrapper> itemsCache = new HashMap<>();
protected Item getItemWrapper(Object item) {
return itemsCache.computeIfAbsent(item, k -> createItemWrapper(item));
}
protected ItemWrapper createItemWrapper(Object item) {
return new ItemWrapper(item, datasource.getMetaClass(), properties);
}
@Override
public Collection getContainerPropertyIds() {
return properties;
}
@Override
public Collection getItemIds() {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
return datasource.getItemIds();
}
@Override
public Property getContainerProperty(Object itemId, Object propertyId) {
final Item item = getItem(itemId);
return item == null ? null : item.getItemProperty(propertyId);
}
@Override
public Class getType(Object propertyId) {
MetaPropertyPath propertyPath = (MetaPropertyPath) propertyId;
return propertyPath.getRangeJavaClass();
}
@Override
public int size() {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
return datasource.size();
}
@Override
public boolean containsId(Object itemId) {
CollectionDsHelper.autoRefreshInvalid(datasource, autoRefresh);
//noinspection unchecked
return datasource.containsItem(itemId);
}
@Override
public Item addItem(Object itemId) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public Object addItem() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public boolean removeItem(Object itemId) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue)
throws UnsupportedOperationException {
if (propertyId instanceof MetaPropertyPath) {
if (this.properties.add((MetaPropertyPath) propertyId)) {
fireContainerPropertySetChange();
return true;
}
return false;
}
throw new UnsupportedOperationException();
}
@Override
public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException {
//noinspection SuspiciousMethodCalls
if (this.properties.remove(propertyId)) {
fireContainerPropertySetChange();
return true;
}
return false;
}
@Override
public boolean removeAllItems() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public void addPropertySetChangeListener(PropertySetChangeListener listener) {
super.addPropertySetChangeListener(listener);
}
@Override
public void addListener(PropertySetChangeListener listener) {
super.addListener(listener);
}
@Override
public void removePropertySetChangeListener(PropertySetChangeListener listener) {
super.removePropertySetChangeListener(listener);
}
@Override
public void removeListener(PropertySetChangeListener listener) {
super.removeListener(listener);
}
@Override
public void addItemSetChangeListener(ItemSetChangeListener listener) {
super.addItemSetChangeListener(listener);
}
@Override
public void addListener(ItemSetChangeListener listener) {
super.addListener(listener);
}
@Override
public void removeItemSetChangeListener(ItemSetChangeListener listener) {
super.removeItemSetChangeListener(listener);
}
@Override
public void removeListener(ItemSetChangeListener listener) {
super.removeListener(listener);
}
@SuppressWarnings("unchecked")
@Override
public void unsubscribe() {
collectionDsListenersWrapper.removeCollectionChangeListener(cdsCollectionChangeListener);
collectionDsListenersWrapper.removeItemPropertyChangeListener(cdsItemPropertyChangeListener);
collectionDsListenersWrapper.removeStateChangeListener(cdsStateChangeListener);
datasource = null;
}
protected void fireItemSetChanged() {
if (ignoreListeners) {
return;
}
ignoreListeners = true;
if (UI.getCurrent().getConnectorTracker().isWritingResponse()) {
// Suppress containerItemSetChange listeners during painting, undefined behavior may be occurred
return;
}
if (getItemSetChangeListeners() != null) {
StaticItemSetChangeEvent event = new StaticItemSetChangeEvent(this);
for (ItemSetChangeListener listener : getItemSetChangeListeners()) {
listener.containerItemSetChange(event);
}
}
}
protected class ContainerDatasourceStateChangeListener implements Datasource.StateChangeListener {
public ContainerDatasourceStateChangeListener() {
}
@Override
public void stateChanged(Datasource.StateChangeEvent e) {
itemsCache.clear();
}
}
protected class ContainerDatasourceCollectionChangeListener implements CollectionDatasource.CollectionChangeListener {
public ContainerDatasourceCollectionChangeListener() {
}
@Override
public void collectionChanged(CollectionDatasource.CollectionChangeEvent e) {
itemsCache.clear();
final boolean prevIgnoreListeners = ignoreListeners;
try {
fireItemSetChanged();
} finally {
ignoreListeners = prevIgnoreListeners;
}
}
}
protected class ContainerDatasourceItemPropertyChangeListener implements Datasource.ItemPropertyChangeListener {
public ContainerDatasourceItemPropertyChangeListener() {
}
@Override
public void itemPropertyChanged(Datasource.ItemPropertyChangeEvent e) {
Item wrapper = getItemWrapper(e.getItem());
// MetaProperty worked wrong with properties from inherited superclasses
MetaClass metaClass = datasource.getMetaClass();
String property = e.getProperty();
MetaPropertyPath metaPropertyPath = metaClass.getPropertyPath(property);
if (metaPropertyPath == null && DynamicAttributesUtils.isDynamicAttribute(property)) {
metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(metaClass, property);
}
if (metaPropertyPath == null) {
return;
}
Property itemProperty = wrapper.getItemProperty(metaPropertyPath);
if (itemProperty instanceof PropertyWrapper) {
((PropertyWrapper) itemProperty).fireValueChangeEvent();
}
}
}
@Override
public String toString() {
return "{ds=" + (datasource == null ? "null" : datasource.getId() + "}");
}
}

View File

@ -1,71 +0,0 @@
/*
* Copyright (c) 2008-2016 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.web.gui.data;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.web.gui.components.WebAbstractDataGrid.CollectionDsListenersWrapper;
import com.vaadin.v7.data.Container;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import static com.haulmont.cuba.gui.data.CollectionDatasource.Sortable.Order;
import static com.haulmont.cuba.gui.data.CollectionDatasource.Sortable.SortInfo;
@Deprecated
public class SortableDataGridIndexedCollectionDsWrapper
extends
DataGridIndexedCollectionDsWrapper
implements
Container.Sortable {
public SortableDataGridIndexedCollectionDsWrapper(CollectionDatasource datasource, boolean autoRefresh,
CollectionDsListenersWrapper collectionDsListenersWrapper) {
super(datasource, autoRefresh, collectionDsListenersWrapper);
}
@SuppressWarnings("unchecked")
public SortableDataGridIndexedCollectionDsWrapper(CollectionDatasource datasource,
Collection<MetaPropertyPath> properties,
boolean autoRefresh,
CollectionDsListenersWrapper collectionDsListenersWrapper) {
super(datasource, properties, autoRefresh, collectionDsListenersWrapper);
}
@Override
public void sort(Object[] propertyId, boolean[] ascending) {
List<SortInfo> infos = new LinkedList<>();
for (int i = 0; i < propertyId.length; i++) {
final MetaPropertyPath propertyPath = (MetaPropertyPath) propertyId[i];
final SortInfo<MetaPropertyPath> info = new SortInfo<>();
info.setPropertyPath(propertyPath);
info.setOrder(ascending[i] ? Order.ASC : Order.DESC);
infos.add(info);
}
SortInfo[] sortInfos = infos.toArray(new SortInfo[infos.size()]);
((CollectionDatasource.Sortable) datasource).sort(sortInfos);
}
@Override
public Collection<?> getSortableContainerPropertyIds() {
return properties;
}
}