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.util.EnumSet;
23  import java.util.Set;
24  
25  public enum Selectable {
26  
27    /**
28     * Not selectable.
29     */
30    none,
31  
32    /**
33     * Multi selection possible. No other limitations.
34     */
35    multi,
36  
37    /**
38     * Only one item is selectable.
39     */
40    single,
41  
42    /**
43     * Only one of no item is selectable.
44     */
45    singleOrNone,
46  
47    /**
48     * Only leafs are selectable.
49     */
50    multiLeafOnly,
51  
52    /**
53     * Only one item is selectable and it must be a leaf.
54     */
55    singleLeafOnly,
56  
57    /**
58     * Only siblings are selectable.
59     */
60    sibling,
61  
62    /**
63     * Only siblings are selectable and they have to be leafs.
64     */
65    siblingLeafOnly,
66  
67    /**
68     * Multi selection possible. When selecting or deselecting an item, the subtree will also
69     * be selected or unselected.
70     */
71    multiCascade;
72  
73    public static final String NONE = "none";
74  
75    public static final String MULTI = "multi";
76  
77    public static final String SINGLE = "single";
78  
79    public static final String SINGLE_OR_NONE = "singleOrNone";
80  
81    public static final String MULTI_LEAF_ONLY = "multiLeafOnly";
82  
83    public static final String SINGLE_LEAF_ONLY = "singleLeafOnly";
84  
85    public static final String SIBLING = "sibling";
86  
87    public static final String SIBLING_LEAF_ONLY = "siblingLeafOnly";
88  
89    public static final String MULTI_CASCADE = "multiCascade";
90  
91    private static final Set<Selectable> SHEET_VALUES = EnumSet.of(
92        none,
93        multi,
94        single,
95        singleOrNone);
96  
97    private static final Set<Selectable> TREE_VALUES = EnumSet.of(
98        none,
99        multi,
100       single,
101       multiLeafOnly,
102       singleLeafOnly,
103       multiCascade);
104 
105   private static final Set<Selectable> TREE_LISTBOX_VALUES = EnumSet.of(
106       single,
107       singleLeafOnly,
108       multiLeafOnly);
109 
110   /**
111    * @param name Name of the Selectable
112    * @return The matching tree selection (can't be null).
113    * @throws IllegalArgumentException When the name doesn't match any Selectable.
114    */
115   public static Selectable parse(final Object name) throws IllegalArgumentException {
116     if (name == null) {
117       return null;
118     }
119     if (name instanceof Selectable) {
120       return (Selectable) name;
121     }
122     return valueOf(name.toString());
123   }
124 
125   public boolean isLeafOnly() {
126     return this == singleLeafOnly || this == multiLeafOnly || this == siblingLeafOnly;
127   }
128 
129   public boolean isSingle() {
130     return this == single || this == singleOrNone || this == singleLeafOnly;
131   }
132 
133   public boolean isMulti() {
134     return this == multi || this == multiLeafOnly || this == multiCascade;
135   }
136 
137   public boolean isSupportedBySheet() {
138     return SHEET_VALUES.contains(this);
139   }
140 
141   public boolean isSupportedByTree() {
142     return TREE_VALUES.contains(this);
143   }
144 
145   public boolean isSupportedByTreeListbox() {
146     return TREE_LISTBOX_VALUES.contains(this);
147   }
148 }