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    /** @deprecated  */
74    @Deprecated
75    public static final Selectable NONE = none;
76  
77    /** @deprecated  */
78    @Deprecated
79    public static final Selectable MULTI = multi;
80  
81    /** @deprecated  */
82    @Deprecated
83    public static final Selectable SINGLE = single;
84  
85    /** @deprecated  */
86    @Deprecated
87    public static final Selectable SINGLE_OR_NONE = singleOrNone;
88  
89    /** @deprecated  */
90    @Deprecated
91    public static final Selectable MULTI_LEAF_ONLY = multiLeafOnly;
92  
93    /** @deprecated  */
94    @Deprecated
95    public static final Selectable SINGLE_LEAF_ONLY = singleLeafOnly;
96  
97    /** @deprecated  */
98    @Deprecated
99    public static final Selectable SIBLING = sibling;
100 
101   /** @deprecated  */
102   @Deprecated
103   public static final Selectable SIBLING_LEAF_ONLY = siblingLeafOnly;
104 
105   /** @deprecated  */
106   @Deprecated
107   public static final Selectable MULTI_CASCADE = multiCascade;
108 
109   public static final String STRING_NONE = "none";
110 
111   public static final String STRING_MULTI = "multi";
112 
113   public static final String STRING_SINGLE = "single";
114 
115   public static final String STRING_SINGLE_OR_NONE = "singleOrNone";
116 
117   public static final String STRING_MULTI_LEAF_ONLY = "multiLeafOnly";
118 
119   public static final String STRING_SINGLE_LEAF_ONLY = "singleLeafOnly";
120 
121   public static final String STRING_SIBLING = "sibling";
122 
123   public static final String STRING_SIBLING_LEAF_ONLY = "siblingLeafOnly";
124 
125   public static final String STRING_MULTI_CASCADE = "multiCascade";
126 
127   public static final Set<Selectable> SHEET_VALUES = EnumSet.of(
128       none,
129       multi,
130       single,
131       singleOrNone);
132 
133   public static final Set<Selectable> TREE_VALUES = EnumSet.of(
134       none,
135       multi,
136       single,
137       multiLeafOnly,
138       singleLeafOnly,
139       multiCascade);
140 
141   public static final Set<Selectable> TREE_LISTBOX_VALUES = EnumSet.of(
142       single,
143       singleLeafOnly,
144       multiLeafOnly);
145 
146   /**
147    * @param name Name of the Selectable
148    * @return The matching tree selection (can't be null).
149    * @throws IllegalArgumentException When the name doesn't match any Selectable.
150    */
151   public static Selectable parse(final Object name) throws IllegalArgumentException {
152     if (name == null) {
153       return null;
154     }
155     if (name instanceof Selectable) {
156       return (Selectable) name;
157     }
158     return valueOf(name.toString());
159   }
160 
161   public boolean isLeafOnly() {
162     return this == singleLeafOnly || this == multiLeafOnly || this == siblingLeafOnly;
163   }
164 
165   public boolean isSingle() {
166     return this == single || this == singleOrNone || this == singleLeafOnly;
167   }
168 
169   public boolean isMulti() {
170     return this == multi || this == multiLeafOnly || this == multiCascade;
171   }
172 
173   public boolean isSupportedBySheet() {
174     return SHEET_VALUES.contains(this);
175   }
176 
177   public boolean isSupportedByTree() {
178     return TREE_VALUES.contains(this);
179   }
180 
181   public boolean isSupportedByTreeListbox() {
182     return TREE_LISTBOX_VALUES.contains(this);
183   }
184 }