    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 java.util.Set;
    018
    019    import org.apache.tapestry5.BaseOptimizedSessionPersistedObject;
    020    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    021
    022    /**
    023     * Manages a Set of String {@link TreeNode} ids.
    024     *
    025     * @param <T>
    026     * @since 5.3
    027     * @see TreeModel
    028     */
    029    public class DefaultTreeExpansionModel<T> extends BaseOptimizedSessionPersistedObject implements TreeExpansionModel<T>
    030    {
    031        private final Set<String> expandedIds = CollectionFactory.newSet();
    032
    033        public boolean isExpanded(TreeNode<T> node)
    034        {
    035            assert node != null;
    036
    037            return expandedIds.contains(node.getId());
    038        }
    039
    040        public void markExpanded(TreeNode<T> node)
    041        {
    042            assert node != null;
    043
    044            if (expandedIds.add(node.getId()))
    045                markDirty();
    046        }
    047
    048        public void markCollapsed(TreeNode<T> node)
    049        {
    050            assert node != null;
    051
    052            if (expandedIds.remove(node.getId()))
    053                markDirty();
    054        }
    055
    056        public void clear()
    057        {
    058            if (!expandedIds.isEmpty())
    059            {
    060                expandedIds.clear();
    061                markDirty();
    062            }
    063        }
    064    }