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