use of com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener in project cuba by cuba-platform.
the class WebTokenList method setDatasource.
@Override
public void setDatasource(CollectionDatasource datasource) {
Preconditions.checkNotNullArgument(datasource, "datasource is null");
if (this.datasource != null) {
throw new UnsupportedOperationException("Changing datasource is not supported by the TokenList component");
}
this.datasource = datasource;
collectionChangeListener = e -> {
if (lookupPickerField != null) {
if (isLookup()) {
if (getLookupScreen() != null) {
lookupAction.setLookupScreen(getLookupScreen());
} else {
lookupAction.setLookupScreen(null);
}
lookupAction.setLookupScreenOpenType(lookupOpenMode);
lookupAction.setLookupScreenParams(lookupScreenParams);
lookupAction.setLookupScreenDialogParams(lookupScreenDialogParams);
}
}
component.refreshComponent();
component.refreshClickListeners(itemClickListener);
};
// noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
}
use of com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener in project cuba by cuba-platform.
the class DesktopAbstractTable method setDatasource.
@Override
public void setDatasource(final CollectionDatasource datasource) {
Preconditions.checkNotNullArgument(datasource, "datasource is null");
final Collection<Object> properties;
if (this.columns.isEmpty()) {
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
MessageTools messageTools = AppBeans.get(MessageTools.NAME);
Collection<MetaPropertyPath> paths = datasource.getView() != null ? // if a view is specified - use view properties
metadataTools.getViewPropertyPaths(datasource.getView(), datasource.getMetaClass()) : // otherwise use only string properties from meta-class - the temporary solution for KeyValue datasources
metadataTools.getPropertyPaths(datasource.getMetaClass()).stream().filter(mpp -> mpp.getRangeJavaClass().equals(String.class)).collect(Collectors.toList());
for (MetaPropertyPath metaPropertyPath : paths) {
MetaProperty property = metaPropertyPath.getMetaProperty();
if (!property.getRange().getCardinality().isMany() && !metadataTools.isSystem(property)) {
Table.Column column = new Table.Column(metaPropertyPath);
String propertyName = property.getName();
MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(metaPropertyPath);
column.setCaption(messageTools.getPropertyCaption(propertyMetaClass, propertyName));
column.setType(metaPropertyPath.getRangeJavaClass());
Element element = DocumentHelper.createElement("column");
column.setXmlDescriptor(element);
addColumn(column);
}
}
}
properties = this.columns.keySet();
this.datasource = datasource;
collectionChangeListener = e -> {
switch(e.getOperation()) {
case CLEAR:
case REFRESH:
fieldDatasources.clear();
break;
case UPDATE:
case REMOVE:
for (Object entity : e.getItems()) {
fieldDatasources.remove(entity);
}
break;
case ADD:
// no action
break;
}
};
// noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
initTableModel(datasource);
initChangeListener();
setColumnIdentifiers();
if (isSortable()) {
impl.setRowSorter(new RowSorterImpl(tableModel));
}
initSelectionListener(datasource);
List<MetaPropertyPath> editableColumns = null;
if (isEditable()) {
editableColumns = new LinkedList<>();
}
MetaClass metaClass = datasource.getMetaClass();
for (final Object property : properties) {
final Table.Column column = this.columns.get(property);
if (column != null) {
if (column.isCollapsed() && getColumnControlVisible()) {
TableColumn tableColumn = getColumn(column);
if (tableColumn instanceof TableColumnExt) {
((TableColumnExt) tableColumn).setVisible(false);
}
}
if (editableColumns != null && column.isEditable() && (property instanceof MetaPropertyPath)) {
MetaPropertyPath propertyPath = (MetaPropertyPath) property;
if (security.isEntityAttrUpdatePermitted(metaClass, property.toString())) {
editableColumns.add(propertyPath);
}
}
}
}
if (editableColumns != null && !editableColumns.isEmpty()) {
setEditableColumns(editableColumns);
}
List<Object> columnsOrder = new ArrayList<>();
for (Table.Column column : this.columnsOrder) {
if (column.getId() instanceof MetaPropertyPath) {
MetaPropertyPath metaPropertyPath = (MetaPropertyPath) column.getId();
if (security.isEntityAttrReadPermitted(metaClass, metaPropertyPath.toString())) {
columnsOrder.add(column.getId());
}
} else {
columnsOrder.add(column.getId());
}
}
setVisibleColumns(columnsOrder);
if (security.isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION)) {
ShowInfoAction action = (ShowInfoAction) getAction(ShowInfoAction.ACTION_ID);
if (action == null) {
action = new ShowInfoAction();
addAction(action);
}
action.setDatasource(datasource);
}
securityCollectionChangeListener = e -> {
onDataChange();
packRows();
// #PL-2035, reload selection from ds
Set<E> selectedItems1 = getSelected();
if (selectedItems1 == null) {
selectedItems1 = Collections.emptySet();
}
Set<E> newSelection = new HashSet<>();
for (E entity : selectedItems1) {
if (e.getDs().containsItem(entity.getId())) {
newSelection.add(entity);
}
}
if (e.getDs().getState() == Datasource.State.VALID && e.getDs().getItem() != null) {
if (e.getDs().containsItem(e.getDs().getItem().getId())) {
newSelection.add((E) e.getDs().getItem());
}
}
if (newSelection.isEmpty()) {
setSelected((E) null);
} else {
setSelected(newSelection);
}
};
// noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, securityCollectionChangeListener));
itemPropertyChangeListener = e -> {
List<Column> columns1 = getColumns();
boolean find = false;
int i = 0;
while ((i < columns1.size()) & !find) {
Object columnId = columns1.get(i).getId();
if (columnId instanceof MetaPropertyPath) {
String propertyName = ((MetaPropertyPath) columnId).getMetaProperty().getName();
if (propertyName.equals(e.getProperty())) {
find = true;
}
}
i++;
}
if (find) {
onDataChange();
}
packRows();
};
// noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
if (rowsCount != null) {
rowsCount.setDatasource(datasource);
}
collectionDsActionsNotifier = new CollectionDsActionsNotifier(this);
collectionDsActionsNotifier.bind(datasource);
for (Action action : getActions()) {
action.refreshState();
}
if (!canBeSorted(datasource))
setSortable(false);
}
use of com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener in project cuba by cuba-platform.
the class DesktopTree method setDatasource.
@Override
public void setDatasource(HierarchicalDatasource datasource) {
this.datasource = datasource;
hierarchyProperty = datasource.getHierarchyPropertyName();
model = new TreeModelAdapter(datasource, captionMode, captionProperty, true);
impl.setModel(model);
impl.addTreeSelectionListener(new SelectionListener());
UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);
if (uss.getUserSession().isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION)) {
ShowInfoAction action = (ShowInfoAction) getAction(ShowInfoAction.ACTION_ID);
if (action == null) {
action = new ShowInfoAction();
addAction(action);
}
action.setDatasource(datasource);
}
collectionChangeListener = e -> {
// #PL-2035, reload selection from ds
Set<E> selectedItems = getSelected();
if (selectedItems == null) {
selectedItems = Collections.emptySet();
}
Set<E> newSelection = new HashSet<>();
for (E entity : selectedItems) {
if (e.getDs().containsItem(entity.getId())) {
newSelection.add(entity);
}
}
if (e.getDs().getState() == Datasource.State.VALID && e.getDs().getItem() != null) {
// noinspection unchecked
newSelection.add((E) e.getDs().getItem());
}
if (newSelection.isEmpty()) {
setSelected((Entity) null);
} else {
setSelected(newSelection);
}
};
// noinspection unchecked
datasource.addCollectionChangeListener(new WeakCollectionChangeListener(datasource, collectionChangeListener));
collectionDsActionsNotifier = new CollectionDsActionsNotifier(this);
collectionDsActionsNotifier.bind(datasource);
for (Action action : getActions()) {
action.refreshState();
}
assignAutoDebugId();
}
use of com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener in project cuba by cuba-platform.
the class DesktopOptionsGroup method setOptionsDatasource.
@Override
public void setOptionsDatasource(CollectionDatasource datasource) {
if (optionsInitialized)
return;
super.setOptionsDatasource(datasource);
if (optionsDatasource != null) {
if (!optionsDatasource.getState().equals(Datasource.State.VALID)) {
optionsDatasource.refresh();
}
for (Entity item : optionsDatasource.getItems()) {
addItem(new EntityWrapper(item));
}
collectionChangeListener = e -> {
Object value = getValue();
removeAllItems();
for (Object id : e.getDs().getItemIds()) {
addItem(new EntityWrapper(e.getDs().getItem(id)));
}
updateComponent(value);
fireChangeListeners(getValue());
impl.revalidate();
impl.repaint();
};
optionsDatasource.addCollectionChangeListener(new WeakCollectionChangeListener(optionsDatasource, collectionChangeListener));
if ((datasource != null) && (datasource.getState() == Datasource.State.VALID)) {
Entity newValue = datasource.getItem();
updateComponent(newValue);
fireChangeListeners(newValue);
}
optionsInitialized = true;
}
assignAutoDebugId();
}
use of com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener in project cuba by cuba-platform.
the class WebRowsCount method setDatasource.
@Override
public void setDatasource(CollectionDatasource datasource) {
Preconditions.checkNotNullArgument(datasource, "datasource is null");
if (this.datasource != null) {
// noinspection unchecked
this.datasource.removeCollectionChangeListener(weakCollectionChangeListener);
weakCollectionChangeListener = null;
} else {
// noinspection unchecked
collectionChangeListener = e -> {
samePage = Operation.REFRESH != e.getOperation() && Operation.CLEAR != e.getOperation();
onCollectionChanged();
};
}
this.datasource = datasource;
weakCollectionChangeListener = new WeakCollectionChangeListener(datasource, collectionChangeListener);
// noinspection unchecked
datasource.addCollectionChangeListener(weakCollectionChangeListener);
component.getCountButton().addClickListener(event -> onLinkClick());
component.getPrevButton().addClickListener(event -> onPrevClick());
component.getNextButton().addClickListener(event -> onNextClick());
component.getFirstButton().addClickListener(event -> onFirstClick());
component.getLastButton().addClickListener(event -> onLastClick());
if (datasource.getState() == Datasource.State.VALID) {
onCollectionChanged();
}
}
Aggregations