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