1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.renderkit;
21
22 import org.apache.myfaces.tobago.component.Attributes;
23 import org.apache.myfaces.tobago.component.SupportsAccessKey;
24 import org.apache.myfaces.tobago.util.ComponentUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import javax.faces.component.UIComponent;
29 import java.lang.invoke.MethodHandles;
30 import java.util.Locale;
31
32 public final class LabelWithAccessKey {
33
34 private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
35
36 private final String label;
37 private final Character accessKey;
38 private final int pos;
39
40 public LabelWithAccessKey(final SupportsAccessKey component) {
41 this(component, false);
42 }
43
44 public LabelWithAccessKey(final SupportsAccessKey component, final boolean preferItemLabel) {
45
46 String label0;
47 Character accessKey0;
48 int pos0 = -1;
49
50 label0 = component.getLabel();
51
52
53 if (preferItemLabel) {
54 final String itemLabel = ComponentUtils.getStringAttribute((UIComponent) component, Attributes.itemLabel);
55 if (itemLabel != null) {
56 label0 = itemLabel;
57 }
58 }
59
60 accessKey0 = component.getAccessKey();
61 if (accessKey0 != null) {
62 accessKey0 = Character.toLowerCase(accessKey0);
63 if (!isPermitted(accessKey0)) {
64 LOG.warn("Ignoring illegal access key: " + accessKey0);
65 accessKey0 = null;
66 }
67 }
68
69 if (accessKey0 != null && label0 != null) {
70 pos0 = label0.toLowerCase(Locale.ENGLISH).indexOf(accessKey0);
71 }
72
73 label = label0;
74 accessKey = accessKey0;
75 pos = pos0;
76 }
77
78 public String getLabel() {
79 return label;
80 }
81
82 public Character getAccessKey() {
83 return accessKey;
84 }
85
86 public int getPos() {
87 return pos;
88 }
89
90
91
92
93
94 private boolean isPermitted(final Character key) {
95 return key == null || key >= 'a' && key <= 'z' || key >= '0' && key <= '9';
96 }
97 }