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.ioc.internal.util.CollectionFactory; 019 020import 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 */ 027public class DefaultTreeSelectionModel<T> extends BaseOptimizedSessionPersistedObject implements TreeSelectionModel<T> 028{ 029 private static final long serialVersionUID = -2568582442906389898L; 030 031 private final Set<String> selectedIds = CollectionFactory.newSet(); 032 033 public boolean isSelected(TreeNode<T> node) 034 { 035 assert node != null; 036 037 return selectedIds.contains(node.getId()); 038 } 039 040 public void select(TreeNode<T> node) 041 { 042 assert node != null; 043 044 if (selectedIds.add(node.getId())) 045 markDirty(); 046 } 047 048 public void unselect(TreeNode<T> node) 049 { 050 assert node != null; 051 052 if (selectedIds.remove(node.getId())) 053 markDirty(); 054 } 055 056 public void clear() 057 { 058 if (!selectedIds.isEmpty()) 059 { 060 selectedIds.clear(); 061 markDirty(); 062 } 063 } 064}