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> selected = new HashSet<TreePath>();
33  
34    /**
35     * Checks if the given is selected.
36     */
37    public boolean isSelected(final TreePath path) {
38      return selected.contains(path);
39    }
40  
41    /**
42     * Select the given path.
43     */
44    public void select(final TreePath path) {
45      selected.add(path);
46    }
47  
48    /**
49     * Unselect the given path.
50     */
51    public void unselect(final TreePath path) {
52      selected.remove(path);
53    }
54  
55    /**
56     * Set the selected path and remove all prior selections.
57     * This is useful for "single selection" mode.
58     */
59    public void clearAndSelect(final TreePath path) {
60      clear();
61      select(path);
62    }
63  
64    /**
65     * Clears the selected state, so that no TreePath is selected.
66     */
67    public void clear() {
68      selected.clear();
69    }
70  
71    /**
72     * Set the selection state of the given path
73     */
74    public void select(final TreePath path, final boolean selected) {
75      if (selected) {
76        select(path);
77      } else {
78        unselect(path);
79      }
80    }
81  }