View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.model;
21  
22  import java.io.Serializable;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  /**
27   * Manages the selected tree paths of a tree.
28   */
29  // TODO: might be renamed to SelectedTreeState?
30  public class SelectedState implements Serializable {
31  
32    private Set<TreePath> selectedPaths = new HashSet<>();
33  
34    /**
35     * Checks if the given path is selected.
36     */
37    public boolean isSelected(final TreePath path) {
38      return selectedPaths.contains(path);
39    }
40  
41    /**
42     * Checks if the given path is an ancestor of a selected node.
43     */
44    public boolean isAncestorOfSelected(final TreePath ancestorPath) {
45      if (ancestorPath.isRoot()) {
46        return !selectedPaths.isEmpty();
47      }
48      for (TreePath selectedPath : selectedPaths) {
49        for (TreePath p = selectedPath; !p.isRoot(); p = p.getParent()) {
50          if (p.equals(ancestorPath)) {
51            return true;
52          }
53        }
54      }
55      return false;
56    }
57  
58    /**
59     * Select the given path.
60     */
61    public void select(final TreePath path) {
62      selectedPaths.add(path);
63    }
64  
65    /**
66     * Unselect the given path.
67     */
68    public void unselect(final TreePath path) {
69      selectedPaths.remove(path);
70    }
71  
72    /**
73     * Set the selected path and remove all prior selections. This is useful for "single selection" mode.
74     */
75    public void clearAndSelect(final TreePath path) {
76      clear();
77      select(path);
78    }
79  
80    /**
81     * Clears the selected state, so that no TreePath is selected.
82     */
83    public void clear() {
84      selectedPaths.clear();
85    }
86  
87    /**
88     * Set the selection state of the given path
89     */
90    public void select(final TreePath path, final boolean selected) {
91      if (selected) {
92        select(path);
93      } else {
94        unselect(path);
95      }
96    }
97  
98    @Override
99    public String toString() {
100     return selectedPaths.toString();
101   }
102 }