Search in sources :

Example 1 with TreeModelAdapter

use of com.haulmont.cuba.desktop.gui.data.TreeModelAdapter 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();
}
Also used : ShowInfoAction(com.haulmont.cuba.gui.components.sys.ShowInfoAction) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ShowInfoAction(com.haulmont.cuba.gui.components.sys.ShowInfoAction) Action(com.haulmont.cuba.gui.components.Action) TreeModelAdapter(com.haulmont.cuba.desktop.gui.data.TreeModelAdapter) CollectionDsActionsNotifier(com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier) WeakCollectionChangeListener(com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener) TreeSelectionListener(javax.swing.event.TreeSelectionListener)

Aggregations

UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 TreeModelAdapter (com.haulmont.cuba.desktop.gui.data.TreeModelAdapter)1 Action (com.haulmont.cuba.gui.components.Action)1 ShowInfoAction (com.haulmont.cuba.gui.components.sys.ShowInfoAction)1 CollectionDsActionsNotifier (com.haulmont.cuba.gui.data.impl.CollectionDsActionsNotifier)1 WeakCollectionChangeListener (com.haulmont.cuba.gui.data.impl.WeakCollectionChangeListener)1 TreeSelectionListener (javax.swing.event.TreeSelectionListener)1