    001    // Copyright 2011 The Apache Software Foundation
    002    //
    003    // Licensed under the Apache License, Version 2.0 (the "License");
    004    // you may not use this file except in compliance with the License.
    005    // You may obtain a copy of the License at
    006    //
    007    // http://www.apache.org/licenses/LICENSE-2.0
    008    //
    009    // Unless required by applicable law or agreed to in writing, software
    010    // distributed under the License is distributed on an "AS IS" BASIS,
    011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012    // See the License for the specific language governing permissions and
    013    // limitations under the License.
    014
    015    package org.apache.tapestry5.tree;
    016
    017    import org.apache.tapestry5.BaseOptimizedSessionPersistedObject;
    018    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    019
    020    import java.util.Set;
    021
    022    /**
    023     * Default implementation of {@link TreeSelectionModel}. This is simply a wrapper around a set of node ids.
    024     *
    025     * @param <T> type of node
    026     */
    027    public class DefaultTreeSelectionModel<T> extends BaseOptimizedSessionPersistedObject implements TreeSelectionModel<T>
    028    {
    029        private final Set<String> selectedIds = CollectionFactory.newSet();
    030
    031        public boolean isSelected(TreeNode<T> node)
    032        {
    033            assert node != null;
    034
    035            return selectedIds.contains(node.getId());
    036        }
    037
    038        public void select(TreeNode<T> node)
    039        {
    040            assert node != null;
    041
    042            if (selectedIds.add(node.getId()))
    043                markDirty();
    044        }
    045
    046        public void unselect(TreeNode<T> node)
    047        {
    048            assert node != null;
    049
    050            if (selectedIds.remove(node.getId()))
    051                markDirty();
    052        }
    053
    054        public void clear()
    055        {
    056            if (!selectedIds.isEmpty())
    057            {
    058                selectedIds.clear();
    059                markDirty();
    060            }
    061        }
    062    }