001    // Copyright 2004, 2005 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.tapestry.contrib.tree.simple;
016    
017    import java.io.Serializable;
018    import java.util.HashSet;
019    import java.util.Set;
020    
021    import org.apache.tapestry.contrib.tree.model.ITreeStateModel;
022    
023    /**
024     * @author ceco
025     */
026    public class SimpleTreeStateModel implements ITreeStateModel, Serializable
027    {
028            private static final long serialVersionUID = 9206852255511734400L;
029            
030            private Set m_setExpanded;
031            private Object m_objSelectedNodeUID = null;
032            
033            /**
034             * Constructor for SimpleTreeStateModel.
035             */
036            public SimpleTreeStateModel() {
037                    super();
038                    initialize();
039            }
040            private void initialize(){
041                    m_setExpanded = new HashSet();
042                    m_objSelectedNodeUID = null;
043            }
044    
045            /**
046             * @see org.apache.tapestry.contrib.tree.model.ITreeStateModel#getExpandSelection()
047             */
048            public Set getExpandSelection() {
049                    return m_setExpanded;
050            }
051            
052            /**
053             * @see org.apache.tapestry.contrib.tree.model.ITreeStateModel#expand(Object)
054             */
055            public void expand(Object objUniqueKey) {
056                    m_setExpanded.add(objUniqueKey);
057                    //setSelectedNode(objUniqueKey);
058            }
059    
060            /**
061             * @see org.apache.tapestry.contrib.tree.model.ITreeStateModel#expandPath(Object)
062             */
063            public void expandPath(Object objUniqueKey) {
064                    m_setExpanded.add(objUniqueKey);
065                    //setSelectedNode(objUniqueKey);
066            }
067    
068            /**
069             * @see org.apache.tapestry.contrib.tree.model.ITreeStateModel#collapse(Object)
070             */
071            public void collapse(Object objUniqueKey) {
072                    m_setExpanded.remove(objUniqueKey);
073                    //setSelectedNode(objUniqueKey);
074            }
075    
076            /**
077             * @see org.apache.tapestry.contrib.tree.model.ITreeStateModel#collapsePath(Object)
078             */
079            public void collapsePath(Object objUniqueKey) {
080                    m_setExpanded.remove(objUniqueKey);
081                    //setSelectedNode(objUniqueKey);
082            }
083    
084            /**
085             * @see org.apache.tapestry.contrib.tree.model.ITreeStateModel#isUniqueKeyExpanded(Object)
086             */
087            public boolean isUniqueKeyExpanded(Object objUniqueKey) {
088                    return m_setExpanded.contains(objUniqueKey);
089            }
090            /**
091             * @see org.apache.tapestry.contrib.tree.model.ITreeStateModel#getSelectedNode()
092             */
093            public Object getSelectedNode() {
094                    return m_objSelectedNodeUID;
095            }
096            
097            public void setSelectedNode(Object objUniqueKey){
098                    if(m_objSelectedNodeUID == null || !m_objSelectedNodeUID.equals(objUniqueKey))
099                            m_objSelectedNodeUID = objUniqueKey;
100            }
101    
102            /**
103             * @see org.apache.tapestry.contrib.tree.model.ITreeStateModel#resetState()
104             */
105            public void resetState() {
106                    initialize();
107            }
108    
109    }