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.renderkit;
21  
22  import org.apache.myfaces.tobago.component.SupportsAccessKey;
23  import org.apache.myfaces.tobago.component.UISelectBooleanCheckbox;
24  import org.apache.myfaces.tobago.internal.util.Deprecation;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import java.util.Locale;
29  
30  public final class LabelWithAccessKey {
31  
32    private static final Logger LOG = LoggerFactory.getLogger(LabelWithAccessKey.class);
33  
34    private final String label;
35    private final Character accessKey;
36    private final int pos;
37  
38    /**
39     * @deprecated since 2.0.0. This is a workaround.
40     */
41    @Deprecated
42    public LabelWithAccessKey(final String label) {
43      this(new SupportsAccessKey() {
44        @Override
45        public Character getAccessKey() {
46          return null;
47        }
48  
49        @Override
50        public String getLabel() {
51          return label;
52        }
53      });
54    }
55  
56    public LabelWithAccessKey(final SupportsAccessKey component) {
57  
58      String label0;
59      Character accessKey0;
60      int pos0 = -1;
61  
62      label0 = component.getLabel();
63  
64      // compatibility since TOBAGO-1093
65      if (component instanceof UISelectBooleanCheckbox) {
66        final String itemLabel = ((UISelectBooleanCheckbox) component).getItemLabel();
67        if (itemLabel != null) {
68          label0 = itemLabel;
69        }
70      }
71  
72      accessKey0 = component.getAccessKey();
73      if (accessKey0 != null) {
74        accessKey0 = Character.toLowerCase(accessKey0);
75        if (!isPermitted(accessKey0)) {
76          LOG.warn("Ignoring illegal access key: " + accessKey0);
77          accessKey0 = null;
78        }
79      }
80  
81      if (accessKey0 != null && label0 != null) {
82        pos0 = label0.toLowerCase(Locale.ENGLISH).indexOf(accessKey0);
83      }
84  
85      label = label0;
86      accessKey = accessKey0;
87      pos = pos0;
88    }
89  
90    /**
91     * @deprecated since 2.0.0. Attributes are final now, use a new instance.
92     */
93    @Deprecated
94    public void setup(final String label) {
95      Deprecation.LOG.error("Ignoring label: " + label);
96    }
97  
98    /**
99     * @deprecated since 2.0.0. Attributes are final now, use a new instance.
100    */
101   @Deprecated
102   public void reset() {
103     Deprecation.LOG.error("Ignoring reset.");
104   }
105 
106   /**
107    * @deprecated since 2.0.0. Please use {@link #getLabel()}.
108    */
109   @Deprecated
110   public String getText() {
111     return label;
112   }
113 
114   public String getLabel() {
115     return label;
116   }
117 
118   public Character getAccessKey() {
119     return accessKey;
120   }
121 
122   public int getPos() {
123     return pos;
124   }
125 
126   /**
127    * @deprecated since 2.0.0. Attributes are final now, use a new instance.
128    */
129   @Deprecated
130   public void setText(final String text) {
131     Deprecation.LOG.error("Ignoring label: " + text);
132   }
133 
134   /**
135    * @deprecated since 2.0.0. Attributes are final now, use a new instance.
136    */
137   @Deprecated
138   public void setAccessKey(Character accessKey) {
139     Deprecation.LOG.error("Ignoring accessKey: " + accessKey);
140   }
141 
142   /**
143    * Ensures, that no illegal character will be write out.
144    * (If this is changed from only allowing normal letters and numbers, the renderers may change the escaping)
145    */
146   private boolean isPermitted(final Character accessKey) {
147     return accessKey == null || accessKey >= 'a' && accessKey <= 'z' || accessKey >= '0' && accessKey <= '9';
148   }
149 }