You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationConfiguration.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. /**
  17. *
  18. */
  19. package com.vaadin.ui;
  20. import java.io.Serializable;
  21. import com.vaadin.shared.ui.ui.NotificationRole;
  22. import com.vaadin.shared.ui.ui.UIState.NotificationTypeConfiguration;
  23. import com.vaadin.ui.Notification.Type;
  24. /**
  25. * Provides methods for configuring the notification.
  26. *
  27. * @author Vaadin Ltd
  28. * @since 7.2
  29. */
  30. public interface NotificationConfiguration extends Serializable {
  31. /**
  32. * Sets the accessibility prefix for a notification type.
  33. * <p>
  34. * This prefix is read to assistive device users before the content of the
  35. * notification, but not visible on the page.
  36. *
  37. * @param type
  38. * type of the notification
  39. * @param prefix
  40. * string that is placed before the notification content
  41. */
  42. public void setAssistivePrefix(Type type, String prefix);
  43. /**
  44. * Gets the accessibility prefix for a notification type.
  45. * <p>
  46. * This prefix is read to assistive device users before the content of the
  47. * notification, but not visible on the page.
  48. *
  49. * @param type
  50. * type of the notification
  51. * @return The accessibility prefix for the provided notification type
  52. */
  53. public String getAssistivePrefix(Type type);
  54. /**
  55. * Sets the accessibility postfix for a notification type.
  56. * <p>
  57. * This postfix is read to assistive device users after the content of the
  58. * notification, but not visible on the page.
  59. *
  60. * @param type
  61. * type of the notification
  62. * @param postfix
  63. * string that is placed after the notification content
  64. */
  65. public void setAssistivePostfix(Type type, String postfix);
  66. /**
  67. * Gets the accessibility postfix for a notification type.
  68. * <p>
  69. * This postfix is read to assistive device users after the content of the
  70. * notification, but not visible on the page.
  71. *
  72. * @param type
  73. * type of the notification
  74. * @return The accessibility postfix for the provided notification type
  75. */
  76. public String getAssistivePostfix(Type type);
  77. /**
  78. * Sets the WAI-ARIA role for a notification type.
  79. * <p>
  80. * This role defines how an assistive device handles a notification.
  81. * Available roles are alert and status (@see <a
  82. * href="http://www.w3.org/TR/2011/CR-wai-aria-20110118/roles">Roles
  83. * Model</a>).
  84. *
  85. * The default role is alert.
  86. *
  87. * @param type
  88. * type of the notification
  89. * @param role
  90. * role to set for the notification type
  91. */
  92. public void setAssistiveRole(Type type, NotificationRole role);
  93. /**
  94. * Gets the WAI-ARIA role for a notification type.
  95. * <p>
  96. * This role defines how an assistive device handles a notification.
  97. * Available roles are alert and status (@see <a
  98. * href="http://www.w3.org/TR/2011/CR-wai-aria-20110118/roles">Roles
  99. * Model</a>)
  100. * <p>
  101. * The default role is alert.
  102. *
  103. * @param type
  104. * type of the notification
  105. * @return role to set for the notification type
  106. */
  107. public NotificationRole getAssistiveRole(Type type);
  108. }
  109. class NotificationConfigurationImpl implements NotificationConfiguration {
  110. private UI ui;
  111. public NotificationConfigurationImpl(UI ui) {
  112. this.ui = ui;
  113. }
  114. @Override
  115. public void setAssistivePrefix(Type type, String prefix) {
  116. getConfigurationBean(type).prefix = prefix;
  117. }
  118. @Override
  119. public String getAssistivePrefix(Type type) {
  120. NotificationTypeConfiguration styleSetup = getTypeConf(type);
  121. if (styleSetup != null) {
  122. return styleSetup.prefix;
  123. }
  124. return null;
  125. }
  126. @Override
  127. public void setAssistivePostfix(Type type, String postfix) {
  128. getConfigurationBean(type).postfix = postfix;
  129. }
  130. @Override
  131. public String getAssistivePostfix(Type type) {
  132. NotificationTypeConfiguration styleSetup = getTypeConf(type);
  133. if (styleSetup != null) {
  134. return styleSetup.postfix;
  135. }
  136. return null;
  137. }
  138. @Override
  139. public void setAssistiveRole(Type type, NotificationRole role) {
  140. getConfigurationBean(type).notificationRole = role;
  141. }
  142. @Override
  143. public NotificationRole getAssistiveRole(Type type) {
  144. NotificationTypeConfiguration styleSetup = getTypeConf(type);
  145. if (styleSetup != null) {
  146. return styleSetup.notificationRole;
  147. }
  148. return null;
  149. }
  150. private NotificationTypeConfiguration getConfigurationBean(Type type) {
  151. NotificationTypeConfiguration styleSetup = getTypeConf(type);
  152. if (styleSetup == null) {
  153. styleSetup = new NotificationTypeConfiguration();
  154. ui.getState().notificationConfigurations.put(type.getStyle(),
  155. styleSetup);
  156. }
  157. return styleSetup;
  158. }
  159. private NotificationTypeConfiguration getTypeConf(Type type) {
  160. return ui.getState().notificationConfigurations.get(type.getStyle());
  161. }
  162. }